@@ -279,6 +279,12 @@ describe('createInterfaceApi', () => {
279279 } )
280280
281281 it ( 'gets array data from a query with placeholder' , async ( ) => {
282+ // Allow the test to control return timing
283+ let resolveGetList : Function = ( ) => { }
284+ const canReturnGetList = new Promise ( ( resolve ) => {
285+ resolveGetList = resolve
286+ } )
287+
282288 const api = createInterfaceApi ( {
283289 actions : { } ,
284290 endpoints : {
@@ -288,8 +294,9 @@ describe('createInterfaceApi', () => {
288294 } ,
289295 } ,
290296 getList : {
291- query : ( ) => {
292- return Promise . resolve ( [ { id : '1' } , { id : '2' } ] )
297+ query : async ( ) => {
298+ await canReturnGetList
299+ return [ { id : '1' } , { id : '2' } ]
293300 } ,
294301 placeholderData : [ ] as { id : string } [ ] ,
295302 } ,
@@ -303,11 +310,16 @@ describe('createInterfaceApi', () => {
303310 return api . useGetList ( ) . getListData
304311 }
305312
313+ // Verify placeholder data is returned before query finishes
306314 let hookResult = await act ( async ( ) => renderHook ( useTestQueryData ) )
307315 expect ( hookResult . result . current ) . toEqual ( [ ] )
316+ expect ( api . getList . current ( ) ) . toEqual ( [ ] )
308317
309- expect ( api . getList . current ( ) ) . toEqual ( [ { id : '1' } , { id : '2' } ] )
318+ // Verify real data
319+ resolveGetList ( )
320+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
310321
322+ expect ( api . getList . current ( ) ) . toEqual ( [ { id : '1' } , { id : '2' } ] )
311323 await act ( ( ) => hookResult . rerender ( ) )
312324 expect ( hookResult . result . current ) . toEqual ( [ { id : '1' } , { id : '2' } ] )
313325 } )
0 commit comments