@@ -4,6 +4,7 @@ import expect from 'expect';
4
4
import { renderWithRedux } from 'ra-test' ;
5
5
import useGetList from './useGetList' ;
6
6
import { DataProviderContext } from '../dataProvider' ;
7
+ import { waitFor } from '@testing-library/react' ;
7
8
8
9
const UseGetList = ( {
9
10
resource = 'posts' ,
@@ -62,9 +63,10 @@ describe('useGetList', () => {
62
63
< UseGetList />
63
64
</ DataProviderContext . Provider >
64
65
) ;
65
- await new Promise ( resolve => setTimeout ( resolve , 10 ) ) ;
66
- expect ( dispatch ) . toBeCalledTimes ( 5 ) ;
67
- expect ( dataProvider . getList ) . toBeCalledTimes ( 1 ) ;
66
+ await waitFor ( ( ) => {
67
+ expect ( dispatch ) . toBeCalledTimes ( 5 ) ;
68
+ expect ( dataProvider . getList ) . toBeCalledTimes ( 1 ) ;
69
+ } ) ;
68
70
} ) ;
69
71
70
72
it ( 'should call the dataProvider on update when the resource changes' , async ( ) => {
@@ -164,14 +166,18 @@ describe('useGetList', () => {
164
166
} ,
165
167
}
166
168
) ;
167
- await new Promise ( resolve => setTimeout ( resolve , 10 ) ) ;
168
- expect ( hookValue . mock . calls . pop ( ) [ 0 ] ) . toEqual ( {
169
- data : { 1 : { id : 1 , title : 'foo' } , 2 : { id : 2 , title : 'bar' } } ,
170
- ids : [ 1 , 2 ] ,
171
- total : 2 ,
172
- loading : false ,
173
- loaded : true ,
174
- error : null ,
169
+ await waitFor ( ( ) => {
170
+ expect ( hookValue . mock . calls . pop ( ) [ 0 ] ) . toEqual ( {
171
+ data : {
172
+ 1 : { id : 1 , title : 'foo' } ,
173
+ 2 : { id : 2 , title : 'bar' } ,
174
+ } ,
175
+ ids : [ 1 , 2 ] ,
176
+ total : 2 ,
177
+ loading : false ,
178
+ loaded : true ,
179
+ error : null ,
180
+ } ) ;
175
181
} ) ;
176
182
} ) ;
177
183
@@ -211,8 +217,9 @@ describe('useGetList', () => {
211
217
}
212
218
) ;
213
219
expect ( hookValue . mock . calls . pop ( ) [ 0 ] . loading ) . toBe ( true ) ;
214
- await new Promise ( resolve => setTimeout ( resolve , 10 ) ) ;
215
- expect ( hookValue . mock . calls . pop ( ) [ 0 ] . loading ) . toBe ( false ) ;
220
+ await waitFor ( ( ) => {
221
+ expect ( hookValue . mock . calls . pop ( ) [ 0 ] . loading ) . toBe ( false ) ;
222
+ } ) ;
216
223
} ) ;
217
224
218
225
it ( 'should set the loading state depending on the availability of the data in the redux store' , ( ) => {
@@ -245,10 +252,11 @@ describe('useGetList', () => {
245
252
</ DataProviderContext . Provider >
246
253
) ;
247
254
expect ( hookValue . mock . calls . pop ( ) [ 0 ] . error ) . toBe ( null ) ;
248
- await new Promise ( resolve => setTimeout ( resolve , 10 ) ) ;
249
- expect ( hookValue . mock . calls . pop ( ) [ 0 ] . error ) . toEqual (
250
- new Error ( 'failed' )
251
- ) ;
255
+ await waitFor ( ( ) => {
256
+ expect ( hookValue . mock . calls . pop ( ) [ 0 ] . error ) . toEqual (
257
+ new Error ( 'failed' )
258
+ ) ;
259
+ } ) ;
252
260
} ) ;
253
261
254
262
it ( 'should execute success side effects on success' , async ( ) => {
@@ -286,22 +294,23 @@ describe('useGetList', () => {
286
294
/>
287
295
</ DataProviderContext . Provider >
288
296
) ;
289
- await new Promise ( resolve => setTimeout ( resolve , 10 ) ) ;
290
- expect ( onSuccess1 ) . toBeCalledTimes ( 1 ) ;
291
- expect ( onSuccess1 . mock . calls . pop ( ) [ 0 ] ) . toEqual ( {
292
- data : [
293
- { id : 1 , title : 'foo' } ,
294
- { id : 2 , title : 'bar' } ,
295
- ] ,
296
- total : 2 ,
297
- } ) ;
298
- expect ( onSuccess2 ) . toBeCalledTimes ( 1 ) ;
299
- expect ( onSuccess2 . mock . calls . pop ( ) [ 0 ] ) . toEqual ( {
300
- data : [
301
- { id : 3 , foo : 1 } ,
302
- { id : 4 , foo : 2 } ,
303
- ] ,
304
- total : 2 ,
297
+ await waitFor ( ( ) => {
298
+ expect ( onSuccess1 ) . toBeCalledTimes ( 1 ) ;
299
+ expect ( onSuccess1 . mock . calls . pop ( ) [ 0 ] ) . toEqual ( {
300
+ data : [
301
+ { id : 1 , title : 'foo' } ,
302
+ { id : 2 , title : 'bar' } ,
303
+ ] ,
304
+ total : 2 ,
305
+ } ) ;
306
+ expect ( onSuccess2 ) . toBeCalledTimes ( 1 ) ;
307
+ expect ( onSuccess2 . mock . calls . pop ( ) [ 0 ] ) . toEqual ( {
308
+ data : [
309
+ { id : 3 , foo : 1 } ,
310
+ { id : 4 , foo : 2 } ,
311
+ ] ,
312
+ total : 2 ,
313
+ } ) ;
305
314
} ) ;
306
315
} ) ;
307
316
@@ -317,8 +326,9 @@ describe('useGetList', () => {
317
326
< UseGetList options = { { onFailure } } />
318
327
</ DataProviderContext . Provider >
319
328
) ;
320
- await new Promise ( resolve => setTimeout ( resolve , 10 ) ) ;
321
- expect ( onFailure ) . toBeCalledTimes ( 1 ) ;
322
- expect ( onFailure . mock . calls . pop ( ) [ 0 ] ) . toEqual ( new Error ( 'failed' ) ) ;
329
+ await waitFor ( ( ) => {
330
+ expect ( onFailure ) . toBeCalledTimes ( 1 ) ;
331
+ expect ( onFailure . mock . calls . pop ( ) [ 0 ] ) . toEqual ( new Error ( 'failed' ) ) ;
332
+ } ) ;
323
333
} ) ;
324
334
} ) ;
0 commit comments