Skip to content

Commit

Permalink
Add RouteState.pop()
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpryan committed Mar 27, 2022
1 parent c68f82f commit 5ef0767
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/src/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class RouteState extends ChangeNotifier {
_globalRouteState.goTo(path, parentRoute: route);
}

void pop() {
_globalRouteState.pop();
}

Map<String, String> get queryParameters =>
_globalRouteState.match.parameters.query;

Expand Down
54 changes: 49 additions & 5 deletions test/route_state_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import 'package:tree_router/src/parser.dart';
import 'package:tree_router/tree_router.dart';

void main() {
group('RouteState.of', () {
testWidgets('Returns a route state object', (WidgetTester tester) async {
group('RouteState', () {
testWidgets('.of() Returns a route state object',
(WidgetTester tester) async {
late final BuildContext childContext;
final routes = [
StackedRoute(
Expand All @@ -32,10 +33,9 @@ void main() {

expect(RouteState.of(childContext), isNotNull);
});
});

group('RouteState.goTo()', () {
testWidgets('Navigates to the correct screen', (WidgetTester tester) async {
testWidgets('goTo() Navigates to the correct screen',
(WidgetTester tester) async {
late final BuildContext childContext;
final routes = [
StackedRoute(
Expand Down Expand Up @@ -71,6 +71,50 @@ void main() {
expect(find.text('Screen A'), findsOneWidget);
});

testWidgets('pop() Navigates to the correct screen',
(WidgetTester tester) async {
BuildContext? childContext;
final routes = [
StackedRoute(
path: '/',
builder: (context) {
return Builder(
builder: (BuildContext context) {
childContext ??= context;
return const Text('Home');
},
);
},
children: [
StackedRoute(
path: 'a',
builder: (context) {
return const Text('Screen A');
},
),
],
),
];

await tester.pumpWidget(
_TestApp(
routes: routes,
),
);

expect(find.text('Home'), findsOneWidget);
final routeState = RouteState.of(childContext!);
if (routeState == null) fail('RouteState.of() returned null.');

routeState.goTo('/a');
await tester.pumpAndSettle();
expect(find.text('Screen A'), findsOneWidget);

routeState.pop();
await tester.pumpAndSettle();
expect(find.text('Home'), findsOneWidget);
});

testWidgets(
'Navigates to the correct screen when provided with a'
' relative route path', (WidgetTester tester) async {
Expand Down

0 comments on commit 5ef0767

Please sign in to comment.