Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions components/common/api/create_interface_api.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ describe('createInterfaceApi', () => {
})

it('gets array data from a query with placeholder', async () => {
// Allow the test to control return timing
let resolveGetList: Function = () => {}
const canReturnGetList = new Promise((resolve) => {
resolveGetList = resolve
})

const api = createInterfaceApi({
actions: {},
endpoints: {
Expand All @@ -288,8 +294,9 @@ describe('createInterfaceApi', () => {
},
},
getList: {
query: () => {
return Promise.resolve([{ id: '1' }, { id: '2' }])
query: async () => {
await canReturnGetList
return [{ id: '1' }, { id: '2' }]
},
placeholderData: [] as { id: string }[],
},
Expand All @@ -303,11 +310,16 @@ describe('createInterfaceApi', () => {
return api.useGetList().getListData
}

// Verify placeholder data is returned before query finishes
let hookResult = await act(async () => renderHook(useTestQueryData))
expect(hookResult.result.current).toEqual([])
expect(api.getList.current()).toEqual([])

expect(api.getList.current()).toEqual([{ id: '1' }, { id: '2' }])
// Verify real data
resolveGetList()
await new Promise((resolve) => setTimeout(resolve, 0))

expect(api.getList.current()).toEqual([{ id: '1' }, { id: '2' }])
await act(() => hookResult.rerender())
expect(hookResult.result.current).toEqual([{ id: '1' }, { id: '2' }])
})
Expand Down
Loading