Skip to content

Commit b202d6e

Browse files
committed
test(react-query): add testcase when used with other mutation util
1 parent 05f4fc0 commit b202d6e

File tree

2 files changed

+152
-2
lines changed

2 files changed

+152
-2
lines changed

packages/react-query/src/__tests__/mutationOptions.test-d.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { assertType, describe, expectTypeOf, it } from 'vitest'
22
import { useMutation } from 'src/useMutation'
3+
import { useIsMutating, useMutationState } from 'src/useMutationState'
4+
import { QueryClient } from '@tanstack/query-core'
35
import { mutationOptions } from '../mutationOptions'
6+
import type { DefaultError, MutationState } from '@tanstack/query-core'
47
import type { UseMutationOptions, UseMutationResult } from 'src/types'
5-
import type { DefaultError } from '@tanstack/query-core'
68

79
describe('mutationOptions', () => {
810
it('should not allow excess properties', () => {
@@ -118,4 +120,37 @@ describe('mutationOptions', () => {
118120
UseMutationResult<{ field: string }, DefaultError, void, unknown>
119121
>()
120122
})
123+
124+
it('should infer types when used with useIsMutating', () => {
125+
const mutationOpts = mutationOptions({
126+
mutationKey: ['key'],
127+
mutationFn: () => Promise.resolve(5),
128+
})
129+
130+
const isMutating = useIsMutating(mutationOpts)
131+
expectTypeOf(isMutating).toEqualTypeOf<number>()
132+
})
133+
134+
it('should infer types when used with queryClient.isMutating', () => {
135+
const queryClient = new QueryClient()
136+
const mutationOpts = mutationOptions({
137+
mutationKey: ['key'],
138+
mutationFn: () => Promise.resolve(5),
139+
})
140+
141+
const isMutating = queryClient.isMutating(mutationOpts)
142+
expectTypeOf(isMutating).toEqualTypeOf<number>()
143+
})
144+
145+
it('should infer types when used with useMutationState', () => {
146+
const mutationOpts = mutationOptions({
147+
mutationKey: ['key'],
148+
mutationFn: () => Promise.resolve(5),
149+
})
150+
151+
const mutationState = useMutationState({ filters: mutationOpts })
152+
expectTypeOf(mutationState).toEqualTypeOf<
153+
Array<MutationState<unknown, Error, unknown, unknown>>
154+
>()
155+
})
121156
})

packages/react-query/src/__tests__/mutationOptions.test.tsx

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { describe, expect, it } from 'vitest'
1+
import { describe, expect, it, vi } from 'vitest'
2+
import { QueryClient } from '@tanstack/query-core'
3+
import { sleep } from '@tanstack/query-test-utils'
4+
import { fireEvent } from '@testing-library/react'
25
import { mutationOptions } from '../mutationOptions'
6+
import { useIsMutating, useMutation, useMutationState } from '..'
7+
import { renderWithClient } from './utils'
8+
import type { MutationState } from '@tanstack/query-core'
39

410
describe('mutationOptions', () => {
511
it('should return the object received as a parameter without any modification.', () => {
@@ -10,4 +16,113 @@ describe('mutationOptions', () => {
1016

1117
expect(mutationOptions(object)).toStrictEqual(object)
1218
})
19+
20+
it('should return the number of fetching mutations when used with useIsMutating', async () => {
21+
const isMutatingArray: Array<number> = []
22+
const queryClient = new QueryClient()
23+
24+
function IsMutating() {
25+
const isMutating = useIsMutating()
26+
isMutatingArray.push(isMutating)
27+
return null
28+
}
29+
30+
const mutationOpts = mutationOptions({
31+
mutationKey: ['key'],
32+
mutationFn: () => sleep(50).then(() => 'data'),
33+
})
34+
35+
function Mutation() {
36+
const { mutate } = useMutation(mutationOpts)
37+
38+
return (
39+
<div>
40+
<button onClick={() => mutate()}>mutate</button>
41+
</div>
42+
)
43+
}
44+
45+
function Page() {
46+
return (
47+
<div>
48+
<IsMutating />
49+
<Mutation />
50+
</div>
51+
)
52+
}
53+
54+
const rendered = renderWithClient(queryClient, <Page />)
55+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
56+
57+
await vi.waitFor(() => expect(isMutatingArray[0]).toEqual(0))
58+
await vi.waitFor(() => expect(isMutatingArray[1]).toEqual(1))
59+
await vi.waitFor(() => expect(isMutatingArray[2]).toEqual(0))
60+
await vi.waitFor(() =>
61+
expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0),
62+
)
63+
})
64+
65+
it('should return the number of fetching mutations when used with queryClient.isMutating', async () => {
66+
const isMutatingArray: Array<number> = []
67+
const queryClient = new QueryClient()
68+
69+
const mutationOpts = mutationOptions({
70+
mutationKey: ['mutation'],
71+
mutationFn: () => sleep(500).then(() => 'data'),
72+
})
73+
74+
function Mutation() {
75+
const isMutating = queryClient.isMutating(mutationOpts)
76+
const { mutate } = useMutation(mutationOpts)
77+
isMutatingArray.push(isMutating)
78+
79+
return (
80+
<div>
81+
<button onClick={() => mutate()}>mutate</button>
82+
</div>
83+
)
84+
}
85+
86+
const rendered = renderWithClient(queryClient, <Mutation />)
87+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
88+
89+
await vi.waitFor(() => expect(isMutatingArray[0]).toEqual(0))
90+
await vi.waitFor(() => expect(isMutatingArray[1]).toEqual(1))
91+
await vi.waitFor(() => expect(isMutatingArray[2]).toEqual(0))
92+
await vi.waitFor(() =>
93+
expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0),
94+
)
95+
})
96+
97+
it('should return the number of fetching mutations when used with useMutationState', async () => {
98+
const mutationStateArray: Array<
99+
MutationState<unknown, Error, unknown, unknown>
100+
> = []
101+
const queryClient = new QueryClient()
102+
103+
const mutationOpts = mutationOptions({
104+
mutationKey: ['mutation'],
105+
mutationFn: () => Promise.resolve('data'),
106+
})
107+
108+
function Mutation() {
109+
const { mutate } = useMutation(mutationOpts)
110+
const data = useMutationState({
111+
filters: { ...mutationOpts, status: 'success' },
112+
})
113+
mutationStateArray.push(...data)
114+
115+
return (
116+
<div>
117+
<button onClick={() => mutate()}>mutate</button>
118+
</div>
119+
)
120+
}
121+
122+
const rendered = renderWithClient(queryClient, <Mutation />)
123+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
124+
125+
await vi.waitFor(() => expect(mutationStateArray.length).toEqual(1))
126+
await vi.waitFor(() => expect(mutationStateArray[0]?.data).toEqual('data'))
127+
})
13128
})

0 commit comments

Comments
 (0)