-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react-query): ensureSuspenseTimers should ALWAYS set staleTime to…
… 1000 when it is below 1000. (#8398) * fix(react-query): ensureSuspenseTimers should ALWAYS set staleTime to 1000 when it is below 1000. * ci: apply automated fixes * fix(react-query): fix ensureSuspenseTimers logic * test(react-query): add test code * test(react-query): delete unnecessary test code * test(react-query): Refectoring test code to reduce test time --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Dominik Dorfmeister <[email protected]>
- Loading branch information
1 parent
bc8b9e7
commit 0503282
Showing
3 changed files
with
203 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { act, render, waitFor } from '@testing-library/react' | ||
import { Suspense } from 'react' | ||
import { | ||
afterAll, | ||
beforeAll, | ||
beforeEach, | ||
describe, | ||
expect, | ||
it, | ||
vi, | ||
} from 'vitest' | ||
import { QueryClient, QueryClientProvider, useSuspenseQuery } from '..' | ||
import { queryKey } from './utils' | ||
import type { QueryKey } from '..' | ||
|
||
function renderWithSuspense(client: QueryClient, ui: React.ReactNode) { | ||
return render( | ||
<QueryClientProvider client={client}> | ||
<Suspense fallback="loading">{ui}</Suspense> | ||
</QueryClientProvider>, | ||
) | ||
} | ||
|
||
function createTestQuery(options: { | ||
fetchCount: { count: number } | ||
queryKey: QueryKey | ||
staleTime?: number | (() => number) | ||
}) { | ||
return function TestComponent() { | ||
const { data } = useSuspenseQuery({ | ||
queryKey: options.queryKey, | ||
queryFn: () => { | ||
options.fetchCount.count++ | ||
return 'data' | ||
}, | ||
staleTime: options.staleTime, | ||
}) | ||
return <div>data: {data}</div> | ||
} | ||
} | ||
|
||
describe('Suspense Timer Tests', () => { | ||
let queryClient: QueryClient | ||
let fetchCount: { count: number } | ||
|
||
beforeAll(() => { | ||
vi.useFakeTimers({ shouldAdvanceTime: true }) | ||
}) | ||
|
||
afterAll(() => { | ||
vi.useRealTimers() | ||
}) | ||
|
||
beforeEach(() => { | ||
queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: false, | ||
}, | ||
}, | ||
}) | ||
fetchCount = { count: 0 } | ||
}) | ||
|
||
it('should enforce minimum staleTime of 1000ms when using suspense with number', async () => { | ||
const TestComponent = createTestQuery({ | ||
fetchCount, | ||
queryKey: ['test'], | ||
staleTime: 10, | ||
}) | ||
|
||
const rendered = renderWithSuspense(queryClient, <TestComponent />) | ||
|
||
await waitFor(() => rendered.getByText('data: data')) | ||
|
||
rendered.rerender( | ||
<QueryClientProvider client={queryClient}> | ||
<Suspense fallback="loading"> | ||
<TestComponent /> | ||
</Suspense> | ||
</QueryClientProvider>, | ||
) | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(100) | ||
}) | ||
|
||
expect(fetchCount.count).toBe(1) | ||
}) | ||
|
||
it('should enforce minimum staleTime of 1000ms when using suspense with function', async () => { | ||
const TestComponent = createTestQuery({ | ||
fetchCount, | ||
queryKey: ['test-func'], | ||
staleTime: () => 10, | ||
}) | ||
|
||
const rendered = renderWithSuspense(queryClient, <TestComponent />) | ||
|
||
await waitFor(() => rendered.getByText('data: data')) | ||
|
||
rendered.rerender( | ||
<QueryClientProvider client={queryClient}> | ||
<Suspense fallback="loading"> | ||
<TestComponent /> | ||
</Suspense> | ||
</QueryClientProvider>, | ||
) | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(100) | ||
}) | ||
|
||
expect(fetchCount.count).toBe(1) | ||
}) | ||
|
||
it('should respect staleTime when value is greater than 1000ms', async () => { | ||
const TestComponent = createTestQuery({ | ||
fetchCount, | ||
queryKey: queryKey(), | ||
staleTime: 2000, | ||
}) | ||
|
||
const rendered = renderWithSuspense(queryClient, <TestComponent />) | ||
|
||
await waitFor(() => rendered.getByText('data: data')) | ||
|
||
rendered.rerender( | ||
<QueryClientProvider client={queryClient}> | ||
<Suspense fallback="loading"> | ||
<TestComponent /> | ||
</Suspense> | ||
</QueryClientProvider>, | ||
) | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1500) | ||
}) | ||
|
||
expect(fetchCount.count).toBe(1) | ||
}) | ||
|
||
it('should enforce minimum staleTime when undefined is provided', async () => { | ||
const TestComponent = createTestQuery({ | ||
fetchCount, | ||
queryKey: queryKey(), | ||
staleTime: undefined, | ||
}) | ||
|
||
const rendered = renderWithSuspense(queryClient, <TestComponent />) | ||
|
||
await waitFor(() => rendered.getByText('data: data')) | ||
|
||
rendered.rerender( | ||
<QueryClientProvider client={queryClient}> | ||
<Suspense fallback="loading"> | ||
<TestComponent /> | ||
</Suspense> | ||
</QueryClientProvider>, | ||
) | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(500) | ||
}) | ||
|
||
expect(fetchCount.count).toBe(1) | ||
}) | ||
|
||
it('should respect staleTime when function returns value greater than 1000ms', async () => { | ||
const TestComponent = createTestQuery({ | ||
fetchCount, | ||
queryKey: queryKey(), | ||
staleTime: () => 3000, | ||
}) | ||
|
||
const rendered = renderWithSuspense(queryClient, <TestComponent />) | ||
|
||
await waitFor(() => rendered.getByText('data: data')) | ||
|
||
rendered.rerender( | ||
<QueryClientProvider client={queryClient}> | ||
<Suspense fallback="loading"> | ||
<TestComponent /> | ||
</Suspense> | ||
</QueryClientProvider>, | ||
) | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(2000) | ||
}) | ||
|
||
expect(fetchCount.count).toBe(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters