Skip to content

Commit 527574a

Browse files
Add explicit test case for mutating in constructor
1 parent 9fcd7dd commit 527574a

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

packages/angular-query-experimental/src/__tests__/inject-mutation-state.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ describe('injectMutationState', () => {
144144
<span>{{ mutation.status }}</span>
145145
}
146146
`,
147-
standalone: true,
148147
})
149148
class FakeComponent {
150149
name = input.required<string>()

packages/angular-query-experimental/src/__tests__/inject-mutation.test.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ describe('injectMutation', () => {
316316
<button (click)="mutate()"></button>
317317
<span>{{ mutation.data() }}</span>
318318
`,
319-
standalone: true,
320319
})
321320
class FakeComponent {
322321
name = input.required<string>()
@@ -339,8 +338,6 @@ describe('injectMutation', () => {
339338
button.triggerEventHandler('click')
340339

341340
await resolveMutations()
342-
fixture.detectChanges()
343-
344341
const text = debugElement.query(By.css('span')).nativeElement.textContent
345342
expect(text).toEqual('value')
346343
const mutation = mutationCache.find({ mutationKey: ['fake', 'value'] })
@@ -357,7 +354,6 @@ describe('injectMutation', () => {
357354
<button (click)="mutate()"></button>
358355
<span>{{ mutation.data() }}</span>
359356
`,
360-
standalone: true,
361357
})
362358
class FakeComponent {
363359
name = input.required<string>()
@@ -400,6 +396,37 @@ describe('injectMutation', () => {
400396
expect(mutation2!.options.mutationKey).toEqual(['fake', 'updatedValue'])
401397
})
402398

399+
test('should have pending state when mutating in constructor', async () => {
400+
@Component({
401+
selector: 'app-fake',
402+
template: `
403+
<span>{{ mutation.isPending() ? 'pending' : 'not pending' }}</span>
404+
`,
405+
})
406+
class FakeComponent {
407+
mutation = injectMutation(() => ({
408+
mutationKey: ['fake'],
409+
mutationFn: () => sleep(0).then(() => 'fake'),
410+
}))
411+
412+
constructor() {
413+
this.mutation.mutate()
414+
}
415+
}
416+
417+
const fixture = TestBed.createComponent(FakeComponent)
418+
const { debugElement } = fixture
419+
const span = debugElement.query(By.css('span'))
420+
421+
vi.advanceTimersByTime(1)
422+
423+
expect(span.nativeElement.textContent).toEqual('pending')
424+
425+
await resolveMutations()
426+
427+
expect(span.nativeElement.textContent).toEqual('not pending')
428+
})
429+
403430
describe('throwOnError', () => {
404431
test('should evaluate throwOnError when mutation is expected to throw', async () => {
405432
const err = new Error('Expected mock error. All is well!')

packages/angular-query-experimental/src/__tests__/inject-query.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,6 @@ describe('injectQuery', () => {
520520
@Component({
521521
selector: 'app-fake',
522522
template: `{{ query.data() }}`,
523-
standalone: true,
524523
})
525524
class FakeComponent {
526525
name = input.required<string>()

0 commit comments

Comments
 (0)