Skip to content

Commit e0f9a2c

Browse files
authored
[createInterfaceApi] fix flakey test with timing of initial non-placeholder data (#34249)
1 parent 693b54c commit e0f9a2c

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

components/common/api/create_interface_api.test.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)