@dhis2/app-service-data 3.2.6 → 3.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -116,10 +116,6 @@ const useDataQuery = (query, {
116
116
  }) => data);
117
117
  }
118
118
 
119
- if (!enabled) {
120
- setEnabled(true);
121
- }
122
-
123
119
  if (newVariables) {
124
120
  // Use cached hash if it exists
125
121
  const currentHash = variablesHash.current || (0, _stableVariablesHash.stableVariablesHash)(variables);
@@ -129,8 +125,11 @@ const useDataQuery = (query, {
129
125
  const mergedHash = (0, _stableVariablesHash.stableVariablesHash)(mergedVariables);
130
126
  const identical = currentHash === mergedHash;
131
127
 
132
- if (identical) {
133
- // If the variables are identical we'll need to trigger the refetch manually
128
+ if (identical && enabled) {
129
+ /**
130
+ * If the variables are identical and the query is enabled
131
+ * we'll need to trigger the refetch manually
132
+ */
134
133
  return queryRefetch({
135
134
  cancelRefetch: true,
136
135
  throwOnError: false
@@ -141,6 +140,11 @@ const useDataQuery = (query, {
141
140
  variablesHash.current = mergedHash;
142
141
  setVariables(mergedVariables);
143
142
  }
143
+ } // Enable the query after the variables have been set to prevent extra request
144
+
145
+
146
+ if (!enabled) {
147
+ setEnabled(true);
144
148
  } // This promise does not currently reject on errors
145
149
 
146
150
 
@@ -507,6 +507,111 @@ describe('useDataQuery', () => {
507
507
  });
508
508
  });
509
509
  describe('return values: refetch', () => {
510
+ it('Should only trigger a single request when refetch is called on a lazy query with new variables', async () => {
511
+ const spy = jest.fn((type, query) => {
512
+ if (query.id === '1') {
513
+ return 42;
514
+ }
515
+
516
+ return 0;
517
+ });
518
+ const data = {
519
+ answer: spy
520
+ };
521
+ const query = {
522
+ x: {
523
+ resource: 'answer',
524
+ id: ({
525
+ id
526
+ }) => id
527
+ }
528
+ };
529
+
530
+ const wrapper = ({
531
+ children
532
+ }) => /*#__PURE__*/React.createElement(_CustomDataProvider.CustomDataProvider, {
533
+ data: data
534
+ }, children);
535
+
536
+ const {
537
+ result,
538
+ waitFor
539
+ } = (0, _reactHooks.renderHook)(() => (0, _useDataQuery.useDataQuery)(query, {
540
+ lazy: true
541
+ }), {
542
+ wrapper
543
+ });
544
+ expect(spy).not.toHaveBeenCalled();
545
+ (0, _reactHooks.act)(() => {
546
+ result.current.refetch({
547
+ id: '1'
548
+ });
549
+ });
550
+ await waitFor(() => {
551
+ expect(result.current).toMatchObject({
552
+ loading: false,
553
+ called: true,
554
+ data: {
555
+ x: 42
556
+ }
557
+ });
558
+ });
559
+ expect(spy).toHaveBeenCalledTimes(1);
560
+ });
561
+ it('Should only trigger a single request when refetch is called on a lazy query with identical variables', async () => {
562
+ const spy = jest.fn((type, query) => {
563
+ if (query.id === '1') {
564
+ return 42;
565
+ }
566
+
567
+ return 0;
568
+ });
569
+ const data = {
570
+ answer: spy
571
+ };
572
+ const query = {
573
+ x: {
574
+ resource: 'answer',
575
+ id: ({
576
+ id
577
+ }) => id
578
+ }
579
+ };
580
+
581
+ const wrapper = ({
582
+ children
583
+ }) => /*#__PURE__*/React.createElement(_CustomDataProvider.CustomDataProvider, {
584
+ data: data
585
+ }, children);
586
+
587
+ const {
588
+ result,
589
+ waitFor
590
+ } = (0, _reactHooks.renderHook)(() => (0, _useDataQuery.useDataQuery)(query, {
591
+ lazy: true,
592
+ variables: {
593
+ id: '1'
594
+ }
595
+ }), {
596
+ wrapper
597
+ });
598
+ expect(spy).not.toHaveBeenCalled();
599
+ (0, _reactHooks.act)(() => {
600
+ result.current.refetch({
601
+ id: '1'
602
+ });
603
+ });
604
+ await waitFor(() => {
605
+ expect(result.current).toMatchObject({
606
+ loading: false,
607
+ called: true,
608
+ data: {
609
+ x: 42
610
+ }
611
+ });
612
+ });
613
+ expect(spy).toHaveBeenCalledTimes(1);
614
+ });
510
615
  it('Should have a stable identity if the variables have not changed', async () => {
511
616
  const data = {
512
617
  answer: () => 42
@@ -104,10 +104,6 @@ export const useDataQuery = (query, {
104
104
  }) => data);
105
105
  }
106
106
 
107
- if (!enabled) {
108
- setEnabled(true);
109
- }
110
-
111
107
  if (newVariables) {
112
108
  // Use cached hash if it exists
113
109
  const currentHash = variablesHash.current || stableVariablesHash(variables);
@@ -117,8 +113,11 @@ export const useDataQuery = (query, {
117
113
  const mergedHash = stableVariablesHash(mergedVariables);
118
114
  const identical = currentHash === mergedHash;
119
115
 
120
- if (identical) {
121
- // If the variables are identical we'll need to trigger the refetch manually
116
+ if (identical && enabled) {
117
+ /**
118
+ * If the variables are identical and the query is enabled
119
+ * we'll need to trigger the refetch manually
120
+ */
122
121
  return queryRefetch({
123
122
  cancelRefetch: true,
124
123
  throwOnError: false
@@ -129,6 +128,11 @@ export const useDataQuery = (query, {
129
128
  variablesHash.current = mergedHash;
130
129
  setVariables(mergedVariables);
131
130
  }
131
+ } // Enable the query after the variables have been set to prevent extra request
132
+
133
+
134
+ if (!enabled) {
135
+ setEnabled(true);
132
136
  } // This promise does not currently reject on errors
133
137
 
134
138
 
@@ -497,6 +497,111 @@ describe('useDataQuery', () => {
497
497
  });
498
498
  });
499
499
  describe('return values: refetch', () => {
500
+ it('Should only trigger a single request when refetch is called on a lazy query with new variables', async () => {
501
+ const spy = jest.fn((type, query) => {
502
+ if (query.id === '1') {
503
+ return 42;
504
+ }
505
+
506
+ return 0;
507
+ });
508
+ const data = {
509
+ answer: spy
510
+ };
511
+ const query = {
512
+ x: {
513
+ resource: 'answer',
514
+ id: ({
515
+ id
516
+ }) => id
517
+ }
518
+ };
519
+
520
+ const wrapper = ({
521
+ children
522
+ }) => /*#__PURE__*/React.createElement(CustomDataProvider, {
523
+ data: data
524
+ }, children);
525
+
526
+ const {
527
+ result,
528
+ waitFor
529
+ } = renderHook(() => useDataQuery(query, {
530
+ lazy: true
531
+ }), {
532
+ wrapper
533
+ });
534
+ expect(spy).not.toHaveBeenCalled();
535
+ act(() => {
536
+ result.current.refetch({
537
+ id: '1'
538
+ });
539
+ });
540
+ await waitFor(() => {
541
+ expect(result.current).toMatchObject({
542
+ loading: false,
543
+ called: true,
544
+ data: {
545
+ x: 42
546
+ }
547
+ });
548
+ });
549
+ expect(spy).toHaveBeenCalledTimes(1);
550
+ });
551
+ it('Should only trigger a single request when refetch is called on a lazy query with identical variables', async () => {
552
+ const spy = jest.fn((type, query) => {
553
+ if (query.id === '1') {
554
+ return 42;
555
+ }
556
+
557
+ return 0;
558
+ });
559
+ const data = {
560
+ answer: spy
561
+ };
562
+ const query = {
563
+ x: {
564
+ resource: 'answer',
565
+ id: ({
566
+ id
567
+ }) => id
568
+ }
569
+ };
570
+
571
+ const wrapper = ({
572
+ children
573
+ }) => /*#__PURE__*/React.createElement(CustomDataProvider, {
574
+ data: data
575
+ }, children);
576
+
577
+ const {
578
+ result,
579
+ waitFor
580
+ } = renderHook(() => useDataQuery(query, {
581
+ lazy: true,
582
+ variables: {
583
+ id: '1'
584
+ }
585
+ }), {
586
+ wrapper
587
+ });
588
+ expect(spy).not.toHaveBeenCalled();
589
+ act(() => {
590
+ result.current.refetch({
591
+ id: '1'
592
+ });
593
+ });
594
+ await waitFor(() => {
595
+ expect(result.current).toMatchObject({
596
+ loading: false,
597
+ called: true,
598
+ data: {
599
+ x: 42
600
+ }
601
+ });
602
+ });
603
+ expect(spy).toHaveBeenCalledTimes(1);
604
+ });
500
605
  it('Should have a stable identity if the variables have not changed', async () => {
501
606
  const data = {
502
607
  answer: () => 42
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhis2/app-service-data",
3
- "version": "3.2.6",
3
+ "version": "3.2.7",
4
4
  "main": "./build/cjs/index.js",
5
5
  "module": "./build/es/index.js",
6
6
  "types": "build/types/index.d.ts",
@@ -22,7 +22,7 @@
22
22
  "build/**"
23
23
  ],
24
24
  "peerDependencies": {
25
- "@dhis2/app-service-config": "3.2.6",
25
+ "@dhis2/app-service-config": "3.2.7",
26
26
  "@dhis2/cli-app-scripts": "^7.1.1",
27
27
  "prop-types": "^15.7.2",
28
28
  "react": "^16.8",