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

inject-mutation.test-d.ts

158 lines5.3 KB

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