-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathmutationOptions.test.tsx
More file actions
140 lines (116 loc) · 4.42 KB
/
mutationOptions.test.tsx
File metadata and controls
140 lines (116 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as React from 'react'
import { QueryClient } from '@tanstack/query-core'
import { fireEvent, waitFor } from '@testing-library/react'
import { mutationOptions } from '../mutationOptions'
import { useIsMutating, useMutation } from '..'
import { renderWithClient, sleep } from './utils'
import type { UseMutationOptions } from '../types'
describe('mutationOptions', () => {
it('should return the object received as a parameter without any modification (with mutationKey)', () => {
const object: UseMutationOptions = {
mutationKey: ['key'],
mutationFn: () => Promise.resolve(5),
} as const
expect(mutationOptions(object)).toBe(object)
})
it('should return the object received as a parameter without any modification (without mutationKey)', () => {
const object: UseMutationOptions = {
mutationFn: () => Promise.resolve(5),
} as const
expect(mutationOptions(object)).toBe(object)
})
it('should work with useMutation (with mutationKey)', async () => {
const queryClient = new QueryClient()
const mutationOpts = mutationOptions({
mutationKey: ['key'],
mutationFn: () => sleep(10).then(() => 'data'),
})
function Page() {
const mutation = useMutation(mutationOpts)
return (
<div>
<button onClick={() => mutation.mutate()}>mutate</button>
<span>{mutation.data ?? 'empty'}</span>
</div>
)
}
const rendered = renderWithClient(queryClient, <Page />)
expect(rendered.getByText('empty')).toBeTruthy()
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
await waitFor(() => rendered.getByText('data'))
})
it('should work with useMutation (without mutationKey)', async () => {
const queryClient = new QueryClient()
const mutationOpts = mutationOptions({
mutationFn: () => sleep(10).then(() => 'data'),
})
function Page() {
const mutation = useMutation(mutationOpts)
return (
<div>
<button onClick={() => mutation.mutate()}>mutate</button>
<span>{mutation.data ?? 'empty'}</span>
</div>
)
}
const rendered = renderWithClient(queryClient, <Page />)
expect(rendered.getByText('empty')).toBeTruthy()
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
await waitFor(() => rendered.getByText('data'))
})
it('should work with useIsMutating filtering by mutationKey', async () => {
const queryClient = new QueryClient()
const mutationOpts1 = mutationOptions({
mutationKey: ['key1'],
mutationFn: () => sleep(50).then(() => 'data1'),
})
const mutationOpts2 = mutationOptions({
mutationKey: ['key2'],
mutationFn: () => sleep(50).then(() => 'data2'),
})
function Page() {
const isMutating = useIsMutating({
mutationKey: mutationOpts1.mutationKey,
})
const { mutate: mutate1 } = useMutation(mutationOpts1)
const { mutate: mutate2 } = useMutation(mutationOpts2)
return (
<div>
<span>isMutating: {isMutating}</span>
<button onClick={() => mutate1()}>mutate1</button>
<button onClick={() => mutate2()}>mutate2</button>
</div>
)
}
const rendered = renderWithClient(queryClient, <Page />)
rendered.getByText('isMutating: 0')
fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
await waitFor(() => rendered.getByText('isMutating: 1'))
await waitFor(() => rendered.getByText('isMutating: 0'))
})
it('should work with queryClient.isMutating', async () => {
const queryClient = new QueryClient()
const mutationOpts = mutationOptions({
mutationKey: ['mutation'],
mutationFn: () => sleep(10).then(() => 'data'),
})
function Page() {
const isMutating = queryClient.isMutating({
mutationKey: mutationOpts.mutationKey,
})
const { mutate } = useMutation(mutationOpts)
return (
<div>
<span>isMutating: {isMutating}</span>
<button onClick={() => mutate()}>mutate</button>
</div>
)
}
const rendered = renderWithClient(queryClient, <Page />)
rendered.getByText('isMutating: 0')
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
await waitFor(() => rendered.getByText('isMutating: 1'))
await waitFor(() => rendered.getByText('isMutating: 0'))
})
})