1
1
import React from 'react' ;
2
2
import gql from 'graphql-tag' ;
3
+ import { waitFor } from '@testing-library/react' ;
3
4
import { renderHook } from '@testing-library/react-hooks' ;
4
5
5
6
import { ApolloClient , InMemoryCache } from '../../../core' ;
@@ -17,7 +18,7 @@ describe('useLazyQuery Hook', () => {
17
18
delay : 20 ,
18
19
} ,
19
20
] ;
20
- const { result, waitForNextUpdate } = renderHook (
21
+ const { result } = renderHook (
21
22
( ) => useLazyQuery ( query ) ,
22
23
{
23
24
wrapper : ( { children } ) => (
@@ -33,11 +34,13 @@ describe('useLazyQuery Hook', () => {
33
34
const execute = result . current [ 0 ] ;
34
35
setTimeout ( ( ) => execute ( ) ) ;
35
36
36
- await waitForNextUpdate ( ) ;
37
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
37
+ await waitFor ( ( ) => {
38
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
39
+ } , { interval : 10 } ) ;
38
40
39
- await waitForNextUpdate ( ) ;
40
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
41
+ await waitFor ( ( ) => {
42
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
43
+ } ) ;
41
44
expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'world' } ) ;
42
45
} ) ;
43
46
@@ -76,7 +79,7 @@ describe('useLazyQuery Hook', () => {
76
79
} ,
77
80
] ;
78
81
79
- const { result, waitForNextUpdate } = renderHook (
82
+ const { result } = renderHook (
80
83
( ) => useLazyQuery ( query ) ,
81
84
{
82
85
wrapper : ( { children } ) => (
@@ -92,12 +95,14 @@ describe('useLazyQuery Hook', () => {
92
95
const execute = result . current [ 0 ] ;
93
96
setTimeout ( ( ) => execute ( ) ) ;
94
97
95
- await waitForNextUpdate ( ) ;
96
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
98
+ await waitFor ( ( ) => {
99
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
100
+ } , { interval : 10 } ) ;
97
101
expect ( result . current [ 1 ] . called ) . toBe ( true ) ;
98
102
99
- await waitForNextUpdate ( ) ;
100
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
103
+ await waitFor ( ( ) => {
104
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
105
+ } ) ;
101
106
expect ( result . current [ 1 ] . called ) . toBe ( true ) ;
102
107
} ) ;
103
108
@@ -111,7 +116,7 @@ describe('useLazyQuery Hook', () => {
111
116
} ,
112
117
] ;
113
118
114
- const { result, waitForNextUpdate } = renderHook (
119
+ const { result } = renderHook (
115
120
// skip isn’t actually an option on the types
116
121
( ) => useLazyQuery ( query , { skip : true } as any ) ,
117
122
{
@@ -128,12 +133,14 @@ describe('useLazyQuery Hook', () => {
128
133
const execute = result . current [ 0 ] ;
129
134
setTimeout ( ( ) => execute ( ) ) ;
130
135
131
- await waitForNextUpdate ( ) ;
132
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
136
+ await waitFor ( ( ) => {
137
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
138
+ } , { interval : 10 } ) ;
133
139
expect ( result . current [ 1 ] . called ) . toBe ( true ) ;
134
140
135
- await waitForNextUpdate ( ) ;
136
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
141
+ await waitFor ( ( ) => {
142
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
143
+ } ) ;
137
144
expect ( result . current [ 1 ] . called ) . toBe ( true ) ;
138
145
} ) ;
139
146
@@ -152,7 +159,7 @@ describe('useLazyQuery Hook', () => {
152
159
} ,
153
160
] ;
154
161
155
- const { result, waitForNextUpdate } = renderHook (
162
+ const { result } = renderHook (
156
163
( ) => useLazyQuery ( query , {
157
164
variables : { id : 1 } ,
158
165
} ) ,
@@ -168,12 +175,13 @@ describe('useLazyQuery Hook', () => {
168
175
const execute = result . current [ 0 ] ;
169
176
setTimeout ( ( ) => execute ( ) ) ;
170
177
171
- await waitForNextUpdate ( ) ;
178
+ await waitFor ( ( ) => {
179
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
180
+ } , { interval : 10 } ) ;
172
181
173
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
174
- await waitForNextUpdate ( ) ;
175
-
176
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
182
+ await waitFor ( ( ) => {
183
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
184
+ } ) ;
177
185
expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'world 1' } ) ;
178
186
} ) ;
179
187
@@ -197,7 +205,7 @@ describe('useLazyQuery Hook', () => {
197
205
} ,
198
206
] ;
199
207
200
- const { result, waitForNextUpdate } = renderHook (
208
+ const { result } = renderHook (
201
209
( ) => useLazyQuery ( query , {
202
210
variables : { id : 1 } ,
203
211
} ) ,
@@ -213,11 +221,13 @@ describe('useLazyQuery Hook', () => {
213
221
const execute = result . current [ 0 ] ;
214
222
setTimeout ( ( ) => execute ( { variables : { id : 2 } } ) ) ;
215
223
216
- await waitForNextUpdate ( ) ;
217
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
224
+ await waitFor ( ( ) => {
225
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
226
+ } , { interval : 10 } ) ;
218
227
219
- await waitForNextUpdate ( ) ;
220
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
228
+ await waitFor ( ( ) => {
229
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
230
+ } ) ;
221
231
expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'world 2' } ) ;
222
232
} ) ;
223
233
@@ -236,7 +246,7 @@ describe('useLazyQuery Hook', () => {
236
246
} ,
237
247
] ;
238
248
239
- const { result, waitForNextUpdate } = renderHook (
249
+ const { result } = renderHook (
240
250
( ) => useLazyQuery ( query , {
241
251
fetchPolicy : 'network-only' ,
242
252
} ) ,
@@ -253,22 +263,26 @@ describe('useLazyQuery Hook', () => {
253
263
const execute = result . current [ 0 ] ;
254
264
setTimeout ( ( ) => execute ( ) ) ;
255
265
256
- await waitForNextUpdate ( ) ;
257
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
266
+ await waitFor ( ( ) => {
267
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
268
+ } , { interval : 10 } ) ;
258
269
259
- await waitForNextUpdate ( ) ;
260
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
270
+ await waitFor ( ( ) => {
271
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
272
+ } ) ;
261
273
expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'world 1' } ) ;
262
274
263
275
setTimeout ( ( ) => execute ( ) ) ;
264
276
265
- await waitForNextUpdate ( ) ;
266
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
277
+ await waitFor ( ( ) => {
278
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
279
+ } ) ;
267
280
expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'world 1' } ) ;
268
281
269
- await waitForNextUpdate ( ) ;
282
+ await waitFor ( ( ) => {
283
+ expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'world 2' } ) ;
284
+ } ) ;
270
285
expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
271
- expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'world 2' } ) ;
272
286
} ) ;
273
287
274
288
it ( 'should persist previous data when a query is re-run' , async ( ) => {
@@ -286,7 +300,7 @@ describe('useLazyQuery Hook', () => {
286
300
} ,
287
301
] ;
288
302
289
- const { result, waitForNextUpdate } = renderHook (
303
+ const { result } = renderHook (
290
304
( ) => useLazyQuery ( query , {
291
305
notifyOnNetworkStatusChange : true ,
292
306
} ) ,
@@ -305,26 +319,30 @@ describe('useLazyQuery Hook', () => {
305
319
const execute = result . current [ 0 ] ;
306
320
setTimeout ( ( ) => execute ( ) ) ;
307
321
308
- await waitForNextUpdate ( ) ;
309
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
322
+ await waitFor ( ( ) => {
323
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
324
+ } , { interval : 10 } ) ;
310
325
expect ( result . current [ 1 ] . data ) . toBe ( undefined ) ;
311
326
expect ( result . current [ 1 ] . previousData ) . toBe ( undefined ) ;
312
327
313
- await waitForNextUpdate ( ) ;
314
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
328
+ await waitFor ( ( ) => {
329
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
330
+ } ) ;
315
331
expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'world 1' } ) ;
316
332
expect ( result . current [ 1 ] . previousData ) . toBe ( undefined ) ;
317
333
318
334
const refetch = result . current [ 1 ] . refetch ;
319
335
setTimeout ( ( ) => refetch ! ( ) ) ;
320
336
321
- await waitForNextUpdate ( ) ;
322
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
337
+ await waitFor ( ( ) => {
338
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
339
+ } , { interval : 10 } ) ;
323
340
expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'world 1' } ) ;
324
341
expect ( result . current [ 1 ] . previousData ) . toEqual ( { hello : 'world 1' } ) ;
325
342
326
- await waitForNextUpdate ( ) ;
327
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
343
+ await waitFor ( ( ) => {
344
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
345
+ } ) ;
328
346
expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'world 2' } ) ;
329
347
expect ( result . current [ 1 ] . previousData ) . toEqual ( { hello : 'world 1' } ) ;
330
348
} ) ;
@@ -351,7 +369,7 @@ describe('useLazyQuery Hook', () => {
351
369
< MockedProvider mocks = { mocks } > { children } </ MockedProvider >
352
370
) ;
353
371
354
- const { result, waitForNextUpdate } = renderHook (
372
+ const { result } = renderHook (
355
373
( ) => useLazyQuery ( query ) ,
356
374
{ wrapper } ,
357
375
) ;
@@ -363,24 +381,33 @@ describe('useLazyQuery Hook', () => {
363
381
result . current [ 1 ] . startPolling ( 10 ) ;
364
382
} ) ;
365
383
366
- await waitForNextUpdate ( ) ;
367
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
384
+ await waitFor ( ( ) => {
385
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
386
+ } , { interval : 10 } ) ;
368
387
369
- await waitForNextUpdate ( ) ;
388
+ await waitFor ( ( ) => {
389
+ expect ( result . current [ 1 ] . data ) . toEqual ( { hello : "world 1" } ) ;
390
+ } , { interval : 10 } ) ;
370
391
expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
371
- expect ( result . current [ 1 ] . data ) . toEqual ( { hello : "world 1" } ) ;
372
392
373
- await waitForNextUpdate ( ) ;
393
+ await waitFor ( ( ) => {
394
+ expect ( result . current [ 1 ] . data ) . toEqual ( { hello : "world 2" } ) ;
395
+ } , { interval : 10 } ) ;
374
396
expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
375
- expect ( result . current [ 1 ] . data ) . toEqual ( { hello : "world 2" } ) ;
376
397
377
- await waitForNextUpdate ( ) ;
398
+ await waitFor ( ( ) => {
399
+ expect ( result . current [ 1 ] . data ) . toEqual ( { hello : "world 3" } ) ;
400
+ } , { interval : 10 } ) ;
378
401
379
402
expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
380
- expect ( result . current [ 1 ] . data ) . toEqual ( { hello : "world 3" } ) ;
381
403
404
+ const previousResult = result . current
382
405
result . current [ 1 ] . stopPolling ( ) ;
383
- await expect ( waitForNextUpdate ( { timeout : 20 } ) ) . rejects . toThrow ( 'Timed out' ) ;
406
+ await expect ( waitFor ( ( ) => {
407
+ if ( previousResult === result . current ) {
408
+ throw new Error ( 'data stayed the same. this is intended' )
409
+ }
410
+ } ) ) . rejects . toThrow ( 'data stayed the same. this is intended' ) ;
384
411
} ) ;
385
412
386
413
it ( 'should persist previous data when a query is re-run and variable changes' , async ( ) => {
@@ -422,7 +449,7 @@ describe('useLazyQuery Hook', () => {
422
449
} ,
423
450
] ;
424
451
425
- const { result, waitForNextUpdate } = renderHook (
452
+ const { result } = renderHook (
426
453
( ) => useLazyQuery ( CAR_QUERY_BY_ID ) ,
427
454
{
428
455
wrapper : ( { children } ) => (
@@ -439,24 +466,28 @@ describe('useLazyQuery Hook', () => {
439
466
const execute = result . current [ 0 ] ;
440
467
setTimeout ( ( ) => execute ( { variables : { id : 1 } } ) ) ;
441
468
442
- await waitForNextUpdate ( ) ;
443
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
469
+ await waitFor ( ( ) => {
470
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
471
+ } , { interval : 10 } ) ;
444
472
expect ( result . current [ 1 ] . data ) . toBe ( undefined ) ;
445
473
expect ( result . current [ 1 ] . previousData ) . toBe ( undefined ) ;
446
474
447
- await waitForNextUpdate ( ) ;
448
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
475
+ await waitFor ( ( ) => {
476
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
477
+ } , { interval : 10 } ) ;
449
478
expect ( result . current [ 1 ] . data ) . toEqual ( data1 ) ;
450
479
expect ( result . current [ 1 ] . previousData ) . toBe ( undefined ) ;
451
480
452
481
setTimeout ( ( ) => execute ( { variables : { id : 2 } } ) ) ;
453
- await waitForNextUpdate ( ) ;
454
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
482
+ await waitFor ( ( ) => {
483
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
484
+ } , { interval : 10 } ) ;
455
485
expect ( result . current [ 1 ] . data ) . toBe ( undefined ) ;
456
486
expect ( result . current [ 1 ] . previousData ) . toEqual ( data1 ) ;
457
487
458
- await waitForNextUpdate ( ) ;
459
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
488
+ await waitFor ( ( ) => {
489
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
490
+ } , { interval : 10 } ) ;
460
491
expect ( result . current [ 1 ] . data ) . toEqual ( data2 ) ;
461
492
expect ( result . current [ 1 ] . previousData ) . toEqual ( data1 ) ;
462
493
} ) ;
@@ -480,7 +511,7 @@ describe('useLazyQuery Hook', () => {
480
511
481
512
cache . writeQuery ( { query, data : { hello : 'from cache' } } ) ;
482
513
483
- const { result, waitForNextUpdate } = renderHook (
514
+ const { result } = renderHook (
484
515
( ) => useLazyQuery ( query , { fetchPolicy : 'cache-and-network' } ) ,
485
516
{
486
517
wrapper : ( { children } ) => (
@@ -496,14 +527,16 @@ describe('useLazyQuery Hook', () => {
496
527
const execute = result . current [ 0 ] ;
497
528
setTimeout ( ( ) => execute ( ) ) ;
498
529
499
- await waitForNextUpdate ( ) ;
530
+ await waitFor ( ( ) => {
531
+ // TODO: FIXME
532
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
533
+ } , { interval : 10 } ) ;
500
534
501
- // TODO: FIXME
502
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
503
535
expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'from cache' } ) ;
504
536
505
- await waitForNextUpdate ( ) ;
506
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
537
+ await waitFor ( ( ) => {
538
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
539
+ } , { interval : 10 } ) ;
507
540
expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'from link' } ) ;
508
541
} ) ;
509
542
@@ -516,7 +549,7 @@ describe('useLazyQuery Hook', () => {
516
549
delay : 20 ,
517
550
} ,
518
551
] ;
519
- const { result, waitForNextUpdate } = renderHook (
552
+ const { result } = renderHook (
520
553
( ) => useLazyQuery ( query ) ,
521
554
{
522
555
wrapper : ( { children } ) => (
@@ -533,11 +566,13 @@ describe('useLazyQuery Hook', () => {
533
566
const mock = jest . fn ( ) ;
534
567
setTimeout ( ( ) => mock ( execute ( ) ) ) ;
535
568
536
- await waitForNextUpdate ( ) ;
537
- expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
569
+ await waitFor ( ( ) => {
570
+ expect ( result . current [ 1 ] . loading ) . toBe ( true ) ;
571
+ } , { interval : 10 } ) ;
538
572
539
- await waitForNextUpdate ( ) ;
540
- expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
573
+ await waitFor ( ( ) => {
574
+ expect ( result . current [ 1 ] . loading ) . toBe ( false ) ;
575
+ } , { interval : 10 } ) ;
541
576
expect ( result . current [ 1 ] . data ) . toEqual ( { hello : 'world' } ) ;
542
577
543
578
expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
0 commit comments