Skip to content

Commit

Permalink
test: add another case
Browse files Browse the repository at this point in the history
  • Loading branch information
TkDodo committed Nov 22, 2024
1 parent 33058d1 commit 6529df2
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/query-core/src/__tests__/queryClient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,38 @@ describe('queryClient', () => {
// another fetch after another sleep because the data is now stale
expect(fetchFn).toHaveBeenCalledTimes(2)
})

test('should fetch if data has been manually invalidated', async () => {
const key = queryKey()

const fetchFn = vi.fn().mockImplementation(() => null)

queryClient.setQueryDefaults(key, { staleTime: Infinity })
queryClient.setQueryData(key, 'data')

await queryClient.fetchQuery({
queryKey: key,
queryFn: fetchFn,
staleTime: 100,
})
// no fetch because we have fresh data (not older than 100ms)
expect(fetchFn).toHaveBeenCalledTimes(0)

// mark query as invalidated
await queryClient.invalidateQueries({
queryKey: key,
refetchType: 'none',
})

await queryClient.fetchQuery({
queryKey: key,
queryFn: fetchFn,
staleTime: 100,
})

// fetch because even if not stale by time, it's manually invalidated
expect(fetchFn).toHaveBeenCalledTimes(1)
})
})

describe('fetchInfiniteQuery', () => {
Expand Down

0 comments on commit 6529df2

Please sign in to comment.