Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

mutationOptions.svelte.test.ts

344 lines11.8 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { fireEvent, render } from '@testing-library/svelte'
3import { QueryClient } from '@tanstack/query-core'
4import { queryKey, sleep } from '@tanstack/query-test-utils'
5import { mutationOptions } from '../../src/index.js'
6import Base from './Base.svelte'
7import Multi from './Multi.svelte'
8​
9describe('mutationOptions', () => {
10 let queryClient: QueryClient
11​
12 beforeEach(() => {
13 vi.useFakeTimers()
14 queryClient = new QueryClient()
15 })
16​
17 afterEach(() => {
18 queryClient.clear()
19 vi.useRealTimers()
20 })
21​
22 it('should return the object received as a parameter without any modification (with mutationKey in mutationOptions)', () => {
23 const object = {
24 mutationKey: ['key'],
25 mutationFn: () => sleep(10).then(() => 5),
26 } as const
27​
28 expect(mutationOptions(object)).toBe(object)
29 })
30​
31 it('should return the object received as a parameter without any modification (without mutationKey in mutationOptions)', () => {
32 const object = {
33 mutationFn: () => sleep(10).then(() => 5),
34 } as const
35​
36 expect(mutationOptions(object)).toBe(object)
37 })
38​
39 it('should return the number of fetching mutations when used with useIsMutating (with mutationKey in mutationOptions)', async () => {
40 const key = queryKey()
41 const mutationOpts = mutationOptions({
42 mutationKey: key,
43 mutationFn: () => sleep(50).then(() => 'data'),
44 })
45​
46 const rendered = render(Base, {
47 props: { queryClient, mutationOpts: () => mutationOpts },
48 })
49​
50 expect(rendered.getByText('isMutating: 0')).toBeInTheDocument()
51​
52 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
53 await vi.advanceTimersByTimeAsync(0)
54 expect(rendered.getByText('isMutating: 1')).toBeInTheDocument()
55 await vi.advanceTimersByTimeAsync(50)
56 expect(rendered.getByText('isMutating: 0')).toBeInTheDocument()
57 })
58​
59 it('should return the number of fetching mutations when used with useIsMutating (without mutationKey in mutationOptions)', async () => {
60 const mutationOpts = mutationOptions({
61 mutationFn: () => sleep(50).then(() => 'data'),
62 })
63​
64 const rendered = render(Base, {
65 props: { queryClient, mutationOpts: () => mutationOpts },
66 })
67​
68 expect(rendered.getByText('isMutating: 0')).toBeInTheDocument()
69​
70 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
71 await vi.advanceTimersByTimeAsync(0)
72 expect(rendered.getByText('isMutating: 1')).toBeInTheDocument()
73 await vi.advanceTimersByTimeAsync(50)
74 expect(rendered.getByText('isMutating: 0')).toBeInTheDocument()
75 })
76​
77 it('should return the number of fetching mutations when used with useIsMutating', async () => {
78 const key = queryKey()
79 const mutationOpts1 = mutationOptions({
80 mutationKey: key,
81 mutationFn: () => sleep(50).then(() => 'data1'),
82 })
83 const mutationOpts2 = mutationOptions({
84 mutationFn: () => sleep(50).then(() => 'data2'),
85 })
86​
87 const rendered = render(Multi, {
88 props: {
89 queryClient,
90 mutationOpts1: () => mutationOpts1,
91 mutationOpts2: () => mutationOpts2,
92 },
93 })
94​
95 expect(rendered.getByText('isMutating: 0')).toBeInTheDocument()
96​
97 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
98 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
99 await vi.advanceTimersByTimeAsync(0)
100 expect(rendered.getByText('isMutating: 2')).toBeInTheDocument()
101 await vi.advanceTimersByTimeAsync(50)
102 expect(rendered.getByText('isMutating: 0')).toBeInTheDocument()
103 })
104​
105 it('should return the number of fetching mutations when used with useIsMutating (filter mutationOpts1.mutationKey)', async () => {
106 const key = queryKey()
107 const mutationOpts1 = mutationOptions({
108 mutationKey: key,
109 mutationFn: () => sleep(50).then(() => 'data1'),
110 })
111 const mutationOpts2 = mutationOptions({
112 mutationFn: () => sleep(50).then(() => 'data2'),
113 })
114​
115 const rendered = render(Multi, {
116 props: {
117 queryClient,
118 mutationOpts1: () => mutationOpts1,
119 mutationOpts2: () => mutationOpts2,
120 isMutatingFilters: { mutationKey: mutationOpts1.mutationKey },
121 },
122 })
123​
124 expect(rendered.getByText('isMutating: 0')).toBeInTheDocument()
125​
126 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
127 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
128 await vi.advanceTimersByTimeAsync(0)
129 expect(rendered.getByText('isMutating: 1')).toBeInTheDocument()
130 await vi.advanceTimersByTimeAsync(50)
131 expect(rendered.getByText('isMutating: 0')).toBeInTheDocument()
132 })
133​
134 it('should return the number of fetching mutations when used with queryClient.isMutating (with mutationKey in mutationOptions)', async () => {
135 const key = queryKey()
136 const mutationOpts = mutationOptions({
137 mutationKey: key,
138 mutationFn: () => sleep(500).then(() => 'data'),
139 })
140​
141 const rendered = render(Base, {
142 props: {
143 queryClient,
144 mutationOpts: () => mutationOpts,
145 isMutatingFilters: { mutationKey: mutationOpts.mutationKey },
146 },
147 })
148​
149 expect(rendered.getByText('clientIsMutating: 0')).toBeInTheDocument()
150​
151 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
152 await vi.advanceTimersByTimeAsync(0)
153 expect(rendered.getByText('clientIsMutating: 1')).toBeInTheDocument()
154 await vi.advanceTimersByTimeAsync(500)
155 expect(rendered.getByText('clientIsMutating: 0')).toBeInTheDocument()
156 })
157​
158 it('should return the number of fetching mutations when used with queryClient.isMutating (without mutationKey in mutationOptions)', async () => {
159 const mutationOpts = mutationOptions({
160 mutationFn: () => sleep(500).then(() => 'data'),
161 })
162​
163 const rendered = render(Base, {
164 props: { queryClient, mutationOpts: () => mutationOpts },
165 })
166​
167 expect(rendered.getByText('clientIsMutating: 0')).toBeInTheDocument()
168​
169 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
170 await vi.advanceTimersByTimeAsync(0)
171 expect(rendered.getByText('clientIsMutating: 1')).toBeInTheDocument()
172 await vi.advanceTimersByTimeAsync(500)
173 expect(rendered.getByText('clientIsMutating: 0')).toBeInTheDocument()
174 })
175​
176 it('should return the number of fetching mutations when used with queryClient.isMutating', async () => {
177 const key = queryKey()
178 const mutationOpts1 = mutationOptions({
179 mutationKey: key,
180 mutationFn: () => sleep(500).then(() => 'data1'),
181 })
182 const mutationOpts2 = mutationOptions({
183 mutationFn: () => sleep(500).then(() => 'data2'),
184 })
185​
186 const rendered = render(Multi, {
187 props: {
188 queryClient,
189 mutationOpts1: () => mutationOpts1,
190 mutationOpts2: () => mutationOpts2,
191 },
192 })
193​
194 expect(rendered.getByText('clientIsMutating: 0')).toBeInTheDocument()
195​
196 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
197 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
198 await vi.advanceTimersByTimeAsync(0)
199 expect(rendered.getByText('clientIsMutating: 2')).toBeInTheDocument()
200 await vi.advanceTimersByTimeAsync(500)
201 expect(rendered.getByText('clientIsMutating: 0')).toBeInTheDocument()
202 })
203​
204 it('should return the number of fetching mutations when used with queryClient.isMutating (filter mutationOpts1.mutationKey)', async () => {
205 const key = queryKey()
206 const mutationOpts1 = mutationOptions({
207 mutationKey: key,
208 mutationFn: () => sleep(500).then(() => 'data1'),
209 })
210 const mutationOpts2 = mutationOptions({
211 mutationFn: () => sleep(500).then(() => 'data2'),
212 })
213​
214 const rendered = render(Multi, {
215 props: {
216 queryClient,
217 mutationOpts1: () => mutationOpts1,
218 mutationOpts2: () => mutationOpts2,
219 isMutatingFilters: { mutationKey: mutationOpts1.mutationKey },
220 },
221 })
222​
223 expect(rendered.getByText('clientIsMutating: 0')).toBeInTheDocument()
224​
225 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
226 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
227 await vi.advanceTimersByTimeAsync(0)
228 expect(rendered.getByText('clientIsMutating: 1')).toBeInTheDocument()
229 await vi.advanceTimersByTimeAsync(500)
230 expect(rendered.getByText('clientIsMutating: 0')).toBeInTheDocument()
231 })
232​
233 it('should return mutation states when used with useMutationState (with mutationKey in mutationOptions)', async () => {
234 const key = queryKey()
235 const mutationOpts = mutationOptions({
236 mutationKey: key,
237 mutationFn: () => sleep(10).then(() => 'data'),
238 })
239​
240 const rendered = render(Base, {
241 props: {
242 queryClient,
243 mutationOpts: () => mutationOpts,
244 mutationStateOpts: {
245 filters: {
246 mutationKey: mutationOpts.mutationKey,
247 status: 'success',
248 },
249 },
250 },
251 })
252​
253 expect(rendered.getByText('mutationState: []')).toBeInTheDocument()
254​
255 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
256 await vi.advanceTimersByTimeAsync(10)
257 expect(rendered.getByText('mutationState: ["data"]')).toBeInTheDocument()
258 })
259​
260 it('should return mutation states when used with useMutationState (without mutationKey in mutationOptions)', async () => {
261 const mutationOpts = mutationOptions({
262 mutationFn: () => sleep(10).then(() => 'data'),
263 })
264​
265 const rendered = render(Base, {
266 props: {
267 queryClient,
268 mutationOpts: () => mutationOpts,
269 mutationStateOpts: {
270 filters: { status: 'success' },
271 },
272 },
273 })
274​
275 expect(rendered.getByText('mutationState: []')).toBeInTheDocument()
276​
277 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
278 await vi.advanceTimersByTimeAsync(10)
279 expect(rendered.getByText('mutationState: ["data"]')).toBeInTheDocument()
280 })
281​
282 it('should return mutation states when used with useMutationState', async () => {
283 const key = queryKey()
284 const mutationOpts1 = mutationOptions({
285 mutationKey: key,
286 mutationFn: () => sleep(10).then(() => 'data1'),
287 })
288 const mutationOpts2 = mutationOptions({
289 mutationFn: () => sleep(10).then(() => 'data2'),
290 })
291​
292 const rendered = render(Multi, {
293 props: {
294 queryClient,
295 mutationOpts1: () => mutationOpts1,
296 mutationOpts2: () => mutationOpts2,
297 mutationStateOpts: {
298 filters: { status: 'success' },
299 },
300 },
301 })
302​
303 expect(rendered.getByText('mutationState: []')).toBeInTheDocument()
304​
305 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
306 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
307 await vi.advanceTimersByTimeAsync(10)
308 expect(
309 rendered.getByText('mutationState: ["data1","data2"]'),
310 ).toBeInTheDocument()
311 })
312​
313 it('should return mutation states when used with useMutationState (filter mutationOpts1.mutationKey)', async () => {
314 const key = queryKey()
315 const mutationOpts1 = mutationOptions({
316 mutationKey: key,
317 mutationFn: () => sleep(10).then(() => 'data1'),
318 })
319 const mutationOpts2 = mutationOptions({
320 mutationFn: () => sleep(10).then(() => 'data2'),
321 })
322​
323 const rendered = render(Multi, {
324 props: {
325 queryClient,
326 mutationOpts1: () => mutationOpts1,
327 mutationOpts2: () => mutationOpts2,
328 mutationStateOpts: {
329 filters: {
330 mutationKey: mutationOpts1.mutationKey,
331 status: 'success',
332 },
333 },
334 },
335 })
336​
337 expect(rendered.getByText('mutationState: []')).toBeInTheDocument()
338​
339 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
340 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
341 await vi.advanceTimersByTimeAsync(10)
342 expect(rendered.getByText('mutationState: ["data1"]')).toBeInTheDocument()
343 })
344})