Skip to content
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

fix(useBaseQuery): set cacheTime to 1 in Suspense mode when cacheTime is set to 0 #2821

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions docs/src/pages/guides/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ setLogger({
})
```

## Set cacheTime to Infinity with Jest

`cacheTime` is set to 5 minutes by default. It means that the cache garbage collector timer will be triggered every 5 minutes. If you use Jest, you can set the `cacheTime` to `Infinity` to prevent "Jest did not exit one second after the test run completed" error message.

TkDodo marked this conversation as resolved.
Show resolved Hide resolved
## Testing Network Calls

The primary use for React Query is to cache network requests, so it's important that we can test our code is making the correct network requests in the first place.
Expand Down
45 changes: 45 additions & 0 deletions src/react/tests/suspense.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -890,4 +890,49 @@ describe("useQuery's in Suspense mode", () => {
await waitFor(() => rendered.getByText('error boundary'))
consoleMock.mockRestore()
})

it('should render the correct amount of times in Suspense mode when cacheTime is set to 0', async () => {
const key = queryKey()
let state: UseQueryResult<number> | null = null

let count = 0
let renders = 0

function Page() {
renders++

state = useQuery(
key,
async () => {
count++
await sleep(10)
return count
},
{ suspense: true, cacheTime: 0 }
)

return (
<div>
<span>rendered</span>
</div>
)
}

const rendered = renderWithClient(
queryClient,
<React.Suspense fallback="loading">
<Page />
</React.Suspense>
)

await waitFor(() =>
expect(state).toMatchObject({
data: 1,
status: 'success',
})
)

expect(renders).toBe(2)
expect(rendered.queryByText('rendered')).not.toBeNull()
})
})
6 changes: 6 additions & 0 deletions src/react/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export function useBaseQuery<
if (typeof defaultedOptions.staleTime !== 'number') {
defaultedOptions.staleTime = 1000
}

// Set cache time to 1 if the option has been set to 0
// when using suspense to prevent infinite loop of fetches
if (defaultedOptions.cacheTime === 0) {
defaultedOptions.cacheTime = 1
}
}

TkDodo marked this conversation as resolved.
Show resolved Hide resolved
if (defaultedOptions.suspense || defaultedOptions.useErrorBoundary) {
Expand Down