Open History to see the last commit
1import {
2 Component,
3 Injector,
4 input,
5 provideExperimentalZonelessChangeDetection,
6 signal,
7} from '@angular/core'
8import { TestBed } from '@angular/core/testing'
9import { describe, expect, test, vi } from 'vitest'
10import { By } from '@angular/platform-browser'
11import {
12 QueryClient,
13 injectMutation,
14 injectMutationState,
15 provideTanStackQuery,
16} from '..'
17import { setFixtureSignalInputs, successMutator } from './test-utils'
18β
19const MUTATION_DURATION = 1000
20β
21const resolveMutations = () => vi.advanceTimersByTimeAsync(MUTATION_DURATION)
22β
23describe('injectMutationState', () => {
24 let queryClient: QueryClient
25β
26 beforeEach(() => {
27 queryClient = new QueryClient()
28 vi.useFakeTimers()
29 TestBed.configureTestingModule({
30 providers: [
31 provideExperimentalZonelessChangeDetection(),
32 provideTanStackQuery(queryClient),
33 ],
34 })
35 })
36β
37 afterEach(() => {
38 vi.useRealTimers()
39 })
40β
41 describe('injectMutationState', () => {
42 test('should return variables after calling mutate 1', () => {
43 const mutationKey = ['mutation']
44 const variables = 'foo123'
45β
46 const mutation = TestBed.runInInjectionContext(() => {
47 return injectMutation(() => ({
48 mutationKey: mutationKey,
49 mutationFn: (params: string) => successMutator(params),
50 }))
51 })
52β
53 mutation.mutate(variables)
54β
55 const mutationState = TestBed.runInInjectionContext(() => {
56 return injectMutationState(() => ({
57 filters: { mutationKey, status: 'pending' },
58 select: (m) => m.state.variables,
59 }))
60 })
61β
62 expect(mutationState()).toEqual([variables])
63 })
64β
65 test('reactive options should update injectMutationState', () => {
66 const mutationKey1 = ['mutation1']
67 const mutationKey2 = ['mutation2']
68 const variables1 = 'foo123'
69 const variables2 = 'bar234'
70β
71 const [mutation1, mutation2] = TestBed.runInInjectionContext(() => {
72 return [
73 injectMutation(() => ({
74 mutationKey: mutationKey1,
75 mutationFn: (params: string) => successMutator(params),
76 })),
77 injectMutation(() => ({
78 mutationKey: mutationKey2,
79 mutationFn: (params: string) => successMutator(params),
80 })),
81 ]
82 })
83β
84 mutation1.mutate(variables1)
85 mutation2.mutate(variables2)
86β
87 const filterKey = signal(mutationKey1)
88β
89 const mutationState = TestBed.runInInjectionContext(() => {
90 return injectMutationState(() => ({
91 filters: { mutationKey: filterKey(), status: 'pending' },
92 select: (m) => m.state.variables,
93 }))
94 })
95β
96 expect(mutationState()).toEqual([variables1])
97β
98 filterKey.set(mutationKey2)
99 expect(mutationState()).toEqual([variables2])
100 })
101β
102 test('should return variables after calling mutate 2', () => {
103 queryClient.clear()
104 const mutationKey = ['mutation']
105 const variables = 'bar234'
106β
107 const mutation = TestBed.runInInjectionContext(() => {
108 return injectMutation(() => ({
109 mutationKey: mutationKey,
110 mutationFn: (params: string) => successMutator(params),
111 }))
112 })
113β
114 mutation.mutate(variables)
115β
116 const mutationState = TestBed.runInInjectionContext(() => {
117 return injectMutationState()
118 })
119β
120 expect(mutationState()[0]?.variables).toEqual(variables)
121 })
122β
123 test('should support required signal inputs', async () => {
124 queryClient.clear()
125 const fakeName = 'name1'
126 const mutationKey1 = ['fake', fakeName]
127β
128 const mutations = TestBed.runInInjectionContext(() => {
129 return [
130 injectMutation(() => ({
131 mutationKey: mutationKey1,
132 mutationFn: () => Promise.resolve('myValue'),
133 })),
134 injectMutation(() => ({
135 mutationKey: mutationKey1,
136 mutationFn: () => Promise.reject('myValue2'),
137 })),
138 ]
139 })
140β
141 mutations.forEach((mutation) => mutation.mutate())
142β
143 @Component({
144 selector: 'app-fake',
145 template: `
146 @for (mutation of mutationState(); track $index) {
147 <span>{{ mutation.status }}</span>
148 }
149 `,
150 standalone: true,
151 })
152 class FakeComponent {
153 name = input.required<string>()
154β
155 mutationState = injectMutationState(() => ({
156 filters: {
157 mutationKey: ['fake', this.name()],
158 exact: true,
159 },
160 }))
161 }
162β
163 const fixture = TestBed.createComponent(FakeComponent)
164 const { debugElement } = fixture
165 setFixtureSignalInputs(fixture, { name: fakeName })
166β
167 let spans = debugElement
168 .queryAll(By.css('span'))
169 .map((span) => span.nativeNode.textContent)
170β
171 expect(spans).toEqual(['pending', 'pending'])
172β
173 await resolveMutations()
174 fixture.detectChanges()
175β
176 spans = debugElement
177 .queryAll(By.css('span'))
178 .map((span) => span.nativeNode.textContent)
179β
180 expect(spans).toEqual(['success', 'error'])
181 })
182β
183 describe('injection context', () => {
184 test('throws NG0203 with descriptive error outside injection context', () => {
185 expect(() => {
186 injectMutationState()
187 }).toThrowError(/NG0203(.*?)injectMutationState/)
188 })
189β
190 test('can be used outside injection context when passing an injector', () => {
191 const injector = TestBed.inject(Injector)
192 expect(
193 injectMutationState(undefined, {
194 injector,
195 }),
196 ).not.toThrow()
197 })
198 })
199 })
200})