@@ -5,6 +5,7 @@ import { renderWithRedux } from 'ra-test';
5
5
import useGetMany from './useGetMany' ;
6
6
import { DataProviderContext } from '../dataProvider' ;
7
7
import { waitFor } from '@testing-library/react' ;
8
+ import { useState } from 'react' ;
8
9
9
10
const UseGetMany = ( {
10
11
resource,
@@ -18,6 +19,26 @@ const UseGetMany = ({
18
19
return < div > hello</ div > ;
19
20
} ;
20
21
22
+ let updateState ;
23
+
24
+ const UseCustomGetMany = ( {
25
+ resource,
26
+ ids,
27
+ options = { } ,
28
+ callback = null ,
29
+ ...rest
30
+ } ) => {
31
+ const [ stateIds , setStateIds ] = useState ( ids ) ;
32
+ const hookValue = useGetMany ( resource , stateIds , options ) ;
33
+ if ( callback ) callback ( hookValue ) ;
34
+
35
+ updateState = newIds => {
36
+ setStateIds ( newIds ) ;
37
+ } ;
38
+
39
+ return < div > hello</ div > ;
40
+ } ;
41
+
21
42
describe ( 'useGetMany' , ( ) => {
22
43
it ( 'should call the dataProvider with a GET_MANY on mount' , async ( ) => {
23
44
const dataProvider = {
@@ -200,6 +221,133 @@ describe('useGetMany', () => {
200
221
} ) ;
201
222
} ) ;
202
223
224
+ it ( 'should update loading state when ids change' , async ( ) => {
225
+ const dataProvider = {
226
+ getMany : jest . fn ( ( resource , params ) => {
227
+ if ( params . ids . length === 1 ) {
228
+ return Promise . resolve ( {
229
+ data : [ { id : 1 , title : 'foo' } ] ,
230
+ } ) ;
231
+ } else {
232
+ return Promise . resolve ( {
233
+ data : [
234
+ { id : 1 , title : 'foo' } ,
235
+ { id : 2 , title : 'bar' } ,
236
+ ] ,
237
+ } ) ;
238
+ }
239
+ } ) ,
240
+ } ;
241
+
242
+ const hookValue = jest . fn ( ) ;
243
+ renderWithRedux (
244
+ < DataProviderContext . Provider value = { dataProvider } >
245
+ < UseCustomGetMany
246
+ resource = "posts"
247
+ ids = { [ 1 ] }
248
+ callback = { hookValue }
249
+ />
250
+ </ DataProviderContext . Provider > ,
251
+ {
252
+ admin : {
253
+ resources : {
254
+ posts : {
255
+ data : { } ,
256
+ } ,
257
+ } ,
258
+ } ,
259
+ }
260
+ ) ;
261
+
262
+ await waitFor ( ( ) => {
263
+ expect ( dataProvider . getMany ) . toBeCalledTimes ( 1 ) ;
264
+ } ) ;
265
+
266
+ expect ( hookValue . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
267
+ data : [ undefined ] ,
268
+ error : null ,
269
+ loaded : false ,
270
+ loading : true ,
271
+ refetch : expect . any ( Function ) ,
272
+ } ) ;
273
+ expect ( hookValue . mock . calls [ 1 ] [ 0 ] ) . toEqual ( {
274
+ data : [ undefined ] ,
275
+ error : null ,
276
+ loaded : false ,
277
+ loading : true ,
278
+ refetch : expect . any ( Function ) ,
279
+ } ) ;
280
+ expect ( hookValue . mock . calls [ 2 ] [ 0 ] ) . toEqual ( {
281
+ data : [ { id : 1 , title : 'foo' } ] ,
282
+ error : null ,
283
+ loaded : false ,
284
+ loading : true ,
285
+ refetch : expect . any ( Function ) ,
286
+ } ) ;
287
+ expect ( hookValue . mock . calls [ 3 ] [ 0 ] ) . toEqual ( {
288
+ data : [ { id : 1 , title : 'foo' } ] ,
289
+ error : null ,
290
+ loaded : true ,
291
+ loading : false ,
292
+ refetch : expect . any ( Function ) ,
293
+ } ) ;
294
+
295
+ // Updating ids...
296
+ updateState ( [ 1 , 2 ] ) ;
297
+
298
+ await waitFor ( ( ) => {
299
+ expect ( dataProvider . getMany ) . toBeCalledTimes ( 2 ) ;
300
+ } ) ;
301
+
302
+ expect ( hookValue . mock . calls [ 4 ] [ 0 ] ) . toEqual ( {
303
+ data : [ { id : 1 , title : 'foo' } ] ,
304
+ error : null ,
305
+ loaded : true ,
306
+ loading : false ,
307
+ refetch : expect . any ( Function ) ,
308
+ } ) ;
309
+ expect ( hookValue . mock . calls [ 5 ] [ 0 ] ) . toEqual ( {
310
+ data : [ { id : 1 , title : 'foo' } ] ,
311
+ error : null ,
312
+ loaded : true ,
313
+ loading : true ,
314
+ refetch : expect . any ( Function ) ,
315
+ } ) ;
316
+ expect ( hookValue . mock . calls [ 6 ] [ 0 ] ) . toEqual ( {
317
+ data : [ { id : 1 , title : 'foo' } ] ,
318
+ error : null ,
319
+ loaded : true ,
320
+ loading : true ,
321
+ refetch : expect . any ( Function ) ,
322
+ } ) ;
323
+ expect ( hookValue . mock . calls [ 7 ] [ 0 ] ) . toEqual ( {
324
+ data : [
325
+ { id : 1 , title : 'foo' } ,
326
+ {
327
+ id : 2 ,
328
+ title : 'bar' ,
329
+ } ,
330
+ ] ,
331
+ error : null ,
332
+ loaded : true ,
333
+ loading : true ,
334
+ refetch : expect . any ( Function ) ,
335
+ } ) ;
336
+ expect ( hookValue . mock . calls [ 8 ] [ 0 ] ) . toEqual ( {
337
+ data : [
338
+ { id : 1 , title : 'foo' } ,
339
+ {
340
+ id : 2 ,
341
+ title : 'bar' ,
342
+ } ,
343
+ ] ,
344
+ error : null ,
345
+ loaded : true ,
346
+ loading : false ,
347
+ refetch : expect . any ( Function ) ,
348
+ } ) ;
349
+ } ) ;
350
+
203
351
it ( 'should retrieve results from redux state on mount' , ( ) => {
204
352
const hookValue = jest . fn ( ) ;
205
353
renderWithRedux (
0 commit comments