Open History to see the last commit
1import { Component, input, signal } from '@angular/core'
2import { QueryClient } from '@tanstack/query-core'
3import { TestBed } from '@angular/core/testing'
4import { describe, expect, test, vi } from 'vitest'
5import { By } from '@angular/platform-browser'
6import { JsonPipe } from '@angular/common'
7import { injectMutation } from '../inject-mutation'
8import { injectMutationState } from '../inject-mutation-state'
9import { provideAngularQuery } from '../providers'
10import { setFixtureSignalInputs, successMutator } from './test-utils'
11β
12const MUTATION_DURATION = 1000
13β
14const resolveMutations = () => vi.advanceTimersByTimeAsync(MUTATION_DURATION)
15β
16describe('injectMutationState', () => {
17 let queryClient: QueryClient
18β
19 beforeEach(() => {
20 queryClient = new QueryClient()
21 vi.useFakeTimers()
22 TestBed.configureTestingModule({
23 providers: [provideAngularQuery(queryClient)],
24 })
25 })
26β
27 afterEach(() => {
28 vi.useRealTimers()
29 })
30β
31 describe('injectMutationState', () => {
32 test('should return variables after calling mutate', async () => {
33 const mutationKey = ['mutation']
34 const variables = 'foo123'
35β
36 const mutation = TestBed.runInInjectionContext(() => {
37 return injectMutation(() => ({
38 mutationKey: mutationKey,
39 mutationFn: (params: string) => successMutator(params),
40 }))
41 })
42β
43 mutation.mutate(variables)
44β
45 const mutationState = TestBed.runInInjectionContext(() => {
46 return injectMutationState(() => ({
47 filters: { mutationKey, status: 'pending' },
48 select: (m) => m.state.variables,
49 }))
50 })
51β
52 expect(mutationState()).toEqual([variables])
53 })
54β
55 test('reactive options should update injectMutationState', async () => {
56 const mutationKey1 = ['mutation1']
57 const mutationKey2 = ['mutation2']
58 const variables1 = 'foo123'
59 const variables2 = 'bar234'
60β
61 const [mutation1, mutation2] = TestBed.runInInjectionContext(() => {
62 return [
63 injectMutation(() => ({
64 mutationKey: mutationKey1,
65 mutationFn: (params: string) => successMutator(params),
66 })),
67 injectMutation(() => ({
68 mutationKey: mutationKey2,
69 mutationFn: (params: string) => successMutator(params),
70 })),
71 ]
72 })
73β
74 mutation1.mutate(variables1)
75 mutation2.mutate(variables2)
76β
77 const filterKey = signal(mutationKey1)
78β
79 const mutationState = TestBed.runInInjectionContext(() => {
80 return injectMutationState(() => ({
81 filters: { mutationKey: filterKey(), status: 'pending' },
82 select: (m) => m.state.variables,
83 }))
84 })
85β
86 expect(mutationState()).toEqual([variables1])
87β
88 filterKey.set(mutationKey2)
89 TestBed.flushEffects()
90 expect(mutationState()).toEqual([variables2])
91 })
92β
93 test('should return variables after calling mutate', async () => {
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 imports: [JsonPipe],
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})