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

mutation-options.test-d.ts

224 lines6.3 KB

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