@tanstack/angular-query-experimental 5.15.5 → 5.16.1
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/build/esm2022/inject-is-fetching.mjs +2 -2
- package/build/esm2022/inject-is-mutating.mjs +2 -2
- package/build/esm2022/inject-mutation.mjs +7 -7
- package/build/esm2022/inject-queries.mjs +2 -2
- package/build/esm2022/inject-query-client.mjs +2 -2
- package/build/esm2022/inject-query.mjs +2 -2
- package/build/esm2022/signal-proxy.mjs +1 -1
- package/build/esm2022/types.mjs +1 -1
- package/build/esm2022/util/assert-injector/assert-injector.mjs +19 -0
- package/build/esm2022/util/create-injection-token/create-injection-token.mjs +60 -0
- package/build/fesm2022/tanstack-angular-query-experimental.mjs +119 -46
- package/build/fesm2022/tanstack-angular-query-experimental.mjs.map +1 -1
- package/build/signal-proxy.d.ts +1 -2
- package/build/types.d.ts +4 -7
- package/build/util/assert-injector/assert-injector.d.ts +54 -0
- package/build/util/create-injection-token/create-injection-token.d.ts +52 -0
- package/package.json +1 -2
- package/src/__tests__/inject-is-mutating.test.ts +1 -5
- package/src/__tests__/inject-mutation.test.ts +276 -24
- package/src/__tests__/inject-query.test.ts +3 -3
- package/src/__tests__/test-utils.ts +36 -0
- package/src/inject-is-fetching.ts +1 -1
- package/src/inject-is-mutating.ts +1 -1
- package/src/inject-mutation.ts +7 -8
- package/src/inject-queries.ts +1 -1
- package/src/inject-query-client.ts +1 -1
- package/src/inject-query.ts +1 -1
- package/src/signal-proxy.ts +1 -1
- package/src/test-setup.ts +2 -3
- package/src/types.ts +4 -9
- package/src/util/assert-injector/assert-injector.test.ts +72 -0
- package/src/util/assert-injector/assert-injector.ts +79 -0
- package/src/util/create-injection-token/create-injection-token.test.ts +30 -0
- package/src/util/create-injection-token/create-injection-token.ts +184 -0
|
@@ -1,52 +1,304 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { signal } from '@angular/core'
|
|
2
2
|
import { QueryClient } from '@tanstack/query-core'
|
|
3
|
-
import {
|
|
4
|
-
import { expect } from 'vitest'
|
|
3
|
+
import { TestBed } from '@angular/core/testing'
|
|
4
|
+
import { expect, test, vi } from 'vitest'
|
|
5
5
|
import { injectMutation } from '../inject-mutation'
|
|
6
6
|
import { provideAngularQuery } from '../providers'
|
|
7
|
-
import {
|
|
7
|
+
import { errorMutator, expectSignals, successMutator } from './test-utils'
|
|
8
|
+
|
|
9
|
+
const MUTATION_DURATION = 1000
|
|
10
|
+
|
|
11
|
+
const resolveMutations = () => vi.advanceTimersByTimeAsync(MUTATION_DURATION)
|
|
8
12
|
|
|
9
13
|
describe('injectMutation', () => {
|
|
10
14
|
let queryClient: QueryClient
|
|
11
15
|
|
|
12
16
|
beforeEach(() => {
|
|
17
|
+
queryClient = new QueryClient()
|
|
18
|
+
vi.useFakeTimers()
|
|
13
19
|
TestBed.configureTestingModule({
|
|
14
|
-
providers: [provideAngularQuery(
|
|
20
|
+
providers: [provideAngularQuery(queryClient)],
|
|
15
21
|
})
|
|
22
|
+
})
|
|
16
23
|
|
|
17
|
-
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
vi.useRealTimers()
|
|
18
26
|
})
|
|
19
27
|
|
|
20
|
-
|
|
21
|
-
|
|
28
|
+
describe('callback helpers', () => {
|
|
29
|
+
test('can access client from options callback', async () => {
|
|
30
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
31
|
+
return injectMutation((client) => ({
|
|
32
|
+
mutationFn: () => {
|
|
33
|
+
expect(client).toBe(queryClient)
|
|
34
|
+
return Promise.resolve()
|
|
35
|
+
},
|
|
36
|
+
}))
|
|
37
|
+
})
|
|
22
38
|
|
|
39
|
+
mutation.mutate()
|
|
40
|
+
expect(mutation.status()).toBe('pending')
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('should be in idle state initially', () => {
|
|
23
45
|
const mutation = TestBed.runInInjectionContext(() => {
|
|
24
46
|
return injectMutation(() => ({
|
|
25
|
-
mutationFn,
|
|
47
|
+
mutationFn: (params) => successMutator(params),
|
|
26
48
|
}))
|
|
27
49
|
})
|
|
28
50
|
|
|
29
|
-
mutation
|
|
30
|
-
|
|
51
|
+
expectSignals(mutation, {
|
|
52
|
+
isIdle: true,
|
|
53
|
+
isPending: false,
|
|
54
|
+
isError: false,
|
|
55
|
+
isSuccess: false,
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('should change state after invoking mutate', () => {
|
|
60
|
+
const result = 'Mock data'
|
|
61
|
+
|
|
62
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
63
|
+
return injectMutation(() => ({
|
|
64
|
+
mutationFn: (params: string) => successMutator(params),
|
|
65
|
+
}))
|
|
31
66
|
})
|
|
32
67
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
68
|
+
mutation.mutate(result)
|
|
69
|
+
|
|
70
|
+
expectSignals(mutation, {
|
|
71
|
+
isIdle: false,
|
|
72
|
+
isPending: true,
|
|
73
|
+
isError: false,
|
|
74
|
+
isSuccess: false,
|
|
75
|
+
data: undefined,
|
|
76
|
+
error: null,
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('should return error when request fails', async () => {
|
|
81
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
82
|
+
return injectMutation(() => ({
|
|
83
|
+
mutationFn: errorMutator,
|
|
84
|
+
}))
|
|
85
|
+
})
|
|
86
|
+
mutation.mutate({})
|
|
87
|
+
|
|
88
|
+
await resolveMutations()
|
|
89
|
+
|
|
90
|
+
expectSignals(mutation, {
|
|
91
|
+
isIdle: false,
|
|
92
|
+
isPending: false,
|
|
93
|
+
isError: true,
|
|
94
|
+
isSuccess: false,
|
|
95
|
+
data: undefined,
|
|
96
|
+
error: Error('Some error'),
|
|
97
|
+
})
|
|
98
|
+
})
|
|
36
99
|
|
|
37
|
-
|
|
100
|
+
test('should return data when request succeeds', async () => {
|
|
101
|
+
const result = 'Mock data'
|
|
38
102
|
const mutation = TestBed.runInInjectionContext(() => {
|
|
39
|
-
return injectMutation((
|
|
40
|
-
mutationFn: () =>
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
103
|
+
return injectMutation(() => ({
|
|
104
|
+
mutationFn: (params: string) => successMutator(params),
|
|
105
|
+
}))
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
mutation.mutate(result)
|
|
109
|
+
|
|
110
|
+
await resolveMutations()
|
|
111
|
+
|
|
112
|
+
expectSignals(mutation, {
|
|
113
|
+
isIdle: false,
|
|
114
|
+
isPending: false,
|
|
115
|
+
isError: false,
|
|
116
|
+
isSuccess: true,
|
|
117
|
+
data: result,
|
|
118
|
+
error: null,
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('reactive options should update mutation', async () => {
|
|
123
|
+
const mutationCache = queryClient.getMutationCache()
|
|
124
|
+
const mutationKey = signal(['foo'])
|
|
125
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
126
|
+
return injectMutation(() => ({
|
|
127
|
+
mutationKey: mutationKey(),
|
|
128
|
+
mutationFn: (params: string) => successMutator(params),
|
|
129
|
+
}))
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
mutationKey.set(['bar'])
|
|
133
|
+
|
|
134
|
+
TestBed.flushEffects()
|
|
135
|
+
|
|
136
|
+
await resolveMutations()
|
|
137
|
+
|
|
138
|
+
mutation.mutate('xyz')
|
|
139
|
+
|
|
140
|
+
await resolveMutations()
|
|
141
|
+
|
|
142
|
+
const mutations = mutationCache.find({ mutationKey: ['bar'] })
|
|
143
|
+
|
|
144
|
+
expect(mutations?.options.mutationKey).toEqual(['bar'])
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('should reset state after invoking mutation.reset', async () => {
|
|
148
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
149
|
+
return injectMutation(() => ({
|
|
150
|
+
mutationFn: (params: string) => errorMutator(params),
|
|
44
151
|
}))
|
|
45
152
|
})
|
|
46
153
|
|
|
47
|
-
mutation
|
|
154
|
+
mutation.mutate('')
|
|
48
155
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
156
|
+
await resolveMutations()
|
|
157
|
+
|
|
158
|
+
expect(mutation.isError()).toBe(true)
|
|
159
|
+
|
|
160
|
+
mutation.reset()
|
|
161
|
+
|
|
162
|
+
expectSignals(mutation, {
|
|
163
|
+
isIdle: true,
|
|
164
|
+
isPending: false,
|
|
165
|
+
isError: false,
|
|
166
|
+
isSuccess: false,
|
|
167
|
+
data: undefined,
|
|
168
|
+
error: null,
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
describe('side effects', () => {
|
|
173
|
+
beforeEach(() => {
|
|
174
|
+
vi.clearAllMocks()
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
test('should call onMutate when passed as an option', async () => {
|
|
178
|
+
const onMutate = vi.fn()
|
|
179
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
180
|
+
return injectMutation(() => ({
|
|
181
|
+
mutationFn: (params: string) => successMutator(params),
|
|
182
|
+
onMutate,
|
|
183
|
+
}))
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
mutation.mutate('')
|
|
187
|
+
|
|
188
|
+
await resolveMutations()
|
|
189
|
+
|
|
190
|
+
expect(onMutate).toHaveBeenCalledTimes(1)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
test('should call onError when passed as an option', async () => {
|
|
194
|
+
const onError = vi.fn()
|
|
195
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
196
|
+
return injectMutation(() => ({
|
|
197
|
+
mutationFn: (params: string) => errorMutator(params),
|
|
198
|
+
onError,
|
|
199
|
+
}))
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
mutation.mutate('')
|
|
203
|
+
|
|
204
|
+
await resolveMutations()
|
|
205
|
+
|
|
206
|
+
expect(onError).toHaveBeenCalledTimes(1)
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
test('should call onSuccess when passed as an option', async () => {
|
|
210
|
+
const onSuccess = vi.fn()
|
|
211
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
212
|
+
return injectMutation(() => ({
|
|
213
|
+
mutationFn: (params: string) => successMutator(params),
|
|
214
|
+
onSuccess,
|
|
215
|
+
}))
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
mutation.mutate('')
|
|
219
|
+
|
|
220
|
+
await resolveMutations()
|
|
221
|
+
|
|
222
|
+
expect(onSuccess).toHaveBeenCalledTimes(1)
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
test('should call onSettled when passed as an option', async () => {
|
|
226
|
+
const onSettled = vi.fn()
|
|
227
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
228
|
+
return injectMutation(() => ({
|
|
229
|
+
mutationFn: (params: string) => successMutator(params),
|
|
230
|
+
onSettled,
|
|
231
|
+
}))
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
mutation.mutate('')
|
|
235
|
+
|
|
236
|
+
await resolveMutations()
|
|
237
|
+
|
|
238
|
+
expect(onSettled).toHaveBeenCalledTimes(1)
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
test('should call onError when passed as an argument of mutate function', async () => {
|
|
242
|
+
const onError = vi.fn()
|
|
243
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
244
|
+
return injectMutation(() => ({
|
|
245
|
+
mutationFn: (params: string) => errorMutator(params),
|
|
246
|
+
}))
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
mutation.mutate('', { onError })
|
|
250
|
+
|
|
251
|
+
await resolveMutations()
|
|
252
|
+
|
|
253
|
+
expect(onError).toHaveBeenCalledTimes(1)
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
test('should call onSuccess when passed as an argument of mutate function', async () => {
|
|
257
|
+
const onSuccess = vi.fn()
|
|
258
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
259
|
+
return injectMutation(() => ({
|
|
260
|
+
mutationFn: (params: string) => successMutator(params),
|
|
261
|
+
}))
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
mutation.mutate('', { onSuccess })
|
|
265
|
+
|
|
266
|
+
await resolveMutations()
|
|
267
|
+
|
|
268
|
+
expect(onSuccess).toHaveBeenCalledTimes(1)
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
test('should call onSettled when passed as an argument of mutate function', async () => {
|
|
272
|
+
const onSettled = vi.fn()
|
|
273
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
274
|
+
return injectMutation(() => ({
|
|
275
|
+
mutationFn: (params: string) => successMutator(params),
|
|
276
|
+
}))
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
mutation.mutate('', { onSettled })
|
|
280
|
+
|
|
281
|
+
await resolveMutations()
|
|
282
|
+
|
|
283
|
+
expect(onSettled).toHaveBeenCalledTimes(1)
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
test('should fire both onSettled functions', async () => {
|
|
287
|
+
const onSettled = vi.fn()
|
|
288
|
+
const onSettledOnFunction = vi.fn()
|
|
289
|
+
const mutation = TestBed.runInInjectionContext(() => {
|
|
290
|
+
return injectMutation(() => ({
|
|
291
|
+
mutationFn: (params: string) => successMutator(params),
|
|
292
|
+
onSettled,
|
|
293
|
+
}))
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
mutation.mutate('', { onSettled: onSettledOnFunction })
|
|
297
|
+
|
|
298
|
+
await resolveMutations()
|
|
299
|
+
|
|
300
|
+
expect(onSettled).toHaveBeenCalledTimes(1)
|
|
301
|
+
expect(onSettledOnFunction).toHaveBeenCalledTimes(1)
|
|
302
|
+
})
|
|
303
|
+
})
|
|
52
304
|
})
|
|
@@ -35,7 +35,7 @@ describe('injectQuery', () => {
|
|
|
35
35
|
flush()
|
|
36
36
|
}))
|
|
37
37
|
|
|
38
|
-
it('should resolve to success and update signal:
|
|
38
|
+
it('should resolve to success and update signal: injectQuery()', fakeAsync(() => {
|
|
39
39
|
const query = TestBed.runInInjectionContext(() => {
|
|
40
40
|
return injectQuery(() => ({
|
|
41
41
|
queryKey: ['key2'],
|
|
@@ -97,7 +97,7 @@ describe('injectQuery', () => {
|
|
|
97
97
|
flush()
|
|
98
98
|
}))
|
|
99
99
|
|
|
100
|
-
it('should only run query once enabled is set to true', fakeAsync(() => {
|
|
100
|
+
it('should only run query once enabled signal is set to true', fakeAsync(() => {
|
|
101
101
|
const spy = vi.fn(simpleFetcher)
|
|
102
102
|
const enabled = signal(false)
|
|
103
103
|
|
|
@@ -160,7 +160,7 @@ describe('injectQuery', () => {
|
|
|
160
160
|
}))
|
|
161
161
|
|
|
162
162
|
it('should use the current value for the queryKey when refetch is called', fakeAsync(() => {
|
|
163
|
-
const fetchFn = vi.fn()
|
|
163
|
+
const fetchFn = vi.fn(simpleFetcher)
|
|
164
164
|
const keySignal = signal('key11')
|
|
165
165
|
|
|
166
166
|
const query = TestBed.runInInjectionContext(() => {
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { untracked } from '@angular/core'
|
|
2
|
+
import { isReactive } from '@angular/core/primitives/signals'
|
|
3
|
+
|
|
1
4
|
export function simpleFetcher(): Promise<string> {
|
|
2
5
|
return new Promise((resolve) => {
|
|
3
6
|
setTimeout(() => {
|
|
@@ -35,3 +38,36 @@ export function successMutator<T>(param: T): Promise<T> {
|
|
|
35
38
|
}, 0)
|
|
36
39
|
})
|
|
37
40
|
}
|
|
41
|
+
|
|
42
|
+
export function errorMutator(_parameter?: unknown): Promise<Error> {
|
|
43
|
+
return rejectFetcher()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Evaluate all signals on an object and return the result
|
|
47
|
+
function evaluateSignals<T extends Record<string, any>>(
|
|
48
|
+
obj: T,
|
|
49
|
+
): { [K in keyof T]: ReturnType<T[K]> } {
|
|
50
|
+
const result: Partial<{ [K in keyof T]: ReturnType<T[K]> }> = {}
|
|
51
|
+
|
|
52
|
+
untracked(() => {
|
|
53
|
+
for (const key in obj) {
|
|
54
|
+
if (
|
|
55
|
+
Object.prototype.hasOwnProperty.call(obj, key) &&
|
|
56
|
+
// Only evaluate signals, not normal functions
|
|
57
|
+
isReactive(obj[key])
|
|
58
|
+
) {
|
|
59
|
+
const func = obj[key]
|
|
60
|
+
result[key] = func()
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
return result as { [K in keyof T]: ReturnType<T[K]> }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const expectSignals = <T extends Record<string, any>>(
|
|
69
|
+
obj: T,
|
|
70
|
+
expected: Partial<{ [K in keyof T]: ReturnType<T[K]> }>,
|
|
71
|
+
): void => {
|
|
72
|
+
expect(evaluateSignals(obj)).toMatchObject(expected)
|
|
73
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DestroyRef, inject, signal } from '@angular/core'
|
|
2
2
|
import { type QueryFilters, notifyManager } from '@tanstack/query-core'
|
|
3
|
-
import { assertInjector } from '
|
|
3
|
+
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
4
4
|
import { injectQueryClient } from './inject-query-client'
|
|
5
5
|
import type { Injector, Signal } from '@angular/core'
|
|
6
6
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DestroyRef, inject, signal } from '@angular/core'
|
|
2
2
|
import { type MutationFilters, notifyManager } from '@tanstack/query-core'
|
|
3
|
-
import { assertInjector } from '
|
|
3
|
+
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
4
4
|
import { injectQueryClient } from './inject-query-client'
|
|
5
5
|
import type { Injector, Signal } from '@angular/core'
|
|
6
6
|
|
package/src/inject-mutation.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DestroyRef, computed, effect, inject, signal } from '@angular/core'
|
|
2
|
-
import { MutationObserver
|
|
3
|
-
import { assertInjector } from '
|
|
2
|
+
import { MutationObserver } from '@tanstack/query-core'
|
|
3
|
+
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
4
|
+
import { signalProxy } from './signal-proxy'
|
|
4
5
|
import { injectQueryClient } from './inject-query-client'
|
|
5
6
|
import type { DefaultError, QueryClient } from '@tanstack/query-core'
|
|
6
7
|
import type { Injector } from '@angular/core'
|
|
@@ -43,19 +44,17 @@ export function injectMutation<
|
|
|
43
44
|
|
|
44
45
|
const result = signal(observer.getCurrentResult())
|
|
45
46
|
|
|
46
|
-
const unsubscribe = observer.subscribe(
|
|
47
|
-
notifyManager.batchCalls((val) => {
|
|
48
|
-
result.set(val)
|
|
49
|
-
}),
|
|
50
|
-
)
|
|
47
|
+
const unsubscribe = observer.subscribe(result.set)
|
|
51
48
|
|
|
52
49
|
destroyRef.onDestroy(unsubscribe)
|
|
53
50
|
|
|
54
|
-
|
|
51
|
+
const resultSignal = computed(() => ({
|
|
55
52
|
...result(),
|
|
56
53
|
mutate,
|
|
57
54
|
mutateAsync: result().mutate,
|
|
58
55
|
}))
|
|
56
|
+
|
|
57
|
+
return signalProxy(resultSignal)
|
|
59
58
|
})
|
|
60
59
|
}
|
|
61
60
|
|
package/src/inject-queries.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { QueriesObserver, notifyManager } from '@tanstack/query-core'
|
|
2
2
|
import { DestroyRef, computed, effect, inject, signal } from '@angular/core'
|
|
3
|
-
import { assertInjector } from '
|
|
3
|
+
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
4
4
|
import { injectQueryClient } from './inject-query-client'
|
|
5
5
|
import type { Injector, Signal } from '@angular/core'
|
|
6
6
|
import type {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createNoopInjectionToken } from '
|
|
1
|
+
import { createNoopInjectionToken } from './util/create-injection-token/create-injection-token'
|
|
2
2
|
import type { QueryClient } from '@tanstack/query-core'
|
|
3
3
|
|
|
4
4
|
const [injectQueryClient, provideQueryClient, QUERY_CLIENT] =
|
package/src/inject-query.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { QueryObserver } from '@tanstack/query-core'
|
|
2
|
-
import { assertInjector } from '
|
|
2
|
+
import { assertInjector } from './util/assert-injector/assert-injector'
|
|
3
3
|
import { injectQueryClient } from './inject-query-client'
|
|
4
4
|
import { createBaseQuery } from './create-base-query'
|
|
5
5
|
import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
|
package/src/signal-proxy.ts
CHANGED
package/src/test-setup.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import '@analogjs/vite-plugin-angular/setup-vitest'
|
|
2
|
-
import '@angular/compiler'
|
|
3
2
|
|
|
4
|
-
import { TestBed } from '@angular/core/testing'
|
|
5
3
|
import {
|
|
6
4
|
BrowserDynamicTestingModule,
|
|
7
5
|
platformBrowserDynamicTesting,
|
|
8
6
|
} from '@angular/platform-browser-dynamic/testing'
|
|
7
|
+
import { getTestBed } from '@angular/core/testing'
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
getTestBed().initTestEnvironment(
|
|
11
10
|
BrowserDynamicTestingModule,
|
|
12
11
|
platformBrowserDynamicTesting(),
|
|
13
12
|
)
|
package/src/types.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
QueryObserverOptions,
|
|
13
13
|
QueryObserverResult,
|
|
14
14
|
} from '@tanstack/query-core'
|
|
15
|
+
import type { MapToSignals } from './signal-proxy'
|
|
15
16
|
|
|
16
17
|
/** Options for createBaseQuery */
|
|
17
18
|
export type CreateBaseQueryOptions<
|
|
@@ -27,9 +28,7 @@ export type CreateBaseQueryResult<
|
|
|
27
28
|
TData = unknown,
|
|
28
29
|
TError = DefaultError,
|
|
29
30
|
State = QueryObserverResult<TData, TError>,
|
|
30
|
-
> =
|
|
31
|
-
[K in keyof State]: State[K] extends Function ? State[K] : Signal<State[K]>
|
|
32
|
-
}
|
|
31
|
+
> = MapToSignals<State>
|
|
33
32
|
/** Result from createBaseQuery */
|
|
34
33
|
|
|
35
34
|
/** Options for createQuery */
|
|
@@ -74,11 +73,7 @@ export type DefinedCreateBaseQueryResult<
|
|
|
74
73
|
TData = unknown,
|
|
75
74
|
TError = DefaultError,
|
|
76
75
|
DefinedQueryObserver = DefinedQueryObserverResult<TData, TError>,
|
|
77
|
-
> =
|
|
78
|
-
[K in keyof DefinedQueryObserver]: DefinedQueryObserver[K] extends Function
|
|
79
|
-
? DefinedQueryObserver[K]
|
|
80
|
-
: Signal<DefinedQueryObserver[K]>
|
|
81
|
-
}
|
|
76
|
+
> = MapToSignals<DefinedQueryObserver>
|
|
82
77
|
|
|
83
78
|
/** Options for createQuery with initialData */
|
|
84
79
|
export type DefinedCreateQueryResult<
|
|
@@ -131,6 +126,6 @@ export type CreateMutationResult<
|
|
|
131
126
|
TError = DefaultError,
|
|
132
127
|
TVariables = unknown,
|
|
133
128
|
TContext = unknown,
|
|
134
|
-
> =
|
|
129
|
+
> = MapToSignals<CreateBaseMutationResult<TData, TError, TVariables, TContext>>
|
|
135
130
|
|
|
136
131
|
type Override<A, B> = { [K in keyof A]: K extends keyof B ? B[K] : A[K] }
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The code in this file is adapted from NG Extension Platform at https://ngxtension.netlify.app.
|
|
3
|
+
*
|
|
4
|
+
* Original Author: Chau Tran
|
|
5
|
+
*
|
|
6
|
+
* NG Extension Platform is an open-source project licensed under the MIT license.
|
|
7
|
+
*
|
|
8
|
+
* For more information about the original code, see
|
|
9
|
+
* https://github.com/nartc/ngxtension-platform
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
InjectionToken,
|
|
14
|
+
Injector,
|
|
15
|
+
inject,
|
|
16
|
+
runInInjectionContext,
|
|
17
|
+
} from '@angular/core'
|
|
18
|
+
import { TestBed } from '@angular/core/testing'
|
|
19
|
+
import { assertInjector } from './assert-injector'
|
|
20
|
+
|
|
21
|
+
describe(assertInjector.name, () => {
|
|
22
|
+
const token = new InjectionToken('token', {
|
|
23
|
+
factory: () => 1,
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
function injectDummy(injector?: Injector) {
|
|
27
|
+
injector = assertInjector(injectDummy, injector)
|
|
28
|
+
return runInInjectionContext(injector, () => inject(token))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function injectDummyTwo(injector?: Injector) {
|
|
32
|
+
return assertInjector(injectDummyTwo, injector, () => inject(token) + 1)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
it('given no custom injector, when run in injection context, then return value', () => {
|
|
36
|
+
TestBed.runInInjectionContext(() => {
|
|
37
|
+
const value = injectDummy()
|
|
38
|
+
const valueTwo = injectDummyTwo()
|
|
39
|
+
expect(value).toEqual(1)
|
|
40
|
+
expect(valueTwo).toEqual(2)
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('given no custom injector, when run outside injection context, then throw', () => {
|
|
45
|
+
expect(() => injectDummy()).toThrowError(
|
|
46
|
+
/injectDummy\(\) can only be used within an injection context/i,
|
|
47
|
+
)
|
|
48
|
+
expect(() => injectDummyTwo()).toThrowError(
|
|
49
|
+
/injectDummyTwo\(\) can only be used within an injection context/i,
|
|
50
|
+
)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('given a custom injector, when run in that injector context without providing number, then throw', () => {
|
|
54
|
+
expect(() => injectDummy(Injector.create({ providers: [] }))).toThrowError(
|
|
55
|
+
/No provider for InjectionToken/i,
|
|
56
|
+
)
|
|
57
|
+
expect(() =>
|
|
58
|
+
injectDummyTwo(Injector.create({ providers: [] })),
|
|
59
|
+
).toThrowError(/No provider for InjectionToken/i)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('given a custom injector, when run in that injector context and providing number, then return value', () => {
|
|
63
|
+
const value = injectDummy(
|
|
64
|
+
Injector.create({ providers: [{ provide: token, useValue: 2 }] }),
|
|
65
|
+
)
|
|
66
|
+
const valueTwo = injectDummyTwo(
|
|
67
|
+
Injector.create({ providers: [{ provide: token, useValue: 2 }] }),
|
|
68
|
+
)
|
|
69
|
+
expect(value).toEqual(2)
|
|
70
|
+
expect(valueTwo).toEqual(3)
|
|
71
|
+
})
|
|
72
|
+
})
|