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

useMutationState.test.tsx

68 lines1.8 KB

1import { describe, expect, it } from 'vitest'
2import { fireEvent, render, waitFor } from '@solidjs/testing-library'
3import { createEffect } from 'solid-js'
4import { useMutationState } from '../useMutationState'
5import { createMutation } from '../createMutation'
6import { QueryClientProvider } from '../QueryClientProvider'
7import { createQueryClient, sleep } from './utils'
8​
9describe('useMutationState', () => {
10 it('should return variables after calling mutate', async () => {
11 const queryClient = createQueryClient()
12 const variables: Array<Array<unknown>> = []
13 const mutationKey = ['mutation']
14​
15 function Variables() {
16 const states = useMutationState(() => ({
17 filters: { mutationKey, status: 'pending' },
18 select: (mutation) => mutation.state.variables,
19 }))
20​
21 createEffect(() => {
22 variables.push(states())
23 })
24​
25 return null
26 }
27​
28 function Mutate() {
29 const mutation = createMutation(() => ({
30 mutationKey,
31 mutationFn: async (input: number) => {
32 await sleep(150)
33 return 'data' + input
34 },
35 }))
36​
37 return (
38 <div>
39 data: {mutation.data ?? 'null'}
40 <button onClick={() => mutation.mutate(1)}>mutate</button>
41 </div>
42 )
43 }
44​
45 function Page() {
46 return (
47 <div>
48 <Variables />
49 <Mutate />
50 </div>
51 )
52 }
53​
54 const rendered = render(() => (
55 <QueryClientProvider client={queryClient}>
56 <Page />
57 </QueryClientProvider>
58 ))
59​
60 await waitFor(() => rendered.getByText('data: null'))
61​
62 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
63​
64 await waitFor(() => rendered.getByText('data: data1'))
65​
66 expect(variables).toEqual([[], [1], []])
67 })
68})