@tanstack/angular-query-experimental 5.34.2 → 5.35.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/rollup.d.ts +591 -0
- package/package.json +10 -6
- package/build/create-base-query.d.ts +0 -6
- package/build/index.d.ts +0 -14
- package/build/infinite-query-options.d.ts +0 -34
- package/build/inject-infinite-query.d.ts +0 -31
- package/build/inject-is-fetching.d.ts +0 -13
- package/build/inject-is-mutating.d.ts +0 -12
- package/build/inject-mutation-state.d.ts +0 -21
- package/build/inject-mutation.d.ts +0 -13
- package/build/inject-queries.d.ts +0 -76
- package/build/inject-query-client.d.ts +0 -30
- package/build/inject-query.d.ts +0 -106
- package/build/providers.d.ts +0 -42
- package/build/query-options.d.ts +0 -66
- package/build/signal-proxy.d.ts +0 -11
- package/build/types.d.ts +0 -95
- package/build/util/assert-injector/assert-injector.d.ts +0 -54
- package/build/util/create-injection-token/create-injection-token.d.ts +0 -52
- package/build/util/index.d.ts +0 -2
- package/build/util/lazy-init/lazy-init.d.ts +0 -1
- package/build/util/lazy-signal-initializer/lazy-signal-initializer.d.ts +0 -4
- package/src/__tests__/inject-infinite-query.test.ts +0 -64
- package/src/__tests__/inject-is-fetching.test.ts +0 -35
- package/src/__tests__/inject-is-mutating.test.ts +0 -39
- package/src/__tests__/inject-mutation-state.test-d.ts +0 -22
- package/src/__tests__/inject-mutation-state.test.ts +0 -175
- package/src/__tests__/inject-mutation.test-d.ts +0 -71
- package/src/__tests__/inject-mutation.test.ts +0 -458
- package/src/__tests__/inject-query.test-d.ts +0 -59
- package/src/__tests__/inject-query.test.ts +0 -349
- package/src/__tests__/query-options.test-d.ts +0 -127
- package/src/__tests__/signal-proxy.test.ts +0 -27
- package/src/__tests__/test-utils.ts +0 -131
- package/src/__tests__/util/lazy-init/lazy-init.test.ts +0 -126
- package/src/__tests__/util/lazy-signal-initializer/lazy-signal-initializer.test.ts +0 -130
- package/src/create-base-query.ts +0 -116
- package/src/index.ts +0 -24
- package/src/infinite-query-options.ts +0 -118
- package/src/inject-infinite-query.ts +0 -125
- package/src/inject-is-fetching.ts +0 -49
- package/src/inject-is-mutating.ts +0 -48
- package/src/inject-mutation-state.ts +0 -107
- package/src/inject-mutation.ts +0 -118
- package/src/inject-queries.ts +0 -265
- package/src/inject-query-client.ts +0 -25
- package/src/inject-query.ts +0 -200
- package/src/providers.ts +0 -65
- package/src/query-options.ts +0 -122
- package/src/signal-proxy.ts +0 -46
- package/src/test-setup.ts +0 -12
- package/src/types.ts +0 -311
- package/src/util/assert-injector/assert-injector.test.ts +0 -74
- package/src/util/assert-injector/assert-injector.ts +0 -81
- package/src/util/create-injection-token/create-injection-token.test.ts +0 -32
- package/src/util/create-injection-token/create-injection-token.ts +0 -185
- package/src/util/index.ts +0 -13
- package/src/util/lazy-init/lazy-init.ts +0 -34
- package/src/util/lazy-signal-initializer/lazy-signal-initializer.ts +0 -28
|
@@ -1,52 +0,0 @@
|
|
|
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
|
-
import { type EnvironmentProviders, type Host, type InjectOptions, InjectionToken, type Injector, type Optional, type Provider, type Self, type SkipSelf, type Type } from '@angular/core';
|
|
12
|
-
type CreateInjectionTokenDep<TTokenType> = Type<TTokenType> | (abstract new (...args: Array<any>) => TTokenType) | InjectionToken<TTokenType>;
|
|
13
|
-
type CreateInjectionTokenDeps<TFactory extends (...args: Array<any>) => any, TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>> = {
|
|
14
|
-
[Index in keyof TFactoryDeps]: CreateInjectionTokenDep<TFactoryDeps[Index]> | [
|
|
15
|
-
...modifiers: Array<Optional | Self | SkipSelf | Host>,
|
|
16
|
-
token: CreateInjectionTokenDep<TFactoryDeps[Index]>
|
|
17
|
-
];
|
|
18
|
-
} & {
|
|
19
|
-
length: TFactoryDeps['length'];
|
|
20
|
-
};
|
|
21
|
-
type CreateInjectionTokenOptions<TFactory extends (...args: Array<any>) => any, TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>> = (TFactoryDeps[0] extends undefined ? {
|
|
22
|
-
deps?: never;
|
|
23
|
-
} : {
|
|
24
|
-
deps: CreateInjectionTokenDeps<TFactory, TFactoryDeps>;
|
|
25
|
-
}) & {
|
|
26
|
-
isRoot?: boolean;
|
|
27
|
-
multi?: boolean;
|
|
28
|
-
token?: InjectionToken<ReturnType<TFactory>>;
|
|
29
|
-
extraProviders?: Provider | EnvironmentProviders;
|
|
30
|
-
};
|
|
31
|
-
type InjectFn<TFactoryReturn> = {
|
|
32
|
-
(): TFactoryReturn;
|
|
33
|
-
(injectOptions: InjectOptions & {
|
|
34
|
-
optional?: false;
|
|
35
|
-
} & {
|
|
36
|
-
injector?: Injector;
|
|
37
|
-
}): TFactoryReturn;
|
|
38
|
-
(injectOptions: InjectOptions & {
|
|
39
|
-
injector?: Injector;
|
|
40
|
-
}): TFactoryReturn | null;
|
|
41
|
-
};
|
|
42
|
-
type ProvideFn<TNoop extends boolean, TFactoryReturn, TReturn = TFactoryReturn extends Array<infer Item> ? Item : TFactoryReturn> = (TNoop extends true ? (value: TReturn | (() => TReturn)) => Provider : () => Provider) & (TReturn extends Function ? (value: TReturn | (() => TReturn), isFunctionValue: boolean) => Provider : (value: TReturn | (() => TReturn)) => Provider);
|
|
43
|
-
type CreateInjectionTokenReturn<TFactoryReturn, TNoop extends boolean = false> = [
|
|
44
|
-
InjectFn<TFactoryReturn>,
|
|
45
|
-
ProvideFn<TNoop, TFactoryReturn>,
|
|
46
|
-
InjectionToken<TFactoryReturn>,
|
|
47
|
-
() => Provider
|
|
48
|
-
];
|
|
49
|
-
export declare function createNoopInjectionToken<TValue, TMulti extends boolean = false, TOptions = Pick<CreateInjectionTokenOptions<() => void, []>, 'extraProviders'> & (TMulti extends true ? {
|
|
50
|
-
multi: true;
|
|
51
|
-
} : Record<string, never>)>(description: string, options?: TOptions): CreateInjectionTokenReturn<TMulti extends true ? TValue[] : TValue, true>;
|
|
52
|
-
export {};
|
package/build/util/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function lazyInit<T extends object>(initializer: () => T): T;
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { TestBed } from '@angular/core/testing'
|
|
2
|
-
import { QueryClient } from '@tanstack/query-core'
|
|
3
|
-
import { afterEach } from 'vitest'
|
|
4
|
-
import { injectInfiniteQuery } from '../inject-infinite-query'
|
|
5
|
-
import { provideAngularQuery } from '../providers'
|
|
6
|
-
import { expectSignals, infiniteFetcher } from './test-utils'
|
|
7
|
-
|
|
8
|
-
const QUERY_DURATION = 1000
|
|
9
|
-
|
|
10
|
-
const resolveQueries = () => vi.advanceTimersByTimeAsync(QUERY_DURATION)
|
|
11
|
-
|
|
12
|
-
describe('injectInfiniteQuery', () => {
|
|
13
|
-
let queryClient: QueryClient
|
|
14
|
-
|
|
15
|
-
beforeEach(() => {
|
|
16
|
-
queryClient = new QueryClient()
|
|
17
|
-
vi.useFakeTimers()
|
|
18
|
-
TestBed.configureTestingModule({
|
|
19
|
-
providers: [provideAngularQuery(queryClient)],
|
|
20
|
-
})
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
afterEach(() => {
|
|
24
|
-
vi.useRealTimers()
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
test('should properly execute infinite query', async () => {
|
|
28
|
-
const query = TestBed.runInInjectionContext(() => {
|
|
29
|
-
return injectInfiniteQuery(() => ({
|
|
30
|
-
queryKey: ['infiniteQuery'],
|
|
31
|
-
queryFn: infiniteFetcher,
|
|
32
|
-
initialPageParam: 0,
|
|
33
|
-
getNextPageParam: () => 12,
|
|
34
|
-
}))
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
expectSignals(query, {
|
|
38
|
-
data: undefined,
|
|
39
|
-
status: 'pending',
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
await resolveQueries()
|
|
43
|
-
|
|
44
|
-
expectSignals(query, {
|
|
45
|
-
data: {
|
|
46
|
-
pageParams: [0],
|
|
47
|
-
pages: ['data on page 0'],
|
|
48
|
-
},
|
|
49
|
-
status: 'success',
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
void query.fetchNextPage()
|
|
53
|
-
|
|
54
|
-
await resolveQueries()
|
|
55
|
-
|
|
56
|
-
expectSignals(query, {
|
|
57
|
-
data: {
|
|
58
|
-
pageParams: [0, 12],
|
|
59
|
-
pages: ['data on page 0', 'data on page 12'],
|
|
60
|
-
},
|
|
61
|
-
status: 'success',
|
|
62
|
-
})
|
|
63
|
-
})
|
|
64
|
-
})
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { TestBed, fakeAsync, flush, tick } from '@angular/core/testing'
|
|
2
|
-
import { QueryClient } from '@tanstack/query-core'
|
|
3
|
-
import { beforeEach, describe, expect } from 'vitest'
|
|
4
|
-
import { injectIsFetching } from '../inject-is-fetching'
|
|
5
|
-
import { injectQuery } from '../inject-query'
|
|
6
|
-
import { provideAngularQuery } from '../providers'
|
|
7
|
-
import { delayedFetcher } from './test-utils'
|
|
8
|
-
|
|
9
|
-
describe('injectIsFetching', () => {
|
|
10
|
-
let queryClient: QueryClient
|
|
11
|
-
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
queryClient = new QueryClient()
|
|
14
|
-
|
|
15
|
-
TestBed.configureTestingModule({
|
|
16
|
-
providers: [provideAngularQuery(queryClient)],
|
|
17
|
-
})
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test('Returns number of fetching queries', fakeAsync(() => {
|
|
21
|
-
const isFetching = TestBed.runInInjectionContext(() => {
|
|
22
|
-
injectQuery(() => ({
|
|
23
|
-
queryKey: ['isFetching1'],
|
|
24
|
-
queryFn: delayedFetcher(100),
|
|
25
|
-
}))
|
|
26
|
-
return injectIsFetching()
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
tick()
|
|
30
|
-
|
|
31
|
-
expect(isFetching()).toStrictEqual(1)
|
|
32
|
-
flush()
|
|
33
|
-
expect(isFetching()).toStrictEqual(0)
|
|
34
|
-
}))
|
|
35
|
-
})
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { QueryClient } from '@tanstack/query-core'
|
|
2
|
-
import { beforeEach, describe } from 'vitest'
|
|
3
|
-
import { TestBed, fakeAsync, tick } from '@angular/core/testing'
|
|
4
|
-
import { injectIsMutating } from '../inject-is-mutating'
|
|
5
|
-
import { injectMutation } from '../inject-mutation'
|
|
6
|
-
import { provideAngularQuery } from '../providers'
|
|
7
|
-
import { successMutator } from './test-utils'
|
|
8
|
-
|
|
9
|
-
describe('injectIsMutating', () => {
|
|
10
|
-
let queryClient: QueryClient
|
|
11
|
-
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
queryClient = new QueryClient()
|
|
14
|
-
|
|
15
|
-
TestBed.configureTestingModule({
|
|
16
|
-
providers: [provideAngularQuery(queryClient)],
|
|
17
|
-
})
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test('should properly return isMutating state', fakeAsync(() => {
|
|
21
|
-
TestBed.runInInjectionContext(() => {
|
|
22
|
-
const isMutating = injectIsMutating()
|
|
23
|
-
const mutation = injectMutation(() => ({
|
|
24
|
-
mutationKey: ['isMutating1'],
|
|
25
|
-
mutationFn: successMutator<{ par1: string }>,
|
|
26
|
-
}))
|
|
27
|
-
|
|
28
|
-
expect(isMutating()).toBe(0)
|
|
29
|
-
|
|
30
|
-
mutation.mutate({
|
|
31
|
-
par1: 'par1',
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
tick()
|
|
35
|
-
|
|
36
|
-
expect(isMutating()).toBe(1)
|
|
37
|
-
})
|
|
38
|
-
}))
|
|
39
|
-
})
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { describe, expectTypeOf } from 'vitest'
|
|
2
|
-
import { injectMutationState } from '../inject-mutation-state'
|
|
3
|
-
import type { MutationState, MutationStatus } from '@tanstack/query-core'
|
|
4
|
-
|
|
5
|
-
describe('injectMutationState', () => {
|
|
6
|
-
it('should default to QueryState', () => {
|
|
7
|
-
const result = injectMutationState(() => ({
|
|
8
|
-
filters: { status: 'pending' },
|
|
9
|
-
}))
|
|
10
|
-
|
|
11
|
-
expectTypeOf(result()).toEqualTypeOf<Array<MutationState>>()
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
it('should infer with select', () => {
|
|
15
|
-
const result = injectMutationState(() => ({
|
|
16
|
-
filters: { status: 'pending' },
|
|
17
|
-
select: (mutation) => mutation.state.status,
|
|
18
|
-
}))
|
|
19
|
-
|
|
20
|
-
expectTypeOf(result()).toEqualTypeOf<Array<MutationStatus>>()
|
|
21
|
-
})
|
|
22
|
-
})
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { Component, input, signal } from '@angular/core'
|
|
2
|
-
import { QueryClient } from '@tanstack/query-core'
|
|
3
|
-
import { TestBed } from '@angular/core/testing'
|
|
4
|
-
import { describe, expect, test, vi } from 'vitest'
|
|
5
|
-
import { By } from '@angular/platform-browser'
|
|
6
|
-
import { injectMutation } from '../inject-mutation'
|
|
7
|
-
import { injectMutationState } from '../inject-mutation-state'
|
|
8
|
-
import { provideAngularQuery } from '../providers'
|
|
9
|
-
import { setFixtureSignalInputs, successMutator } from './test-utils'
|
|
10
|
-
|
|
11
|
-
const MUTATION_DURATION = 1000
|
|
12
|
-
|
|
13
|
-
const resolveMutations = () => vi.advanceTimersByTimeAsync(MUTATION_DURATION)
|
|
14
|
-
|
|
15
|
-
describe('injectMutationState', () => {
|
|
16
|
-
let queryClient: QueryClient
|
|
17
|
-
|
|
18
|
-
beforeEach(() => {
|
|
19
|
-
queryClient = new QueryClient()
|
|
20
|
-
vi.useFakeTimers()
|
|
21
|
-
TestBed.configureTestingModule({
|
|
22
|
-
providers: [provideAngularQuery(queryClient)],
|
|
23
|
-
})
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
afterEach(() => {
|
|
27
|
-
vi.useRealTimers()
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
describe('injectMutationState', () => {
|
|
31
|
-
test('should return variables after calling mutate', async () => {
|
|
32
|
-
const mutationKey = ['mutation']
|
|
33
|
-
const variables = 'foo123'
|
|
34
|
-
|
|
35
|
-
const mutation = TestBed.runInInjectionContext(() => {
|
|
36
|
-
return injectMutation(() => ({
|
|
37
|
-
mutationKey: mutationKey,
|
|
38
|
-
mutationFn: (params: string) => successMutator(params),
|
|
39
|
-
}))
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
mutation.mutate(variables)
|
|
43
|
-
|
|
44
|
-
const mutationState = TestBed.runInInjectionContext(() => {
|
|
45
|
-
return injectMutationState(() => ({
|
|
46
|
-
filters: { mutationKey, status: 'pending' },
|
|
47
|
-
select: (m) => m.state.variables,
|
|
48
|
-
}))
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
expect(mutationState()).toEqual([variables])
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
test('reactive options should update injectMutationState', async () => {
|
|
55
|
-
const mutationKey1 = ['mutation1']
|
|
56
|
-
const mutationKey2 = ['mutation2']
|
|
57
|
-
const variables1 = 'foo123'
|
|
58
|
-
const variables2 = 'bar234'
|
|
59
|
-
|
|
60
|
-
const [mutation1, mutation2] = TestBed.runInInjectionContext(() => {
|
|
61
|
-
return [
|
|
62
|
-
injectMutation(() => ({
|
|
63
|
-
mutationKey: mutationKey1,
|
|
64
|
-
mutationFn: (params: string) => successMutator(params),
|
|
65
|
-
})),
|
|
66
|
-
injectMutation(() => ({
|
|
67
|
-
mutationKey: mutationKey2,
|
|
68
|
-
mutationFn: (params: string) => successMutator(params),
|
|
69
|
-
})),
|
|
70
|
-
]
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
mutation1.mutate(variables1)
|
|
74
|
-
mutation2.mutate(variables2)
|
|
75
|
-
|
|
76
|
-
const filterKey = signal(mutationKey1)
|
|
77
|
-
|
|
78
|
-
const mutationState = TestBed.runInInjectionContext(() => {
|
|
79
|
-
return injectMutationState(() => ({
|
|
80
|
-
filters: { mutationKey: filterKey(), status: 'pending' },
|
|
81
|
-
select: (m) => m.state.variables,
|
|
82
|
-
}))
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
expect(mutationState()).toEqual([variables1])
|
|
86
|
-
|
|
87
|
-
filterKey.set(mutationKey2)
|
|
88
|
-
TestBed.flushEffects()
|
|
89
|
-
expect(mutationState()).toEqual([variables2])
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
test('should return variables after calling mutate', async () => {
|
|
93
|
-
queryClient.clear()
|
|
94
|
-
const mutationKey = ['mutation']
|
|
95
|
-
const variables = 'bar234'
|
|
96
|
-
|
|
97
|
-
const mutation = TestBed.runInInjectionContext(() => {
|
|
98
|
-
return injectMutation(() => ({
|
|
99
|
-
mutationKey: mutationKey,
|
|
100
|
-
mutationFn: (params: string) => successMutator(params),
|
|
101
|
-
}))
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
mutation.mutate(variables)
|
|
105
|
-
|
|
106
|
-
const mutationState = TestBed.runInInjectionContext(() => {
|
|
107
|
-
return injectMutationState()
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
expect(mutationState()[0]?.variables).toEqual(variables)
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
test('should support required signal inputs', async () => {
|
|
114
|
-
queryClient.clear()
|
|
115
|
-
const fakeName = 'name1'
|
|
116
|
-
const mutationKey1 = ['fake', fakeName]
|
|
117
|
-
|
|
118
|
-
const mutations = TestBed.runInInjectionContext(() => {
|
|
119
|
-
return [
|
|
120
|
-
injectMutation(() => ({
|
|
121
|
-
mutationKey: mutationKey1,
|
|
122
|
-
mutationFn: () => Promise.resolve('myValue'),
|
|
123
|
-
})),
|
|
124
|
-
injectMutation(() => ({
|
|
125
|
-
mutationKey: mutationKey1,
|
|
126
|
-
mutationFn: () => Promise.reject('myValue2'),
|
|
127
|
-
})),
|
|
128
|
-
]
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
mutations.forEach((mutation) => mutation.mutate())
|
|
132
|
-
|
|
133
|
-
@Component({
|
|
134
|
-
selector: 'app-fake',
|
|
135
|
-
template: `
|
|
136
|
-
@for (mutation of mutationState(); track mutation) {
|
|
137
|
-
<span>{{ mutation.status }}</span>
|
|
138
|
-
}
|
|
139
|
-
`,
|
|
140
|
-
standalone: true,
|
|
141
|
-
})
|
|
142
|
-
class FakeComponent {
|
|
143
|
-
name = input.required<string>()
|
|
144
|
-
|
|
145
|
-
mutationState = injectMutationState(() => ({
|
|
146
|
-
filters: {
|
|
147
|
-
mutationKey: ['fake', this.name()],
|
|
148
|
-
exact: true,
|
|
149
|
-
},
|
|
150
|
-
}))
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const fixture = TestBed.createComponent(FakeComponent)
|
|
154
|
-
const { debugElement } = fixture
|
|
155
|
-
setFixtureSignalInputs(fixture, { name: fakeName })
|
|
156
|
-
|
|
157
|
-
fixture.detectChanges()
|
|
158
|
-
|
|
159
|
-
let spans = debugElement
|
|
160
|
-
.queryAll(By.css('span'))
|
|
161
|
-
.map((span) => span.nativeNode.textContent)
|
|
162
|
-
|
|
163
|
-
expect(spans).toEqual(['pending', 'pending'])
|
|
164
|
-
|
|
165
|
-
await resolveMutations()
|
|
166
|
-
fixture.detectChanges()
|
|
167
|
-
|
|
168
|
-
spans = debugElement
|
|
169
|
-
.queryAll(By.css('span'))
|
|
170
|
-
.map((span) => span.nativeNode.textContent)
|
|
171
|
-
|
|
172
|
-
expect(spans).toEqual(['success', 'error'])
|
|
173
|
-
})
|
|
174
|
-
})
|
|
175
|
-
})
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { describe, expectTypeOf } from 'vitest'
|
|
2
|
-
import { injectMutation } from '../inject-mutation'
|
|
3
|
-
import { successMutator } from './test-utils'
|
|
4
|
-
import type { Signal } from '@angular/core'
|
|
5
|
-
|
|
6
|
-
describe('Discriminated union return type', () => {
|
|
7
|
-
test('data should be possibly undefined by default', () => {
|
|
8
|
-
const mutation = injectMutation(() => ({
|
|
9
|
-
mutationFn: successMutator<string>,
|
|
10
|
-
}))
|
|
11
|
-
|
|
12
|
-
expectTypeOf(mutation.data).toEqualTypeOf<Signal<string | undefined>>()
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
test('data should be defined when mutation is success', () => {
|
|
16
|
-
const mutation = injectMutation(() => ({
|
|
17
|
-
mutationFn: successMutator<string>,
|
|
18
|
-
}))
|
|
19
|
-
|
|
20
|
-
if (mutation.isSuccess()) {
|
|
21
|
-
expectTypeOf(mutation.data).toEqualTypeOf<Signal<string>>()
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
test('error should be null when mutation is success', () => {
|
|
26
|
-
const mutation = injectMutation(() => ({
|
|
27
|
-
mutationFn: successMutator<string>,
|
|
28
|
-
}))
|
|
29
|
-
|
|
30
|
-
if (mutation.isSuccess()) {
|
|
31
|
-
expectTypeOf(mutation.error).toEqualTypeOf<Signal<null>>()
|
|
32
|
-
}
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
test('data should be undefined when mutation is pending', () => {
|
|
36
|
-
const mutation = injectMutation(() => ({
|
|
37
|
-
mutationFn: successMutator<string>,
|
|
38
|
-
}))
|
|
39
|
-
|
|
40
|
-
if (mutation.isPending()) {
|
|
41
|
-
expectTypeOf(mutation.data).toEqualTypeOf<Signal<undefined>>()
|
|
42
|
-
}
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
test('error should be defined when mutation is error', () => {
|
|
46
|
-
const mutation = injectMutation(() => ({
|
|
47
|
-
mutationFn: successMutator<string>,
|
|
48
|
-
}))
|
|
49
|
-
|
|
50
|
-
if (mutation.isError()) {
|
|
51
|
-
expectTypeOf(mutation.error).toEqualTypeOf<Signal<Error>>()
|
|
52
|
-
}
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test('should narrow variables', () => {
|
|
56
|
-
const mutation = injectMutation(() => ({
|
|
57
|
-
mutationFn: successMutator<string>,
|
|
58
|
-
}))
|
|
59
|
-
|
|
60
|
-
if (mutation.isIdle()) {
|
|
61
|
-
expectTypeOf(mutation.variables).toEqualTypeOf<Signal<undefined>>()
|
|
62
|
-
}
|
|
63
|
-
if (mutation.isPending()) {
|
|
64
|
-
expectTypeOf(mutation.variables).toEqualTypeOf<Signal<string>>()
|
|
65
|
-
}
|
|
66
|
-
if (mutation.isSuccess()) {
|
|
67
|
-
expectTypeOf(mutation.variables).toEqualTypeOf<Signal<string>>()
|
|
68
|
-
}
|
|
69
|
-
expectTypeOf(mutation.variables).toEqualTypeOf<Signal<string | undefined>>()
|
|
70
|
-
})
|
|
71
|
-
})
|