Skip to content

Commit

Permalink
fix: don't fire router events on render (#325)
Browse files Browse the repository at this point in the history
Closes #318
  • Loading branch information
timdeschryver authored Nov 21, 2022
1 parent 80f3341 commit d4a6ef4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
26 changes: 13 additions & 13 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { RenderComponentOptions, RenderTemplateOptions, RenderResult } from './m
import { getConfig } from './config';

const mountedFixtures = new Set<ComponentFixture<any>>();
const inject = TestBed.inject || TestBed.get;
const safeInject = TestBed.inject || TestBed.get;

export async function render<ComponentType>(
component: Type<ComponentType>,
Expand Down Expand Up @@ -97,6 +97,17 @@ export async function render<SutType, WrapperType = SutType>(

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<SutType>;
let detectChanges: () => void;

Expand All @@ -118,17 +129,6 @@ export async function render<SutType, WrapperType = SutType>(
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<boolean> => {
const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath.getAttribute('href');
const [path, params] = (basePath + href).split('?');
Expand Down Expand Up @@ -227,7 +227,7 @@ export async function render<SutType, WrapperType = SutType>(

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

Expand Down
43 changes: 43 additions & 0 deletions projects/testing-library/tests/issues/issue-318.spec.ts
Original file line number Diff line number Diff line change
@@ -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<void>();

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();
});

0 comments on commit d4a6ef4

Please sign in to comment.