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