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

useQueries.test.ts

193 lines4.0 KB

1import { onScopeDispose, reactive } from 'vue-demi'
2​
3import {
4 flushPromises,
5 rejectFetcher,
6 simpleFetcher,
7 getSimpleFetcherWithReturnData,
8} from './test-utils'
9import { useQueries } from '../useQueries'
10​
11jest.mock('../useQueryClient')
12​
13describe('useQueries', () => {
14 test('should return result for each query', () => {
15 const queries = [
16 {
17 queryKey: ['key1'],
18 queryFn: simpleFetcher,
19 },
20 {
21 queryKey: ['key2'],
22 queryFn: simpleFetcher,
23 },
24 ]
25 const queriesState = useQueries({ queries })
26​
27 expect(queriesState).toMatchObject([
28 {
29 status: 'loading',
30 isLoading: true,
31 isFetching: true,
32 isStale: true,
33 },
34 {
35 status: 'loading',
36 isLoading: true,
37 isFetching: true,
38 isStale: true,
39 },
40 ])
41 })
42​
43 test('should resolve to success and update reactive state', async () => {
44 const queries = [
45 {
46 queryKey: ['key11'],
47 queryFn: simpleFetcher,
48 },
49 {
50 queryKey: ['key12'],
51 queryFn: simpleFetcher,
52 },
53 ]
54 const queriesState = useQueries({ queries })
55​
56 await flushPromises()
57​
58 expect(queriesState).toMatchObject([
59 {
60 status: 'success',
61 isLoading: false,
62 isFetching: false,
63 isStale: true,
64 },
65 {
66 status: 'success',
67 isLoading: false,
68 isFetching: false,
69 isStale: true,
70 },
71 ])
72 })
73​
74 test('should reject one of the queries and update reactive state', async () => {
75 const queries = [
76 {
77 queryKey: ['key21'],
78 queryFn: rejectFetcher,
79 },
80 {
81 queryKey: ['key22'],
82 queryFn: simpleFetcher,
83 },
84 ]
85 const queriesState = useQueries({ queries })
86​
87 await flushPromises()
88​
89 expect(queriesState).toMatchObject([
90 {
91 status: 'error',
92 isLoading: false,
93 isFetching: false,
94 isStale: true,
95 },
96 {
97 status: 'success',
98 isLoading: false,
99 isFetching: false,
100 isStale: true,
101 },
102 ])
103 })
104​
105 test('should return state for new queries', async () => {
106 const queries = reactive([
107 {
108 queryKey: ['key31'],
109 queryFn: getSimpleFetcherWithReturnData('value31'),
110 },
111 {
112 queryKey: ['key32'],
113 queryFn: getSimpleFetcherWithReturnData('value32'),
114 },
115 {
116 queryKey: ['key33'],
117 queryFn: getSimpleFetcherWithReturnData('value33'),
118 },
119 ])
120 const queriesState = useQueries({ queries })
121​
122 await flushPromises()
123​
124 queries.splice(
125 0,
126 queries.length,
127 {
128 queryKey: ['key31'],
129 queryFn: getSimpleFetcherWithReturnData('value31'),
130 },
131 {
132 queryKey: ['key34'],
133 queryFn: getSimpleFetcherWithReturnData('value34'),
134 },
135 )
136​
137 await flushPromises()
138 await flushPromises()
139​
140 expect(queriesState.length).toEqual(2)
141 expect(queriesState).toMatchObject([
142 {
143 data: 'value31',
144 status: 'success',
145 isLoading: false,
146 isFetching: false,
147 isStale: true,
148 },
149 {
150 data: 'value34',
151 status: 'success',
152 isLoading: false,
153 isFetching: false,
154 isStale: true,
155 },
156 ])
157 })
158​
159 test('should stop listening to changes on onScopeDispose', async () => {
160 const onScopeDisposeMock = onScopeDispose as jest.MockedFunction<
161 typeof onScopeDispose
162 >
163 onScopeDisposeMock.mockImplementationOnce((fn) => fn())
164​
165 const queries = [
166 {
167 queryKey: ['key41'],
168 queryFn: simpleFetcher,
169 },
170 {
171 queryKey: ['key42'],
172 queryFn: simpleFetcher,
173 },
174 ]
175 const queriesState = useQueries({ queries })
176 await flushPromises()
177​
178 expect(queriesState).toMatchObject([
179 {
180 status: 'loading',
181 isLoading: true,
182 isFetching: true,
183 isStale: true,
184 },
185 {
186 status: 'loading',
187 isLoading: true,
188 isFetching: true,
189 isStale: true,
190 },
191 ])
192 })
193})