Open History to see the last commit
1import { QueryClient } from '@tanstack/query-core'
2import { createKey } from '@tanstack/query-test-utils'
3import { LitElement } from 'lit'
4import { assertType, describe, expectTypeOf, it } from 'vitest'
5import { createMutationController } from '../createMutationController.js'
6import { mutationOptions } from '../mutationOptions.js'
7import { useIsMutating } from '../useIsMutating.js'
8import type {
9 DefaultError,
10 MutationFunctionContext,
11} from '@tanstack/query-core'
12β
13class Host extends LitElement {}
14β
15describe('mutationOptions', () => {
16 it('should not allow excess properties', () => {
17 mutationOptions({
18 mutationFn: () => Promise.resolve(5),
19 // @ts-expect-error this is a good error, because onMutates does not exist!
20 onMutates: 1000,
21 mutationKey: createKey(),
22 onSuccess: (data) => {
23 expectTypeOf(data).toEqualTypeOf<number>()
24 },
25 })
26 })
27β
28 it('should infer types for callbacks', () => {
29 mutationOptions({
30 mutationFn: () => Promise.resolve(5),
31 mutationKey: createKey(),
32 onSuccess: (data) => {
33 expectTypeOf(data).toEqualTypeOf<number>()
34 },
35 })
36 })
37β
38 it('should infer types for onError callback', () => {
39 mutationOptions({
40 mutationFn: () => {
41 throw new Error('fail')
42 },
43 mutationKey: createKey(),
44 onError: (error) => {
45 expectTypeOf(error).toEqualTypeOf<DefaultError>()
46 },
47 })
48 })
49β
50 it('should infer types for variables', () => {
51 mutationOptions<number, DefaultError, { id: string }>({
52 mutationFn: (vars) => {
53 expectTypeOf(vars).toEqualTypeOf<{ id: string }>()
54 return Promise.resolve(5)
55 },
56 mutationKey: createKey(),
57 })
58 })
59β
60 it('should infer result type correctly', () => {
61 mutationOptions<number, DefaultError, void, { name: string }>({
62 mutationFn: () => Promise.resolve(5),
63 mutationKey: createKey(),
64 onMutate: () => {
65 return { name: 'onMutateResult' }
66 },
67 onSuccess: (_data, _variables, onMutateResult) => {
68 expectTypeOf(onMutateResult).toEqualTypeOf<{ name: string }>()
69 },
70 })
71 })
72β
73 it('should infer context type correctly', () => {
74 mutationOptions<number>({
75 mutationFn: (_variables, context) => {
76 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
77 return Promise.resolve(5)
78 },
79 mutationKey: createKey(),
80 onMutate: (_variables, context) => {
81 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
82 },
83 onSuccess: (_data, _variables, _onMutateResult, context) => {
84 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
85 },
86 onError: (_error, _variables, _onMutateResult, context) => {
87 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
88 },
89 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
90 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
91 },
92 })
93 })
94β
95 it('should error if mutationFn return type mismatches TData', () => {
96 assertType(
97 mutationOptions<number>({
98 // @ts-expect-error this is a good error, because return type is string, not number
99 mutationFn: async () => Promise.resolve('wrong return'),
100 }),
101 )
102 })
103β
104 it('should allow mutationKey to be omitted', () => {
105 return mutationOptions({
106 mutationFn: () => Promise.resolve(123),
107 onSuccess: (data) => {
108 expectTypeOf(data).toEqualTypeOf<number>()
109 },
110 })
111 })
112β
113 it('should infer types when used with createMutationController', () => {
114 const mutation = createMutationController(
115 new Host(),
116 mutationOptions({
117 mutationFn: (input: { id: number }) =>
118 Promise.resolve(input.id.toString()),
119 }),
120 new QueryClient(),
121 )
122β
123 expectTypeOf(mutation().data).toEqualTypeOf<string | undefined>()
124 expectTypeOf(mutation().variables).toEqualTypeOf<
125 { id: number } | undefined
126 >()
127 })
128β
129 it('should infer types when used with useIsMutating', () => {
130 const isMutating = useIsMutating(
131 new Host(),
132 mutationOptions({
133 mutationKey: createKey(),
134 mutationFn: () => Promise.resolve(5),
135 }),
136 new QueryClient(),
137 )
138 expectTypeOf(isMutating()).toEqualTypeOf<number>()
139 })
140β
141 it('should infer types when used with queryClient.isMutating', () => {
142 const queryClient = new QueryClient()
143β
144 const isMutating = queryClient.isMutating(
145 mutationOptions({
146 mutationKey: createKey(),
147 mutationFn: () => Promise.resolve(5),
148 }),
149 )
150 expectTypeOf(isMutating).toEqualTypeOf<number>()
151 })
152})