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 { 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})