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