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

useMutation.test-d.tsx

142 lines4.4 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { useMutation } from '../useMutation'
3import { QueryClient } from '../QueryClient'
4import type { DefaultError } from '@tanstack/query-core'
5import type { UseMutationResult } from '../types'
6​
7describe('useMutation', () => {
8 it('should infer TData from mutationFn return type', () => {
9 const mutation = useMutation(() => ({
10 mutationFn: () => Promise.resolve('data'),
11 }))
12​
13 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
14 expectTypeOf(mutation.error).toEqualTypeOf<DefaultError | null>()
15 })
16​
17 it('should infer TVariables from mutationFn parameter', () => {
18 const mutation = useMutation(() => ({
19 mutationFn: (vars: { id: string }) => Promise.resolve(vars.id),
20 }))
21​
22 expectTypeOf(mutation.mutate).toBeCallableWith({ id: '1' })
23 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
24 })
25​
26 it('should infer TOnMutateResult from onMutate return type', () => {
27 useMutation(() => ({
28 mutationFn: () => Promise.resolve('data'),
29 onMutate: () => {
30 return { token: 'abc' }
31 },
32 onSuccess: (_data, _variables, onMutateResult) => {
33 expectTypeOf(onMutateResult).toEqualTypeOf<{ token: string }>()
34 },
35 onError: (_error, _variables, onMutateResult) => {
36 expectTypeOf(onMutateResult).toEqualTypeOf<
37 { token: string } | undefined
38 >()
39 },
40 }))
41 })
42​
43 it('should allow explicit generic types', () => {
44 const mutation = useMutation<string, Error, { id: number }>(() => ({
45 mutationFn: (vars) => {
46 expectTypeOf(vars).toEqualTypeOf<{ id: number }>()
47 return Promise.resolve('result')
48 },
49 }))
50​
51 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
52 expectTypeOf(mutation.error).toEqualTypeOf<Error | null>()
53 })
54​
55 it('should return correct UseMutationResult type', () => {
56 const mutation = useMutation(() => ({
57 mutationFn: () => Promise.resolve(42),
58 }))
59​
60 expectTypeOf(mutation).toEqualTypeOf<
61 UseMutationResult<number, DefaultError, void, unknown>
62 >()
63 })
64​
65 it('should type mutate with a Promise return type and no mutateAsync', () => {
66 const mutation = useMutation(() => ({
67 mutationFn: (id: string) => Promise.resolve(id.length),
68 }))
69​
70 expectTypeOf(mutation.mutate).toBeCallableWith('test')
71 expectTypeOf(mutation.mutate('test')).toEqualTypeOf<Promise<number>>()
72 // mutateAsync is removed; the single mutate returns the promise.
73 expectTypeOf(mutation).not.toHaveProperty('mutateAsync')
74 // context is removed from the result: no onMutate context threading.
75 expectTypeOf(mutation).not.toHaveProperty('context')
76 })
77​
78 it('should default TVariables to void when mutationFn has no parameters', () => {
79 const mutation = useMutation(() => ({
80 mutationFn: () => Promise.resolve('data'),
81 }))
82​
83 expectTypeOf(mutation.mutate).toBeCallableWith()
84 })
85​
86 it('should infer custom TError type', () => {
87 class CustomError extends Error {
88 code: number
89 constructor(code: number) {
90 super()
91 this.code = code
92 }
93 }
94​
95 const mutation = useMutation<string, CustomError>(() => ({
96 mutationFn: () => Promise.resolve('data'),
97 }))
98​
99 expectTypeOf(mutation.error).toEqualTypeOf<CustomError | null>()
100 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
101 })
102​
103 it('should infer types for onSettled callback', () => {
104 useMutation(() => ({
105 mutationFn: () => Promise.resolve(42),
106 onSettled: (data, error, _variables, _onMutateResult) => {
107 expectTypeOf(data).toEqualTypeOf<number | undefined>()
108 expectTypeOf(error).toEqualTypeOf<DefaultError | null>()
109 },
110 }))
111 })
112​
113 it('should infer custom TError in onError callback', () => {
114 class CustomError extends Error {
115 code: number
116 constructor(code: number) {
117 super()
118 this.code = code
119 }
120 }
121​
122 useMutation<string, CustomError>(() => ({
123 mutationFn: () => Promise.resolve('data'),
124 onError: (error) => {
125 expectTypeOf(error).toEqualTypeOf<CustomError>()
126 },
127 }))
128 })
129​
130 it('should accept queryClient as second argument', () => {
131 const queryClient = new QueryClient()
132​
133 const mutation = useMutation(
134 () => ({
135 mutationFn: () => Promise.resolve('data'),
136 }),
137 () => queryClient,
138 )
139​
140 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
141 })
142})