-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathuseMutationState.test-d.tsx
More file actions
46 lines (41 loc) · 1.35 KB
/
useMutationState.test-d.tsx
File metadata and controls
46 lines (41 loc) · 1.35 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
import { describe, expectTypeOf, it } from 'vitest'
import { useMutationState } from '../useMutationState'
import type {
Mutation,
MutationState,
MutationStatus,
} from '@tanstack/query-core'
describe('useMutationState', () => {
it('should default to QueryState', () => {
const result = useMutationState(() => ({
filters: { status: 'pending' },
}))
expectTypeOf(result()).toEqualTypeOf<Array<MutationState>>()
})
it('should infer with select', () => {
const result = useMutationState(() => ({
filters: { status: 'pending' },
select: (mutation) => mutation.state.status,
}))
expectTypeOf(result()).toEqualTypeOf<Array<MutationStatus>>()
})
it('should propagate generics to select callback when TResult is typed MutationState', () => {
type MyData = { data: Array<string> }
type MyError = { code: number; message: string }
type MyVars = { id: number }
const result = useMutationState<MutationState<MyData, MyError, MyVars>>(
() => ({
filters: { mutationKey: ['key'] },
select: (mutation) => {
expectTypeOf(mutation).toEqualTypeOf<
Mutation<MyData, MyError, MyVars, unknown>
>()
return mutation.state
},
}),
)
expectTypeOf(result()).toEqualTypeOf<
Array<MutationState<MyData, MyError, MyVars, unknown>>
>()
})
})