|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 | import { TestBed } from '@angular/core/testing' |
3 | | -import { Injector, provideZonelessChangeDetection } from '@angular/core' |
| 3 | +import { |
| 4 | + Component, |
| 5 | + Injector, |
| 6 | + provideZonelessChangeDetection, |
| 7 | +} from '@angular/core' |
| 8 | +import { render } from '@testing-library/angular' |
4 | 9 | import { queryKey, sleep } from '@tanstack/query-test-utils' |
5 | 10 | import { |
6 | 11 | QueryClient, |
@@ -30,25 +35,31 @@ describe('injectIsMutating', () => { |
30 | 35 |
|
31 | 36 | it('should properly return isMutating state', async () => { |
32 | 37 | const key = queryKey() |
33 | | - const [mutation, isMutating] = TestBed.runInInjectionContext(() => [ |
34 | | - injectMutation(() => ({ |
| 38 | + |
| 39 | + @Component({ |
| 40 | + template: `<div>mutating: {{ isMutating() }}</div>`, |
| 41 | + }) |
| 42 | + class Page { |
| 43 | + readonly mutation = injectMutation(() => ({ |
35 | 44 | mutationKey: key, |
36 | 45 | mutationFn: (params: { par1: string }) => sleep(10).then(() => params), |
37 | | - })), |
38 | | - injectIsMutating(), |
39 | | - ]) |
| 46 | + })) |
| 47 | + readonly isMutating = injectIsMutating() |
| 48 | + } |
40 | 49 |
|
41 | | - expect(isMutating()).toBe(0) |
| 50 | + const rendered = await render(Page) |
42 | 51 |
|
43 | | - mutation.mutate({ |
44 | | - par1: 'par1', |
45 | | - }) |
| 52 | + expect(rendered.getByText('mutating: 0')).toBeInTheDocument() |
| 53 | + |
| 54 | + rendered.fixture.componentInstance.mutation.mutate({ par1: 'par1' }) |
46 | 55 |
|
47 | | - expect(isMutating()).toBe(0) |
48 | 56 | await vi.advanceTimersByTimeAsync(0) |
49 | | - expect(isMutating()).toBe(1) |
| 57 | + rendered.fixture.detectChanges() |
| 58 | + expect(rendered.getByText('mutating: 1')).toBeInTheDocument() |
| 59 | + |
50 | 60 | await vi.advanceTimersByTimeAsync(11) |
51 | | - expect(isMutating()).toBe(0) |
| 61 | + rendered.fixture.detectChanges() |
| 62 | + expect(rendered.getByText('mutating: 0')).toBeInTheDocument() |
52 | 63 | }) |
53 | 64 |
|
54 | 65 | describe('injection context', () => { |
|
0 commit comments