-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathmutationOptions.test-d.tsx
51 lines (43 loc) · 1.54 KB
/
mutationOptions.test-d.tsx
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
import { describe, expectTypeOf, it } from 'vitest'
import { dataTagSymbol } from '@tanstack/query-core'
import { mutationOptions } from '../mutationOptions'
describe('mutationOptions', () => {
it('should not allow excess properties', () => {
return mutationOptions({
mutationFn: () => Promise.resolve(5),
mutationKey: ['key'],
// @ts-expect-error this is a good error, because onMutates does not exist!
onMutates: 1000,
})
})
it('should infer types for callbacks', () => {
return mutationOptions({
mutationFn: () => Promise.resolve(5),
mutationKey: ['key'],
onSuccess: (data) => {
expectTypeOf(data).toEqualTypeOf<number>()
},
})
})
it('should tag the mutationKey with the result type of the MutationFn', () => {
const { mutationKey } = mutationOptions({
mutationKey: ['key'],
mutationFn: () => Promise.resolve(5),
})
expectTypeOf(mutationKey[dataTagSymbol]).toEqualTypeOf<number>()
})
it('should tag the mutationKey with unknown if there is no mutationFn', () => {
const { mutationKey } = mutationOptions({
mutationKey: ['key'],
})
expectTypeOf(mutationKey[dataTagSymbol]).toEqualTypeOf<unknown>()
})
it('should tag the mutationKey with the result type of the MutationFn if onSuccess is used', () => {
const { mutationKey } = mutationOptions({
mutationKey: ['key'],
mutationFn: () => Promise.resolve(5),
onSuccess: () => {},
})
expectTypeOf(mutationKey[dataTagSymbol]).toEqualTypeOf<number>()
})
})