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 { createKey, 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 = createKey()
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 await vi.advanceTimersByTimeAsync(150)
86 expect(rendered.getByText('count: 1')).toBeInTheDocument()
87 })
88β
89 it('should return variables while the mutation is pending', async () => {
90 const variables: Array<ReadonlyArray<unknown>> = []
91 const mutationKey = createKey()
92β
93 function Variables() {
94 const states = useMutationState(() => ({
95 filters: { mutationKey, status: 'pending' },
96 select: (mutation) => mutation.state.variables,
97 }))
98β
99 createRenderEffect(
100 () => [...states()],
101 (s) => {
102 variables.push(s)
103 },
104 )
105β
106 return null
107 }
108β
109 function Page() {
110 return (
111 <div>
112 <Variables />
113 <button
114 onClick={() =>
115 startMutation(
116 queryClient,
117 {
118 mutationKey,
119 mutationFn: (input: number) =>
120 sleep(150).then(() => 'data' + input),
121 },
122 1,
123 )
124 }
125 >
126 mutate
127 </button>
128 </div>
129 )
130 }
131β
132 const rendered = renderWithClient(queryClient, () => <Page />)
133β
134 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
135 await vi.advanceTimersByTimeAsync(0)
136 expect(variables).toStrictEqual([[], [1]])
137β
138 await vi.advanceTimersByTimeAsync(150)
139 expect(variables).toStrictEqual([[], [1], []])
140 })
141β
142 it('should observe pending state of mutations started by useMutation', async () => {
143 const mutationKey = createKey()
144β
145 function States() {
146 const mutationStates = useMutationState(() => ({
147 filters: { mutationKey, status: 'pending' },
148 }))
149β
150 return <div>pending: {mutationStates().length}</div>
151 }
152β
153 function Mutate() {
154 const mutation = useMutation(() => ({
155 mutationKey,
156 mutationFn: (input: number) => sleep(150).then(() => `data${input}`),
157 }))
158β
159 return <button onClick={() => mutation.mutate(1)}>mutate</button>
160 }
161β
162 function Page() {
163 return (
164 <div>
165 <States />
166 <Mutate />
167 </div>
168 )
169 }
170β
171 const rendered = renderWithClient(queryClient, () => <Page />)
172β
173 expect(rendered.getByText('pending: 0')).toBeInTheDocument()
174β
175 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
176 await vi.advanceTimersByTimeAsync(0)
177 expect(rendered.getByText('pending: 1')).toBeInTheDocument()
178β
179 await vi.advanceTimersByTimeAsync(150)
180 expect(rendered.getByText('pending: 0')).toBeInTheDocument()
181 })
182})