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
1 change: 1 addition & 0 deletions docs/framework/react/reference/useQuery.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
data,
dataUpdatedAt,
error,
errorUpdateCount,
errorUpdatedAt,
failureCount,
failureReason,
Expand Down
1 change: 1 addition & 0 deletions docs/framework/solid/reference/useQuery.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
data,
dataUpdatedAt,
error,
errorUpdateCount,
errorUpdatedAt,
failureCount,
failureReason,
Expand Down
2 changes: 1 addition & 1 deletion packages/lit-query/src/tests/infinite-and-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ describe('createInfiniteQueryController', () => {

const nextPageResult = await infinite.fetchNextPage()
expect(nextPageResult.isFetchNextPageError).toBe(true)
expect(nextPageResult.error).toBeInstanceOf(Error)
expect(nextPageResult.error).toEqual(new Error('next-page-failed'))
await waitFor(() => infinite().isFetchNextPageError)
expect(infinite().data?.pages).toEqual([0])
})
Expand Down
6 changes: 3 additions & 3 deletions packages/lit-query/src/tests/mutation-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('createMutationController', () => {
await waitFor(() => mutation().isPending)
await expect(errorPromise).rejects.toThrow('negative-not-allowed')
await waitFor(() => mutation().isError)
expect(mutation().error).toBeInstanceOf(Error)
expect(mutation().error).toEqual(new Error('negative-not-allowed'))
})

