Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't fire router events on render #325

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});