Open History to see the last commit
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β
118 await vi.advanceTimersByTimeAsync(10)
119 expect(rendered.getByText('Variables: ["success"]')).toBeInTheDocument()
120 })
121β
122 it('should select specific mutation using mutation key', async () => {
123 const successKey = queryKey()
124 const errorKey = queryKey()
125 const successMutationFn = vi.fn(() => sleep(10).then(() => 'data'))
126 const errorMutationFn = vi
127 .fn()
128 .mockImplementation(() =>
129 sleep(20).then(() => Promise.reject(new Error('error'))),
130 )
131β
132 const rendered = render(Base, {
133 props: {
134 queryClient,
135 successMutationOpts: () => ({
136 mutationKey: successKey,
137 mutationFn: successMutationFn,
138 }),
139 errorMutationOpts: () => ({
140 mutationKey: errorKey,
141 mutationFn: errorMutationFn,
142 }),
143 mutationStateOpts: {
144 filters: { mutationKey: successKey },
145 },
146 },
147 })
148β
149 fireEvent.click(rendered.getByRole('button', { name: /Success/i }))
150 await vi.advanceTimersByTimeAsync(10)
151 expect(successMutationFn).toHaveBeenCalledTimes(1)
152 expect(rendered.getByText('Data: ["success"]')).toBeInTheDocument()
153β
154 fireEvent.click(rendered.getByRole('button', { name: /Error/i }))
155 await vi.advanceTimersByTimeAsync(20)
156 expect(errorMutationFn).toHaveBeenCalledTimes(1)
157 expect(rendered.getByText('Data: ["success"]')).toBeInTheDocument()
158 })
159})