Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

usePrefetchInfiniteQuery.test.ts

163 lines4.4 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { nextTick, ref } from 'vue-demi'
3import { queryKey } from '@tanstack/query-test-utils'
4import { QueryClient } from '../queryClient'
5import { usePrefetchInfiniteQuery } from '../usePrefetchInfiniteQuery'
6​
7describe('usePrefetchInfiniteQuery', () => {
8 beforeEach(() => {
9 vi.useFakeTimers()
10 })
11​
12 afterEach(() => {
13 vi.useRealTimers()
14 })
15​
16 it('should prefetch infinite query if query state does not exist', () => {
17 const queryClient = new QueryClient()
18 const prefetchInfiniteQuerySpy = vi.spyOn(
19 queryClient,
20 'prefetchInfiniteQuery',
21 )
22 const queryFn = vi.fn(() =>
23 Promise.resolve({ data: 'prefetched', currentPage: 1 }),
24 )
25​
26 const key = queryKey()
27​
28 usePrefetchInfiniteQuery(
29 {
30 queryKey: key,
31 queryFn,
32 initialPageParam: 1,
33 getNextPageParam: () => undefined,
34 },
35 queryClient,
36 )
37​
38 expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(1)
39 expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith({
40 queryKey: key,
41 queryFn,
42 initialPageParam: 1,
43 getNextPageParam: expect.any(Function),
44 })
45 })
46​
47 it('should not prefetch infinite query if query state exists', () => {
48 const queryClient = new QueryClient()
49 const prefetchInfiniteQuerySpy = vi.spyOn(
50 queryClient,
51 'prefetchInfiniteQuery',
52 )
53 const queryFn = vi.fn(() =>
54 Promise.resolve({ data: 'prefetched', currentPage: 1 }),
55 )
56​
57 const key = queryKey()
58 queryClient.setQueryData(key, {
59 pages: [{ data: 'existing', currentPage: 1 }],
60 pageParams: [1],
61 })
62​
63 usePrefetchInfiniteQuery(
64 {
65 queryKey: key,
66 queryFn,
67 initialPageParam: 1,
68 getNextPageParam: () => undefined,
69 },
70 queryClient,
71 )
72​
73 expect(prefetchInfiniteQuerySpy).not.toHaveBeenCalled()
74 })
75​
76 it('should unwrap refs in infinite query options', () => {
77 const queryClient = new QueryClient()
78 const prefetchInfiniteQuerySpy = vi.spyOn(
79 queryClient,
80 'prefetchInfiniteQuery',
81 )
82 const nestedRef = ref('value')
83 const key = queryKey()
84​
85 usePrefetchInfiniteQuery(
86 {
87 queryKey: [...key, nestedRef],
88 queryFn: () => Promise.resolve({ data: 'prefetched', currentPage: 1 }),
89 initialPageParam: 1,
90 getNextPageParam: () => undefined,
91 },
92 queryClient,
93 )
94​
95 expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith(
96 expect.objectContaining({
97 queryKey: [...key, 'value'],
98 }),
99 )
100 })
101​
102 it('should prefetch infinite query again when query key changes reactively', async () => {
103 const queryClient = new QueryClient()
104 const prefetchInfiniteQuerySpy = vi.spyOn(
105 queryClient,
106 'prefetchInfiniteQuery',
107 )
108 const keyRef = ref('first')
109 const key = queryKey()
110​
111 usePrefetchInfiniteQuery(
112 () => ({
113 queryKey: [...key, keyRef.value],
114 queryFn: () => Promise.resolve({ data: keyRef.value, currentPage: 1 }),
115 initialPageParam: 1,
116 getNextPageParam: () => undefined,
117 }),
118 queryClient,
119 )
120​
121 expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(1)
122 expect(prefetchInfiniteQuerySpy).toHaveBeenLastCalledWith(
123 expect.objectContaining({
124 queryKey: [...key, 'first'],
125 }),
126 )
127​
128 keyRef.value = 'second'
129 await nextTick()
130​
131 expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(2)
132 expect(prefetchInfiniteQuerySpy).toHaveBeenLastCalledWith(
133 expect.objectContaining({
134 queryKey: [...key, 'second'],
135 }),
136 )
137 })
138​
139 it('should warn when used outside of setup function in development mode', () => {
140 vi.stubEnv('NODE_ENV', 'development')
141 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
142​
143 try {
144 usePrefetchInfiniteQuery(
145 {
146 queryKey: queryKey(),
147 queryFn: () =>
148 Promise.resolve({ data: 'prefetched', currentPage: 1 }),
149 initialPageParam: 1,
150 getNextPageParam: () => undefined,
151 },
152 new QueryClient(),
153 )
154​
155 expect(warnSpy).toHaveBeenCalledWith(
156 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
157 )
158 } finally {
159 warnSpy.mockRestore()
160 vi.unstubAllEnvs()
161 }
162 })
163})