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

useMutationState.svelte.test.ts

190 lines6.0 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 Base from './Base.svelte'
6import Select from './Select.svelte'
7import type { Mutation } from '@tanstack/query-core'
8​
9describe('useMutationState', () => {
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 run few mutation functions and check from useMutationState', async () => {
23 const successKey = queryKey()
24 const errorKey = queryKey()
25 const successMutationFn = vi.fn(() => sleep(10).then(() => 'data'))
26 const errorMutationFn = vi
27 .fn()
28 .mockImplementation(() =>
29 sleep(20).then(() => Promise.reject(new Error('error'))),
30 )
31​
32 const rendered = render(Base, {
33 props: {
34 queryClient,
35 successMutationOpts: () => ({
36 mutationKey: successKey,
37 mutationFn: successMutationFn,
38 }),
39 errorMutationOpts: () => ({
40 mutationKey: errorKey,
41 mutationFn: errorMutationFn,
42 }),
43 },
44 })
45​
46 fireEvent.click(rendered.getByRole('button', { name: /Success/i }))
47 await vi.advanceTimersByTimeAsync(10)
48 expect(successMutationFn).toHaveBeenCalledTimes(1)
49 expect(rendered.getByText('Data: ["success"]')).toBeInTheDocument()
50​
51 fireEvent.click(rendered.getByRole('button', { name: /Error/i }))
52 await vi.advanceTimersByTimeAsync(20)
53 expect(errorMutationFn).toHaveBeenCalledTimes(1)
54 expect(rendered.getByText('Data: ["success","error"]')).toBeInTheDocument()
55 })
56​
57 it('should select specific type of mutation ( i.e: error only )', async () => {
58 const successKey = queryKey()
59 const errorKey = queryKey()
60 const successMutationFn = vi.fn(() => sleep(10).then(() => 'data'))
61 const errorMutationFn = vi
62 .fn()
63 .mockImplementation(() =>
64 sleep(20).then(() => Promise.reject(new Error('error'))),
65 )
66​
67 const rendered = render(Base, {
68 props: {
69 queryClient,
70 successMutationOpts: () => ({
71 mutationKey: successKey,
72 mutationFn: successMutationFn,
73 }),
74 errorMutationOpts: () => ({
75 mutationKey: errorKey,
76 mutationFn: errorMutationFn,
77 }),
78 mutationStateOpts: {
79 filters: { status: 'error' },
80 },
81 },
82 })
83​
84 fireEvent.click(rendered.getByRole('button', { name: /Success/i }))
85 await vi.advanceTimersByTimeAsync(10)
86 expect(successMutationFn).toHaveBeenCalledTimes(1)
87 expect(rendered.getByText('Data: []')).toBeInTheDocument()
88​
89 fireEvent.click(rendered.getByRole('button', { name: /Error/i }))
90 await vi.advanceTimersByTimeAsync(20)
91 expect(errorMutationFn).toHaveBeenCalledTimes(1)
92 expect(rendered.getByText('Data: ["error"]')).toBeInTheDocument()
93 })
94​
95 it('should return selected value when using select option', async () => {
96 const mutationKey = queryKey()
97​
98 const rendered = render(Select, {
99 props: {
100 queryClient,
101 mutationOpts: () => ({
102 mutationKey,
103 mutationFn: () => sleep(10).then(() => 'data'),
104 }),
105 mutationStateOpts: {
106 filters: { mutationKey },
107 select: (mutation: Mutation) => mutation.state.status,
108 },
109 },
110 })
111​
112 expect(rendered.getByText('Variables: []')).toBeInTheDocument()
113​
114 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
115 await vi.advanceTimersByTimeAsync(0)
116 expect(rendered.getByText('Variables: ["pending"]')).toBeInTheDocument()
117 await vi.advanceTimersByTimeAsync(10)
118 expect(rendered.getByText('Variables: ["success"]')).toBeInTheDocument()
119 })
120​
121 it('should select specific mutation using mutation key', async () => {
122 const successKey = queryKey()
123 const errorKey = queryKey()
124 const successMutationFn = vi.fn(() => sleep(10).then(() => 'data'))
125 const errorMutationFn = vi
126 .fn()
127 .mockImplementation(() =>
128 sleep(20).then(() => Promise.reject(new Error('error'))),
129 )
130​
131 const rendered = render(Base, {
132 props: {
133 queryClient,
134 successMutationOpts: () => ({
135 mutationKey: successKey,
136 mutationFn: successMutationFn,
137 }),
138 errorMutationOpts: () => ({
139 mutationKey: errorKey,
140 mutationFn: errorMutationFn,
141 }),
142 mutationStateOpts: {
143 filters: { mutationKey: successKey },
144 },
145 },
146 })
147​
148 fireEvent.click(rendered.getByRole('button', { name: /Success/i }))
149 await vi.advanceTimersByTimeAsync(10)
150 expect(successMutationFn).toHaveBeenCalledTimes(1)
151 expect(rendered.getByText('Data: ["success"]')).toBeInTheDocument()
152​
153 fireEvent.click(rendered.getByRole('button', { name: /Error/i }))
154 await vi.advanceTimersByTimeAsync(20)
155 expect(errorMutationFn).toHaveBeenCalledTimes(1)
156 expect(rendered.getByText('Data: ["success"]')).toBeInTheDocument()
157 })
158​
159 it('should remove mutations that no longer match the filter', async () => {
160 const firstKey = queryKey()
161 const secondKey = queryKey()
162 const mutationFn = vi.fn(() => sleep(10).then(() => 'data'))
163​
164 const rendered = render(Base, {
165 props: {
166 queryClient,
167 successMutationOpts: () => ({
168 mutationKey: firstKey,
169 mutationFn,
170 }),
171 errorMutationOpts: () => ({
172 mutationKey: secondKey,
173 mutationFn,
174 }),
175 mutationStateOpts: {
176 filters: { status: 'pending' },
177 },
178 },
179 })
180​
181 fireEvent.click(rendered.getByRole('button', { name: /Success/i }))
182 fireEvent.click(rendered.getByRole('button', { name: /Error/i }))
183 await vi.advanceTimersByTimeAsync(0)
184 expect(
185 rendered.getByText('Data: ["pending","pending"]'),
186 ).toBeInTheDocument()
187 await vi.advanceTimersByTimeAsync(10)
188 expect(rendered.getByText('Data: []')).toBeInTheDocument()
189 })
190})