Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { fireEvent, render } from '@solidjs/testing-library'
3import { createEffect } from 'solid-js'
4import { sleep } from '@tanstack/query-test-utils'
5import {
6 QueryClient,
7 QueryClientProvider,
8 useMutation,
9 useMutationState,
10} from '..'
11β
12describe('useMutationState', () => {
13 beforeEach(() => {
14 vi.useFakeTimers()
15 })
16β
17 afterEach(() => {
18 vi.useRealTimers()
19 })
20β
21 it('should return all mutation states when called without options', async () => {
22 const queryClient = new QueryClient()
23 const mutationKey = ['mutation']
24β
25 function States() {
26 const mutationStates = useMutationState()
27β
28 return <div>count: {mutationStates().length}</div>
29 }
30β
31 function Mutate() {
32 const mutation = useMutation(() => ({
33 mutationKey,
34 mutationFn: (input: number) => sleep(150).then(() => 'data' + input),
35 }))
36β
37 return (
38 <div>
39 <button onClick={() => mutation.mutate(1)}>mutate</button>
40 </div>
41 )
42 }
43β
44 function Page() {
45 return (
46 <div>
47 <States />
48 <Mutate />
49 </div>
50 )
51 }
52β
53 const rendered = render(() => (
54 <QueryClientProvider client={queryClient}>
55 <Page />
56 </QueryClientProvider>
57 ))
58β
59 expect(rendered.getByText('count: 0')).toBeInTheDocument()
60β
61 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
62 await vi.advanceTimersByTimeAsync(0)
63 expect(rendered.getByText('count: 1')).toBeInTheDocument()
64β
65 await vi.advanceTimersByTimeAsync(150)
66 expect(rendered.getByText('count: 1')).toBeInTheDocument()
67 })
68β
69 it('should return variables after calling mutate', async () => {
70 const queryClient = new QueryClient()
71 const variables: Array<Array<unknown>> = []
72 const mutationKey = ['mutation']
73β
74 function Variables() {
75 const states = useMutationState(() => ({
76 filters: { mutationKey, status: 'pending' },
77 select: (mutation) => mutation.state.variables,
78 }))
79β
80 createEffect(() => {
81 variables.push(states())
82 })
83β
84 return null
85 }
86β
87 function Mutate() {
88 const mutation = useMutation(() => ({
89 mutationKey,
90 mutationFn: async (input: number) => {
91 await sleep(150)
92 return 'data' + input
93 },
94 }))
95β
96 return (
97 <div>
98 data: {mutation.data ?? 'null'}
99 <button onClick={() => mutation.mutate(1)}>mutate</button>
100 </div>
101 )
102 }
103β
104 function Page() {
105 return (
106 <div>
107 <Variables />
108 <Mutate />
109 </div>
110 )
111 }
112β
113 const rendered = render(() => (
114 <QueryClientProvider client={queryClient}>
115 <Page />
116 </QueryClientProvider>
117 ))
118β
119 expect(rendered.getByText('data: null')).toBeInTheDocument()
120β
121 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
122 await vi.advanceTimersByTimeAsync(150)
123 expect(rendered.getByText('data: data1')).toBeInTheDocument()
124β
125 expect(variables).toEqual([[], [1], []])
126 })
127})