Skip to content
Open
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
10 changes: 6 additions & 4 deletions packages/query-core/src/__tests__/hydration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('dehydration and rehydration', () => {
hydrate(hydrationClient, parsed)
expect(hydrationCache.find({ queryKey: key })?.state.data).toBe('string')
await vi.advanceTimersByTimeAsync(100)
expect(hydrationCache.find({ queryKey: key })).toBeTruthy()
expect(hydrationCache.find({ queryKey: key })?.state.data).toBe('string')

queryClient.clear()
hydrationClient.clear()
Expand Down Expand Up @@ -330,9 +330,11 @@ describe('dehydration and rehydration', () => {
const hydrationClient = new QueryClient({ queryCache: hydrationCache })
hydrate(hydrationClient, parsed)

expect(hydrationCache.find({ queryKey: successKey })).toBeTruthy()
expect(hydrationCache.find({ queryKey: loadingKey })).toBeFalsy()
expect(hydrationCache.find({ queryKey: errorKey })).toBeFalsy()
expect(hydrationCache.find({ queryKey: successKey })?.state.data).toBe(
'success',
)
expect(hydrationCache.find({ queryKey: loadingKey })).toBeUndefined()
expect(hydrationCache.find({ queryKey: errorKey })).toBeUndefined()

queryClient.clear()
hydrationClient.clear()
Expand Down
4 changes: 3 additions & 1 deletion packages/query-core/src/__tests__/queriesObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ describe('queriesObserver', () => {
const queryCache = queryClient.getQueryCache()

expect(queryCache.find({ queryKey: key1, type: 'active' })).toBeUndefined()
expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeDefined()
expect(
queryCache.find({ queryKey: key2, type: 'active' })?.queryKey,
).toEqual(key2)
unsubscribe()
expect(queryCache.find({ queryKey: key1, type: 'active' })).toBeUndefined()
expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeUndefined()
Expand Down
21 changes: 10 additions & 11 deletions packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ describe('query', () => {

expect(queryFn).toHaveBeenCalledTimes(1)
const args = queryFn.mock.calls[0]![0]
expect(args).toBeDefined()
expect(args.pageParam).toBeUndefined()
expect(args.queryKey).toEqual(key)
expect(args.signal).toBeInstanceOf(AbortSignal)
Expand Down Expand Up @@ -601,9 +600,9 @@ describe('query', () => {
queryFn: () => 'data',
gcTime: 0,
})
expect(queryCache.find({ queryKey: key })).toBeDefined()
expect(queryCache.find({ queryKey: key })?.state.status).toBe('pending')
const unsubscribe = observer.subscribe(() => undefined)
expect(queryCache.find({ queryKey: key })).toBeDefined()
expect(queryCache.find({ queryKey: key })?.state.status).toBe('pending')
unsubscribe()

await vi.advanceTimersByTimeAsync(0)
Expand All @@ -619,11 +618,11 @@ describe('query', () => {
})
const unsubscribe = observer.subscribe(() => undefined)
await vi.advanceTimersByTimeAsync(20)
expect(queryCache.find({ queryKey: key })).toBeDefined()
expect(queryCache.find({ queryKey: key })?.state.data).toBe('data')
observer.refetch()
unsubscribe()
// unsubscribe should not remove even though gcTime has elapsed b/c query is still fetching
expect(queryCache.find({ queryKey: key })).toBeDefined()
expect(queryCache.find({ queryKey: key })?.state.data).toBe('data')
// should be removed after an additional staleTime wait
await vi.advanceTimersByTimeAsync(30)
expect(queryCache.find({ queryKey: key })).toBeUndefined()
Expand All @@ -636,16 +635,16 @@ describe('query', () => {
queryFn: () => 'data',
gcTime: 0,
})
expect(queryCache.find({ queryKey: key })).toBeDefined()
expect(queryCache.find({ queryKey: key })?.state.status).toBe('pending')
const unsubscribe = observer.subscribe(() => undefined)
await vi.advanceTimersByTimeAsync(100)
expect(queryCache.find({ queryKey: key })).toBeDefined()
expect(queryCache.find({ queryKey: key })?.state.data).toBe('data')
unsubscribe()
await vi.advanceTimersByTimeAsync(100)
expect(queryCache.find({ queryKey: key })).toBeUndefined()
queryClient.setQueryData(key, 'data')
await vi.advanceTimersByTimeAsync(100)
expect(queryCache.find({ queryKey: key })).toBeDefined()
expect(queryCache.find({ queryKey: key })?.state.data).toBe('data')
})

it('should return proper count of observers', () => {
Expand Down Expand Up @@ -1103,9 +1102,9 @@ describe('query', () => {
expect(queryFn).toHaveBeenCalledTimes(1)

expect(query.state.status).toBe('error')
expect(
query.state.error?.message.includes('Maximum call stack size exceeded'),
).toBeTruthy()
expect(query.state.error?.message).toContain(
'Maximum call stack size exceeded',
)

expect(consoleMock).toHaveBeenCalledWith(
expect.stringContaining(
Expand Down
7 changes: 4 additions & 3 deletions packages/query-core/src/__tests__/queryCache.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ describe('queryCache', () => {
'observerResultsUpdated', // 8. Observer result updated -> stale
])

const cachedQuery = queryCache.find({ queryKey: key })
queries.forEach((query) => {
expect(query).toBeDefined()
expect(query).toBe(cachedQuery)
})

unsubscribe()
Expand Down Expand Up @@ -161,7 +162,7 @@ describe('queryCache', () => {
})
await vi.advanceTimersByTimeAsync(100)
const query = queryCache.find({ queryKey: key })!
expect(query).toBeDefined()
expect(query.state.data).toBe('data1')
})

it('find should filter correctly with exact set to false', async () => {
Expand All @@ -172,7 +173,7 @@ describe('queryCache', () => {
})
await vi.advanceTimersByTimeAsync(100)
const query = queryCache.find({ queryKey: key, exact: false })!
expect(query).toBeDefined()
expect(query.state.data).toBe('data1')
})
})

Expand Down
12 changes: 5 additions & 7 deletions packages/query-core/src/__tests__/queryClient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -965,9 +965,9 @@ describe('queryClient', () => {
queryFn: () => 'data',
gcTime: 10,
})
expect(queryCache.find({ queryKey: key })).toBeDefined()
expect(queryCache.find({ queryKey: key })?.state.data).toBe('data')
await vi.advanceTimersByTimeAsync(15)
expect(queryCache.find({ queryKey: key })).not.toBeDefined()
expect(queryCache.find({ queryKey: key })).toBeUndefined()
})
})

Expand All @@ -979,15 +979,15 @@ describe('queryClient', () => {

// check the query was added to the cache
await queryClient.prefetchQuery({ queryKey: key, queryFn: fetchFn })
expect(queryCache.find({ queryKey: key })).toBeTruthy()
expect(queryCache.find({ queryKey: key })?.state.data).toBe('data')

// check the error doesn't occur
expect(() =>
queryClient.removeQueries({ queryKey: key, exact: true }),
).not.toThrow()

// check query was successful removed
expect(queryCache.find({ queryKey: key })).toBeFalsy()
expect(queryCache.find({ queryKey: key })).toBeUndefined()
})
})

Expand Down Expand Up @@ -1638,7 +1638,6 @@ describe('queryClient', () => {

state = queryClient.getQueryState(key)

expect(state).toBeTruthy()
expect(state?.data).toBeUndefined()
expect(state?.status).toEqual('pending')
expect(state?.fetchStatus).toEqual('idle')
Expand All @@ -1660,7 +1659,6 @@ describe('queryClient', () => {

state = queryClient.getQueryState(key)

expect(state).toBeTruthy()
expect(state?.data).toEqual('initial')
})

Expand Down Expand Up @@ -1936,7 +1934,7 @@ describe('queryClient', () => {
// still paused because we are still offline
expect(
newQueryClient.getMutationCache().getAll()[0]?.state.isPaused,
).toBeTruthy()
).toBe(true)

await newQueryClient.resumePausedMutations()

Expand Down