@tanstack/angular-query-experimental 5.103.1 → 5.103.3
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.
- package/dist/create-base-query.d.mts +169 -0
- package/dist/create-base-query.d.mts.map +1 -0
- package/dist/create-base-query.mjs +91 -0
- package/dist/create-base-query.mjs.map +1 -0
- package/dist/devtools/index.d.mts +3 -0
- package/dist/devtools/index.mjs +2 -0
- package/dist/devtools/production/index.d.mts +4 -0
- package/dist/devtools/production/index.mjs +2 -0
- package/dist/devtools/stub.d.mts +6 -0
- package/dist/devtools/stub.d.mts.map +1 -0
- package/dist/devtools/stub.mjs +9 -0
- package/dist/devtools/stub.mjs.map +1 -0
- package/dist/devtools/types.d.mts +107 -0
- package/dist/devtools/types.d.mts.map +1 -0
- package/dist/devtools/types.mjs +1 -0
- package/{devtools/with-devtools.d.ts → dist/devtools/with-devtools.d.mts} +6 -2
- package/dist/devtools/with-devtools.d.mts.map +1 -0
- package/dist/devtools/with-devtools.mjs +110 -0
- package/dist/devtools/with-devtools.mjs.map +1 -0
- package/dist/devtools-panel/index.d.mts +3 -0
- package/dist/devtools-panel/index.mjs +2 -0
- package/{devtools-panel/inject-devtools-panel.d.ts → dist/devtools-panel/inject-devtools-panel.d.mts} +6 -2
- package/dist/devtools-panel/inject-devtools-panel.d.mts.map +1 -0
- package/dist/devtools-panel/inject-devtools-panel.mjs +67 -0
- package/dist/devtools-panel/inject-devtools-panel.mjs.map +1 -0
- package/dist/devtools-panel/production/index.d.mts +4 -0
- package/dist/devtools-panel/production/index.mjs +2 -0
- package/dist/devtools-panel/stub.d.mts +6 -0
- package/dist/devtools-panel/stub.d.mts.map +1 -0
- package/dist/devtools-panel/stub.mjs +7 -0
- package/dist/devtools-panel/stub.mjs.map +1 -0
- package/dist/devtools-panel/types.d.mts +52 -0
- package/dist/devtools-panel/types.d.mts.map +1 -0
- package/dist/devtools-panel/types.mjs +1 -0
- package/dist/index.d.mts +16 -0
- package/{index.mjs → dist/index.mjs} +8 -21
- package/dist/index.mjs.map +1 -0
- package/{infinite-query-options.d.ts → dist/infinite-query-options.d.mts} +35 -31
- package/dist/infinite-query-options.d.mts.map +1 -0
- package/dist/infinite-query-options.mjs +8 -0
- package/dist/infinite-query-options.mjs.map +1 -0
- package/{inject-infinite-query.d.ts → dist/inject-infinite-query.d.mts} +18 -14
- package/dist/inject-infinite-query.d.mts.map +1 -0
- package/dist/inject-infinite-query.mjs +13 -0
- package/dist/inject-infinite-query.mjs.map +1 -0
- package/{inject-is-fetching.d.ts → dist/inject-is-fetching.d.mts} +14 -10
- package/dist/inject-is-fetching.d.mts.map +1 -0
- package/dist/inject-is-fetching.mjs +69 -0
- package/dist/inject-is-fetching.mjs.map +1 -0
- package/{inject-is-mutating.d.ts → dist/inject-is-mutating.d.mts} +14 -10
- package/dist/inject-is-mutating.d.mts.map +1 -0
- package/dist/inject-is-mutating.mjs +52 -0
- package/dist/inject-is-mutating.mjs.map +1 -0
- package/{inject-is-restoring.d.ts → dist/inject-is-restoring.d.mts} +13 -10
- package/dist/inject-is-restoring.d.mts.map +1 -0
- package/dist/inject-is-restoring.mjs +35 -0
- package/dist/inject-is-restoring.mjs.map +1 -0
- package/{inject-mutation-state.d.ts → dist/inject-mutation-state.d.mts} +16 -13
- package/dist/inject-mutation-state.d.mts.map +1 -0
- package/dist/inject-mutation-state.mjs +103 -0
- package/dist/inject-mutation-state.mjs.map +1 -0
- package/{inject-mutation.d.ts → dist/inject-mutation.d.mts} +15 -11
- package/dist/inject-mutation.d.mts.map +1 -0
- package/dist/inject-mutation.mjs +226 -0
- package/dist/inject-mutation.mjs.map +1 -0
- package/dist/inject-queries-experimental/index.d.mts +2 -0
- package/dist/inject-queries-experimental/index.mjs +2 -0
- package/{inject-queries.d.ts → dist/inject-queries.d.mts} +36 -55
- package/dist/inject-queries.d.mts.map +1 -0
- package/dist/inject-queries.mjs +203 -0
- package/dist/inject-queries.mjs.map +1 -0
- package/{inject-query-client.d.ts → dist/inject-query-client.d.mts} +8 -4
- package/dist/inject-query-client.d.mts.map +1 -0
- package/dist/inject-query-client.mjs +23 -0
- package/dist/inject-query-client.mjs.map +1 -0
- package/{inject-query.d.ts → dist/inject-query.d.mts} +18 -14
- package/dist/inject-query.d.mts.map +1 -0
- package/dist/inject-query.mjs +12 -0
- package/dist/inject-query.mjs.map +1 -0
- package/{mutation-options.d.ts → dist/mutation-options.d.mts} +8 -4
- package/dist/mutation-options.d.mts.map +1 -0
- package/dist/mutation-options.mjs +8 -0
- package/dist/mutation-options.mjs.map +1 -0
- package/dist/pending-tasks-compat.d.mts +10 -0
- package/dist/pending-tasks-compat.d.mts.map +1 -0
- package/dist/pending-tasks-compat.mjs +13 -0
- package/dist/pending-tasks-compat.mjs.map +1 -0
- package/{providers.d.ts → dist/providers.d.mts} +16 -13
- package/dist/providers.d.mts.map +1 -0
- package/dist/providers.mjs +124 -0
- package/dist/providers.mjs.map +1 -0
- package/{query-options.d.ts → dist/query-options.d.mts} +41 -37
- package/dist/query-options.d.mts.map +1 -0
- package/dist/query-options.mjs +8 -0
- package/dist/query-options.mjs.map +1 -0
- package/dist/signal-proxy.d.mts +13 -0
- package/dist/signal-proxy.d.mts.map +1 -0
- package/dist/signal-proxy.mjs +35 -0
- package/dist/signal-proxy.mjs.map +1 -0
- package/{types.d.ts → dist/types.d.mts} +36 -37
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +6 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +48 -20
- package/src/__tests__/infinite-query-options.test-d.ts +314 -0
- package/src/__tests__/infinite-query-options.test.ts +19 -0
- package/src/__tests__/inject-devtools-panel.test.ts +196 -0
- package/src/__tests__/inject-infinite-query.test-d.ts +66 -0
- package/src/__tests__/inject-infinite-query.test.ts +227 -0
- package/src/__tests__/inject-is-fetching.test.ts +108 -0
- package/src/__tests__/inject-is-mutating.test.ts +113 -0
- package/src/__tests__/inject-is-restoring.test.ts +93 -0
- package/src/__tests__/inject-mutation-state.test-d.ts +22 -0
- package/src/__tests__/inject-mutation-state.test.ts +198 -0
- package/src/__tests__/inject-mutation.test-d.ts +158 -0
- package/src/__tests__/inject-mutation.test.ts +1161 -0
- package/src/__tests__/inject-queries.test-d.ts +196 -0
- package/src/__tests__/inject-queries.test.ts +240 -0
- package/src/__tests__/inject-query.test-d.ts +357 -0
- package/src/__tests__/inject-query.test.ts +700 -0
- package/src/__tests__/mutation-options.test-d.ts +234 -0
- package/src/__tests__/mutation-options.test.ts +344 -0
- package/src/__tests__/pending-tasks.test.ts +644 -0
- package/src/__tests__/provide-query-client.test.ts +38 -0
- package/src/__tests__/provide-tanstack-query.test.ts +38 -0
- package/src/__tests__/query-options.test-d.ts +262 -0
- package/src/__tests__/query-options.test.ts +14 -0
- package/src/__tests__/signal-proxy.test.ts +27 -0
- package/src/__tests__/test-utils.ts +77 -0
- package/src/__tests__/with-devtools.test.ts +674 -0
- package/src/create-base-query.ts +175 -0
- package/src/devtools/index.ts +8 -0
- package/src/devtools/production/index.ts +1 -0
- package/src/devtools/stub.ts +7 -0
- package/src/devtools/types.ts +116 -0
- package/src/devtools/with-devtools.ts +182 -0
- package/src/devtools-panel/index.ts +8 -0
- package/src/devtools-panel/inject-devtools-panel.ts +110 -0
- package/src/devtools-panel/production/index.ts +1 -0
- package/src/devtools-panel/stub.ts +7 -0
- package/src/devtools-panel/types.ts +57 -0
- package/src/index.ts +60 -0
- package/src/infinite-query-options.ts +354 -0
- package/src/inject-infinite-query.ts +296 -0
- package/src/inject-is-fetching.ts +97 -0
- package/src/inject-is-mutating.ts +80 -0
- package/src/inject-is-restoring.ts +53 -0
- package/src/inject-mutation-state.ts +167 -0
- package/src/inject-mutation.ts +328 -0
- package/src/inject-queries-experimental/index.ts +1 -0
- package/src/inject-queries.ts +479 -0
- package/src/inject-query-client.ts +22 -0
- package/src/inject-query.ts +208 -0
- package/src/mutation-options.ts +121 -0
- package/src/pending-tasks-compat.ts +27 -0
- package/src/providers.ts +170 -0
- package/src/query-options.ts +292 -0
- package/src/signal-proxy.ts +46 -0
- package/src/types.ts +429 -0
- package/create-base-query.d.ts +0 -164
- package/create-base-query.mjs +0 -99
- package/create-base-query.mjs.map +0 -1
- package/devtools/index.d.ts +0 -2
- package/devtools/index.mjs +0 -5
- package/devtools/index.mjs.map +0 -1
- package/devtools/production/index.d.ts +0 -1
- package/devtools/stub.d.ts +0 -2
- package/devtools/stub.mjs +0 -8
- package/devtools/stub.mjs.map +0 -1
- package/devtools/types.d.ts +0 -100
- package/devtools/with-devtools.mjs +0 -106
- package/devtools/with-devtools.mjs.map +0 -1
- package/devtools-panel/index.d.ts +0 -2
- package/devtools-panel/index.mjs +0 -5
- package/devtools-panel/index.mjs.map +0 -1
- package/devtools-panel/inject-devtools-panel.mjs +0 -72
- package/devtools-panel/inject-devtools-panel.mjs.map +0 -1
- package/devtools-panel/production/index.d.ts +0 -1
- package/devtools-panel/stub.d.ts +0 -2
- package/devtools-panel/stub.mjs +0 -8
- package/devtools-panel/stub.mjs.map +0 -1
- package/devtools-panel/types.d.ts +0 -48
- package/index.d.ts +0 -25
- package/index.mjs.map +0 -1
- package/infinite-query-options.mjs +0 -7
- package/infinite-query-options.mjs.map +0 -1
- package/inject-infinite-query.mjs +0 -18
- package/inject-infinite-query.mjs.map +0 -1
- package/inject-is-fetching.mjs +0 -31
- package/inject-is-fetching.mjs.map +0 -1
- package/inject-is-mutating.mjs +0 -31
- package/inject-is-mutating.mjs.map +0 -1
- package/inject-is-restoring.mjs +0 -21
- package/inject-is-restoring.mjs.map +0 -1
- package/inject-mutation-state.mjs +0 -51
- package/inject-mutation-state.mjs.map +0 -1
- package/inject-mutation.mjs +0 -94
- package/inject-mutation.mjs.map +0 -1
- package/inject-queries-experimental/index.d.ts +0 -1
- package/inject-queries-experimental/index.mjs +0 -5
- package/inject-queries-experimental/index.mjs.map +0 -1
- package/inject-queries.mjs +0 -82
- package/inject-queries.mjs.map +0 -1
- package/inject-query-client.mjs +0 -9
- package/inject-query-client.mjs.map +0 -1
- package/inject-query.mjs +0 -14
- package/inject-query.mjs.map +0 -1
- package/mutation-options.mjs +0 -7
- package/mutation-options.mjs.map +0 -1
- package/pending-tasks-compat.d.ts +0 -7
- package/pending-tasks-compat.mjs +0 -19
- package/pending-tasks-compat.mjs.map +0 -1
- package/providers.mjs +0 -32
- package/providers.mjs.map +0 -1
- package/query-options.mjs +0 -7
- package/query-options.mjs.map +0 -1
- package/signal-proxy.d.ts +0 -11
- package/signal-proxy.mjs +0 -29
- 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
|
+
})
|