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
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
Loading