Skip to content

Commit d4a6ef4

Browse files
fix: don't fire router events on render (#325)
Closes #318
1 parent 80f3341 commit d4a6ef4

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

projects/testing-library/src/lib/testing-library.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { RenderComponentOptions, RenderTemplateOptions, RenderResult } from './m
2929
import { getConfig } from './config';
3030

3131
const mountedFixtures = new Set<ComponentFixture<any>>();
32-
const inject = TestBed.inject || TestBed.get;
32+
const safeInject = TestBed.inject || TestBed.get;
3333

3434
export async function render<ComponentType>(
3535
component: Type<ComponentType>,
@@ -97,6 +97,17 @@ export async function render<SutType, WrapperType = SutType>(
9797

9898
const componentContainer = createComponentFixture(sut, wrapper);
9999

100+
const zone = safeInject(NgZone);
101+
const router = safeInject(Router);
102+
103+
if (typeof router?.initialNavigation === 'function') {
104+
if (zone) {
105+
zone.run(() => router?.initialNavigation());
106+
} else {
107+
router?.initialNavigation();
108+
}
109+
}
110+
100111
let fixture: ComponentFixture<SutType>;
101112
let detectChanges: () => void;
102113

@@ -118,17 +129,6 @@ export async function render<SutType, WrapperType = SutType>(
118129
fixture.componentRef.injector.get(ChangeDetectorRef).detectChanges();
119130
};
120131

121-
const zone = inject(NgZone);
122-
123-
const router = inject(Router);
124-
if (typeof router?.initialNavigation === 'function') {
125-
if (zone) {
126-
zone.run(() => router?.initialNavigation());
127-
} else {
128-
router?.initialNavigation();
129-
}
130-
}
131-
132132
const navigate = async (elementOrPath: Element | string, basePath = ''): Promise<boolean> => {
133133
const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath.getAttribute('href');
134134
const [path, params] = (basePath + href).split('?');
@@ -227,7 +227,7 @@ export async function render<SutType, WrapperType = SutType>(
227227

228228
async function createComponent<SutType>(component: Type<SutType>): Promise<ComponentFixture<SutType>> {
229229
/* Make sure angular application is initialized before creating component */
230-
await inject(ApplicationInitStatus).donePromise;
230+
await safeInject(ApplicationInitStatus).donePromise;
231231
return TestBed.createComponent(component);
232232
}
233233

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {Component, OnDestroy, OnInit} from '@angular/core';
2+
import {Router} from '@angular/router';
3+
import {RouterTestingModule} from '@angular/router/testing';
4+
import {Subject, takeUntil} from 'rxjs';
5+
import {render} from "@testing-library/angular";
6+
7+
@Component({
8+
selector: 'atl-app-fixture',
9+
template: '',
10+
})
11+
class FixtureComponent implements OnInit, OnDestroy {
12+
unsubscribe$ = new Subject<void>();
13+
14+
constructor(private router: Router) {}
15+
16+
ngOnInit(): void {
17+
this.router.events.pipe(takeUntil(this.unsubscribe$)).subscribe((evt) => {
18+
this.eventReceived(evt)
19+
});
20+
}
21+
22+
ngOnDestroy(): void {
23+
this.unsubscribe$.next();
24+
this.unsubscribe$.complete();
25+
}
26+
27+
eventReceived(evt: any) {
28+
console.log(evt);
29+
}
30+
}
31+
32+
33+
test('it does not invoke router events on init', async () => {
34+
const eventReceived = jest.fn();
35+
await render(FixtureComponent, {
36+
imports: [RouterTestingModule],
37+
componentProperties: {
38+
eventReceived
39+
}
40+
});
41+
expect(eventReceived).not.toHaveBeenCalled();
42+
});
43+

0 commit comments

Comments
 (0)