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

useMutationState.test-d.tsx

42 lines1.2 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { useMutationState } from '../useMutationState'
3import type {
4 Mutation,
5 MutationState,
6 MutationStatus,
7} from '@tanstack/query-core'
8​
9describe('useMutationState', () => {
10 it('should default to QueryState', () => {
11 const result = useMutationState({
12 filters: { status: 'pending' },
13 })
14​
15 expectTypeOf(result).toEqualTypeOf<
16 Array<MutationState<unknown, Error, unknown, unknown>>
17 >()
18 })
19 it('should infer with select', () => {
20 const result = useMutationState({
21 filters: { status: 'pending' },
22 select: (mutation) => mutation.state.status,
23 })
24​
25 expectTypeOf(result).toEqualTypeOf<Array<MutationStatus>>()
26 })
27 it('should propagate generics to select callback when TResult is typed MutationState', () => {
28 type MyData = { data: Array<string> }
29 type MyError = { code: number; message: string }
30 type MyVars = { id: number }
31​
32 useMutationState<MutationState<MyData, MyError, MyVars>>({
33 filters: { mutationKey: ['key'] },
34 select: (mutation) => {
35 expectTypeOf(mutation).toEqualTypeOf<
36 Mutation<MyData, MyError, MyVars, unknown>
37 >()
38 return mutation.state
39 },
40 })
41 })
42})