Skip to content
This repository was archived by the owner on Sep 16, 2022. It is now read-only.

Commit 6ded0a0

Browse files
leonsenftmatanlurey
authored andcommitted
fix(Router): fail navigation to '/' if there's no matching route
Fixes #1389. Closes #1390 PiperOrigin-RevId: 200572462
1 parent f3d12e9 commit 6ded0a0

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

angular_router/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
* Added `canNavigate` to `RouterHook`.
66

7+
* Navigation will no longer succeed for an empty path if it doesn't match a
8+
route.
9+
710
## 2.0.0-alpha+13
811

912
### New features

angular_router/lib/src/router/router_impl.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ class RouterImpl extends Router {
182182
}
183183

184184
MutableRouterState nextState = await _resolveState(path, navigationParams);
185-
if (nextState == null) {
185+
// In the event that `path` is empty and doesn't match any routes,
186+
// `_resolveState` will return a state with no routes, instead of null.
187+
if (nextState == null || nextState.routes.isEmpty) {
186188
return NavigationResult.INVALID_ROUTE;
187189
}
188190

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@TestOn('browser')
2+
import 'package:angular/angular.dart';
3+
import 'package:angular_router/angular_router.dart';
4+
import 'package:angular_router/testing.dart';
5+
import 'package:angular_test/angular_test.dart';
6+
import 'package:test/test.dart';
7+
8+
// ignore: uri_has_not_been_generated
9+
import '1389_empty_path_test.template.dart' as ng;
10+
11+
void main() {
12+
tearDown(disposeAnyRunningTest);
13+
14+
test('navigation to empty path should fail', () async {
15+
final testBed =
16+
NgTestBed.forComponent<TestComponent>(ng.TestComponentNgFactory);
17+
final testFixture = await testBed.create();
18+
final router = testFixture.assertOnlyInstance.router;
19+
final result = await router.navigate('/');
20+
expect(result, NavigationResult.INVALID_ROUTE);
21+
});
22+
}
23+
24+
@Component(
25+
selector: 'test',
26+
template: '',
27+
providers: routerProvidersTest,
28+
)
29+
class TestComponent {
30+
final Router router;
31+
32+
TestComponent(this.router);
33+
}

0 commit comments

Comments
 (0)