Open History to see the last commit
1import {
2 Component,
3 Injector,
4 input,
5 provideZonelessChangeDetection,
6 signal,
7} from '@angular/core'
8import { TestBed } from '@angular/core/testing'
9import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
10import { By } from '@angular/platform-browser'
11import { sleep } from '@tanstack/query-test-utils'
12import {
13 QueryClient,
14 injectMutation,
15 injectMutationState,
16 provideTanStackQuery,
17} from '..'
18import { setFixtureSignalInputs } from './test-utils'
19β
20describe('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 test('should return variables after calling mutate 1', () => {
40 const mutationKey = ['mutation']
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 test('reactive options should update injectMutationState', () => {
63 const mutationKey1 = ['mutation1']
64 const mutationKey2 = ['mutation2']
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 test('should return variables after calling mutate 2', () => {
100 queryClient.clear()
101 const mutationKey = ['mutation']
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 test('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 test('throws NG0203 with descriptive error outside injection context', () => {
183 expect(() => {
184 injectMutationState()
185 }).toThrowError(/NG0203(.*?)injectMutationState/)
186 })
187β
188 test('can be used 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})