Skip to content

test(query-core/queryObserver): remove 'vi.waitFor' and add 'advanceTimersByTimeAsync' #9343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
63 changes: 34 additions & 29 deletions packages/query-core/src/__tests__/queryObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ describe('queryObserver', () => {
fetchStatus: 'idle',
data: undefined,
})
await vi.waitFor(() => expect(count).toBe(0))
await vi.advanceTimersByTimeAsync(10)
expect(count).toBe(0)

unsubscribe()
})
Expand All @@ -128,7 +129,8 @@ describe('queryObserver', () => {
data: undefined,
})

await vi.waitFor(() => expect(count).toBe(1))
await vi.advanceTimersByTimeAsync(10)
expect(count).toBe(1)
expect(observer.getCurrentResult()).toMatchObject({
status: 'success',
fetchStatus: 'idle',
Expand Down Expand Up @@ -157,8 +159,8 @@ describe('queryObserver', () => {
fetchStatus: 'fetching',
data: undefined,
})

await vi.waitFor(() => expect(count).toBe(1))
await vi.advanceTimersByTimeAsync(10)
expect(count).toBe(1)

unsubscribe()
})
Expand Down Expand Up @@ -194,7 +196,8 @@ describe('queryObserver', () => {
fetchStatus: 'fetching',
data: undefined,
})
await vi.waitFor(() => expect(count).toBe(1))
await vi.advanceTimersByTimeAsync(10)
expect(count).toBe(1)

unsubscribe()
})
Expand All @@ -208,20 +211,23 @@ describe('queryObserver', () => {
queryClient.invalidateQueries({ queryKey: key, refetchType: 'inactive' })

// should not refetch since it was active and we only refetch inactive
await vi.waitFor(() => expect(count).toBe(0))
await vi.advanceTimersByTimeAsync(10)
expect(count).toBe(0)

queryClient.invalidateQueries({ queryKey: key, refetchType: 'active' })

// should refetch since it was active and we refetch active
await vi.waitFor(() => expect(count).toBe(1))
await vi.advanceTimersByTimeAsync(10)
expect(count).toBe(1)

// Toggle enabled
enabled = false

// should not refetch since it is not active and we only refetch active
queryClient.invalidateQueries({ queryKey: key, refetchType: 'active' })

await vi.waitFor(() => expect(count).toBe(1))
await vi.advanceTimersByTimeAsync(10)
expect(count).toBe(1)

unsubscribe()
})
Expand Down Expand Up @@ -251,7 +257,8 @@ describe('queryObserver', () => {
data: undefined,
})

await vi.waitFor(() => expect(count).toBe(1))
await vi.advanceTimersByTimeAsync(10)
expect(count).toBe(1)

// re-subscribe after data comes in
unsubscribe = observer.subscribe(vi.fn())
Expand Down Expand Up @@ -1172,8 +1179,10 @@ describe('queryObserver', () => {
}
})

await vi.waitFor(() => expect(results[0]?.isStale).toBe(false))
await vi.waitFor(() => expect(results[1]?.isStale).toBe(true))
await vi.advanceTimersByTimeAsync(25)
expect(results[0]?.isStale).toBe(false)
await vi.advanceTimersByTimeAsync(1)
expect(results[1]?.isStale).toBe(true)

unsubscribe()
})
Expand All @@ -1200,7 +1209,8 @@ describe('queryObserver', () => {
}
})

await vi.waitFor(() => expect(results[0]?.isStale).toBe(false))
await vi.advanceTimersByTimeAsync(5)
expect(results[0]?.isStale).toBe(false)

unsubscribe()
})
Expand All @@ -1224,9 +1234,8 @@ describe('queryObserver', () => {
results.push(observer.getCurrentResult())
})

await vi.waitFor(() => {
expect(results.at(-1)?.data).toBe('data')
})
await vi.advanceTimersByTimeAsync(10)
expect(results.at(-1)?.data).toBe('data')

const numberOfUniquePromises = new Set(
results.map((result) => result.promise),
Expand Down Expand Up @@ -1257,9 +1266,8 @@ describe('queryObserver', () => {
results.push(observer.getCurrentResult())
})

await vi.waitFor(() => {
expect(results.at(-1)?.status).toBe('error')
})
await vi.advanceTimersByTimeAsync(5)
expect(results.at(-1)?.status).toBe('error')

expect(
results.every((result) => result.promise === results[0]!.promise),
Expand All @@ -1269,10 +1277,9 @@ describe('queryObserver', () => {
// fail again
const lengthBefore = results.length
observer.refetch()
await vi.waitFor(() => {
expect(results.length).toBeGreaterThan(lengthBefore)
expect(results.at(-1)?.status).toBe('error')
})
await vi.advanceTimersByTimeAsync(5)
expect(results.length).toBeGreaterThan(lengthBefore)
expect(results.at(-1)?.status).toBe('error')

const numberOfUniquePromises = new Set(
results.map((result) => result.promise),
Expand All @@ -1285,9 +1292,8 @@ describe('queryObserver', () => {
succeeds = true
observer.refetch()

await vi.waitFor(() => {
results.at(-1)?.status === 'success'
})
await vi.advanceTimersByTimeAsync(5)
results.at(-1)?.status === 'success'

const numberOfUniquePromises = new Set(
results.map((result) => result.promise),
Expand Down Expand Up @@ -1359,10 +1365,9 @@ describe('queryObserver', () => {
results.push(result)
})

await vi.waitFor(() => {
const lastResult = results[results.length - 1]
expect(lastResult?.status).toBe('error')
})
await vi.advanceTimersByTimeAsync(0)
const lastResult = results[results.length - 1]
expect(lastResult?.status).toBe('error')

expect(results.length).toBe(1)
expect(results[0]).toMatchObject({
Expand Down
Loading