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

usePrefetchInfiniteQuery.test.ts

170 lines4.5 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 = () =>
23 Promise.resolve({ data: 'prefetched', currentPage: 1 })
24 const getNextPageParam = () => undefined
25​
26 const key = queryKey()
27​
28 usePrefetchInfiniteQuery(
29 {
30 queryKey: key,
31 queryFn,
32 initialPageParam: 1,
33 getNextPageParam,
34 },
35 queryClient,
36 )
37​
38 expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(1)
39 expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith({
40 queryKey: key,
41 queryFn,
42 initialPageParam: 1,
43 getNextPageParam,
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 = () =>
54 Promise.resolve({ data: 'prefetched', currentPage: 1 })
55​
56 const key = queryKey()
57 queryClient.setQueryData(key, {
58 pages: [{ data: 'existing', currentPage: 1 }],
59 pageParams: [1],
60 })
61​
62 usePrefetchInfiniteQuery(
63 {
64 queryKey: key,
65 queryFn,
66 initialPageParam: 1,
67 getNextPageParam: () => undefined,
68 },
69 queryClient,
70 )
71​
72 expect(prefetchInfiniteQuerySpy).not.toHaveBeenCalled()
73 })
74​
75 it('should unwrap refs in infinite query options', () => {
76 const queryClient = new QueryClient()
77 const prefetchInfiniteQuerySpy = vi.spyOn(
78 queryClient,
79 'prefetchInfiniteQuery',
80 )
81 const nestedRef = ref('value')
82 const key = queryKey()
83 const queryFn = () =>
84 Promise.resolve({ data: 'prefetched', currentPage: 1 })
85 const getNextPageParam = () => undefined
86​
87 usePrefetchInfiniteQuery(
88 {
89 queryKey: [...key, nestedRef],
90 queryFn,
91 initialPageParam: 1,
92 getNextPageParam,
93 },
94 queryClient,
95 )
96​
97 expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith({
98 queryKey: [...key, 'value'],
99 queryFn,
100 initialPageParam: 1,
101 getNextPageParam,
102 })
103 })
104​
105 it('should prefetch infinite query again when query key changes reactively', async () => {
106 const queryClient = new QueryClient()
107 const prefetchInfiniteQuerySpy = vi.spyOn(
108 queryClient,
109 'prefetchInfiniteQuery',
110 )
111 const keyRef = ref('first')
112 const key = queryKey()
113 const queryFn = () =>
114 Promise.resolve({ data: keyRef.value, currentPage: 1 })
115 const getNextPageParam = () => undefined
116​
117 usePrefetchInfiniteQuery(
118 () => ({
119 queryKey: [...key, keyRef.value],
120 queryFn,
121 initialPageParam: 1,
122 getNextPageParam,
123 }),
124 queryClient,
125 )
126​
127 expect(prefetchInfiniteQuerySpy).toHaveBeenNthCalledWith(1, {
128 queryKey: [...key, 'first'],
129 queryFn,
130 initialPageParam: 1,
131 getNextPageParam,
132 })
133​
134 keyRef.value = 'second'
135 await nextTick()
136​
137 expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(2)
138 expect(prefetchInfiniteQuerySpy).toHaveBeenNthCalledWith(2, {
139 queryKey: [...key, 'second'],
140 queryFn,
141 initialPageParam: 1,
142 getNextPageParam,
143 })
144 })
145​
146 it('should warn when used outside of setup function in development mode', () => {
147 vi.stubEnv('NODE_ENV', 'development')
148 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
149​
150 try {
151 usePrefetchInfiniteQuery(
152 {
153 queryKey: queryKey(),
154 queryFn: () =>
155 Promise.resolve({ data: 'prefetched', currentPage: 1 }),
156 initialPageParam: 1,
157 getNextPageParam: () => undefined,
158 },
159 new QueryClient(),
160 )
161​
162 expect(warnSpy).toHaveBeenCalledWith(
163 '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.',
164 )
165 } finally {
166 warnSpy.mockRestore()
167 vi.unstubAllEnvs()
168 }
169 })
170})