Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { fireEvent, render, waitFor } 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 it('should return variables after calling mutate', async () => {
14 const queryClient = new QueryClient()
15 const variables: Array<Array<unknown>> = []
16 const mutationKey = ['mutation']
17β
18 function Variables() {
19 const states = useMutationState(() => ({
20 filters: { mutationKey, status: 'pending' },
21 select: (mutation) => mutation.state.variables,
22 }))
23β
24 createEffect(() => {
25 variables.push(states())
26 })
27β
28 return null
29 }
30β
31 function Mutate() {
32 const mutation = useMutation(() => ({
33 mutationKey,
34 mutationFn: async (input: number) => {
35 await sleep(150)
36 return 'data' + input
37 },
38 }))
39β
40 return (
41 <div>
42 data: {mutation.data ?? 'null'}
43 <button onClick={() => mutation.mutate(1)}>mutate</button>
44 </div>
45 )
46 }
47β
48 function Page() {
49 return (
50 <div>
51 <Variables />
52 <Mutate />
53 </div>
54 )
55 }
56β
57 const rendered = render(() => (
58 <QueryClientProvider client={queryClient}>
59 <Page />
60 </QueryClientProvider>
61 ))
62β
63 await waitFor(() => rendered.getByText('data: null'))
64β
65 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
66β
67 await waitFor(() => rendered.getByText('data: data1'))
68β
69 expect(variables).toEqual([[], [1], []])
70 })
71})