Skip to content

Commit 552fe4b

Browse files
authored
test(solid-query/useIsMutating): simplify 'mutationFn' and add 'expect' for 'isMutating' state transitions (#9486)
1 parent fba2c86 commit 552fe4b

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

packages/solid-query/src/__tests__/useIsMutating.test.tsx

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,22 @@ describe('useIsMutating', () => {
2626

2727
function IsMutating() {
2828
const isMutating = useIsMutating()
29+
2930
createRenderEffect(() => {
3031
isMutatingArray.push(isMutating())
3132
})
33+
3234
return null
3335
}
3436

3537
function Mutations() {
3638
const { mutate: mutate1 } = useMutation(() => ({
3739
mutationKey: ['mutation1'],
38-
mutationFn: async () => {
39-
await sleep(150)
40-
return 'data'
41-
},
40+
mutationFn: () => sleep(150).then(() => 'data'),
4241
}))
4342
const { mutate: mutate2 } = useMutation(() => ({
4443
mutationKey: ['mutation2'],
45-
mutationFn: async () => {
46-
await sleep(50)
47-
return 'data'
48-
},
44+
mutationFn: () => sleep(50).then(() => 'data'),
4945
}))
5046

5147
createEffect(() => {
@@ -74,6 +70,7 @@ describe('useIsMutating', () => {
7470
))
7571

7672
await vi.advanceTimersByTimeAsync(150)
73+
7774
expect(isMutatingArray).toEqual([0, 1, 2, 1, 0])
7875
})
7976

@@ -83,26 +80,22 @@ describe('useIsMutating', () => {
8380

8481
function IsMutating() {
8582
const isMutating = useIsMutating(() => ({ mutationKey: ['mutation1'] }))
83+
8684
createRenderEffect(() => {
8785
isMutatingArray.push(isMutating())
8886
})
87+
8988
return null
9089
}
9190

9291
function Page() {
9392
const { mutate: mutate1 } = useMutation(() => ({
9493
mutationKey: ['mutation1'],
95-
mutationFn: async () => {
96-
await sleep(100)
97-
return 'data'
98-
},
94+
mutationFn: () => sleep(100).then(() => 'data'),
9995
}))
10096
const { mutate: mutate2 } = useMutation(() => ({
10197
mutationKey: ['mutation2'],
102-
mutationFn: async () => {
103-
await sleep(100)
104-
return 'data'
105-
},
98+
mutationFn: () => sleep(100).then(() => 'data'),
10699
}))
107100

108101
createEffect(() => {
@@ -121,6 +114,7 @@ describe('useIsMutating', () => {
121114

122115
// Unlike React, IsMutating Wont re-render twice with mutation2
123116
await vi.advanceTimersByTimeAsync(100)
117+
124118
expect(isMutatingArray).toEqual([0, 1, 0])
125119
})
126120

@@ -133,26 +127,22 @@ describe('useIsMutating', () => {
133127
predicate: (mutation) =>
134128
mutation.options.mutationKey?.[0] === 'mutation1',
135129
}))
130+
136131
createRenderEffect(() => {
137132
isMutatingArray.push(isMutating())
138133
})
134+
139135
return null
140136
}
141137

142138
function Page() {
143139
const { mutate: mutate1 } = useMutation(() => ({
144140
mutationKey: ['mutation1'],
145-
mutationFn: async () => {
146-
await sleep(100)
147-
return 'data'
148-
},
141+
mutationFn: () => sleep(100).then(() => 'data'),
149142
}))
150143
const { mutate: mutate2 } = useMutation(() => ({
151144
mutationKey: ['mutation2'],
152-
mutationFn: async () => {
153-
await sleep(100)
154-
return 'data'
155-
},
145+
mutationFn: () => sleep(100).then(() => 'data'),
156146
}))
157147

158148
createEffect(() => {
@@ -171,6 +161,7 @@ describe('useIsMutating', () => {
171161

172162
// Again, No unnecessary re-renders like React
173163
await vi.advanceTimersByTimeAsync(100)
164+
174165
expect(isMutatingArray).toEqual([0, 1, 0])
175166
})
176167

@@ -182,16 +173,17 @@ describe('useIsMutating', () => {
182173
const { mutate } = useMutation(
183174
() => ({
184175
mutationKey: ['mutation1'],
185-
mutationFn: async () => {
186-
await sleep(10)
187-
return 'data'
188-
},
176+
mutationFn: () => sleep(20).then(() => 'data'),
189177
}),
190178
() => queryClient,
191179
)
180+
192181
createEffect(() => {
193-
mutate()
182+
setActTimeout(() => {
183+
mutate()
184+
}, 10)
194185
})
186+
195187
return (
196188
<div>
197189
<div>mutating: {isMutating()}</div>
@@ -201,8 +193,11 @@ describe('useIsMutating', () => {
201193

202194
const rendered = render(() => <Page></Page>)
203195

204-
await vi.advanceTimersByTimeAsync(0)
196+
expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
197+
await vi.advanceTimersByTimeAsync(10)
205198
expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
199+
await vi.advanceTimersByTimeAsync(20)
200+
expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
206201
})
207202

208203
// eslint-disable-next-line vitest/expect-expect
@@ -231,12 +226,10 @@ describe('useIsMutating', () => {
231226

232227
function Page() {
233228
const [mounted, setMounted] = createSignal(true)
229+
234230
const { mutate: mutate1 } = useMutation(() => ({
235231
mutationKey: ['mutation1'],
236-
mutationFn: async () => {
237-
await sleep(10)
238-
return 'data'
239-
},
232+
mutationFn: () => sleep(10).then(() => 'data'),
240233
}))
241234

242235
createEffect(() => {
@@ -258,6 +251,7 @@ describe('useIsMutating', () => {
258251
<Page />
259252
</QueryClientProvider>
260253
))
254+
261255
fireEvent.click(rendered.getByText('unmount'))
262256

263257
// Should not display the console error

0 commit comments

Comments
 (0)