@dhis2/app-service-data 3.11.3 → 3.12.0-alpha.2

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.
Files changed (113) hide show
  1. package/build/cjs/__tests__/integration.test.js +6 -15
  2. package/build/cjs/__tests__/mutations.test.js +4 -11
  3. package/build/cjs/engine/DataEngine.js +4 -14
  4. package/build/cjs/engine/DataEngine.test.js +0 -2
  5. package/build/cjs/engine/helpers/getMutationFetchType.js +0 -2
  6. package/build/cjs/engine/helpers/getMutationFetchType.test.js +0 -1
  7. package/build/cjs/engine/helpers/resolveDynamicQuery.js +0 -2
  8. package/build/cjs/engine/helpers/resolveDynamicQuery.test.js +3 -4
  9. package/build/cjs/engine/helpers/validate.js +0 -21
  10. package/build/cjs/engine/helpers/validate.test.js +0 -1
  11. package/build/cjs/engine/index.js +0 -18
  12. package/build/cjs/engine/types/FetchError.js +3 -8
  13. package/build/cjs/engine/types/FetchError.test.js +0 -1
  14. package/build/cjs/engine/types/InvalidQueryError.js +3 -8
  15. package/build/cjs/index.js +0 -2
  16. package/build/cjs/links/CustomDataLink.js +3 -17
  17. package/build/cjs/links/CustomDataLink.test.js +0 -1
  18. package/build/cjs/links/ErrorLink.js +3 -7
  19. package/build/cjs/links/RestAPILink/fetchData.js +12 -22
  20. package/build/cjs/links/RestAPILink/fetchData.test.js +0 -2
  21. package/build/cjs/links/RestAPILink/metadataResources.js +9 -7
  22. package/build/cjs/links/RestAPILink/path.js +0 -3
  23. package/build/cjs/links/RestAPILink/path.test.js +0 -1
  24. package/build/cjs/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.js +9 -14
  25. package/build/cjs/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.test.js +0 -1
  26. package/build/cjs/links/RestAPILink/queryToRequestOptions/requestContentType.js +4 -31
  27. package/build/cjs/links/RestAPILink/queryToRequestOptions/requestContentType.test.js +0 -1
  28. package/build/cjs/links/RestAPILink/queryToRequestOptions/textPlainMatchers.js +22 -39
  29. package/build/cjs/links/RestAPILink/queryToRequestOptions/textPlainMatchers.test.js +0 -1
  30. package/build/cjs/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.js +0 -2
  31. package/build/cjs/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.test.js +0 -1
  32. package/build/cjs/links/RestAPILink/queryToRequestOptions.js +0 -9
  33. package/build/cjs/links/RestAPILink/queryToRequestOptions.test.js +0 -1
  34. package/build/cjs/links/RestAPILink/queryToResourcePath.js +3 -24
  35. package/build/cjs/links/RestAPILink/queryToResourcePath.test.js +2 -4
  36. package/build/cjs/links/RestAPILink/validateQuery.js +4 -18
  37. package/build/cjs/links/RestAPILink/validateQuery.test.js +0 -1
  38. package/build/cjs/links/RestAPILink.js +3 -14
  39. package/build/cjs/links/RestAPILink.test.js +0 -2
  40. package/build/cjs/links/index.js +0 -6
  41. package/build/cjs/react/components/CustomDataProvider.js +3 -12
  42. package/build/cjs/react/components/DataMutation.js +1 -4
  43. package/build/cjs/react/components/DataProvider.js +11 -16
  44. package/build/cjs/react/components/DataProvider.test.js +2 -9
  45. package/build/cjs/react/components/DataQuery.js +1 -4
  46. package/build/cjs/react/context/DataContext.js +2 -8
  47. package/build/cjs/react/context/defaultContext.js +2 -6
  48. package/build/cjs/react/context/defaultContext.test.js +0 -1
  49. package/build/cjs/react/hooks/mergeAndCompareVariables.js +4 -6
  50. package/build/cjs/react/hooks/mergeAndCompareVariables.test.js +2 -3
  51. package/build/cjs/react/hooks/stableVariablesHash.js +11 -16
  52. package/build/cjs/react/hooks/stableVariablesHash.test.js +0 -1
  53. package/build/cjs/react/hooks/useDataEngine.js +0 -4
  54. package/build/cjs/react/hooks/useDataMutation.js +0 -7
  55. package/build/cjs/react/hooks/useDataMutation.test.js +42 -71
  56. package/build/cjs/react/hooks/useDataQuery.js +26 -44
  57. package/build/cjs/react/hooks/useDataQuery.test.js +318 -374
  58. package/build/cjs/react/hooks/useQueryExecutor.js +6 -14
  59. package/build/cjs/react/hooks/useQueryExecutor.test.js +42 -45
  60. package/build/cjs/react/hooks/useStaticInput.js +0 -3
  61. package/build/cjs/react/hooks/useStaticInput.test.js +8 -10
  62. package/build/cjs/react/index.js +0 -11
  63. package/build/cjs/setupRTL.js +1 -2
  64. package/build/es/__tests__/integration.test.js +4 -8
  65. package/build/es/__tests__/mutations.test.js +2 -4
  66. package/build/es/engine/DataEngine.js +3 -8
  67. package/build/es/engine/DataEngine.test.js +0 -1
  68. package/build/es/engine/helpers/resolveDynamicQuery.test.js +3 -3
  69. package/build/es/engine/helpers/validate.js +0 -13
  70. package/build/es/engine/types/FetchError.js +3 -6
  71. package/build/es/engine/types/InvalidQueryError.js +3 -6
  72. package/build/es/links/CustomDataLink.js +3 -15
  73. package/build/es/links/ErrorLink.js +3 -5
  74. package/build/es/links/RestAPILink/fetchData.js +12 -16
  75. package/build/es/links/RestAPILink/metadataResources.js +8 -3
  76. package/build/es/links/RestAPILink/path.js +0 -1
  77. package/build/es/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.js +9 -4
  78. package/build/es/links/RestAPILink/queryToRequestOptions/requestContentType.js +2 -19
  79. package/build/es/links/RestAPILink/queryToRequestOptions/textPlainMatchers.js +22 -19
  80. package/build/es/links/RestAPILink/queryToRequestOptions.js +0 -7
  81. package/build/es/links/RestAPILink/queryToResourcePath.js +3 -21
  82. package/build/es/links/RestAPILink/queryToResourcePath.test.js +2 -3
  83. package/build/es/links/RestAPILink/validateQuery.js +4 -16
  84. package/build/es/links/RestAPILink.js +3 -8
  85. package/build/es/react/components/CustomDataProvider.js +2 -2
  86. package/build/es/react/components/DataMutation.js +1 -1
  87. package/build/es/react/components/DataProvider.js +9 -3
  88. package/build/es/react/components/DataProvider.test.js +1 -1
  89. package/build/es/react/components/DataQuery.js +1 -1
  90. package/build/es/react/hooks/mergeAndCompareVariables.js +4 -3
  91. package/build/es/react/hooks/mergeAndCompareVariables.test.js +2 -1
  92. package/build/es/react/hooks/stableVariablesHash.js +11 -14
  93. package/build/es/react/hooks/useDataMutation.test.js +17 -39
  94. package/build/es/react/hooks/useDataQuery.js +26 -37
  95. package/build/es/react/hooks/useDataQuery.test.js +268 -318
  96. package/build/es/react/hooks/useQueryExecutor.js +6 -9
  97. package/build/es/react/hooks/useQueryExecutor.test.js +33 -34
  98. package/build/es/react/hooks/useStaticInput.test.js +6 -6
  99. package/build/es/setupRTL.js +1 -1
  100. package/build/types/engine/types/ExecuteOptions.d.ts +1 -1
  101. package/build/types/engine/types/FetchError.d.ts +2 -2
  102. package/build/types/engine/types/JsonValue.d.ts +1 -1
  103. package/build/types/engine/types/Mutation.d.ts +2 -2
  104. package/build/types/engine/types/PossiblyDynamic.d.ts +1 -1
  105. package/build/types/engine/types/Query.d.ts +3 -3
  106. package/build/types/engine/types/QueryParameters.d.ts +4 -4
  107. package/build/types/links/CustomDataLink.d.ts +2 -2
  108. package/build/types/links/RestAPILink/queryToRequestOptions/requestContentType.d.ts +1 -1
  109. package/build/types/links/RestAPILink/queryToRequestOptions.d.ts +1 -1
  110. package/build/types/react/components/DataProvider.d.ts +2 -11
  111. package/build/types/react/hooks/mergeAndCompareVariables.d.ts +1 -1
  112. package/build/types/types.d.ts +6 -6
  113. package/package.json +5 -5
