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

inject-mutation.test-d.ts

175 lines5.8 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { sleep } from '@tanstack/query-test-utils'
3import { injectMutation } from '..'
4import type { CreateMutationOptions, CreateMutationResult } from '..'
5import type { Signal } from '@angular/core'
6import type {
7 MutationFunctionContext,
8 MutationKey,
9 QueryClient,
10} from '@tanstack/query-core'
11​
12describe('injectMutation', () => {
13 it('supports reading data from a generic result type', () => {
14 const readId = <T extends { id: number }>(
15 mutation: CreateMutationResult<T>,
16 ) => mutation.data()?.id
17​
18 expectTypeOf(readId).toBeCallableWith(
19 {} as CreateMutationResult<{ id: number }>,
20 )
21 })
22​
23 describe('Discriminated union return type', () => {
24 it('should have possibly undefined data by default', () => {
25 const mutation = injectMutation(() => ({
26 mutationFn: () => sleep(0).then(() => 'string'),
27 }))
28​
29 expectTypeOf(mutation.data).toEqualTypeOf<Signal<string | undefined>>()
30 })
31​
32 it('should have defined data when mutation is success', () => {
33 const mutation = injectMutation(() => ({
34 mutationFn: () => sleep(0).then(() => 'string'),
35 }))
36​
37 if (mutation.isSuccess()) {
38 expectTypeOf(mutation.data).toEqualTypeOf<Signal<string>>()
39 }
40 })
41​
42 it('should have null error when mutation is success', () => {
43 const mutation = injectMutation(() => ({
44 mutationFn: () => sleep(0).then(() => 'string'),
45 }))
46​
47 if (mutation.isSuccess()) {
48 expectTypeOf(mutation.error).toEqualTypeOf<Signal<null>>()
49 }
50 })
51​
52 it('should have undefined data when mutation is pending', () => {
53 const mutation = injectMutation(() => ({
54 mutationFn: () => sleep(0).then(() => 'string'),
55 }))
56​
57 if (mutation.isPending()) {
58 expectTypeOf(mutation.data).toEqualTypeOf<Signal<undefined>>()
59 }
60 })
61​
62 it('should have defined error when mutation is error', () => {
63 const mutation = injectMutation(() => ({
64 mutationFn: () => sleep(0).then(() => 'string'),
65 }))
66​
67 if (mutation.isError()) {
68 expectTypeOf(mutation.error).toEqualTypeOf<Signal<Error>>()
69 }
70 })
71​
72 it('should narrow variables', () => {
73 const mutation = injectMutation(() => ({
74 mutationFn: (_variables: string) => sleep(0).then(() => 'string'),
75 }))
76​
77 if (mutation.isIdle()) {
78 expectTypeOf(mutation.variables).toEqualTypeOf<Signal<undefined>>()
79 }
80 if (mutation.isPending()) {
81 expectTypeOf(mutation.variables).toEqualTypeOf<Signal<string>>()
82 }
83 if (mutation.isSuccess()) {
84 expectTypeOf(mutation.variables).toEqualTypeOf<Signal<string>>()
85 }
86 expectTypeOf(mutation.variables).toEqualTypeOf<
87 Signal<string | undefined>
88 >()
89 })
90 })
91​
92 it('should infer TOnMutateResult from onMutate return type', () => {
93 injectMutation(() => ({
94 mutationFn: () => sleep(0).then(() => 'string'),
95 onMutate: () => {
96 return { token: 'abc' }
97 },
98 onSuccess: (_data, _variables, onMutateResult) => {
99 expectTypeOf(onMutateResult).toEqualTypeOf<{ token: string }>()
100 },
101 onError: (_error, _variables, onMutateResult) => {
102 expectTypeOf(onMutateResult).toEqualTypeOf<
103 { token: string } | undefined
104 >()
105 },
106 }))
107 })
108​
109 it('should allow calling mutate with no arguments when TVariables is optional undefinable', () => {
110 const mutation = injectMutation(() => ({
111 mutationFn: (_variables: number | undefined) => sleep(0).then(() => 1),
112 }))
113​
114 expectTypeOf(mutation.mutate).toBeCallableWith()
115 expectTypeOf(mutation.mutateAsync).toBeCallableWith()
116 })
117​
118 it('should type context as the last argument for mutationFn and every hook-level callback', () => {
119 injectMutation(() => ({
120 mutationFn: (_variables, context) => {
121 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
122 expectTypeOf(context.client).toEqualTypeOf<QueryClient>()
123 return Promise.resolve('data')
124 },
125 onMutate: (_variables, context) => {
126 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
127 },
128 onSuccess: (_data, _variables, _onMutateResult, context) => {
129 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
130 },
131 onError: (_error, _variables, _onMutateResult, context) => {
132 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
133 },
134 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
135 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
136 },
137 }))
138 })
139​
140 it('should type context as the last argument for every per-call mutate option', () => {
141 const mutation = injectMutation(() => ({
142 mutationFn: () => Promise.resolve('data'),
143 }))
144​
145 mutation.mutate(undefined, {
146 onSuccess: (_data, _variables, _onMutateResult, context) => {
147 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
148 },
149 onError: (_error, _variables, _onMutateResult, context) => {
150 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
151 },
152 onSettled: (_data, _error, _variables, _onMutateResult, context) => {
153 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
154 },
155 })
156 })
157​
158 it('should type context.mutationKey as MutationKey', () => {
159 injectMutation(() => ({
160 mutationKey: ['todos', 'add'] as const,
161 mutationFn: () => Promise.resolve('data'),
162 onSuccess: (_data, _variables, _onMutateResult, context) => {
163 expectTypeOf(context.mutationKey).toEqualTypeOf<
164 MutationKey | undefined
165 >()
166 },
167 }))
168 })
169})
170​
171describe('injectMutation options', () => {
172 it('omits observer error reporting options', () => {
173 expectTypeOf<CreateMutationOptions>().not.toHaveProperty('throwOnError')
174 })
175})