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