Skip to content

Commit

Permalink
fix(router): RouterTestingHarness should throw if a component is ex…
Browse files Browse the repository at this point in the history
…pected but navigation fails (#52357)

The `RouterTestingHarness` should throw an error if the call to `navigateByUrl`
expects a component to be activated but the navigation failed.

fixes #52344

PR Close #52357
  • Loading branch information
atscott authored and dylhunn committed Oct 25, 2023
1 parent 019a0f4 commit b79b4ac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/router/testing/src/router_testing_harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ export class RouterTestingHarness {
}
return activatedComponent as T;
} else {
if (requiredRoutedComponentType !== undefined) {
throw new Error(`Unexpected routed component type. Expected ${
requiredRoutedComponentType.name} but the navigation did not activate any component.`);
}
return null;
}
}
Expand Down
14 changes: 14 additions & 0 deletions packages/router/testing/test/router_testing_harness.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ describe('navigateForTest', () => {
await expectAsync(harness.navigateByUrl('/123', OtherCmp)).toBeRejected();
});

it('throws an error if navigation fails but expected a component instance', async () => {
@Component({standalone: true, template: ''})
class TestCmp {
}

TestBed.configureTestingModule({
providers: [
provideRouter([{path: '**', canActivate: [() => false], component: TestCmp}]),
]
});
const harness = await RouterTestingHarness.create();
await expectAsync(harness.navigateByUrl('/123', TestCmp)).toBeRejected();
});

it('waits for redirects using router.navigate', async () => {
@Component({standalone: true, template: 'test'})
class TestCmp {
Expand Down

0 comments on commit b79b4ac

Please sign in to comment.