Skip to content

Commit 41b775f

Browse files
authored
test(query-core/queryObserver): add test for pending state with a synchronous queryFn (#10998)
1 parent 610e8d1 commit 41b775f

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

packages/query-core/src/__tests__/queryObserver.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,34 @@ describe('queryObserver', () => {
4242
expect(queryFn).toHaveBeenCalledTimes(1)
4343
})
4444

45+
it('should go through a pending state even when the queryFn returns synchronously', async () => {
46+
const key = queryKey()
47+
const queryFn = vi
48+
.fn<(...args: Array<unknown>) => string>()
49+
.mockReturnValue('data')
50+
const observer = new QueryObserver(queryClient, { queryKey: key, queryFn })
51+
const unsubscribe = observer.subscribe(() => undefined)
52+
53+
// A synchronous return value is still wrapped in a promise, so the query
54+
// goes through a fetching state before it resolves.
55+
expect(queryFn).toHaveBeenCalledTimes(1)
56+
expect(observer.getCurrentResult()).toMatchObject({
57+
status: 'pending',
58+
fetchStatus: 'fetching',
59+
data: undefined,
60+
})
61+
62+
await vi.advanceTimersByTimeAsync(0)
63+
64+
expect(observer.getCurrentResult()).toMatchObject({
65+
status: 'success',
66+
fetchStatus: 'idle',
67+
data: 'data',
68+
})
69+
70+
unsubscribe()
71+
})
72+
4573
it('should be able to read latest data after subscribing', () => {
4674
const key = queryKey()
4775
queryClient.setQueryData(key, 'data')

0 commit comments

Comments
 (0)