diff --git a/projects/testing-library/src/lib/testing-library.ts b/projects/testing-library/src/lib/testing-library.ts index 6c54fe85..2c7734f8 100644 --- a/projects/testing-library/src/lib/testing-library.ts +++ b/projects/testing-library/src/lib/testing-library.ts @@ -29,7 +29,7 @@ import { RenderComponentOptions, RenderTemplateOptions, RenderResult } from './m import { getConfig } from './config'; const mountedFixtures = new Set>(); -const inject = TestBed.inject || TestBed.get; +const safeInject = TestBed.inject || TestBed.get; export async function render( component: Type, @@ -97,6 +97,17 @@ export async function render( const componentContainer = createComponentFixture(sut, wrapper); + const zone = safeInject(NgZone); + const router = safeInject(Router); + + if (typeof router?.initialNavigation === 'function') { + if (zone) { + zone.run(() => router?.initialNavigation()); + } else { + router?.initialNavigation(); + } + } + let fixture: ComponentFixture; let detectChanges: () => void; @@ -118,17 +129,6 @@ export async function render( fixture.componentRef.injector.get(ChangeDetectorRef).detectChanges(); }; - const zone = inject(NgZone); - - const router = inject(Router); - if (typeof router?.initialNavigation === 'function') { - if (zone) { - zone.run(() => router?.initialNavigation()); - } else { - router?.initialNavigation(); - } - } - const navigate = async (elementOrPath: Element | string, basePath = ''): Promise => { const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath.getAttribute('href'); const [path, params] = (basePath + href).split('?'); @@ -227,7 +227,7 @@ export async function render( async function createComponent(component: Type): Promise> { /* Make sure angular application is initialized before creating component */ - await inject(ApplicationInitStatus).donePromise; + await safeInject(ApplicationInitStatus).donePromise; return TestBed.createComponent(component); } diff --git a/projects/testing-library/tests/issues/issue-318.spec.ts b/projects/testing-library/tests/issues/issue-318.spec.ts new file mode 100644 index 00000000..3f1430e8 --- /dev/null +++ b/projects/testing-library/tests/issues/issue-318.spec.ts @@ -0,0 +1,43 @@ +import {Component, OnDestroy, OnInit} from '@angular/core'; +import {Router} from '@angular/router'; +import {RouterTestingModule} from '@angular/router/testing'; +import {Subject, takeUntil} from 'rxjs'; +import {render} from "@testing-library/angular"; + +@Component({ + selector: 'atl-app-fixture', + template: '', +}) +class FixtureComponent implements OnInit, OnDestroy { + unsubscribe$ = new Subject(); + + constructor(private router: Router) {} + + ngOnInit(): void { + this.router.events.pipe(takeUntil(this.unsubscribe$)).subscribe((evt) => { + this.eventReceived(evt) + }); + } + + ngOnDestroy(): void { + this.unsubscribe$.next(); + this.unsubscribe$.complete(); + } + + eventReceived(evt: any) { + console.log(evt); + } +} + + +test('it does not invoke router events on init', async () => { + const eventReceived = jest.fn(); + await render(FixtureComponent, { + imports: [RouterTestingModule], + componentProperties: { + eventReceived + } + }); + expect(eventReceived).not.toHaveBeenCalled(); +}); +