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

inject-mutation-state.test.ts

194 lines5.4 KB

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