Open History to see the last commit
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})