Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

inject-mutation-state.test.ts

197 lines5.4 KB

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