Skip to content

Commit 0037c21

Browse files
atscottdylhunn
authored andcommitted
fix(router): RouterTestingHarness should throw if a component is expected but navigation fails (angular#52357)
The `RouterTestingHarness` should throw an error if the call to `navigateByUrl` expects a component to be activated but the navigation failed. fixes angular#52344 PR Close angular#52357
1 parent 8ee0f27 commit 0037c21

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

packages/router/testing/src/router_testing_harness.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ export class RouterTestingHarness {
155155
}
156156
return activatedComponent as T;
157157
} else {
158+
if (requiredRoutedComponentType !== undefined) {
159+
throw new Error(`Unexpected routed component type. Expected ${
160+
requiredRoutedComponentType.name} but the navigation did not activate any component.`);
161+
}
158162
return null;
159163
}
160164
}

packages/router/testing/test/router_testing_harness.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ describe('navigateForTest', () => {
100100
await expectAsync(harness.navigateByUrl('/123', OtherCmp)).toBeRejected();
101101
});
102102

103+
it('throws an error if navigation fails but expected a component instance', async () => {
104+
@Component({standalone: true, template: ''})
105+
class TestCmp {
106+
}
107+
108+
TestBed.configureTestingModule({
109+
providers: [
110+
provideRouter([{path: '**', canActivate: [() => false], component: TestCmp}]),
111+
]
112+
});
113+
const harness = await RouterTestingHarness.create();
114+
await expectAsync(harness.navigateByUrl('/123', TestCmp)).toBeRejected();
115+
});
116+
103117
it('waits for redirects using router.navigate', async () => {
104118
@Component({standalone: true, template: 'test'})
105119
class TestCmp {

0 commit comments

Comments
 (0)