Skip to content

Commit

Permalink
feat(router): exposes the fixture of the RouterTestingHarness (#5…
Browse files Browse the repository at this point in the history
…0280)

The component exposed by the fixture is not important thus marked as `unknown`.

Exposing the fixture of the `RouterTestingHarness` allows to use the methods & properties of that fixture and makes it compatible with testing libraries relying on `ComponentFixture`

Fixes #48855

PR Close #50280
  • Loading branch information
JeanMeche authored and pkozlowski-opensource committed Jun 14, 2023
1 parent 82adc86 commit 0b14e4e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions goldens/public-api/router/testing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { ChildrenOutletContexts } from '@angular/router';
import { Compiler } from '@angular/core';
import { ComponentFixture } from '@angular/core/testing';
import { DebugElement } from '@angular/core';
import { ExtraOptions } from '@angular/router';
import * as i0 from '@angular/core';
Expand All @@ -26,6 +27,7 @@ import { UrlSerializer } from '@angular/router';
export class RouterTestingHarness {
static create(initialUrl?: string): Promise<RouterTestingHarness>;
detectChanges(): void;
readonly fixture: ComponentFixture<unknown>;
navigateByUrl(url: string): Promise<null | {}>;
navigateByUrl<T>(url: string, requiredRoutedComponentType: Type<T>): Promise<T>;
get routeDebugElement(): DebugElement | null;
Expand Down
13 changes: 10 additions & 3 deletions packages/router/testing/src/router_testing_harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,23 @@ export class RouterTestingHarness {
return harness;
}

/**
* Fixture of the root component of the RouterTestingHarness
*/
public readonly fixture: ComponentFixture<unknown>;

/** @internal */
constructor(private readonly fixture: ComponentFixture<RootCmp>) {}
constructor(fixture: ComponentFixture<unknown>) {
this.fixture = fixture;
}

/** Instructs the root fixture to run change detection. */
detectChanges(): void {
this.fixture.detectChanges();
}
/** The `DebugElement` of the `RouterOutlet` component. `null` if the outlet is not activated. */
get routeDebugElement(): DebugElement|null {
const outlet = this.fixture.componentInstance.outlet;
const outlet = (this.fixture.componentInstance as RootCmp).outlet;
if (!outlet || !outlet.isActivated) {
return null;
}
Expand Down Expand Up @@ -136,7 +143,7 @@ export class RouterTestingHarness {
await router.navigateByUrl(url);
await redirectTrackingPromise;
this.fixture.detectChanges();
const outlet = this.fixture.componentInstance.outlet;
const outlet = (this.fixture.componentInstance as RootCmp).outlet;
// The outlet might not be activated if the user is testing a navigation for a guard that
// rejects
if (outlet && outlet.isActivated && outlet.activatedRoute.component) {
Expand Down

0 comments on commit 0b14e4e

Please sign in to comment.