@tanstack/angular-query-experimental 5.103.2 → 5.104.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. package/dist/create-base-query.d.mts +168 -0
  2. package/dist/create-base-query.d.mts.map +1 -0
  3. package/dist/create-base-query.mjs +91 -0
  4. package/dist/create-base-query.mjs.map +1 -0
  5. package/dist/devtools/index.d.mts +3 -0
  6. package/dist/devtools/index.mjs +2 -0
  7. package/dist/devtools/production/index.d.mts +4 -0
  8. package/dist/devtools/production/index.mjs +2 -0
  9. package/dist/devtools/stub.d.mts +5 -0
  10. package/dist/devtools/stub.d.mts.map +1 -0
  11. package/dist/devtools/stub.mjs +9 -0
  12. package/dist/devtools/stub.mjs.map +1 -0
  13. package/dist/devtools/types.d.mts +106 -0
  14. package/dist/devtools/types.d.mts.map +1 -0
  15. package/dist/devtools/types.mjs +1 -0
  16. package/{devtools/with-devtools.d.ts → dist/devtools/with-devtools.d.mts} +4 -1
  17. package/dist/devtools/with-devtools.d.mts.map +1 -0
  18. package/dist/devtools/with-devtools.mjs +110 -0
  19. package/dist/devtools/with-devtools.mjs.map +1 -0
  20. package/dist/devtools-panel/index.d.mts +3 -0
  21. package/dist/devtools-panel/index.mjs +2 -0
  22. package/{devtools-panel/inject-devtools-panel.d.ts → dist/devtools-panel/inject-devtools-panel.d.mts} +4 -1
  23. package/dist/devtools-panel/inject-devtools-panel.d.mts.map +1 -0
  24. package/dist/devtools-panel/inject-devtools-panel.mjs +67 -0
  25. package/dist/devtools-panel/inject-devtools-panel.mjs.map +1 -0
  26. package/dist/devtools-panel/production/index.d.mts +4 -0
  27. package/dist/devtools-panel/production/index.mjs +2 -0
  28. package/dist/devtools-panel/stub.d.mts +5 -0
  29. package/dist/devtools-panel/stub.d.mts.map +1 -0
  30. package/dist/devtools-panel/stub.mjs +7 -0
  31. package/dist/devtools-panel/stub.mjs.map +1 -0
  32. package/dist/devtools-panel/types.d.mts +51 -0
  33. package/dist/devtools-panel/types.d.mts.map +1 -0
  34. package/dist/devtools-panel/types.mjs +1 -0
  35. package/dist/index.d.mts +16 -0
  36. package/{index.mjs → dist/index.mjs} +8 -21
  37. package/dist/index.mjs.map +1 -0
  38. package/{infinite-query-options.d.ts → dist/infinite-query-options.d.mts} +28 -25
  39. package/dist/infinite-query-options.d.mts.map +1 -0
  40. package/dist/infinite-query-options.mjs +8 -0
  41. package/dist/infinite-query-options.mjs.map +1 -0
  42. package/{inject-infinite-query.d.ts → dist/inject-infinite-query.d.mts} +13 -10
  43. package/dist/inject-infinite-query.d.mts.map +1 -0
  44. package/dist/inject-infinite-query.mjs +13 -0
  45. package/dist/inject-infinite-query.mjs.map +1 -0
  46. package/{inject-is-fetching.d.ts → dist/inject-is-fetching.d.mts} +11 -8
  47. package/dist/inject-is-fetching.d.mts.map +1 -0
  48. package/dist/inject-is-fetching.mjs +69 -0
  49. package/dist/inject-is-fetching.mjs.map +1 -0
  50. package/{inject-is-mutating.d.ts → dist/inject-is-mutating.d.mts} +11 -8
  51. package/dist/inject-is-mutating.d.mts.map +1 -0
  52. package/dist/inject-is-mutating.mjs +52 -0
  53. package/dist/inject-is-mutating.mjs.map +1 -0
  54. package/{inject-is-restoring.d.ts → dist/inject-is-restoring.d.mts} +10 -8
  55. package/dist/inject-is-restoring.d.mts.map +1 -0
  56. package/dist/inject-is-restoring.mjs +35 -0
  57. package/dist/inject-is-restoring.mjs.map +1 -0
  58. package/{inject-mutation-state.d.ts → dist/inject-mutation-state.d.mts} +13 -11
  59. package/dist/inject-mutation-state.d.mts.map +1 -0
  60. package/dist/inject-mutation-state.mjs +103 -0
  61. package/dist/inject-mutation-state.mjs.map +1 -0
  62. package/{inject-mutation.d.ts → dist/inject-mutation.d.mts} +12 -9
  63. package/dist/inject-mutation.d.mts.map +1 -0
  64. package/dist/inject-mutation.mjs +226 -0
  65. package/dist/inject-mutation.mjs.map +1 -0
  66. package/dist/inject-queries-experimental/index.d.mts +2 -0
  67. package/dist/inject-queries-experimental/index.mjs +2 -0
  68. package/{inject-queries.d.ts → dist/inject-queries.d.mts} +33 -53
  69. package/dist/inject-queries.d.mts.map +1 -0
  70. package/dist/inject-queries.mjs +203 -0
  71. package/dist/inject-queries.mjs.map +1 -0
  72. package/{inject-query-client.d.ts → dist/inject-query-client.d.mts} +6 -3
  73. package/dist/inject-query-client.d.mts.map +1 -0
  74. package/dist/inject-query-client.mjs +23 -0
  75. package/dist/inject-query-client.mjs.map +1 -0
  76. package/{inject-query.d.ts → dist/inject-query.d.mts} +13 -10
  77. package/dist/inject-query.d.mts.map +1 -0
  78. package/dist/inject-query.mjs +12 -0
  79. package/dist/inject-query.mjs.map +1 -0
  80. package/{mutation-options.d.ts → dist/mutation-options.d.mts} +5 -2
  81. package/dist/mutation-options.d.mts.map +1 -0
  82. package/dist/mutation-options.mjs +8 -0
  83. package/dist/mutation-options.mjs.map +1 -0
  84. package/dist/pending-tasks-compat.d.mts +9 -0
  85. package/dist/pending-tasks-compat.d.mts.map +1 -0
  86. package/dist/pending-tasks-compat.mjs +13 -0
  87. package/dist/pending-tasks-compat.mjs.map +1 -0
  88. package/{providers.d.ts → dist/providers.d.mts} +7 -5
  89. package/dist/providers.d.mts.map +1 -0
  90. package/dist/providers.mjs +124 -0
  91. package/dist/providers.mjs.map +1 -0
  92. package/{query-options.d.ts → dist/query-options.d.mts} +34 -31
  93. package/dist/query-options.d.mts.map +1 -0
  94. package/dist/query-options.mjs +8 -0
  95. package/dist/query-options.mjs.map +1 -0
  96. package/{signal-proxy.d.ts → dist/signal-proxy.d.mts} +5 -4
  97. package/dist/signal-proxy.d.mts.map +1 -0
  98. package/dist/signal-proxy.mjs +35 -0
  99. package/dist/signal-proxy.mjs.map +1 -0
  100. package/{types.d.ts → dist/types.d.mts} +24 -26
  101. package/dist/types.d.mts.map +1 -0
  102. package/dist/types.mjs +6 -0
  103. package/dist/types.mjs.map +1 -0
  104. package/package.json +50 -20
  105. package/src/__tests__/infinite-query-options.test-d.ts +314 -0
  106. package/src/__tests__/infinite-query-options.test.ts +19 -0
  107. package/src/__tests__/inject-devtools-panel.test.ts +196 -0
  108. package/src/__tests__/inject-infinite-query.test-d.ts +66 -0
  109. package/src/__tests__/inject-infinite-query.test.ts +227 -0
  110. package/src/__tests__/inject-is-fetching.test.ts +108 -0
  111. package/src/__tests__/inject-is-mutating.test.ts +113 -0
  112. package/src/__tests__/inject-is-restoring.test.ts +93 -0
  113. package/src/__tests__/inject-mutation-state.test-d.ts +22 -0
  114. package/src/__tests__/inject-mutation-state.test.ts +198 -0
  115. package/src/__tests__/inject-mutation.test-d.ts +158 -0
  116. package/src/__tests__/inject-mutation.test.ts +1161 -0
  117. package/src/__tests__/inject-queries.test-d.ts +196 -0
  118. package/src/__tests__/inject-queries.test.ts +240 -0
  119. package/src/__tests__/inject-query.test-d.ts +357 -0
  120. package/src/__tests__/inject-query.test.ts +700 -0
  121. package/src/__tests__/mutation-options.test-d.ts +234 -0
  122. package/src/__tests__/mutation-options.test.ts +344 -0
  123. package/src/__tests__/pending-tasks.test.ts +644 -0
  124. package/src/__tests__/provide-query-client.test.ts +38 -0
  125. package/src/__tests__/provide-tanstack-query.test.ts +38 -0
  126. package/src/__tests__/query-options.test-d.ts +262 -0
  127. package/src/__tests__/query-options.test.ts +14 -0
  128. package/src/__tests__/signal-proxy.test.ts +27 -0
  129. package/src/__tests__/test-utils.ts +77 -0
  130. package/src/__tests__/with-devtools.test.ts +674 -0
  131. package/src/create-base-query.ts +176 -0
  132. package/src/devtools/index.ts +8 -0
  133. package/src/devtools/production/index.ts +1 -0
  134. package/src/devtools/stub.ts +7 -0
  135. package/src/devtools/types.ts +116 -0
  136. package/src/devtools/with-devtools.ts +182 -0
  137. package/src/devtools-panel/index.ts +8 -0
  138. package/src/devtools-panel/inject-devtools-panel.ts +110 -0
  139. package/src/devtools-panel/production/index.ts +1 -0
  140. package/src/devtools-panel/stub.ts +7 -0
  141. package/src/devtools-panel/types.ts +57 -0
  142. package/src/index.ts +60 -0
  143. package/src/infinite-query-options.ts +354 -0
  144. package/src/inject-infinite-query.ts +296 -0
  145. package/src/inject-is-fetching.ts +97 -0
  146. package/src/inject-is-mutating.ts +80 -0
  147. package/src/inject-is-restoring.ts +53 -0
  148. package/src/inject-mutation-state.ts +167 -0
  149. package/src/inject-mutation.ts +328 -0
  150. package/src/inject-queries-experimental/index.ts +1 -0
  151. package/src/inject-queries.ts +480 -0
  152. package/src/inject-query-client.ts +22 -0
  153. package/src/inject-query.ts +208 -0
  154. package/src/mutation-options.ts +121 -0
  155. package/src/pending-tasks-compat.ts +27 -0
  156. package/src/providers.ts +170 -0
  157. package/src/query-options.ts +292 -0
  158. package/src/signal-proxy.ts +46 -0
  159. package/src/types.ts +429 -0
  160. package/create-base-query.d.ts +0 -164
  161. package/create-base-query.mjs +0 -99
  162. package/create-base-query.mjs.map +0 -1
  163. package/devtools/index.d.ts +0 -2
  164. package/devtools/index.mjs +0 -5
  165. package/devtools/index.mjs.map +0 -1
  166. package/devtools/production/index.d.ts +0 -1
  167. package/devtools/stub.d.ts +0 -2
  168. package/devtools/stub.mjs +0 -8
  169. package/devtools/stub.mjs.map +0 -1
  170. package/devtools/types.d.ts +0 -103
  171. package/devtools/with-devtools.mjs +0 -106
  172. package/devtools/with-devtools.mjs.map +0 -1
  173. package/devtools-panel/index.d.ts +0 -2
  174. package/devtools-panel/index.mjs +0 -5
  175. package/devtools-panel/index.mjs.map +0 -1
  176. package/devtools-panel/inject-devtools-panel.mjs +0 -72
  177. package/devtools-panel/inject-devtools-panel.mjs.map +0 -1
  178. package/devtools-panel/production/index.d.ts +0 -1
  179. package/devtools-panel/stub.d.ts +0 -2
  180. package/devtools-panel/stub.mjs +0 -8
  181. package/devtools-panel/stub.mjs.map +0 -1
  182. package/devtools-panel/types.d.ts +0 -48
  183. package/index.d.ts +0 -25
  184. package/index.mjs.map +0 -1
  185. package/infinite-query-options.mjs +0 -7
  186. package/infinite-query-options.mjs.map +0 -1
  187. package/inject-infinite-query.mjs +0 -18
  188. package/inject-infinite-query.mjs.map +0 -1
  189. package/inject-is-fetching.mjs +0 -31
  190. package/inject-is-fetching.mjs.map +0 -1
  191. package/inject-is-mutating.mjs +0 -31
  192. package/inject-is-mutating.mjs.map +0 -1
  193. package/inject-is-restoring.mjs +0 -21
  194. package/inject-is-restoring.mjs.map +0 -1
  195. package/inject-mutation-state.mjs +0 -51
  196. package/inject-mutation-state.mjs.map +0 -1
  197. package/inject-mutation.mjs +0 -94
  198. package/inject-mutation.mjs.map +0 -1
  199. package/inject-queries-experimental/index.d.ts +0 -1
  200. package/inject-queries-experimental/index.mjs +0 -5
  201. package/inject-queries-experimental/index.mjs.map +0 -1
  202. package/inject-queries.mjs +0 -82
  203. package/inject-queries.mjs.map +0 -1
  204. package/inject-query-client.mjs +0 -9
  205. package/inject-query-client.mjs.map +0 -1
  206. package/inject-query.mjs +0 -14
  207. package/inject-query.mjs.map +0 -1
  208. package/mutation-options.mjs +0 -7
  209. package/mutation-options.mjs.map +0 -1
  210. package/pending-tasks-compat.d.ts +0 -7
  211. package/pending-tasks-compat.mjs +0 -19
  212. package/pending-tasks-compat.mjs.map +0 -1
  213. package/providers.mjs +0 -32
  214. package/providers.mjs.map +0 -1
  215. package/query-options.mjs +0 -7
  216. package/query-options.mjs.map +0 -1
  217. package/signal-proxy.mjs +0 -29
  218. package/signal-proxy.mjs.map +0 -1
