Skip to content

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

Closed

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

Activity

  1. JeanMeche commented on Oct 24, 2023

    @JeanMeche
    Member

    I'd say this is expected.

    Your situation falls under this :

    * When testing `Routes` with a guards that reject the navigation, the `RouterOutlet` might not be
    * activated and the `activatedComponent` may be `null`.

  2. added
    area: testing
    on Oct 24, 2023
  3. added this to the needsTriage milestone on Oct 24, 2023
  4. added a commit that references this issue on Oct 24, 2023
    9251b89
  5. reduckted commented on Oct 24, 2023

    @reduckted
    Author

    I'd say this is expected.

    Your situation falls under this :

    No, that's the overload that does not take a required routed component type.

  6. added 2 commits that reference this issue on Oct 25, 2023
    b79b4ac
    6858149
  7. angular-automatic-lock-bot commented on Nov 25, 2023

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  8. locked and limited conversation to collaborators on Nov 25, 2023
  9. added a commit that references this issue on Jan 23, 2024
    db556d4
  10. added a commit that references this issue on Jan 26, 2024
    0037c21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routerarea: testing

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions