Skip to content

test(query-core): use fakeTimers for infiniteQueryBehavior.test.tsx #8961

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
38 changes: 23 additions & 15 deletions packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { waitFor } from '@testing-library/dom'
import { CancelledError, InfiniteQueryObserver } from '..'
import { createQueryClient, queryKey, sleep } from './utils'
import type {
Expand All @@ -14,13 +13,15 @@ describe('InfiniteQueryBehavior', () => {
let queryCache: QueryCache

beforeEach(() => {
vi.useFakeTimers()
queryClient = createQueryClient()
queryCache = queryClient.getQueryCache()
queryClient.mount()
})

afterEach(() => {
queryClient.clear()
vi.useRealTimers()
})

test('InfiniteQueryBehavior should throw an error if the queryFn is not defined', async () => {
Expand All @@ -41,7 +42,7 @@ describe('InfiniteQueryBehavior', () => {
observerResult = result
})

await waitFor(() => {
await vi.waitFor(() => {
const query = queryCache.find({ queryKey: key })!
return expect(observerResult).toMatchObject({
isError: true,
Expand Down Expand Up @@ -79,7 +80,7 @@ describe('InfiniteQueryBehavior', () => {
})

// Wait for the first page to be fetched
await waitFor(() =>
await vi.waitFor(() =>
expect(observerResult).toMatchObject({
isFetching: false,
data: { pages: [1], pageParams: [1] },
Expand Down Expand Up @@ -231,7 +232,7 @@ describe('InfiniteQueryBehavior', () => {
query.cancel()

// Wait for the first page to be cancelled
await waitFor(() =>
await vi.waitFor(() =>
expect(observerResult).toMatchObject({
isFetching: false,
isError: true,
Expand Down Expand Up @@ -280,7 +281,7 @@ describe('InfiniteQueryBehavior', () => {
})

// Wait for the first page to be fetched
await waitFor(() =>
await vi.waitFor(() =>
expect(observerResult).toMatchObject({
isFetching: false,
data: { pages: [1], pageParams: [1] },
Expand Down Expand Up @@ -385,25 +386,32 @@ describe('InfiniteQueryBehavior', () => {
})

// Fetch Page 1
const page1Data = await observer.fetchNextPage()
expect(page1Data.data?.pageParams).toEqual([1])
await vi.waitFor(async () => {
const page1Data = await observer.fetchNextPage()
expect(page1Data.data?.pageParams).toEqual([1])
})

// Fetch Page 2, as per the queryFn, this will reject 2 times then resolves
const page2Data = await observer.fetchNextPage()
expect(page2Data.data?.pageParams).toEqual([1, 2])
await vi.waitFor(async () => {
const page2Data = await observer.fetchNextPage()
expect(page2Data.data?.pageParams).toEqual([1, 2])
})

// Fetch Page 3
const page3Data = await observer.fetchNextPage()
expect(page3Data.data?.pageParams).toEqual([1, 2, 3])
await vi.waitFor(async () => {
const page3Data = await observer.fetchNextPage()
expect(page3Data.data?.pageParams).toEqual([1, 2, 3])
})

// Now the real deal; re-fetching this query **should not** stamp into an
// infinite loop where the retryer every time restarts from page 1
// once it reaches the page where it errors.
// For this to work, we'd need to reset the error count so we actually retry
errorCount = 0
const reFetchedData = await observer.refetch()

expect(reFetchedData.data?.pageParams).toEqual([1, 2, 3])
await vi.waitFor(async () => {
const reFetchedData = await observer.refetch()
expect(reFetchedData.data?.pageParams).toEqual([1, 2, 3])
})
})

test('should fetch even if initialPageParam is null', async () => {
Expand All @@ -424,7 +432,7 @@ describe('InfiniteQueryBehavior', () => {
observerResult = result
})

await waitFor(() =>
await vi.waitFor(() =>
expect(observerResult).toMatchObject({
isFetching: false,
data: { pages: ['data'], pageParams: [null] },
Expand Down
Loading