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

useMutationState.test-d.tsx

43 lines1.2 KB

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