@@ -0,0 +1,198 @@
1
+ import {
2
+ Component,
3
+ Injector,
4
+ input,
5
+ provideZonelessChangeDetection,
6
+ signal,
7
+ } from '@angular/core'
8
+ import { TestBed } from '@angular/core/testing'
9
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
10
+ import { By } from '@angular/platform-browser'
11
+ import { queryKey, sleep } from '@tanstack/query-test-utils'
12
+ import {
13
+ QueryClient,
14
+ injectMutation,
15
+ injectMutationState,
16
+ provideTanStackQuery,
17
+ } from '..'
18
+ import { setFixtureSignalInputs } from './test-utils'
19
+
20
+ describe('injectMutationState', () => {
21
+ let queryClient: QueryClient
22
+
23
+ beforeEach(() => {
24
+ queryClient = new QueryClient()
25
+ vi.useFakeTimers()
26
+ TestBed.configureTestingModule({
27
+ providers: [
28
+ provideZonelessChangeDetection(),
29
+ provideTanStackQuery(queryClient),
30
+ ],
31
+ })
32
+ })
33
+
34
+ afterEach(() => {
35
+ vi.useRealTimers()
36
+ })
37
+
38
+ describe('injectMutationState', () => {
39
+ it('should return variables after calling mutate 1', () => {
40
+ const mutationKey = queryKey()
41
+ const variables = 'foo123'
42
+
43
+ const mutation = TestBed.runInInjectionContext(() => {
44
+ return injectMutation(() => ({
45
+ mutationKey: mutationKey,
46
+ mutationFn: (params: string) => sleep(0).then(() => params),
47
+ }))
48
+ })
49
+
50
+ mutation.mutate(variables)
51
+
52
+ const mutationState = TestBed.runInInjectionContext(() => {
53
+ return injectMutationState(() => ({
54
+ filters: { mutationKey, status: 'pending' },
55
+ select: (m) => m.state.variables,
56
+ }))
57
+ })
58
+
59
+ expect(mutationState()).toEqual([variables])
60
+ })
61
+
62
+ it('should update injectMutationState when reactive options change', () => {
63
+ const mutationKey1 = queryKey()
64
+ const mutationKey2 = queryKey()
65
+ const variables1 = 'foo123'
66
+ const variables2 = 'bar234'
67
+
68
+ const [mutation1, mutation2] = TestBed.runInInjectionContext(() => {
69
+ return [
70
+ injectMutation(() => ({
71
+ mutationKey: mutationKey1,
72
+ mutationFn: (params: string) => sleep(0).then(() => params),
73
+ })),
74
+ injectMutation(() => ({
75
+ mutationKey: mutationKey2,
76
+ mutationFn: (params: string) => sleep(0).then(() => params),
77
+ })),
78
+ ]
79
+ })
80
+
81
+ mutation1.mutate(variables1)
82
+ mutation2.mutate(variables2)
83
+
84
+ const filterKey = signal(mutationKey1)
85
+
86
+ const mutationState = TestBed.runInInjectionContext(() => {
87
+ return injectMutationState(() => ({
88
+ filters: { mutationKey: filterKey(), status: 'pending' },
89
+ select: (m) => m.state.variables,
90
+ }))
91
+ })
92
+
93
+ expect(mutationState()).toEqual([variables1])
94
+
95
+ filterKey.set(mutationKey2)
96
+ expect(mutationState()).toEqual([variables2])
97
+ })
98
+
99
+ it('should return variables after calling mutate 2', () => {
100
+ queryClient.clear()
101
+ const mutationKey = queryKey()
102
+ const variables = 'bar234'
103
+
104
+ const mutation = TestBed.runInInjectionContext(() => {
105
+ return injectMutation(() => ({
106
+ mutationKey: mutationKey,
107
+ mutationFn: (params: string) => sleep(0).then(() => params),
108
+ }))
109
+ })
110
+
111
+ mutation.mutate(variables)
112
+
113
+ const mutationState = TestBed.runInInjectionContext(() => {
114
+ return injectMutationState()
115
+ })
116
+
117
+ expect(mutationState()[0]?.variables).toEqual(variables)
118
+ })
119
+
120
+ it('should support required signal inputs', async () => {
121
+ queryClient.clear()
122
+ const fakeName = 'name1'
123
+ const mutationKey1 = ['fake', fakeName]
124
+
125
+ const mutations = TestBed.runInInjectionContext(() => {
126
+ return [
127
+ injectMutation(() => ({
128
+ mutationKey: mutationKey1,
129
+ mutationFn: () => sleep(10).then(() => 'myValue'),
130
+ })),
131
+ injectMutation(() => ({
132
+ mutationKey: mutationKey1,
133
+ mutationFn: () =>
134
+ sleep(10).then(() => Promise.reject(new Error('myValue2'))),
135
+ })),
136
+ ]
137
+ })
138
+
139
+ mutations.forEach((mutation) => mutation.mutate())
140
+
141
+ @Component({
142
+ selector: 'app-fake',
143
+ template: `
144
+ @for (mutation of mutationState(); track $index) {
145
+ <span>{{ mutation.status }}</span>
146
+ }
147
+ `,
148
+ })
149
+ class FakeComponent {
150
+ name = input.required<string>()
151
+
152
+ mutationState = injectMutationState(() => ({
153
+ filters: {
154
+ mutationKey: ['fake', this.name()],
155
+ exact: true,
156
+ },
157
+ }))
158
+ }
159
+
160
+ const fixture = TestBed.createComponent(FakeComponent)
161
+ const { debugElement } = fixture
162
+ setFixtureSignalInputs(fixture, { name: fakeName })
163
+ await vi.advanceTimersByTimeAsync(0)
164
+
165
+ let spans = debugElement
166
+ .queryAll(By.css('span'))
167
+ .map((span) => span.nativeNode.textContent)
168
+
169
+ expect(spans).toEqual(['pending', 'pending'])
170
+
171
+ await vi.advanceTimersByTimeAsync(11)
172
+ fixture.detectChanges()
173
+
174
+ spans = debugElement
175
+ .queryAll(By.css('span'))
176
+ .map((span) => span.nativeNode.textContent)
177
+
178
+ expect(spans).toEqual(['success', 'error'])
179
+ })
180
+
181
+ describe('injection context', () => {
182
+ it('should throw NG0203 with descriptive error outside injection context', () => {
183
+ expect(() => {
184
+ injectMutationState()
185
+ }).toThrow(/NG0203(.*?)injectMutationState/)
186
+ })
187
+
188
+ it('should be usable outside injection context when passing an injector', () => {
189
+ const injector = TestBed.inject(Injector)
190
+ expect(
191
+ injectMutationState(undefined, {
192
+ injector,
193
+ }),
194
+ ).not.toThrow()
195
+ })
196
+ })
197
+ })
198
+ })
@@ -0,0 +1,158 @@
1
+ import { describe, expectTypeOf, it } from 'vitest'
2
+ import { sleep } from '@tanstack/query-test-utils'
3
+ import { injectMutation } from '..'
4
+ import type { Signal } from '@angular/core'
5
+ import type {
6
+ MutationFunctionContext,
7
+ MutationKey,
8
+ QueryClient,
9
+ } from '@tanstack/query-core'
10
+
11
+ describe('injectMutation', () => {
12
+ describe('Discriminated union return type', () => {
13
+ it('data should be possibly undefined 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('data should be defined 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('error should be null 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('data should be undefined 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('error should be defined 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
+ })