Skip to content
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
40 changes: 40 additions & 0 deletions packages/query-core/src/__tests__/queryClient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,46 @@ describe('queryClient', () => {
expect(queryFn2).toHaveBeenCalledTimes(2)
})

test('should not refetch disabled inactive queries even if "refetchType" is "all', async () => {
const queryFn = vi
.fn<(...args: Array<unknown>) => string>()
.mockReturnValue('data1')
const observer = new QueryObserver(queryClient, {
queryKey: queryKey(),
queryFn: queryFn,
staleTime: Infinity,
enabled: false,
})
const unsubscribe = observer.subscribe(() => undefined)
unsubscribe()
await queryClient.invalidateQueries({
refetchType: 'all',
})
expect(queryFn).toHaveBeenCalledTimes(0)
})

test('should not refetch inactive queries that have a skipToken queryFn even if "refetchType" is "all', async () => {
const key = queryKey()
const observer = new QueryObserver(queryClient, {
queryKey: key,
queryFn: skipToken,
staleTime: Infinity,
})

queryClient.setQueryData(key, 'data1')

const unsubscribe = observer.subscribe(() => undefined)
unsubscribe()

expect(queryClient.getQueryState(key)?.dataUpdateCount).toBe(1)

await queryClient.invalidateQueries({
refetchType: 'all',
})

expect(queryClient.getQueryState(key)?.dataUpdateCount).toBe(1)
})

test('should cancel ongoing fetches if cancelRefetch option is set (default value)', async () => {
const key = queryKey()
const abortFn = vi.fn()
Expand Down
10 changes: 9 additions & 1 deletion packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
noop,
replaceData,
resolveEnabled,
skipToken,
timeUntilStale,
} from './utils'
import { notifyManager } from './notifyManager'
Expand Down Expand Up @@ -256,7 +257,14 @@ export class Query<
}

isDisabled(): boolean {
return this.getObserversCount() > 0 && !this.isActive()
if (this.getObserversCount() > 0) {
return !this.isActive()
}
// if a query has no observers, it should still be considered disabled if it never attempted a fetch
return (
this.options.queryFn === skipToken ||
this.state.dataUpdateCount + this.state.errorUpdateCount === 0
)
}

isStale(): boolean {
Expand Down