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

useMutation.test-d.tsx

173 lines5.1 KB

1import { sleep } from '@tanstack/query-test-utils'
2import { describe, expectTypeOf, it } from 'vitest'
3import { reactive } from 'vue-demi'
4import { useMutation } from '../useMutation'
5import type {
6 MutationFunctionContext,
7 MutationKey,
8 QueryClient,
9} from '@tanstack/query-core'
10import type { MutationResult, UseMutationReturnType } from '..'
11​
12describe('Discriminated union return type', () => {
13 it('should have possibly undefined data by default', () => {
14 const mutation = reactive(
15 useMutation({
16 mutationFn: (params: string) => sleep(0).then(() => params),
17 }),
18 )
19​
20 expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
21 })
22​
23 it('should have defined data when mutation is success', () => {
24 const mutation = reactive(
25 useMutation({
26 mutationFn: (params: string) => sleep(0).then(() => params),
27 }),
28 )
29​
30 if (mutation.isSuccess) {
31 expectTypeOf(mutation.data).toEqualTypeOf<string>()
32 }
33 })
34​
35 it('should have null error when mutation is success', () => {
36 const mutation = reactive(
37 useMutation({
38 mutationFn: (params: string) => sleep(0).then(() => params),
39 }),
40 )
41​
42 if (mutation.isSuccess) {
43 expectTypeOf(mutation.error).toEqualTypeOf<null>()
44 }
45 })
46​
47 it('should have undefined data when mutation is pending', () => {
48 const mutation = reactive(
49 useMutation({
50 mutationFn: (params: string) => sleep(0).then(() => params),
51 }),
52 )
53​
54 if (mutation.isPending) {
55 expectTypeOf(mutation.data).toEqualTypeOf<undefined>()
56 }
57 })
58​
59 it('should have defined error when mutation is error', () => {
60 const mutation = reactive(
61 useMutation({
62 mutationFn: (params: string) => sleep(0).then(() => params),
63 }),
64 )
65​
66 if (mutation.isError) {
67 expectTypeOf(mutation.error).toEqualTypeOf<Error>()
68 }
69 })
70​
71 it('should narrow variables', () => {
72 const mutation = reactive(
73 useMutation({
74 mutationFn: (params: string) => sleep(0).then(() => params),
75 }),
76 )
77​
78 if (mutation.isIdle) {
79 expectTypeOf(mutation.variables).toEqualTypeOf<undefined>()
80 return
81 }
82 if (mutation.isPending) {
83 expectTypeOf(mutation.variables).toEqualTypeOf<string>()
84 return
85 }
86 if (mutation.isSuccess) {
87 expectTypeOf(mutation.variables).toEqualTypeOf<string>()
88 return
89 }
90 expectTypeOf(mutation.variables).toEqualTypeOf<string>()
91 })
92})
93​
94describe('useMutation', () => {
95 it('should allow calling mutate with no arguments when TVariables is optional undefinable', () => {
96 const mutation = reactive(
97 useMutation({
98 mutationFn: (_variables: number | undefined) => sleep(0).then(() => 1),
99 }),
100 )
101​
102 expectTypeOf(mutation.mutate).toBeCallableWith()
103 expectTypeOf(mutation.mutateAsync).toBeCallableWith()
104 })
105​
106 it('should type context as the last argument for mutationFn and every hook-level callback', () => {
107 useMutation({
108 mutationFn: (_variables, context) => {
109 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
110 expectTypeOf(context.client).toEqualTypeOf<QueryClient>()
111 return Promise.resolve('data')
112 },
113 onMutate: (_variables, context) => {
114 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
115 },
116 onSuccess: (_data, _variables, _onMutateResult, context) => {
117 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
118 },
119 onError: (_error, _variables, _onMutateResult, context) => {
120 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
121 },
122 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
123 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
124 },
125 })
126 })
127​
128 it('should type context as the last argument for every per-call mutate option', () => {
129 const mutation = useMutation({
130 mutationFn: () => Promise.resolve('data'),
131 })
132​
133 mutation.mutate(undefined, {
134 onSuccess: (_data, _variables, _onMutateResult, context) => {
135 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
136 },
137 onError: (_error, _variables, _onMutateResult, context) => {
138 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
139 },
140 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
141 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
142 },
143 })
144 })
145​
146 it('should type context.mutationKey as MutationKey', () => {
147 useMutation({
148 mutationKey: ['todos', 'add'] as const,
149 mutationFn: () => Promise.resolve('data'),
150 onSuccess: (_data, _variables, _onMutateResult, context) => {
151 expectTypeOf(context.mutationKey).toEqualTypeOf<
152 MutationKey | undefined
153 >()
154 },
155 })
156 })
157​
158 it('should return the result typed with the exported MutationResult', () => {
159 const mutation = useMutation({
160 mutationFn: (params: string) => sleep(0).then(() => params),
161 })
162​
163 expectTypeOf(mutation).toEqualTypeOf<
164 UseMutationReturnType<
165 string,
166 Error,
167 string,
168 unknown,
169 MutationResult<string, Error, string, unknown>
170 >
171 >()
172 })
173})