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

mutationObserver.test.tsx

129 lines3.1 KB

1import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2import { waitFor } from '@testing-library/react'
3import { MutationObserver } from '..'
4import { createQueryClient, queryKey, sleep } from './utils'
5import type { QueryClient } from '..'
6​
7describe('mutationObserver', () => {
8 let queryClient: QueryClient
9​
10 beforeEach(() => {
11 queryClient = createQueryClient()
12 queryClient.mount()
13 })
14​
15 afterEach(() => {
16 queryClient.clear()
17 })
18​
19 test('onUnsubscribe should not remove the current mutation observer if there is still a subscription', async () => {
20 const mutation = new MutationObserver(queryClient, {
21 mutationFn: async (text: string) => {
22 await sleep(20)
23 return text
24 },
25 })
26​
27 const subscription1Handler = vi.fn()
28 const subscription2Handler = vi.fn()
29​
30 const unsubscribe1 = mutation.subscribe(subscription1Handler)
31 const unsubscribe2 = mutation.subscribe(subscription2Handler)
32​
33 mutation.mutate('input')
34​
35 unsubscribe1()
36​
37 await waitFor(() => {
38 // 1 call: loading
39 expect(subscription1Handler).toBeCalledTimes(1)
40 // 2 calls: loading, success
41 expect(subscription2Handler).toBeCalledTimes(2)
42 })
43​
44 // Clean-up
45 unsubscribe2()
46 })
47​
48 test('unsubscribe should remove observer to trigger GC', async () => {
49 const mutation = new MutationObserver(queryClient, {
50 mutationFn: async (text: string) => {
51 await sleep(5)
52 return text
53 },
54 gcTime: 10,
55 })
56​
57 const subscriptionHandler = vi.fn()
58​
59 const unsubscribe = mutation.subscribe(subscriptionHandler)
60​
61 await mutation.mutate('input')
62​
63 expect(queryClient.getMutationCache().findAll()).toHaveLength(1)
64​
65 unsubscribe()
66​
67 await waitFor(() =>
68 expect(queryClient.getMutationCache().findAll()).toHaveLength(0),
69 )
70 })
71​
72 test('reset should remove observer to trigger GC', async () => {
73 const mutation = new MutationObserver(queryClient, {
74 mutationFn: async (text: string) => {
75 await sleep(5)
76 return text
77 },
78 gcTime: 10,
79 })
80​
81 const subscriptionHandler = vi.fn()
82​
83 const unsubscribe = mutation.subscribe(subscriptionHandler)
84​
85 await mutation.mutate('input')
86​
87 expect(queryClient.getMutationCache().findAll()).toHaveLength(1)
88​
89 mutation.reset()
90​
91 await waitFor(() =>
92 expect(queryClient.getMutationCache().findAll()).toHaveLength(0),
93 )
94​
95 unsubscribe()
96 })
97​
98 test('changing mutation keys should reset the observer', async () => {
99 const key = queryKey()
100 const mutation = new MutationObserver(queryClient, {
101 mutationKey: [...key, '1'],
102 mutationFn: async (text: string) => {
103 await sleep(5)
104 return text
105 },
106 })
107​
108 const subscriptionHandler = vi.fn()
109​
110 const unsubscribe = mutation.subscribe(subscriptionHandler)
111​
112 await mutation.mutate('input')
113​
114 expect(mutation.getCurrentResult()).toMatchObject({
115 status: 'success',
116 data: 'input',
117 })
118​
119 mutation.setOptions({
120 mutationKey: [...key, '2'],
121 })
122​
123 expect(mutation.getCurrentResult()).toMatchObject({
124 status: 'idle',
125 })
126​
127 unsubscribe()
128 })
129})