Skip to content

RouterTestingHarness.navigateByUrl does not throw error if navigation is cancelled when the required routed component type is specified #52344

@reduckted

Description

@reduckted

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

No

Description

When using RouterTestingHarness.navigateByUrl, if you specify the required routed component type, but a route guard cancels navigation, an error should be thrown, but is not.

The documentation states:

* @param requiredRoutedComponentType After navigation completes, the required type for the
* activated component of the `RouterOutlet`. If the outlet is not activated or a different
* component is activated, this function will throw an error.

Specifically, this part:

If the outlet is not activated or a different component is activated, this function will throw an error.

Instead of throwing an error, the function returns null. If the route guard redirects to a different component, then this function does correctly throw an exception.

The linked repo has a full reproduction, but here's some code that highlights the problem.

describe('Bug', () => {
    it(`with incorrect expected component.`, async () => {
        TestBed.configureTestingModule({
            providers: [
                provideRouter([
                    {
                        path: 'delta',
                        component: DeltaComponent,
                        canActivate: [() => false],
                    },
                ]),
                provideLocationMocks(),
            ],
        });

        let harness = await RouterTestingHarness.create();

        await expectAsync(
            harness.navigateByUrl('/delta', DeltaComponent) // <-- Doesn't throw. Returns null instead.
        ).toBeRejectedWithError();
    });
});

Please provide a link to a minimal reproduction of the bug

https://github.com/reduckted/repro-router-testing-harness-bug

Please provide the exception or error you saw

N/A

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 16.2.7
Node: 18.18.2
Package Manager: npm 9.8.1
OS: win32 x64

Angular: 16.2.10
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1602.7
@angular-devkit/build-angular   16.2.7
@angular-devkit/core            16.2.7
@angular-devkit/schematics      16.2.7
@angular/cli                    16.2.7
@schematics/angular             16.2.7
rxjs                            7.8.1
typescript                      5.1.6
zone.js                         0.13.3

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routerarea: testingIssues related to Angular testing features, such as TestBed

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions