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

mutation-options.test-d.ts

222 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<{ name: string }>()
71 },
72 })
73 })
74​
75 it('should infer context type correctly', () => {
76 mutationOptions<number>({
77 mutationFn: (_variables, context) => {
78 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
79 return Promise.resolve(5)
80 },
81 mutationKey: ['key'],
82 onMutate: (_variables, context) => {
83 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
84 },
85 onSuccess: (_data, _variables, _onMutateResult, context) => {
86 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
87 },
88 onError: (_error, _variables, _onMutateResult, context) => {
89 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
90 },
91 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
92 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
93 },
94 })
95 })
96​
97 it('should error if mutationFn return type mismatches TData', () => {
98 assertType(
99 mutationOptions<number>({
100 // @ts-expect-error this is a good error, because return type is string, not number
101 mutationFn: async () => Promise.resolve('wrong return'),
102 }),
103 )
104 })
105​
106 it('should allow mutationKey to be omitted', () => {
107 return mutationOptions({
108 mutationFn: () => Promise.resolve(123),
109 onSuccess: (data) => {
110 expectTypeOf(data).toEqualTypeOf<number>()
111 },
112 })
113 })
114​
115 it('should infer all types when not explicitly provided', () => {
116 expectTypeOf(
117 mutationOptions({
118 mutationFn: (id: string) => Promise.resolve(id.length),
119 mutationKey: ['key'],
120 onSuccess: (data) => {
121 expectTypeOf(data).toEqualTypeOf<number>()
122 },
123 }),
124 ).toEqualTypeOf<
125 WithRequired<
126 CreateMutationOptions<number, DefaultError, string>,
127 'mutationKey'
128 >
129 >()
130 expectTypeOf(
131 mutationOptions({
132 mutationFn: (id: string) => Promise.resolve(id.length),
133 onSuccess: (data) => {
134 expectTypeOf(data).toEqualTypeOf<number>()
135 },
136 }),
137 ).toEqualTypeOf<
138 Omit<CreateMutationOptions<number, DefaultError, string>, 'mutationKey'>
139 >()
140 })
141​
142 it('should infer types when used with injectMutation', () => {
143 const mutation = injectMutation(() =>
144 mutationOptions({
145 mutationKey: ['key'],
146 mutationFn: () => Promise.resolve('data'),
147 onSuccess: (data) => {
148 expectTypeOf(data).toEqualTypeOf<string>()
149 },
150 }),
151 )
152 expectTypeOf(mutation).toEqualTypeOf<
153 CreateMutationResult<string, DefaultError, void, unknown>
154 >()
155​
156 injectMutation(
157 // should allow when used with injectMutation without mutationKey
158 () =>
159 mutationOptions({
160 mutationFn: () => Promise.resolve('data'),
161 onSuccess: (data) => {
162 expectTypeOf(data).toEqualTypeOf<string>()
163 },
164 }),
165 )
166 })
167​
168 it('should infer types when used with injectIsMutating', () => {
169 const isMutating = injectIsMutating(
170 mutationOptions({
171 mutationKey: ['key'],
172 mutationFn: () => Promise.resolve(5),
173 }),
174 )
175 expectTypeOf(isMutating()).toEqualTypeOf<number>()
176​
177 injectIsMutating(
178 // @ts-expect-error filters should have mutationKey
179 mutationOptions({
180 mutationFn: () => Promise.resolve(5),
181 }),
182 )
183 })
184​
185 it('should infer types when used with queryClient.isMutating', () => {
186 const queryClient = new QueryClient()
187​
188 const isMutating = queryClient.isMutating(
189 mutationOptions({
190 mutationKey: ['key'],
191 mutationFn: () => Promise.resolve(5),
192 }),
193 )
194 expectTypeOf(isMutating).toEqualTypeOf<number>()
195​
196 queryClient.isMutating(
197 // @ts-expect-error filters should have mutationKey
198 mutationOptions({
199 mutationFn: () => Promise.resolve(5),
200 }),
201 )
202 })
203​
204 it('should infer types when used with injectMutationState', () => {
205 const mutationState = injectMutationState(() => ({
206 filters: mutationOptions({
207 mutationKey: ['key'],
208 mutationFn: () => Promise.resolve(5),
209 }),
210 }))
211 expectTypeOf(mutationState()).toEqualTypeOf<
212 Array<MutationState<unknown, Error, unknown, unknown>>
213 >()
214​
215 injectMutationState({
216 // @ts-expect-error filters should have mutationKey
217 filters: mutationOptions({
218 mutationFn: () => Promise.resolve(5),
219 }),
220 })
221 })
222})