Skip to content

Commit

Permalink
fix: allow passing enabled:true when skipToken is present (#8407)
Browse files Browse the repository at this point in the history
  • Loading branch information
TkDodo authored Dec 6, 2024
1 parent d6621a8 commit 32d7189
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
5 changes: 1 addition & 4 deletions packages/query-core/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,7 @@ export class QueryClient {
defaultedOptions.networkMode = 'offlineFirst'
}

if (
defaultedOptions.enabled !== true &&
defaultedOptions.queryFn === skipToken
) {
if (defaultedOptions.queryFn === skipToken) {
defaultedOptions.enabled = false
}

Expand Down
33 changes: 33 additions & 0 deletions packages/react-query/src/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6422,6 +6422,39 @@ describe('useQuery', () => {
await waitFor(() => rendered.getByText('data: data'))
})

it('should allow enabled: true and queryFn: skipToken', async () => {
const consoleMock = vi
.spyOn(console, 'error')
.mockImplementation(() => undefined)
const key = queryKey()

function App() {
const query = useQuery({
queryKey: key,
queryFn: skipToken,
enabled: true,
})

return (
<div>
<div>
status: {query.status}, fetchStatus: {query.fetchStatus}
</div>
</div>
)
}

const rendered = renderWithClient(queryClient, <App />)

await waitFor(() =>
rendered.getByText('status: pending, fetchStatus: idle'),
)

// no warnings expected about skipToken / missing queryFn
expect(consoleMock).toHaveBeenCalledTimes(0)
consoleMock.mockRestore()
})

it('should return correct optimistic result when fetching after error', async () => {
const key = queryKey()
const error = new Error('oh no')
Expand Down

0 comments on commit 32d7189

Please sign in to comment.