Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { fireEvent } from '@solidjs/testing-library'
3import { createRenderEffect } from 'solid-js'
4import { queryKey, sleep } from '@tanstack/query-test-utils'
5import { QueryClient, useMutation, useMutationState } from '..'
6import { renderWithClient } from './utils'
7import type { MutationOptions } from '@tanstack/query-core'
8β
9/**
10 * Drives a mutation through the mutation cache directly, outside any
11 * useMutation action transaction. Cache events emitted during a
12 * useMutation flight are delivered inside the action's transaction, where
13 * useMutationState's plain-signal write is held until settle β the
14 * in-flight state never commits to the DOM (see the skipped test below).
15 * Feeding the cache directly keeps the hook's own contract observable.
16 */
17function startMutation<TData, TVariables>(
18 client: QueryClient,
19 options: MutationOptions<TData, Error, TVariables>,
20 variables: TVariables,
21): Promise<TData> {
22 return client.getMutationCache().build(client, options).execute(variables)
23}
24β
25describe('useMutationState', () => {
26 let queryClient: QueryClient
27β
28 beforeEach(() => {
29 vi.useFakeTimers()
30 queryClient = new QueryClient()
31 })
32β
33 afterEach(() => {
34 queryClient.clear()
35 vi.useRealTimers()
36 })
37β
38 it('should return all mutation states when called without options', async () => {
39 const mutationKey = queryKey()
40β
41 function States() {
42 const mutationStates = useMutationState()
43β
44 return <div>count: {mutationStates().length}</div>
45 }
46β
47 function Mutate() {
48 return (
49 <div>
50 <button
51 onClick={() =>
52 startMutation(
53 queryClient,
54 {
55 mutationKey,
56 mutationFn: (input: number) =>
57 sleep(150).then(() => 'data' + input),
58 },
59 1,
60 )
61 }
62 >
63 mutate
64 </button>
65 </div>
66 )
67 }
68β
69 function Page() {
70 return (
71 <div>
72 <States />
73 <Mutate />
74 </div>
75 )
76 }
77β
78 const rendered = renderWithClient(queryClient, () => <Page />)
79β
80 expect(rendered.getByText('count: 0')).toBeInTheDocument()
81β
82 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
83 await vi.advanceTimersByTimeAsync(0)
84 expect(rendered.getByText('count: 1')).toBeInTheDocument()
85β
86 await vi.advanceTimersByTimeAsync(150)
87 expect(rendered.getByText('count: 1')).toBeInTheDocument()
88 })
89β
90 it('should return variables while the mutation is pending', async () => {
91 const variables: Array<Array<unknown>> = []
92 const mutationKey = queryKey()
93β
94 function Variables() {
95 const states = useMutationState(() => ({
96 filters: { mutationKey, status: 'pending' },
97 select: (mutation) => mutation.state.variables,
98 }))
99β
100 createRenderEffect(
101 () => [...states()],
102 (s) => {
103 variables.push(s)
104 },
105 )
106β
107 return null
108 }
109β
110 function Page() {
111 return (
112 <div>
113 <Variables />
114 <button
115 onClick={() =>
116 startMutation(
117 queryClient,
118 {
119 mutationKey,
120 mutationFn: (input: number) =>
121 sleep(150).then(() => 'data' + input),
122 },
123 1,
124 )
125 }
126 >
127 mutate
128 </button>
129 </div>
130 )
131 }
132β
133 const rendered = renderWithClient(queryClient, () => <Page />)
134β
135 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
136 await vi.advanceTimersByTimeAsync(0)
137 expect(variables).toEqual([[], [1]])
138β
139 await vi.advanceTimersByTimeAsync(150)
140 expect(variables).toEqual([[], [1], []])
141 })
142β
143 it('should observe pending state of mutations started by useMutation', async () => {
144 const mutationKey = queryKey()
145β
146 function States() {
147 const mutationStates = useMutationState(() => ({
148 filters: { mutationKey, status: 'pending' },
149 }))
150β
151 return <div>pending: {mutationStates().length}</div>
152 }
153β
154 function Mutate() {
155 const mutation = useMutation(() => ({
156 mutationKey,
157 mutationFn: (input: number) => sleep(150).then(() => 'data' + input),
158 }))
159β
160 return <button onClick={() => mutation.mutate(1)}>mutate</button>
161 }
162β
163 function Page() {
164 return (
165 <div>
166 <States />
167 <Mutate />
168 </div>
169 )
170 }
171β
172 const rendered = renderWithClient(queryClient, () => <Page />)
173β
174 expect(rendered.getByText('pending: 0')).toBeInTheDocument()
175β
176 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
177 await vi.advanceTimersByTimeAsync(0)
178 expect(rendered.getByText('pending: 1')).toBeInTheDocument()
179β
180 await vi.advanceTimersByTimeAsync(150)
181 expect(rendered.getByText('pending: 0')).toBeInTheDocument()
182 })
183})