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

inject-mutation-state.test.ts

191 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', () => {
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', () => {
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 expect(mutationState()).toEqual([variables2])
91 })
92​
93 test('should return variables after calling mutate 2', () => {
94 queryClient.clear()
95 const mutationKey = ['mutation']
96 const variables = 'bar234'
97​
98 const mutation = TestBed.runInInjectionContext(() => {
99 return injectMutation(() => ({
100 mutationKey: mutationKey,
101 mutationFn: (params: string) => successMutator(params),
102 }))
103 })
104​
105 mutation.mutate(variables)
106​
107 const mutationState = TestBed.runInInjectionContext(() => {
108 return injectMutationState()
109 })
110​
111 expect(mutationState()[0]?.variables).toEqual(variables)
112 })
113​
114 test('should support required signal inputs', async () => {
115 queryClient.clear()
116 const fakeName = 'name1'
117 const mutationKey1 = ['fake', fakeName]
118​
119 const mutations = TestBed.runInInjectionContext(() => {
120 return [
121 injectMutation(() => ({
122 mutationKey: mutationKey1,
123 mutationFn: () => Promise.resolve('myValue'),
124 })),
125 injectMutation(() => ({
126 mutationKey: mutationKey1,
127 mutationFn: () => Promise.reject('myValue2'),
128 })),
129 ]
130 })
131​
132 mutations.forEach((mutation) => mutation.mutate())
133​
134 @Component({
135 selector: 'app-fake',
136 template: `
137 @for (mutation of mutationState(); track mutation) {
138 <span>{{ mutation.status }}</span>
139 }
140 `,
141 standalone: true,
142 })
143 class FakeComponent {
144 name = input.required<string>()
145​
146 mutationState = injectMutationState(() => ({
147 filters: {
148 mutationKey: ['fake', this.name()],
149 exact: true,
150 },
151 }))
152 }
153​
154 const fixture = TestBed.createComponent(FakeComponent)
155 const { debugElement } = fixture
156 setFixtureSignalInputs(fixture, { name: fakeName })
157​
158 let spans = debugElement
159 .queryAll(By.css('span'))
160 .map((span) => span.nativeNode.textContent)
161​
162 expect(spans).toEqual(['pending', 'pending'])
163​
164 await resolveMutations()
165 fixture.detectChanges()
166​
167 spans = debugElement
168 .queryAll(By.css('span'))
169 .map((span) => span.nativeNode.textContent)
170​
171 expect(spans).toEqual(['success', 'error'])
172 })
173​
174 describe('injection context', () => {
175 test('throws NG0203 with descriptive error outside injection context', () => {
176 expect(() => {
177 injectMutationState()
178 }).toThrowError(/NG0203(.*?)injectMutationState/)
179 })
180​
181 test('can be used outside injection context when passing an injector', () => {
182 const injector = TestBed.inject(Injector)
183 expect(
184 injectMutationState(undefined, {
185 injector,
186 }),
187 ).not.toThrow()
188 })
189 })
190 })
191})