it('M10: reset clears mutation state back to idle baseline', async () => {
Expand All @@ -188,7 +188,7 @@ describe('createMutationController', () => {
'reset-target',
)
await waitFor(() => mutation().isError)
expect(mutation().error).toBeInstanceOf(Error)
expect(mutation().error).toEqual(new Error('reset-target'))

mutation.reset()
expect(mutation().isIdle).toBe(true)
Expand Down Expand Up @@ -221,7 +221,7 @@ describe('createMutationController', () => {

expect(() => mutation.mutate(-1)).not.toThrow()
await waitFor(() => mutation().isError)
expect(mutation().error).toBeInstanceOf(Error)
expect(mutation().error).toEqual(new Error('negative-not-allowed'))

await expect(mutation.mutateAsync(-1)).rejects.toThrow(
'negative-not-allowed',
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/__tests__/timeoutManager.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('timeoutManager', () => {
expect.stringMatching(
/\[timeoutManager\]: Switching .* might result in unexpected behavior\..*/,
),
expect.anything(),
{ previous: customProvider, provider: customProvider2 },
)

// 3. Switching again with no intermediate calls should not warn
Expand Down
2 changes: 1 addition & 1 deletion packages/query-devtools/src/__tests__/Explorer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ describe('Explorer', () => {
).toBeInTheDocument()
expect(consoleError).toHaveBeenCalledWith(
'Failed to copy: ',
expect.any(Error),
new Error('denied'),
)
})

Expand Down
63 changes: 35 additions & 28 deletions packages/vue-query/src/__tests__/usePrefetchInfiniteQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ describe('usePrefetchInfiniteQuery', () => {
queryClient,
'prefetchInfiniteQuery',
)
const queryFn = vi.fn(() =>
Promise.resolve({ data: 'prefetched', currentPage: 1 }),
)
const queryFn = () =>
Promise.resolve({ data: 'prefetched', currentPage: 1 })
const getNextPageParam = () => undefined

const key = queryKey()

Expand All @@ -30,7 +30,7 @@ describe('usePrefetchInfiniteQuery', () => {
queryKey: key,
queryFn,
initialPageParam: 1,
getNextPageParam: () => undefined,
getNextPageParam,
},
queryClient,
)
Expand All @@ -40,7 +40,7 @@ describe('usePrefetchInfiniteQuery', () => {
queryKey: key,
queryFn,
initialPageParam: 1,
getNextPageParam: expect.any(Function),
getNextPageParam,
})
})

Expand All @@ -50,9 +50,8 @@ describe('usePrefetchInfiniteQuery', () => {
queryClient,
'prefetchInfiniteQuery',
)
const queryFn = vi.fn(() =>
Promise.resolve({ data: 'prefetched', currentPage: 1 }),
)
const queryFn = () =>
Promise.resolve({ data: 'prefetched', currentPage: 1 })

const key = queryKey()
queryClient.setQueryData(key, {
Expand Down Expand Up @@ -81,22 +80,26 @@ describe('usePrefetchInfiniteQuery', () => {
)
const nestedRef = ref('value')
const key = queryKey()
const queryFn = () =>
Promise.resolve({ data: 'prefetched', currentPage: 1 })
const getNextPageParam = () => undefined

usePrefetchInfiniteQuery(
{
queryKey: [...key, nestedRef],
queryFn: () => Promise.resolve({ data: 'prefetched', currentPage: 1 }),
queryFn,
initialPageParam: 1,
getNextPageParam: () => undefined,
getNextPageParam,
},
queryClient,
)

expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith(
expect.objectContaining({
queryKey: [...key, 'value'],
}),
)
expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith({
queryKey: [...key, 'value'],
queryFn,
initialPageParam: 1,
getNextPageParam,
})
})

it('should prefetch infinite query again when query key changes reactively', async () => {
Expand All @@ -107,33 +110,37 @@ describe('usePrefetchInfiniteQuery', () => {
)
const keyRef = ref('first')
const key = queryKey()
const queryFn = () =>
Promise.resolve({ data: keyRef.value, currentPage: 1 })
const getNextPageParam = () => undefined

usePrefetchInfiniteQuery(
() => ({
queryKey: [...key, keyRef.value],
queryFn: () => Promise.resolve({ data: keyRef.value, currentPage: 1 }),
queryFn,
initialPageParam: 1,
getNextPageParam: () => undefined,
getNextPageParam,
}),
queryClient,
)

expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(1)
expect(prefetchInfiniteQuerySpy).toHaveBeenLastCalledWith(
expect.objectContaining({
queryKey: [...key, 'first'],
}),
)
expect(prefetchInfiniteQuerySpy).toHaveBeenNthCalledWith(1, {
queryKey: [...key, 'first'],
queryFn,
initialPageParam: 1,
getNextPageParam,
})

keyRef.value = 'second'
await nextTick()

expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(2)
expect(prefetchInfiniteQuerySpy).toHaveBeenLastCalledWith(
expect.objectContaining({
queryKey: [...key, 'second'],
}),
)
expect(prefetchInfiniteQuerySpy).toHaveBeenNthCalledWith(2, {
queryKey: [...key, 'second'],
queryFn,
initialPageParam: 1,
getNextPageParam,
})
})

it('should warn when used outside of setup function in development mode', () => {
Expand Down
28 changes: 14 additions & 14 deletions packages/vue-query/src/__tests__/usePrefetchQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('usePrefetchQuery', () => {
it('should prefetch query if query state does not exist', () => {
const queryClient = new QueryClient()
const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery')
const queryFn = vi.fn(() => Promise.resolve('prefetched'))
const queryFn = () => Promise.resolve('prefetched')
const key = queryKey()

usePrefetchQuery(
Expand All @@ -37,7 +37,7 @@ describe('usePrefetchQuery', () => {
it('should not prefetch query if query state exists', () => {
const queryClient = new QueryClient()
const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery')
const queryFn = vi.fn(() => Promise.resolve('prefetched'))
const queryFn = () => Promise.resolve('prefetched')
const key = queryKey()
queryClient.setQueryData(key, 'existing')

Expand All @@ -57,49 +57,49 @@ describe('usePrefetchQuery', () => {
const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery')
const nestedRef = ref('value')
const key = queryKey()
const queryFn = () => Promise.resolve('prefetched')

usePrefetchQuery(
{
queryKey: [...key, nestedRef],
queryFn: () => Promise.resolve('prefetched'),
queryFn,
},
queryClient,
)

expect(prefetchQuerySpy).toHaveBeenCalledWith(
expect.objectContaining({
queryKey: [...key, 'value'],
}),
)
expect(prefetchQuerySpy).toHaveBeenCalledWith({
queryKey: [...key, 'value'],
queryFn,
})
})

it('should prefetch again when query key changes reactively', async () => {
const queryClient = new QueryClient()
const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery')
const keyRef = ref('first')
const key = queryKey()
const queryFn = () => Promise.resolve(keyRef.value)

usePrefetchQuery(
() => ({
queryKey: [...key, keyRef.value],
queryFn: () => Promise.resolve(keyRef.value),
queryFn,
}),
queryClient,
)

expect(prefetchQuerySpy).toHaveBeenCalledTimes(1)
expect(prefetchQuerySpy).toHaveBeenLastCalledWith({
expect(prefetchQuerySpy).toHaveBeenNthCalledWith(1, {
queryKey: [...key, 'first'],
queryFn: expect.any(Function),
queryFn,
})

keyRef.value = 'second'
await nextTick()

expect(prefetchQuerySpy).toHaveBeenCalledTimes(2)
expect(prefetchQuerySpy).toHaveBeenLastCalledWith({
expect(prefetchQuerySpy).toHaveBeenNthCalledWith(2, {
queryKey: [...key, 'second'],
queryFn: expect.any(Function),
queryFn,
})
})

Expand Down
8 changes: 6 additions & 2 deletions packages/vue-query/src/__tests__/useQueryClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ describe('useQueryClient', () => {
it('should throw an error when queryClient does not exist in the context', () => {
injectSpy.mockReturnValueOnce(undefined)

expect(useQueryClient).toThrow()
expect(useQueryClient).toThrow(
"No 'queryClient' found in Vue context, use 'VueQueryPlugin' to properly initialize the library.",
)
expect(injectSpy).toHaveBeenCalledTimes(1)
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT)
})

it('should throw an error when used outside of setup function', () => {
hasInjectionContextSpy.mockReturnValueOnce(false)

expect(useQueryClient).toThrow()
expect(useQueryClient).toThrow(
'vue-query hooks can only be used inside setup() function or functions that support injection context.',
)
expect(hasInjectionContextSpy).toHaveBeenCalledTimes(1)
})

Expand Down
Loading