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

usePrefetchInfiniteQuery.test.ts

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