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

mutationObserver.test.tsx

310 lines7.7 KB

1import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2import { MutationObserver } from '..'
3import { createQueryClient, queryKey, sleep } from './utils'
4import type { QueryClient } from '..'
5​
6describe('mutationObserver', () => {
7 let queryClient: QueryClient
8​
9 beforeEach(() => {
10 vi.useFakeTimers()
11 queryClient = createQueryClient()
12 queryClient.mount()
13 })
14​
15 afterEach(() => {
16 queryClient.clear()
17 vi.useRealTimers()
18 })
19​
20 test('onUnsubscribe should not remove the current mutation observer if there is still a subscription', async () => {
21 const mutation = new MutationObserver(queryClient, {
22 mutationFn: (text: string) => sleep(20).then(() => text),
23 })
24​
25 const subscription1Handler = vi.fn()
26 const subscription2Handler = vi.fn()
27​
28 const unsubscribe1 = mutation.subscribe(subscription1Handler)
29 const unsubscribe2 = mutation.subscribe(subscription2Handler)
30​
31 mutation.mutate('input')
32​
33 unsubscribe1()
34​
35 await vi.waitFor(() => {
36 // 1 call: loading
37 expect(subscription1Handler).toBeCalledTimes(1)
38 // 2 calls: loading, success
39 expect(subscription2Handler).toBeCalledTimes(2)
40 })
41​
42 // Clean-up
43 unsubscribe2()
44 })
45​
46 test('unsubscribe should remove observer to trigger GC', async () => {
47 const mutation = new MutationObserver(queryClient, {
48 mutationFn: (text: string) => sleep(5).then(() => text),
49 gcTime: 10,
50 })
51​
52 const subscriptionHandler = vi.fn()
53​
54 const unsubscribe = mutation.subscribe(subscriptionHandler)
55​
56 mutation.mutate('input')
57 await vi.waitFor(() =>
58 expect(queryClient.getMutationCache().findAll()).toHaveLength(1),
59 )
60​
61 unsubscribe()
62​
63 await vi.waitFor(() =>
64 expect(queryClient.getMutationCache().findAll()).toHaveLength(0),
65 )
66 })
67​
68 test('reset should remove observer to trigger GC', async () => {
69 const mutation = new MutationObserver(queryClient, {
70 mutationFn: (text: string) => sleep(5).then(() => text),
71 gcTime: 10,
72 })
73​
74 const subscriptionHandler = vi.fn()
75​
76 const unsubscribe = mutation.subscribe(subscriptionHandler)
77​
78 mutation.mutate('input')
79 await vi.waitFor(() =>
80 expect(queryClient.getMutationCache().findAll()).toHaveLength(1),
81 )
82​
83 mutation.reset()
84​
85 await vi.waitFor(() =>
86 expect(queryClient.getMutationCache().findAll()).toHaveLength(0),
87 )
88​
89 unsubscribe()
90 })
91​
92 test('changing mutation keys should reset the observer', async () => {
93 const key = queryKey()
94 const mutation = new MutationObserver(queryClient, {
95 mutationKey: [...key, '1'],
96 mutationFn: (text: string) => sleep(5).then(() => text),
97 })
98​
99 const subscriptionHandler = vi.fn()
100​
101 const unsubscribe = mutation.subscribe(subscriptionHandler)
102​
103 mutation.mutate('input')
104​
105 await vi.waitFor(() =>
106 expect(mutation.getCurrentResult()).toMatchObject({
107 status: 'success',
108 data: 'input',
109 }),
110 )
111​
112 mutation.setOptions({
113 mutationKey: [...key, '2'],
114 })
115​
116 expect(mutation.getCurrentResult()).toMatchObject({
117 status: 'idle',
118 })
119​
120 unsubscribe()
121 })
122​
123 test('changing mutation keys should not affect already existing mutations', async () => {
124 const key = queryKey()
125 const mutationObserver = new MutationObserver(queryClient, {
126 mutationKey: [...key, '1'],
127 mutationFn: (text: string) => sleep(5).then(() => text),
128 })
129​
130 const subscriptionHandler = vi.fn()
131​
132 const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
133​
134 mutationObserver.mutate('input')
135​
136 await vi.waitFor(() =>
137 expect(
138 queryClient.getMutationCache().find({ mutationKey: [...key, '1'] }),
139 ).toMatchObject({
140 options: { mutationKey: [...key, '1'] },
141 state: {
142 status: 'success',
143 data: 'input',
144 },
145 }),
146 )
147​
148 mutationObserver.setOptions({
149 mutationKey: [...key, '2'],
150 })
151​
152 expect(
153 queryClient.getMutationCache().find({ mutationKey: [...key, '1'] }),
154 ).toMatchObject({
155 options: { mutationKey: [...key, '1'] },
156 state: {
157 status: 'success',
158 data: 'input',
159 },
160 })
161​
162 unsubscribe()
163 })
164​
165 test('changing mutation meta should not affect successful mutations', async () => {
166 const mutationObserver = new MutationObserver(queryClient, {
167 meta: { a: 1 },
168 mutationFn: (text: string) => sleep(5).then(() => text),
169 })
170​
171 const subscriptionHandler = vi.fn()
172​
173 const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
174​
175 mutationObserver.mutate('input')
176​
177 await vi.waitFor(() =>
178 expect(queryClient.getMutationCache().find({})).toMatchObject({
179 options: { meta: { a: 1 } },
180 state: {
181 status: 'success',
182 data: 'input',
183 },
184 }),
185 )
186​
187 mutationObserver.setOptions({
188 meta: { a: 2 },
189 })
190​
191 expect(queryClient.getMutationCache().find({})).toMatchObject({
192 options: { meta: { a: 1 } },
193 state: {
194 status: 'success',
195 data: 'input',
196 },
197 })
198​
199 unsubscribe()
200 })
201​
202 test('mutation cache should have different meta when updated between mutations', async () => {
203 const mutationFn = (text: string) => sleep(5).then(() => text)
204 const mutationObserver = new MutationObserver(queryClient, {
205 meta: { a: 1 },
206 mutationFn,
207 })
208​
209 const subscriptionHandler = vi.fn()
210​
211 const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
212​
213 mutationObserver.mutate('input')
214 await vi.advanceTimersByTimeAsync(5)
215​
216 mutationObserver.setOptions({
217 meta: { a: 2 },
218 mutationFn,
219 })
220​
221 mutationObserver.mutate('input')
222 await vi.advanceTimersByTimeAsync(5)
223​
224 const mutations = queryClient.getMutationCache().findAll()
225 expect(mutations[0]).toMatchObject({
226 options: { meta: { a: 1 } },
227 state: {
228 status: 'success',
229 data: 'input',
230 },
231 })
232 expect(mutations[1]).toMatchObject({
233 options: { meta: { a: 2 } },
234 state: {
235 status: 'success',
236 data: 'input',
237 },
238 })
239​
240 unsubscribe()
241 })
242​
243 test('changing mutation meta should not affect rejected mutations', async () => {
244 const mutationObserver = new MutationObserver(queryClient, {
245 meta: { a: 1 },
246 mutationFn: (_: string) =>
247 sleep(5).then(() => Promise.reject(new Error('err'))),
248 })
249​
250 const subscriptionHandler = vi.fn()
251​
252 const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
253​
254 mutationObserver.mutate('input').catch(() => undefined)
255 await vi.waitFor(() =>
256 expect(queryClient.getMutationCache().find({})).toMatchObject({
257 options: { meta: { a: 1 } },
258 state: {
259 status: 'error',
260 },
261 }),
262 )
263​
264 mutationObserver.setOptions({
265 meta: { a: 2 },
266 })
267​
268 expect(queryClient.getMutationCache().find({})).toMatchObject({
269 options: { meta: { a: 1 } },
270 state: {
271 status: 'error',
272 },
273 })
274​
275 unsubscribe()
276 })
277​
278 test('changing mutation meta should affect pending mutations', async () => {
279 const mutationObserver = new MutationObserver(queryClient, {
280 meta: { a: 1 },
281 mutationFn: (text: string) => sleep(20).then(() => text),
282 })
283​
284 const subscriptionHandler = vi.fn()
285​
286 const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
287​
288 mutationObserver.mutate('input')
289 await vi.advanceTimersByTimeAsync(5)
290 expect(queryClient.getMutationCache().find({})).toMatchObject({
291 options: { meta: { a: 1 } },
292 state: {
293 status: 'pending',
294 },
295 })
296​
297 mutationObserver.setOptions({
298 meta: { a: 2 },
299 })
300​
301 expect(queryClient.getMutationCache().find({})).toMatchObject({
302 options: { meta: { a: 2 } },
303 state: {
304 status: 'pending',
305 },
306 })
307​
308 unsubscribe()
309 })
310})