Open History to see the last commit
1import {
2 ChangeDetectionStrategy,
3 Component,
4 Injector,
5 input,
6 provideZonelessChangeDetection,
7 signal,
8} from '@angular/core'
9import { TestBed } from '@angular/core/testing'
10import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
11import { sleep } from '@tanstack/query-test-utils'
12import {
13 QueryClient,
14 injectMutation,
15 injectMutationState,
16 provideTanStackQuery,
17} from '..'
18import { registerSignalInput } 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 changeDetection: ChangeDetectionStrategy.OnPush,
149 })
150 class FakeComponent {
151 name = input.required<string>()
152β
153 mutationState = injectMutationState(() => ({
154 filters: {
155 mutationKey: ['fake', this.name()],
156 exact: true,
157 },
158 }))
159 }
160β
161 registerSignalInput(FakeComponent, 'name')
162β
163 @Component({
164 template: `<app-fake [name]="name()" />`,
165 imports: [FakeComponent],
166 })
167 class HostComponent {
168 protected readonly name = signal(fakeName)
169 }
170β
171 const fixture = TestBed.createComponent(HostComponent)
172 fixture.detectChanges()
173 await vi.advanceTimersByTimeAsync(0)
174β
175 const readSpans = () =>
176 Array.from(
177 fixture.nativeElement.querySelectorAll(
178 'span',
179 ) as NodeListOf<HTMLSpanElement>,
180 ).map((span) => span.textContent)
181β
182 let spans = readSpans()
183β
184 expect(spans).toEqual(['pending', 'pending'])
185β
186 await vi.advanceTimersByTimeAsync(11)
187 fixture.detectChanges()
188β
189 spans = readSpans()
190β
191 expect(spans).toEqual(['success', 'error'])
192 })
193β
194 describe('injection context', () => {
195 test('throws NG0203 with descriptive error outside injection context', () => {
196 expect(() => {
197 injectMutationState()
198 }).toThrowError(/NG0203(.*?)injectMutationState/)
199 })
200β
201 test('can be used outside injection context when passing an injector', () => {
202 const injector = TestBed.inject(Injector)
203 expect(
204 injectMutationState(undefined, {
205 injector,
206 }),
207 ).not.toThrow()
208 })
209 })
210 })
211})