Open History to see the last commit
1import { waitFor, fireEvent } from '@testing-library/react'
2import React from 'react'
3import { QueryClient } from '../../core'
4import { useIsMutating } from '../useIsMutating'
5import { useMutation } from '../useMutation'
6import { renderWithClient, setActTimeout, sleep } from './utils'
7import * as MutationCacheModule from '../../core/mutationCache'
8β
9describe('useIsMutating', () => {
10 it('should return the number of fetching mutations', async () => {
11 const isMutatings: number[] = []
12 const queryClient = new QueryClient()
13β
14 function IsMutating() {
15 const isMutating = useIsMutating()
16 isMutatings.push(isMutating)
17 return null
18 }
19β
20 function Page() {
21 const { mutate: mutate1 } = useMutation('mutation1', async () => {
22 await sleep(150)
23 return 'data'
24 })
25 const { mutate: mutate2 } = useMutation('mutation2', async () => {
26 await sleep(50)
27 return 'data'
28 })
29β
30 React.useEffect(() => {
31 mutate1()
32 setActTimeout(() => {
33 mutate2()
34 }, 50)
35 }, [mutate1, mutate2])
36β
37 return <IsMutating />
38 }
39β
40 renderWithClient(queryClient, <Page />)
41 await waitFor(() => expect(isMutatings).toEqual([0, 1, 1, 2, 2, 1, 0]))
42 })
43β
44 it('should filter correctly by mutationKey', async () => {
45 const isMutatings: number[] = []
46 const queryClient = new QueryClient()
47β
48 function IsMutating() {
49 const isMutating = useIsMutating('mutation1')
50 isMutatings.push(isMutating)
51 return null
52 }
53β
54 function Page() {
55 const { mutate: mutate1 } = useMutation('mutation1', async () => {
56 await sleep(100)
57 return 'data'
58 })
59 const { mutate: mutate2 } = useMutation('mutation2', async () => {
60 await sleep(100)
61 return 'data'
62 })
63β
64 React.useEffect(() => {
65 mutate1()
66 mutate2()
67 }, [mutate1, mutate2])
68β
69 return <IsMutating />
70 }
71β
72 renderWithClient(queryClient, <Page />)
73 await waitFor(() => expect(isMutatings).toEqual([0, 1, 1, 1, 0, 0]))
74 })
75β
76 it('should filter correctly by predicate', async () => {
77 const isMutatings: number[] = []
78 const queryClient = new QueryClient()
79β
80 function IsMutating() {
81 const isMutating = useIsMutating({
82 predicate: mutation => mutation.options.mutationKey === 'mutation1',
83 })
84 isMutatings.push(isMutating)
85 return null
86 }
87β
88 function Page() {
89 const { mutate: mutate1 } = useMutation('mutation1', async () => {
90 await sleep(100)
91 return 'data'
92 })
93 const { mutate: mutate2 } = useMutation('mutation2', async () => {
94 await sleep(100)
95 return 'data'
96 })
97β
98 React.useEffect(() => {
99 mutate1()
100 mutate2()
101 }, [mutate1, mutate2])
102β
103 return <IsMutating />
104 }
105β
106 renderWithClient(queryClient, <Page />)
107 await waitFor(() => expect(isMutatings).toEqual([0, 1, 1, 1, 0, 0]))
108 })
109β
110 it('should not change state if unmounted', async () => {
111 // We have to mock the MutationCache to not unsubscribe
112 // the listener when the component is unmounted
113 class MutationCacheMock extends MutationCacheModule.MutationCache {
114 subscribe(listener: any) {
115 super.subscribe(listener)
116 return () => void 0
117 }
118 }
119β
120 const MutationCacheSpy = jest
121 .spyOn(MutationCacheModule, 'MutationCache')
122 .mockImplementation(fn => {
123 return new MutationCacheMock(fn)
124 })
125β
126 const queryClient = new QueryClient()
127β
128 function IsMutating() {
129 useIsMutating()
130 return null
131 }
132β
133 function Page() {
134 const [mounted, setMounted] = React.useState(true)
135 const { mutate: mutate1 } = useMutation('mutation1', async () => {
136 await sleep(10)
137 return 'data'
138 })
139β
140 React.useEffect(() => {
141 mutate1()
142 }, [mutate1])
143β
144 return (
145 <div>
146 <button onClick={() => setMounted(false)}>unmount</button>
147 {mounted && <IsMutating />}
148 </div>
149 )
150 }
151β
152 const { getByText } = renderWithClient(queryClient, <Page />)
153 fireEvent.click(getByText('unmount'))
154β
155 // Should not display the console error
156 // "Warning: Can't perform a React state update on an unmounted component"
157β
158 await sleep(20)
159 MutationCacheSpy.mockRestore()
160 })
161})