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 it('should infer with select', () => {
18 const result = useMutationState(() => ({
19 filters: { status: 'pending' },
20 select: (mutation) => mutation.state.status,
21 }))
22β
23 expectTypeOf(result()).toEqualTypeOf<Array<MutationStatus>>()
24 })
25 it('should propagate generics to select callback when TResult is typed MutationState', () => {
26 type MyData = { data: Array<string> }
27 type MyError = { code: number; message: string }
28 type MyVars = { id: number }
29β
30 const result = useMutationState<MutationState<MyData, MyError, MyVars>>(
31 () => ({
32 filters: { mutationKey: ['key'] },
33 select: (mutation) => {
34 expectTypeOf(mutation).toEqualTypeOf<
35 Mutation<MyData, MyError, MyVars, unknown>
36 >()
37 return mutation.state
38 },
39 }),
40 )
41β
42 expectTypeOf(result()).toEqualTypeOf<
43 Array<MutationState<MyData, MyError, MyVars, unknown>>
44 >()
45 })
46})