@@ -1,4 +1,4 @@
1
- import { renderHook, act } from '@testing-library/react-hooks';
1
+ import { renderHook, waitFor, act } from '@testing-library/react';
2
2
  import * as React from 'react';
3
3
  import { CustomDataProvider } from '../components/CustomDataProvider';
4
4
  import { useDataQuery } from './useDataQuery';
@@ -13,8 +13,7 @@ describe('useDataQuery', () => {
13
13
  const data = {
14
14
  answer: 42
15
15
  };
16
-
17
- const wrapper = (_ref) => {
16
+ const wrapper = _ref => {
18
17
  let {
19
18
  children
20
19
  } = _ref;
@@ -22,12 +21,10 @@ describe('useDataQuery', () => {
22
21
  data: data
23
22
  }, children);
24
23
  };
25
-
26
24
  const onComplete = jest.fn();
27
25
  const onError = jest.fn();
28
26
  const {
29
- result,
30
- waitForNextUpdate
27
+ result
31
28
  } = renderHook(() => useDataQuery(query, {
32
29
  lazy: false,
33
30
  onComplete,
@@ -39,15 +36,16 @@ describe('useDataQuery', () => {
39
36
  loading: true,
40
37
  called: true
41
38
  });
42
- await waitForNextUpdate();
43
- expect(onComplete).toHaveBeenCalledWith({
44
- x: 42
45
- });
46
- expect(onError).not.toHaveBeenCalled();
47
- expect(result.current).toMatchObject({
48
- data: {
39
+ await waitFor(() => {
40
+ expect(onComplete).toHaveBeenCalledWith({
49
41
  x: 42
50
- }
42
+ });
43
+ expect(onError).not.toHaveBeenCalled();
44
+ expect(result.current).toMatchObject({
45
+ data: {
46
+ x: 42
47
+ }
48
+ });
51
49
  });
52
50
  });
53
51
  });
@@ -64,8 +62,7 @@ describe('useDataQuery', () => {
64
62
  throw expectedError;
65
63
  }
66
64
  };
67
-
68
- const wrapper = (_ref2) => {
65
+ const wrapper = _ref2 => {
69
66
  let {
70
67
  children
71
68
  } = _ref2;
@@ -73,12 +70,10 @@ describe('useDataQuery', () => {
73
70
  data: data
74
71
  }, children);
75
72
  };
76
-
77
73
  const onComplete = jest.fn();
78
74
  const onError = jest.fn();
79
75
  const {
80
- result,
81
- waitForNextUpdate
76
+ result
82
77
  } = renderHook(() => useDataQuery(query, {
83
78
  onError,
84
79
  onComplete
@@ -89,11 +84,12 @@ describe('useDataQuery', () => {
89
84
  loading: true,
90
85
  called: true
91
86
  });
92
- await waitForNextUpdate();
93
- expect(onError).toHaveBeenCalledWith(expectedError);
94
- expect(onComplete).not.toHaveBeenCalled();
95
- expect(result.current).toMatchObject({
96
- error: expectedError
87
+ await waitFor(() => {
88
+ expect(onError).toHaveBeenCalledWith(expectedError);
89
+ expect(onComplete).not.toHaveBeenCalled();
90
+ expect(result.current).toMatchObject({
91
+ error: expectedError
92
+ });
97
93
  });
98
94
  });
99
95
  });
@@ -106,7 +102,7 @@ describe('useDataQuery', () => {
106
102
  const query = {
107
103
  x: {
108
104
  resource: 'answer',
109
- id: (_ref3) => {
105
+ id: _ref3 => {
110
106
  let {
111
107
  id
112
108
  } = _ref3;
@@ -118,11 +114,9 @@ describe('useDataQuery', () => {
118
114
  let {
119
115
  id
120
116
  } = _ref4;
121
-
122
117
  switch (id) {
123
118
  case one:
124
119
  return Promise.resolve(resultOne);
125
-
126
120
  case two:
127
121
  return Promise.resolve(resultTwo);
128
122
  }
@@ -130,8 +124,7 @@ describe('useDataQuery', () => {
130
124
  const data = {
131
125
  answer: mockSpy
132
126
  };
133
-
134
- const wrapper = (_ref5) => {
127
+ const wrapper = _ref5 => {
135
128
  let {
136
129
  children
137
130
  } = _ref5;
@@ -139,7 +132,6 @@ describe('useDataQuery', () => {
139
132
  data: data
140
133
  }, children);
141
134
  };
142
-
143
135
  const initialProps = {
144
136
  query,
145
137
  options: {
@@ -150,7 +142,6 @@ describe('useDataQuery', () => {
150
142
  };
151
143
  const {
152
144
  result,
153
- waitForNextUpdate,
154
145
  rerender
155
146
  } = renderHook(props => useDataQuery(props.query, props.options), {
156
147
  wrapper,
@@ -161,11 +152,12 @@ describe('useDataQuery', () => {
161
152
  loading: true,
162
153
  called: true
163
154
  });
164
- await waitForNextUpdate();
165
- expect(result.current).toMatchObject({
166
- data: {
167
- x: resultOne
168
- }
155
+ await waitFor(() => {
156
+ expect(result.current).toMatchObject({
157
+ data: {
158
+ x: resultOne
159
+ }
160
+ });
169
161
  });
170
162
  act(() => {
171
163
  const newProps = {
@@ -197,8 +189,7 @@ describe('useDataQuery', () => {
197
189
  const data = {
198
190
  answer: mockSpy
199
191
  };
200
-
201
- const wrapper = (_ref6) => {
192
+ const wrapper = _ref6 => {
202
193
  let {
203
194
  children
204
195
  } = _ref6;
@@ -206,10 +197,8 @@ describe('useDataQuery', () => {
206
197
  data: data
207
198
  }, children);
208
199
  };
209
-
210
200
  const {
211
- result,
212
- waitForNextUpdate
201
+ result
213
202
  } = renderHook(() => useDataQuery(query), {
214
203
  wrapper
215
204
  });
@@ -217,14 +206,15 @@ describe('useDataQuery', () => {
217
206
  loading: true,
218
207
  called: true
219
208
  });
220
- await waitForNextUpdate();
221
- expect(mockSpy).toHaveBeenCalledTimes(1);
222
- expect(result.current).toMatchObject({
223
- loading: false,
224
- called: true,
225
- data: {
226
- x: 42
227
- }
209
+ await waitFor(() => {
210
+ expect(mockSpy).toHaveBeenCalledTimes(1);
211
+ expect(result.current).toMatchObject({
212
+ loading: false,
213
+ called: true,
214
+ data: {
215
+ x: 42
216
+ }
217
+ });
228
218
  });
229
219
  });
230
220
  });
@@ -248,8 +238,7 @@ describe('useDataQuery', () => {
248
238
  resource: 'answer'
249
239
  }
250
240
  };
251
-
252
- const wrapper = (_ref7) => {
241
+ const wrapper = _ref7 => {
253
242
  let {
254
243
  children
255
244
  } = _ref7;
@@ -258,10 +247,8 @@ describe('useDataQuery', () => {
258
247
  queryClientOptions: queryClientOptions
259
248
  }, children);
260
249
  };
261
-
262
250
  const {
263
- result,
264
- waitForNextUpdate
251
+ result
265
252
  } = renderHook(() => useDataQuery(query), {
266
253
  wrapper
267
254
  });
@@ -270,14 +257,15 @@ describe('useDataQuery', () => {
270
257
  loading: true,
271
258
  called: true
272
259
  });
273
- await waitForNextUpdate(); // Now the cache will contain a value for 'answer' without variables
274
-
275
- expect(result.current).toMatchObject({
276
- loading: false,
277
- called: true,
278
- data: {
279
- x: 42
280
- }
260
+ await waitFor(() => {
261
+ // Now the cache will contain a value for 'answer' without variables
262
+ expect(result.current).toMatchObject({
263
+ loading: false,
264
+ called: true,
265
+ data: {
266
+ x: 42
267
+ }
268
+ });
281
269
  });
282
270
  act(() => {
283
271
  // Add a variable to the request to ensure a different cache key
@@ -289,21 +277,23 @@ describe('useDataQuery', () => {
289
277
  expect(result.current).toMatchObject({
290
278
  loading: true
291
279
  });
292
- await waitForNextUpdate(); // Now the cache will contain a value for 'answer' with and without variables
293
-
294
- expect(result.current).toMatchObject({
295
- loading: false,
296
- data: {
297
- x: 43
298
- }
280
+ await waitFor(() => {
281
+ // Now the cache will contain a value for 'answer' with and without variables
282
+ expect(result.current).toMatchObject({
283
+ loading: false,
284
+ data: {
285
+ x: 43
286
+ }
287
+ });
299
288
  });
300
289
  act(() => {
301
290
  // Request the resource without variables again
302
291
  result.current.refetch({
303
292
  one: undefined
304
293
  });
305
- }); // This should return the resource from the cache without fetching
294
+ });
306
295
 
296
+ // This should return the resource from the cache without fetching
307
297
  expect(mockSpy).toHaveBeenCalledTimes(2);
308
298
  expect(result.current).toMatchObject({
309
299
  loading: false,
@@ -325,8 +315,7 @@ describe('useDataQuery', () => {
325
315
  resource: 'answer'
326
316
  }
327
317
  };
328
-
329
- const wrapper = (_ref8) => {
318
+ const wrapper = _ref8 => {
330
319
  let {
331
320
  children
332
321
  } = _ref8;
@@ -334,10 +323,8 @@ describe('useDataQuery', () => {
334
323
  data: data
335
324
  }, children);
336
325
  };
337
-
338
326
  const {
339
- result,
340
- waitForNextUpdate
327
+ result
341
328
  } = renderHook(() => {
342
329
  const one = useDataQuery(query);
343
330
  const two = useDataQuery(query);
@@ -352,16 +339,17 @@ describe('useDataQuery', () => {
352
339
  };
353
340
  expect(mockSpy).toHaveBeenCalledTimes(1);
354
341
  expect(result.current).toMatchObject([loading, loading, loading]);
355
- await waitForNextUpdate();
356
- const done = {
357
- loading: false,
358
- called: true,
359
- data: {
360
- x: 42
361
- }
362
- };
363
- expect(mockSpy).toHaveBeenCalledTimes(1);
364
- expect(result.current).toMatchObject([done, done, done]);
342
+ await waitFor(() => {
343
+ const done = {
344
+ loading: false,
345
+ called: true,
346
+ data: {
347
+ x: 42
348
+ }
349
+ };
350
+ expect(mockSpy).toHaveBeenCalledTimes(1);
351
+ expect(result.current).toMatchObject([done, done, done]);
352
+ });
365
353
  });
366
354
  });
367
355
  describe('return values: data', () => {
@@ -374,8 +362,7 @@ describe('useDataQuery', () => {
374
362
  const data = {
375
363
  answer: 42
376
364
  };
377
-
378
- const wrapper = (_ref9) => {
365
+ const wrapper = _ref9 => {
379
366
  let {
380
367
  children
381
368
  } = _ref9;
@@ -383,10 +370,8 @@ describe('useDataQuery', () => {
383
370
  data: data
384
371
  }, children);
385
372
  };
386
-
387
373
  const {
388
- result,
389
- waitForNextUpdate
374
+ result
390
375
  } = renderHook(() => useDataQuery(query), {
391
376
  wrapper
392
377
  });
@@ -394,13 +379,14 @@ describe('useDataQuery', () => {
394
379
  loading: true,
395
380
  called: true
396
381
  });
397
- await waitForNextUpdate();
398
- expect(result.current).toMatchObject({
399
- loading: false,
400
- called: true,
401
- data: {
402
- x: 42
403
- }
382
+ await waitFor(() => {
383
+ expect(result.current).toMatchObject({
384
+ loading: false,
385
+ called: true,
386
+ data: {
387
+ x: 42
388
+ }
389
+ });
404
390
  });
405
391
  });
406
392
  it('Should return the data for a multiple resource query on success', async () => {
@@ -416,8 +402,7 @@ describe('useDataQuery', () => {
416
402
  answer: 42,
417
403
  opposite: 24
418
404
  };
419
-
420
- const wrapper = (_ref10) => {
405
+ const wrapper = _ref10 => {
421
406
  let {
422
407
  children
423
408
  } = _ref10;
@@ -425,10 +410,8 @@ describe('useDataQuery', () => {
425
410
  data: data
426
411
  }, children);
427
412
  };
428
-
429
413
  const {
430
- result,
431
- waitForNextUpdate
414
+ result
432
415
  } = renderHook(() => useDataQuery(query), {
433
416
  wrapper
434
417
  });
@@ -436,14 +419,15 @@ describe('useDataQuery', () => {
436
419
  loading: true,
437
420
  called: true
438
421
  });
439
- await waitForNextUpdate();
440
- expect(result.current).toMatchObject({
441
- loading: false,
442
- called: true,
443
- data: {
444
- x: 42,
445
- y: 24
446
- }
422
+ await waitFor(() => {
423
+ expect(result.current).toMatchObject({
424
+ loading: false,
425
+ called: true,
426
+ data: {
427
+ x: 42,
428
+ y: 24
429
+ }
430
+ });
447
431
  });
448
432
  });
449
433
  });
@@ -460,8 +444,7 @@ describe('useDataQuery', () => {
460
444
  throw expectedError;
461
445
  }
462
446
  };
463
-
464
- const wrapper = (_ref11) => {
447
+ const wrapper = _ref11 => {
465
448
  let {
466
449
  children
467
450
  } = _ref11;
@@ -469,10 +452,8 @@ describe('useDataQuery', () => {
469
452
  data: data
470
453
  }, children);
471
454
  };
472
-
473
455
  const {
474
- result,
475
- waitForNextUpdate
456
+ result
476
457
  } = renderHook(() => useDataQuery(query), {
477
458
  wrapper
478
459
  });
@@ -480,11 +461,12 @@ describe('useDataQuery', () => {
480
461
  loading: true,
481
462
  called: true
482
463
  });
483
- await waitForNextUpdate();
484
- expect(result.current).toMatchObject({
485
- loading: false,
486
- called: true,
487
- error: expectedError
464
+ await waitFor(() => {
465
+ expect(result.current).toMatchObject({
466
+ loading: false,
467
+ called: true,
468
+ error: expectedError
469
+ });
488
470
  });
489
471
  });
490
472
  it('Should return errors it encounters for multiple resource queries', async () => {
@@ -503,8 +485,7 @@ describe('useDataQuery', () => {
503
485
  },
504
486
  opposite: 24
505
487
  };
506
-
507
- const wrapper = (_ref12) => {
488
+ const wrapper = _ref12 => {
508
489
  let {
509
490
  children
510
491
  } = _ref12;
@@ -512,10 +493,8 @@ describe('useDataQuery', () => {
512
493
  data: data
513
494
  }, children);
514
495
  };
515
-
516
496
  const {
517
- result,
518
- waitForNextUpdate
497
+ result
519
498
  } = renderHook(() => useDataQuery(query), {
520
499
  wrapper
521
500
  });
@@ -523,11 +502,12 @@ describe('useDataQuery', () => {
523
502
  loading: true,
524
503
  called: true
525
504
  });
526
- await waitForNextUpdate();
527
- expect(result.current).toMatchObject({
528
- loading: false,
529
- called: true,
530
- error: expectedError
505
+ await waitFor(() => {
506
+ expect(result.current).toMatchObject({
507
+ loading: false,
508
+ called: true,
509
+ error: expectedError
510
+ });
531
511
  });
532
512
  });
533
513
  });
@@ -546,8 +526,7 @@ describe('useDataQuery', () => {
546
526
  resource: 'answer'
547
527
  }
548
528
  };
549
-
550
- const wrapper = (_ref13) => {
529
+ const wrapper = _ref13 => {
551
530
  let {
552
531
  children
553
532
  } = _ref13;
@@ -555,10 +534,8 @@ describe('useDataQuery', () => {
555
534
  data: data
556
535
  }, children);
557
536
  };
558
-
559
537
  const {
560
- result,
561
- waitFor
538
+ result
562
539
  } = renderHook(() => useDataQuery(query, {
563
540
  lazy: true
564
541
  }), {
@@ -599,7 +576,6 @@ describe('useDataQuery', () => {
599
576
  if (query.id === '1') {
600
577
  return 42;
601
578
  }
602
-
603
579
  return 0;
604
580
  });
605
581
  const data = {
@@ -608,7 +584,7 @@ describe('useDataQuery', () => {
608
584
  const query = {
609
585
  x: {
610
586
  resource: 'answer',
611
- id: (_ref14) => {
587
+ id: _ref14 => {
612
588
  let {
613
589
  id
614
590
  } = _ref14;
@@ -616,8 +592,7 @@ describe('useDataQuery', () => {
616
592
  }
617
593
  }
618
594
  };
619
-
620
- const wrapper = (_ref15) => {
595
+ const wrapper = _ref15 => {
621
596
  let {
622
597
  children
623
598
  } = _ref15;
@@ -625,10 +600,8 @@ describe('useDataQuery', () => {
625
600
  data: data
626
601
  }, children);
627
602
  };
628
-
629
603
  const {
630
- result,
631
- waitFor
604
+ result
632
605
  } = renderHook(() => useDataQuery(query, {
633
606
  lazy: true
634
607
  }), {
@@ -656,7 +629,6 @@ describe('useDataQuery', () => {
656
629
  if (query.id === '1') {
657
630
  return 42;
658
631
  }
659
-
660
632
  return 0;
661
633
  });
662
634
  const data = {
@@ -665,7 +637,7 @@ describe('useDataQuery', () => {
665
637
  const query = {
666
638
  x: {
667
639
  resource: 'answer',
668
- id: (_ref16) => {
640
+ id: _ref16 => {
669
641
  let {
670
642
  id
671
643
  } = _ref16;
@@ -673,8 +645,7 @@ describe('useDataQuery', () => {
673
645
  }
674
646
  }
675
647
  };
676
-
677
- const wrapper = (_ref17) => {
648
+ const wrapper = _ref17 => {
678
649
  let {
679
650
  children
680
651
  } = _ref17;
@@ -682,10 +653,8 @@ describe('useDataQuery', () => {
682
653
  data: data
683
654
  }, children);
684
655
  };
685
-
686
656
  const {
687
- result,
688
- waitFor
657
+ result
689
658
  } = renderHook(() => useDataQuery(query, {
690
659
  lazy: true,
691
660
  variables: {
@@ -720,8 +689,7 @@ describe('useDataQuery', () => {
720
689
  resource: 'answer'
721
690
  }
722
691
  };
723
-
724
- const wrapper = (_ref18) => {
692
+ const wrapper = _ref18 => {
725
693
  let {
726
694
  children
727
695
  } = _ref18;
@@ -729,28 +697,29 @@ describe('useDataQuery', () => {
729
697
  data: data
730
698
  }, children);
731
699
  };
732
-
733
700
  const {
734
701
  result,
735
- waitForNextUpdate,
736
702
  rerender
737
703
  } = renderHook(() => useDataQuery(query), {
738
704
  wrapper
739
705
  });
740
706
  const firstRefetch = result.current.refetch;
741
- await waitForNextUpdate();
707
+
708
+ // await waitFor(() => {})
709
+
742
710
  act(() => {
743
711
  result.current.refetch();
712
+
744
713
  /**
745
714
  * FIXME: https://github.com/tannerlinsley/react-query/issues/2481
746
715
  * This forced rerender is not necessary in the app, just when testing.
747
716
  * It is unclear why.
748
717
  */
749
-
750
718
  rerender();
751
719
  });
752
- await waitForNextUpdate();
753
- expect(result.current.refetch).toBe(firstRefetch);
720
+ await waitFor(() => {
721
+ expect(result.current.refetch).toBe(firstRefetch);
722
+ });
754
723
  });
755
724
  it('Should return stale data and set loading to true on refetch', async () => {
756
725
  const answers = [42, 43];
@@ -763,8 +732,7 @@ describe('useDataQuery', () => {
763
732
  resource: 'answer'
764
733
  }
765
734
  };
766
-
767
- const wrapper = (_ref19) => {
735
+ const wrapper = _ref19 => {
768
736
  let {
769
737
  children
770
738
  } = _ref19;
@@ -772,10 +740,8 @@ describe('useDataQuery', () => {
772
740
  data: data
773
741
  }, children);
774
742
  };
775
-
776
743
  const {
777
744
  result,
778
- waitForNextUpdate,
779
745
  rerender
780
746
  } = renderHook(() => useDataQuery(query), {
781
747
  wrapper
@@ -785,22 +751,23 @@ describe('useDataQuery', () => {
785
751
  loading: true,
786
752
  called: true
787
753
  });
788
- await waitForNextUpdate();
789
- expect(result.current).toMatchObject({
790
- loading: false,
791
- called: true,
792
- data: {
793
- x: 42
794
- }
754
+ await waitFor(() => {
755
+ expect(result.current).toMatchObject({
756
+ loading: false,
757
+ called: true,
758
+ data: {
759
+ x: 42
760
+ }
761
+ });
795
762
  });
796
763
  act(() => {
797
764
  result.current.refetch();
765
+
798
766
  /**
799
767
  * FIXME: https://github.com/tannerlinsley/react-query/issues/2481
800
768
  * This forced rerender is not necessary in the app, just when testing.
801
769
  * It is unclear why.
802
770
  */
803
-
804
771
  rerender();
805
772
  });
806
773
  expect(mockSpy).toHaveBeenCalledTimes(2);
@@ -812,15 +779,16 @@ describe('useDataQuery', () => {
812
779
  x: 42
813
780
  }
814
781
  });
815
- await waitForNextUpdate();
816
- expect(mockSpy).toHaveBeenCalledTimes(2);
817
- expect(result.current).toMatchObject({
818
- loading: false,
819
- fetching: false,
820
- called: true,
821
- data: {
822
- x: 43
823
- }
782
+ await waitFor(() => {
783
+ expect(mockSpy).toHaveBeenCalledTimes(2);
784
+ expect(result.current).toMatchObject({
785
+ loading: false,
786
+ fetching: false,
787
+ called: true,
788
+ data: {
789
+ x: 43
790
+ }
791
+ });
824
792
  });
825
793
  });
826
794
  it('Should not fetch until refetch has been called if lazy', async () => {
@@ -833,8 +801,7 @@ describe('useDataQuery', () => {
833
801
  const data = {
834
802
  answer: mockSpy
835
803
  };
836
-
837
- const wrapper = (_ref20) => {
804
+ const wrapper = _ref20 => {
838
805
  let {
839
806
  children
840
807
  } = _ref20;
@@ -842,10 +809,8 @@ describe('useDataQuery', () => {
842
809
  data: data
843
810
  }, children);
844
811
  };
845
-
846
812
  const {
847
- result,
848
- waitForNextUpdate
813
+ result
849
814
  } = renderHook(() => useDataQuery(query, {
850
815
  lazy: true
851
816
  }), {
@@ -864,13 +829,14 @@ describe('useDataQuery', () => {
864
829
  loading: true,
865
830
  called: true
866
831
  });
867
- await waitForNextUpdate();
868
- expect(result.current).toMatchObject({
869
- loading: false,
870
- called: true,
871
- data: {
872
- x: 42
873
- }
832
+ await waitFor(() => {
833
+ expect(result.current).toMatchObject({
834
+ loading: false,
835
+ called: true,
836
+ data: {
837
+ x: 42
838
+ }
839
+ });
874
840
  });
875
841
  });
876
842
  it('Should call onComplete after a successful refetch', async () => {
@@ -882,8 +848,7 @@ describe('useDataQuery', () => {
882
848
  const data = {
883
849
  answer: 42
884
850
  };
885
-
886
- const wrapper = (_ref21) => {
851
+ const wrapper = _ref21 => {
887
852
  let {
888
853
  children
889
854
  } = _ref21;
@@ -891,12 +856,10 @@ describe('useDataQuery', () => {
891
856
  data: data
892
857
  }, children);
893
858
  };
894
-
895
859
  const onComplete = jest.fn();
896
860
  const onError = jest.fn();
897
861
  const {
898
- result,
899
- waitForNextUpdate
862
+ result
900
863
  } = renderHook(() => useDataQuery(query, {
901
864
  lazy: true,
902
865
  onComplete,
@@ -907,18 +870,19 @@ describe('useDataQuery', () => {
907
870
  act(() => {
908
871
  result.current.refetch();
909
872
  });
910
- await waitForNextUpdate();
911
- expect(result.current).toMatchObject({
912
- loading: false,
913
- called: true,
914
- data: {
873
+ await waitFor(() => {
874
+ expect(result.current).toMatchObject({
875
+ loading: false,
876
+ called: true,
877
+ data: {
878
+ x: 42
879
+ }
880
+ });
881
+ expect(onComplete).toHaveBeenCalledWith({
915
882
  x: 42
916
- }
917
- });
918
- expect(onComplete).toHaveBeenCalledWith({
919
- x: 42
883
+ });
884
+ expect(onError).not.toHaveBeenCalled();
920
885
  });
921
- expect(onError).not.toHaveBeenCalled();
922
886
  });
923
887
  it('Should call onError after a failed refetch', async () => {
924
888
  const expectedError = new Error('Something went wrong');
@@ -932,8 +896,7 @@ describe('useDataQuery', () => {
932
896
  throw expectedError;
933
897
  }
934
898
  };
935
-
936
- const wrapper = (_ref22) => {
899
+ const wrapper = _ref22 => {
937
900
  let {
938
901
  children
939
902
  } = _ref22;
@@ -941,12 +904,10 @@ describe('useDataQuery', () => {
941
904
  data: data
942
905
  }, children);
943
906
  };
944
-
945
907
  const onComplete = jest.fn();
946
908
  const onError = jest.fn();
947
909
  const {
948
- result,
949
- waitForNextUpdate
910
+ result
950
911
  } = renderHook(() => useDataQuery(query, {
951
912
  lazy: true,
952
913
  onComplete,
@@ -957,11 +918,12 @@ describe('useDataQuery', () => {
957
918
  act(() => {
958
919
  result.current.refetch();
959
920
  });
960
- await waitForNextUpdate();
961
- expect(onError).toHaveBeenCalledWith(expectedError);
962
- expect(onComplete).not.toHaveBeenCalled();
963
- expect(result.current).toMatchObject({
964
- error: expectedError
921
+ await waitFor(() => {
922
+ expect(onError).toHaveBeenCalledWith(expectedError);
923
+ expect(onComplete).not.toHaveBeenCalled();
924
+ expect(result.current).toMatchObject({
925
+ error: expectedError
926
+ });
965
927
  });
966
928
  });
967
929
  it('Should refetch when refetch is called with variables that resolve to the same query key', async () => {
@@ -973,7 +935,7 @@ describe('useDataQuery', () => {
973
935
  const query = {
974
936
  x: {
975
937
  resource: 'answer',
976
- params: (_ref23) => {
938
+ params: _ref23 => {
977
939
  let {
978
940
  one,
979
941
  two,
@@ -991,8 +953,7 @@ describe('useDataQuery', () => {
991
953
  const data = {
992
954
  answer: spy
993
955
  };
994
-
995
- const wrapper = (_ref24) => {
956
+ const wrapper = _ref24 => {
996
957
  let {
997
958
  children
998
959
  } = _ref24;
@@ -1000,35 +961,35 @@ describe('useDataQuery', () => {
1000
961
  data: data
1001
962
  }, children);
1002
963
  };
1003
-
1004
964
  const {
1005
- result,
1006
- waitForNextUpdate
965
+ result
1007
966
  } = renderHook(() => useDataQuery(query, {
1008
967
  variables
1009
968
  }), {
1010
969
  wrapper
1011
970
  });
1012
- await waitForNextUpdate();
1013
- expect(spy).toHaveBeenCalledTimes(1);
1014
- expect(result.current).toMatchObject({
1015
- loading: false,
1016
- called: true,
1017
- data: {
1018
- x: 42
1019
- }
971
+ await waitFor(() => {
972
+ expect(spy).toHaveBeenCalledTimes(1);
973
+ expect(result.current).toMatchObject({
974
+ loading: false,
975
+ called: true,
976
+ data: {
977
+ x: 42
978
+ }
979
+ });
1020
980
  });
1021
981
  act(() => {
1022
982
  result.current.refetch(variables);
1023
983
  });
1024
- await waitForNextUpdate();
1025
- expect(spy).toHaveBeenCalledTimes(2);
1026
- expect(result.current).toMatchObject({
1027
- loading: false,
1028
- called: true,
1029
- data: {
1030
- x: 42
1031
- }
984
+ await waitFor(() => {
985
+ expect(spy).toHaveBeenCalledTimes(2);
986
+ expect(result.current).toMatchObject({
987
+ loading: false,
988
+ called: true,
989
+ data: {
990
+ x: 42
991
+ }
992
+ });
1032
993
  });
1033
994
  });
1034
995
  it('Should return a promise that resolves with the data on success when refetching and lazy', async () => {
@@ -1040,8 +1001,7 @@ describe('useDataQuery', () => {
1040
1001
  const data = {
1041
1002
  answer: 42
1042
1003
  };
1043
-
1044
- const wrapper = (_ref25) => {
1004
+ const wrapper = _ref25 => {
1045
1005
  let {
1046
1006
  children
1047
1007
  } = _ref25;
@@ -1049,10 +1009,8 @@ describe('useDataQuery', () => {
1049
1009
  data: data
1050
1010
  }, children);
1051
1011
  };
1052
-
1053
1012
  const {
1054
- result,
1055
- waitForNextUpdate
1013
+ result
1056
1014
  } = renderHook(() => useDataQuery(query, {
1057
1015
  lazy: true
1058
1016
  }), {
@@ -1063,16 +1021,17 @@ describe('useDataQuery', () => {
1063
1021
  // This refetch will trigger our own refetch logic as the query is lazy
1064
1022
  ourPromise = result.current.refetch();
1065
1023
  });
1066
- await waitForNextUpdate();
1067
- expect(ourPromise).resolves.toEqual({
1068
- x: 42
1069
- });
1070
- expect(result.current).toMatchObject({
1071
- loading: false,
1072
- called: true,
1073
- data: {
1024
+ await waitFor(() => {
1025
+ expect(ourPromise).resolves.toEqual({
1074
1026
  x: 42
1075
- }
1027
+ });
1028
+ expect(result.current).toMatchObject({
1029
+ loading: false,
1030
+ called: true,
1031
+ data: {
1032
+ x: 42
1033
+ }
1034
+ });
1076
1035
  });
1077
1036
  });
1078
1037
  it('Should return a promise that resolves with the data on success when refetching and not lazy', async () => {
@@ -1084,8 +1043,7 @@ describe('useDataQuery', () => {
1084
1043
  const data = {
1085
1044
  answer: 42
1086
1045
  };
1087
-
1088
- const wrapper = (_ref26) => {
1046
+ const wrapper = _ref26 => {
1089
1047
  let {
1090
1048
  children
1091
1049
  } = _ref26;
@@ -1093,10 +1051,8 @@ describe('useDataQuery', () => {
1093
1051
  data: data
1094
1052
  }, children);
1095
1053
  };
1096
-
1097
1054
  const {
1098
- result,
1099
- waitForNextUpdate
1055
+ result
1100
1056
  } = renderHook(() => useDataQuery(query, {
1101
1057
  lazy: false
1102
1058
  }), {
@@ -1107,16 +1063,17 @@ describe('useDataQuery', () => {
1107
1063
  // This refetch will trigger react query's refetch logic as the query is not lazy
1108
1064
  reactQueryPromise = result.current.refetch();
1109
1065
  });
1110
- await waitForNextUpdate();
1111
- expect(reactQueryPromise).resolves.toEqual({
1112
- x: 42
1113
- });
1114
- expect(result.current).toMatchObject({
1115
- loading: false,
1116
- called: true,
1117
- data: {
1066
+ await waitFor(() => {
1067
+ expect(reactQueryPromise).resolves.toEqual({
1118
1068
  x: 42
1119
- }
1069
+ });
1070
+ expect(result.current).toMatchObject({
1071
+ loading: false,
1072
+ called: true,
1073
+ data: {
1074
+ x: 42
1075
+ }
1076
+ });
1120
1077
  });
1121
1078
  });
1122
1079
  it('Should return a promise that does not reject on errors when refetching and lazy', async () => {
@@ -1131,8 +1088,7 @@ describe('useDataQuery', () => {
1131
1088
  throw expectedError;
1132
1089
  }
1133
1090
  };
1134
-
1135
- const wrapper = (_ref27) => {
1091
+ const wrapper = _ref27 => {
1136
1092
  let {
1137
1093
  children
1138
1094
  } = _ref27;
@@ -1140,10 +1096,8 @@ describe('useDataQuery', () => {
1140
1096
  data: data
1141
1097
  }, children);
1142
1098
  };
1143
-
1144
1099
  const {
1145
- result,
1146
- waitForNextUpdate
1100
+ result
1147
1101
  } = renderHook(() => useDataQuery(query, {
1148
1102
  lazy: true
1149
1103
  }), {
@@ -1154,10 +1108,11 @@ describe('useDataQuery', () => {
1154
1108
  // This refetch will trigger our own refetch logic as the query is lazy
1155
1109
  ourPromise = result.current.refetch();
1156
1110
  });
1157
- await waitForNextUpdate();
1158
- expect(ourPromise).resolves.toBeUndefined();
1159
- expect(result.current).toMatchObject({
1160
- error: expectedError
1111
+ await waitFor(() => {
1112
+ expect(ourPromise).resolves.toBeUndefined();
1113
+ expect(result.current).toMatchObject({
1114
+ error: expectedError
1115
+ });
1161
1116
  });
1162
1117
  });
1163
1118
  it('Should return a promise that does not reject on errors when refetching and not lazy', async () => {
@@ -1172,8 +1127,7 @@ describe('useDataQuery', () => {
1172
1127
  throw expectedError;
1173
1128
  }
1174
1129
  };
1175
-
1176
- const wrapper = (_ref28) => {
1130
+ const wrapper = _ref28 => {
1177
1131
  let {
1178
1132
  children
1179
1133
  } = _ref28;
@@ -1181,10 +1135,8 @@ describe('useDataQuery', () => {
1181
1135
  data: data
1182
1136
  }, children);
1183
1137
  };
1184
-
1185
1138
  const {
1186
- result,
1187
- waitForNextUpdate
1139
+ result
1188
1140
  } = renderHook(() => useDataQuery(query, {
1189
1141
  lazy: false
1190
1142
  }), {
@@ -1195,10 +1147,11 @@ describe('useDataQuery', () => {
1195
1147
  // This refetch will trigger react query's refetch logic as the query is not lazy
1196
1148
  reactQueryPromise = result.current.refetch();
1197
1149
  });
1198
- await waitForNextUpdate();
1199
- expect(reactQueryPromise).resolves.toBeUndefined();
1200
- expect(result.current).toMatchObject({
1201
- error: expectedError
1150
+ await waitFor(() => {
1151
+ expect(reactQueryPromise).resolves.toBeUndefined();
1152
+ expect(result.current).toMatchObject({
1153
+ error: expectedError
1154
+ });
1202
1155
  });
1203
1156
  });
1204
1157
  it('Should accept new variables from refetch', async () => {
@@ -1209,7 +1162,7 @@ describe('useDataQuery', () => {
1209
1162
  const query = {
1210
1163
  x: {
1211
1164
  resource: 'answer',
1212
- id: (_ref29) => {
1165
+ id: _ref29 => {
1213
1166
  let {
1214
1167
  id
1215
1168
  } = _ref29;
@@ -1221,11 +1174,9 @@ describe('useDataQuery', () => {
1221
1174
  let {
1222
1175
  id
1223
1176
  } = _ref30;
1224
-
1225
1177
  switch (id) {
1226
1178
  case one:
1227
1179
  return Promise.resolve(resultOne);
1228
-
1229
1180
  case two:
1230
1181
  return Promise.resolve(resultTwo);
1231
1182
  }
@@ -1233,8 +1184,7 @@ describe('useDataQuery', () => {
1233
1184
  const data = {
1234
1185
  answer: mockSpy
1235
1186
  };
1236
-
1237
- const wrapper = (_ref31) => {
1187
+ const wrapper = _ref31 => {
1238
1188
  let {
1239
1189
  children
1240
1190
  } = _ref31;
@@ -1242,7 +1192,6 @@ describe('useDataQuery', () => {
1242
1192
  data: data
1243
1193
  }, children);
1244
1194
  };
1245
-
1246
1195
  const initialProps = {
1247
1196
  query,
1248
1197
  options: {
@@ -1252,29 +1201,30 @@ describe('useDataQuery', () => {
1252
1201
  }
1253
1202
  };
1254
1203
  const {
1255
- result,
1256
- waitForNextUpdate
1204
+ result
1257
1205
  } = renderHook(props => useDataQuery(props.query, props.options), {
1258
1206
  wrapper,
1259
1207
  initialProps
1260
1208
  });
1261
- await waitForNextUpdate();
1262
- expect(result.current).toMatchObject({
1263
- data: {
1264
- x: resultOne
1265
- }
1209
+ await waitFor(() => {
1210
+ expect(result.current).toMatchObject({
1211
+ data: {
1212
+ x: resultOne
1213
+ }
1214
+ });
1266
1215
  });
1267
1216
  act(() => {
1268
1217
  result.current.refetch({
1269
1218
  id: two
1270
1219
  });
1271
1220
  });
1272
- await waitForNextUpdate();
1273
- expect(mockSpy).toHaveBeenCalledTimes(2);
1274
- expect(result.current).toMatchObject({
1275
- data: {
1276
- x: resultTwo
1277
- }
1221
+ await waitFor(() => {
1222
+ expect(mockSpy).toHaveBeenCalledTimes(2);
1223
+ expect(result.current).toMatchObject({
1224
+ data: {
1225
+ x: resultTwo
1226
+ }
1227
+ });
1278
1228
  });
1279
1229
  });
1280
1230
  it('Should merge new variables from refetch with the existing variables', async () => {
@@ -1290,7 +1240,7 @@ describe('useDataQuery', () => {
1290
1240
  const query = {
1291
1241
  x: {
1292
1242
  resource: 'answer',
1293
- params: (_ref32) => {
1243
+ params: _ref32 => {
1294
1244
  let {
1295
1245
  one,
1296
1246
  two,
@@ -1308,8 +1258,7 @@ describe('useDataQuery', () => {
1308
1258
  const data = {
1309
1259
  answer: mockSpy
1310
1260
  };
1311
-
1312
- const wrapper = (_ref33) => {
1261
+ const wrapper = _ref33 => {
1313
1262
  let {
1314
1263
  children
1315
1264
  } = _ref33;
@@ -1317,28 +1266,29 @@ describe('useDataQuery', () => {
1317
1266
  data: data
1318
1267
  }, children);
1319
1268
  };
1320
-
1321
1269
  const {
1322
- result,
1323
- waitForNextUpdate
1270
+ result
1324
1271
  } = renderHook(() => useDataQuery(query, {
1325
1272
  variables: initialVariables
1326
1273
  }), {
1327
1274
  wrapper
1328
1275
  });
1329
- await waitForNextUpdate();
1330
- expect(mockSpy).toHaveBeenLastCalledWith(expect.anything(), expect.objectContaining({
1331
- params: initialVariables
1332
- }), expect.anything());
1276
+ await waitFor(() => {
1277
+ expect(mockSpy).toHaveBeenLastCalledWith(expect.anything(), expect.objectContaining({
1278
+ params: initialVariables
1279
+ }), expect.anything());
1280
+ });
1333
1281
  act(() => {
1334
1282
  result.current.refetch(newVariables);
1335
1283
  });
1336
- await waitForNextUpdate();
1337
- expect(mockSpy).toHaveBeenLastCalledWith(expect.anything(), expect.objectContaining({
1338
- params: { ...initialVariables,
1339
- ...newVariables
1340
- }
1341
- }), expect.anything());
1284
+ await waitFor(() => {
1285
+ expect(mockSpy).toHaveBeenLastCalledWith(expect.anything(), expect.objectContaining({
1286
+ params: {
1287
+ ...initialVariables,
1288
+ ...newVariables
1289
+ }
1290
+ }), expect.anything());
1291
+ });
1342
1292
  });
1343
1293
  });
1344
1294
  });