Skip to content

Commit dec269e

Browse files
Philclaude
andcommitted
test: guard a guard rejecting the first navigation (flutter/flutter#189582)
Open the app at a link whose guard rejects it: the app starts with no error, on an empty stack. A guard that rejects and opens a fallback page with replaceAll or push shows that page. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8d9334f commit dec269e

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'dart:async';
2+
3+
import 'package:auto_route/auto_route.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
import 'package:material_ui/material_ui.dart';
6+
7+
import '../build_counts.dart';
8+
9+
// flutter/flutter#189582: a guard that stopped the app's first navigation
10+
// crashed the app on start, in an assertion.
11+
// Open the app at /protected, whose guard rejects it: the app starts with no
12+
// error, on an empty stack. The same with a guard that rejects and opens
13+
// /fallback instead, with replaceAll or push: the app shows FallbackRoute.
14+
Widget _page(String name) => Scaffold(body: Text(name));
15+
16+
void main() {
17+
RootStackRouter buildRouter(void Function(NavigationResolver resolver, StackRouter router) onProtected) => .build(
18+
routes: [
19+
NamedRouteDef(name: 'HomeRoute', path: '/', builder: (_, _) => _page('HomeRoute')),
20+
NamedRouteDef(
21+
name: 'ProtectedRoute',
22+
path: '/protected',
23+
guards: [AutoRouteGuard.simple(onProtected)],
24+
builder: (_, _) => _page('ProtectedRoute'),
25+
),
26+
NamedRouteDef(name: 'FallbackRoute', path: '/fallback', builder: (_, _) => _page('FallbackRoute')),
27+
],
28+
);
29+
30+
testWidgets('A guard rejecting the first navigation starts the app without an error', (tester) async {
31+
final router = buildRouter((resolver, _) => resolver.reject());
32+
await pumpAtDeepLink(tester, router, '/protected', includePrefixMatches: false);
33+
expect(tester.takeException(), isNull);
34+
expect(router.stack, isEmpty);
35+
expect(find.text('ProtectedRoute'), findsNothing);
36+
});
37+
38+
for (final how in ['replaceAll', 'push']) {
39+
testWidgets('A guard rejecting the first navigation can open another page with $how', (tester) async {
40+
final router = buildRouter((resolver, router) {
41+
resolver.reject();
42+
const fallback = NamedRoute('FallbackRoute');
43+
unawaited(how == 'push' ? router.push(fallback) : router.replaceAll([fallback]));
44+
});
45+
await pumpAtDeepLink(tester, router, '/protected', includePrefixMatches: false);
46+
expect(tester.takeException(), isNull);
47+
expect(router.stack.map((p) => p.name), ['FallbackRoute']);
48+
expect(find.text('FallbackRoute'), findsOneWidget);
49+
expect(router.currentUrl, '/fallback');
50+
});
51+
}
52+
}

0 commit comments

Comments
 (0)