Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

mutationOptions.test-d.tsx

218 lines6.3 KB

1import { QueryClient } from '@tanstack/query-core'
2import { createKey } from '@tanstack/query-test-utils'
3import { assertType, describe, expectTypeOf, it } from 'vitest'
4import { useIsMutating, useMutation, useMutationState } from '..'
5import { mutationOptions } from '../mutationOptions'
6import type {
7 DefaultError,
8 MutationFunctionContext,
9 MutationState,
10 WithRequired,
11} from '@tanstack/query-core'
12import type { UseMutationOptions, UseMutationResult } from '../types'
13​
14describe('mutationOptions', () => {
15 it('should not allow excess properties', () => {
16 mutationOptions({
17 mutationFn: () => Promise.resolve(5),
18 // @ts-expect-error this is a good error, because onMutates does not exist!
19 onMutates: 1000,
20 mutationKey: createKey(),
21 onSuccess: (data) => {
22 expectTypeOf(data).toEqualTypeOf<number>()
23 },
24 })
25 })
26​
27 it('should infer types for callbacks', () => {
28 mutationOptions({
29 mutationFn: () => Promise.resolve(5),
30 mutationKey: createKey(),
31 onSuccess: (data) => {
32 expectTypeOf(data).toEqualTypeOf<number>()
33 },
34 })
35 })
36​
37 it('should infer types for onError callback', () => {
38 mutationOptions({
39 mutationFn: () => {
40 throw new Error('fail')
41 },
42 mutationKey: createKey(),
43 onError: (error) => {
44 expectTypeOf(error).toEqualTypeOf<DefaultError>()
45 },
46 })
47 })
48​
49 it('should infer types for variables', () => {
50 mutationOptions<number, DefaultError, { id: string }>({
51 mutationFn: (vars) => {
52 expectTypeOf(vars).toEqualTypeOf<{ id: string }>()
53 return Promise.resolve(5)
54 },
55 mutationKey: createKey(),
56 })
57 })
58​
59 it('should infer result type correctly', () => {
60 mutationOptions<number, DefaultError, void, { name: string }>({
61 mutationFn: () => Promise.resolve(5),
62 mutationKey: createKey(),
63 onMutate: () => {
64 return { name: 'onMutateResult' }
65 },
66 onSuccess: (_data, _variables, onMutateResult) => {
67 expectTypeOf(onMutateResult).toEqualTypeOf<{ name: string }>()
68 },
69 })
70 })
71​
72 it('should infer context type correctly', () => {
73 mutationOptions<number>({
74 mutationFn: (_variables, context) => {
75 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
76 return Promise.resolve(5)
77 },
78 mutationKey: createKey(),
79 onMutate: (_variables, context) => {
80 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
81 },
82 onSuccess: (_data, _variables, _onMutateResult, context) => {
83 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
84 },
85 onError: (_error, _variables, _onMutateResult, context) => {
86 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
87 },
88 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
89 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
90 },
91 })
92 })
93​
94 it('should error if mutationFn return type mismatches TData', () => {
95 assertType(
96 mutationOptions<number>({
97 // @ts-expect-error this is a good error, because return type is string, not number
98 mutationFn: async () => Promise.resolve('wrong return'),
99 }),
100 )
101 })
102​
103 it('should allow mutationKey to be omitted', () => {
104 return mutationOptions({
105 mutationFn: () => Promise.resolve(123),
106 onSuccess: (data) => {
107 expectTypeOf(data).toEqualTypeOf<number>()
108 },
109 })
110 })
111​
112 it('should infer all types when not explicitly provided', () => {
113 expectTypeOf(
114 mutationOptions({
115 mutationFn: (id: string) => Promise.resolve(id.length),
116 mutationKey: createKey(),
117 onSuccess: (data) => {
118 expectTypeOf(data).toEqualTypeOf<number>()
119 },
120 }),
121 ).toEqualTypeOf<
122 WithRequired<
123 UseMutationOptions<number, DefaultError, string>,
124 'mutationKey'
125 >
126 >()
127 expectTypeOf(
128 mutationOptions({
129 mutationFn: (id: string) => Promise.resolve(id.length),
130 onSuccess: (data) => {
131 expectTypeOf(data).toEqualTypeOf<number>()
132 },
133 }),
134 ).toEqualTypeOf<
135 Omit<UseMutationOptions<number, DefaultError, string>, 'mutationKey'>
136 >()
137 })
138​
139 it('should infer types when used with useMutation', () => {
140 const mutation = useMutation(
141 mutationOptions({
142 mutationKey: createKey(),
143 mutationFn: () => Promise.resolve('data'),
144 onSuccess: (data) => {
145 expectTypeOf(data).toEqualTypeOf<string>()
146 },
147 }),
148 )
149 expectTypeOf(mutation).toEqualTypeOf<
150 UseMutationResult<string, DefaultError, void, unknown>
151 >()
152​
153 useMutation(
154 // should allow when used with useMutation without mutationKey
155 mutationOptions({
156 mutationFn: () => Promise.resolve('data'),
157 onSuccess: (data) => {
158 expectTypeOf(data).toEqualTypeOf<string>()
159 },
160 }),
161 )
162 })
163​
164 it('should infer types when used with useIsMutating', () => {
165 const isMutating = useIsMutating(
166 mutationOptions({
167 mutationKey: createKey(),
168 mutationFn: () => Promise.resolve(5),
169 }),
170 )
171 expectTypeOf(isMutating).toEqualTypeOf<number>()
172​
173 useIsMutating(
174 // @ts-expect-error filters should have mutationKey
175 mutationOptions({
176 mutationFn: () => Promise.resolve(5),
177 }),
178 )
179 })
180​
181 it('should infer types when used with queryClient.isMutating', () => {
182 const queryClient = new QueryClient()
183​
184 const isMutating = queryClient.isMutating(
185 mutationOptions({
186 mutationKey: createKey(),
187 mutationFn: () => Promise.resolve(5),
188 }),
189 )
190 expectTypeOf(isMutating).toEqualTypeOf<number>()
191​
192 queryClient.isMutating(
193 // @ts-expect-error filters should have mutationKey
194 mutationOptions({
195 mutationFn: () => Promise.resolve(5),
196 }),
197 )
198 })
199​
200 it('should infer types when used with useMutationState', () => {
201 const mutationState = useMutationState({
202 filters: mutationOptions({
203 mutationKey: createKey(),
204 mutationFn: () => Promise.resolve(5),
205 }),
206 })
207 expectTypeOf(mutationState).toEqualTypeOf<
208 Array<MutationState<unknown, Error, unknown, unknown>>
209 >()
210​
211 useMutationState({
212 // @ts-expect-error filters should have mutationKey
213 filters: mutationOptions({
214 mutationFn: () => Promise.resolve(5),
215 }),
216 })
217 })
218})