Open History to see the last commit
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<Array<MutationState>>()
16 })
17β
18 it('should infer with select', () => {
19 const result = useMutationState(() => ({
20 filters: { status: 'pending' },
21 select: (mutation) => mutation.state.status,
22 }))
23β
24 expectTypeOf(result()).toEqualTypeOf<Array<MutationStatus>>()
25 })
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 const result = useMutationState<MutationState<MyData, MyError, MyVars>>(
33 () => ({
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β
44 expectTypeOf(result()).toEqualTypeOf<
45 Array<MutationState<MyData, MyError, MyVars, unknown>>
46 >()
47 })
48})