Open History to see the last commit
1import { QueryClient } from '@tanstack/query-core'
2import { LitElement } from 'lit'
3import { describe, expectTypeOf, it } from 'vitest'
4import { createMutationController } from '../createMutationController.js'
5import type {
6 DefaultError,
7 MutationFunctionContext,
8 MutationKey,
9} from '@tanstack/query-core'
10import type { MutationResultAccessor } from '../createMutationController.js'
11β
12class Host extends LitElement {}
13β
14describe('createMutationController', () => {
15 it('should infer TData from mutationFn return type', () => {
16 const mutation = createMutationController(
17 new Host(),
18 {
19 mutationFn: () => Promise.resolve('data'),
20 },
21 new QueryClient(),
22 )
23β
24 expectTypeOf(mutation().data).toEqualTypeOf<string | undefined>()
25 expectTypeOf(mutation().error).toEqualTypeOf<DefaultError | null>()
26 })
27β
28 it('should infer TVariables from mutationFn parameter', () => {
29 const mutation = createMutationController(
30 new Host(),
31 {
32 mutationFn: (vars: { id: string }) => Promise.resolve(vars.id),
33 },
34 new QueryClient(),
35 )
36β
37 expectTypeOf(mutation.mutate).toBeCallableWith({ id: '1' })
38 expectTypeOf(mutation().data).toEqualTypeOf<string | undefined>()
39 })
40β
41 it('should infer TOnMutateResult from onMutate return type', () => {
42 createMutationController(
43 new Host(),
44 {
45 mutationFn: () => Promise.resolve('data'),
46 onMutate: () => {
47 return { token: 'abc' }
48 },
49 onSuccess: (_data, _variables, onMutateResult) => {
50 expectTypeOf(onMutateResult).toEqualTypeOf<{ token: string }>()
51 },
52 onError: (_error, _variables, onMutateResult) => {
53 expectTypeOf(onMutateResult).toEqualTypeOf<
54 { token: string } | undefined
55 >()
56 },
57 },
58 new QueryClient(),
59 )
60 })
61β
62 it('should allow explicit generic types', () => {
63 const mutation = createMutationController<string, Error, { id: number }>(
64 new Host(),
65 {
66 mutationFn: (vars) => {
67 expectTypeOf(vars).toEqualTypeOf<{ id: number }>()
68 return Promise.resolve('result')
69 },
70 },
71 new QueryClient(),
72 )
73β
74 expectTypeOf(mutation().data).toEqualTypeOf<string | undefined>()
75 expectTypeOf(mutation().error).toEqualTypeOf<Error | null>()
76 })
77β
78 it('should return correct MutationResultAccessor type', () => {
79 const mutation = createMutationController(
80 new Host(),
81 {
82 mutationFn: () => Promise.resolve(42),
83 },
84 new QueryClient(),
85 )
86β
87 expectTypeOf(mutation).toEqualTypeOf<
88 MutationResultAccessor<number, DefaultError, void, unknown>
89 >()
90 })
91β
92 it('should type mutateAsync with correct return type', () => {
93 const mutation = createMutationController(
94 new Host(),
95 {
96 mutationFn: (id: string) => Promise.resolve(id.length),
97 },
98 new QueryClient(),
99 )
100β
101 expectTypeOf(mutation.mutateAsync).toBeCallableWith('test')
102 expectTypeOf(mutation.mutateAsync('test')).toEqualTypeOf<Promise<number>>()
103 })
104β
105 it('should type reset with correct type', () => {
106 const mutation = createMutationController(
107 new Host(),
108 {
109 mutationFn: (id: string) => Promise.resolve(id.length),
110 },
111 new QueryClient(),
112 )
113β
114 expectTypeOf(mutation.reset).toEqualTypeOf<() => void>()
115 })
116β
117 it('should default TVariables to void when mutationFn has no parameters', () => {
118 const mutation = createMutationController(
119 new Host(),
120 {
121 mutationFn: () => Promise.resolve('data'),
122 },
123 new QueryClient(),
124 )
125β
126 expectTypeOf(mutation.mutate).toBeCallableWith()
127 })
128β
129 it('should allow calling mutate with no arguments when TVariables is optional undefinable', () => {
130 const mutation = createMutationController(
131 new Host(),
132 {
133 mutationFn: (_variables: number | undefined) => Promise.resolve(1),
134 },
135 new QueryClient(),
136 )
137β
138 expectTypeOf(mutation.mutate).toBeCallableWith()
139 expectTypeOf(mutation.mutateAsync).toBeCallableWith()
140 })
141β
142 it('should infer custom TError type', () => {
143 class CustomError extends Error {
144 code: number
145 constructor(code: number) {
146 super()
147 this.code = code
148 }
149 }
150β
151 const mutation = createMutationController<string, CustomError>(
152 new Host(),
153 {
154 mutationFn: () => Promise.resolve('data'),
155 },
156 new QueryClient(),
157 )
158β
159 expectTypeOf(mutation().error).toEqualTypeOf<CustomError | null>()
160 expectTypeOf(mutation().data).toEqualTypeOf<string | undefined>()
161 })
162β
163 it('should infer types for onSettled callback', () => {
164 createMutationController(
165 new Host(),
166 {
167 mutationFn: () => Promise.resolve(42),
168 onSettled: (data, error, _variables, _onMutateResult) => {
169 expectTypeOf(data).toEqualTypeOf<number | undefined>()
170 expectTypeOf(error).toEqualTypeOf<DefaultError | null>()
171 },
172 },
173 new QueryClient(),
174 )
175 })
176β
177 it('should infer custom TError in onError callback', () => {
178 class CustomError extends Error {
179 code: number
180 constructor(code: number) {
181 super()
182 this.code = code
183 }
184 }
185β
186 createMutationController<string, CustomError>(
187 new Host(),
188 {
189 mutationFn: () => Promise.resolve('data'),
190 onError: (error) => {
191 expectTypeOf(error).toEqualTypeOf<CustomError>()
192 },
193 },
194 new QueryClient(),
195 )
196 })
197β
198 it('should type context as the last argument for mutationFn and every hook-level callback', () => {
199 createMutationController(
200 new Host(),
201 {
202 mutationFn: (_variables, context) => {
203 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
204 expectTypeOf(context.client).toEqualTypeOf<QueryClient>()
205 return Promise.resolve('data')
206 },
207 onMutate: (_variables, context) => {
208 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
209 },
210 onSuccess: (_data, _variables, _onMutateResult, context) => {
211 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
212 },
213 onError: (_error, _variables, _onMutateResult, context) => {
214 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
215 },
216 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
217 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
218 },
219 },
220 new QueryClient(),
221 )
222 })
223β
224 it('should type context as the last argument for every per-call mutate option', () => {
225 const mutation = createMutationController(
226 new Host(),
227 {
228 mutationFn: () => Promise.resolve('data'),
229 },
230 new QueryClient(),
231 )
232β
233 mutation.mutate(undefined, {
234 onSuccess: (_data, _variables, _onMutateResult, context) => {
235 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
236 },
237 onError: (_error, _variables, _onMutateResult, context) => {
238 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
239 },
240 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
241 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
242 },
243 })
244 })
245β
246 it('should type context.mutationKey as MutationKey', () => {
247 createMutationController(
248 new Host(),
249 {
250 mutationKey: ['todos', 'add'] as const,
251 mutationFn: () => Promise.resolve('data'),
252 onSuccess: (_data, _variables, _onMutateResult, context) => {
253 expectTypeOf(context.mutationKey).toEqualTypeOf<
254 MutationKey | undefined
255 >()
256 },
257 },
258 new QueryClient(),
259 )
260 })
261})