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

Chore/improve coverage #381

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### 3.0.2

- Improves coverage to 92.9%.

### 3.0.1

- pass along the error to the `navigatorBuilder` to allow for different
Expand Down
247 changes: 246 additions & 1 deletion go_router/test/go_router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/diagnostics.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';
import 'package:go_router/src/go_route_match.dart';
Expand Down Expand Up @@ -1396,6 +1395,240 @@ void main() {
});
});
});

group('GoRouterHelper', () {
final key = GlobalKey<_DummyStatefulWidgetState>();
final routes = [
GoRoute(
path: '/',
name: 'home',
builder: (context, state) => DummyStatefulWidget(key: key),
),
GoRoute(
path: '/page1',
name: 'page1',
builder: (context, state) => const Page1Screen(),
),
];

const name = 'page1';
final params = {
'a-param-key': 'a-param-value',
};
final queryParams = {'a-query-key': 'a-query-value'};
const location = '/page1';
const extra = 'Hello';

testWidgets('calls [namedLocation] on closest GoRouter', (tester) async {
final router = GoRouterNamedLocationSpy(routes: routes);
await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: router.routeInformationParser,
routerDelegate: router.routerDelegate,
title: 'GoRouter Example',
),
);
key.currentContext!.namedLocation(
name,
params: params,
queryParams: queryParams,
);
expect(router.name, router.name);
expect(router.params, params);
expect(router.queryParams, queryParams);
});

testWidgets('calls [go] on closest GoRouter', (tester) async {
final router = GoRouterGoSpy(routes: routes);
await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: router.routeInformationParser,
routerDelegate: router.routerDelegate,
title: 'GoRouter Example',
),
);
key.currentContext!.go(
location,
extra: extra,
);
expect(router.myLocation, location);
expect(router.extra, extra);
});

testWidgets('calls [goNamed] on closest GoRouter', (tester) async {
final router = GoRouterGoNamedSpy(routes: routes);
await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: router.routeInformationParser,
routerDelegate: router.routerDelegate,
title: 'GoRouter Example',
),
);
key.currentContext!.goNamed(
name,
params: params,
queryParams: queryParams,
extra: extra,
);
expect(router.name, name);
expect(router.params, params);
expect(router.queryParams, queryParams);
expect(router.extra, extra);
});

testWidgets('calls [push] on closest GoRouter', (tester) async {
final router = GoRouterPushSpy(routes: routes);
await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: router.routeInformationParser,
routerDelegate: router.routerDelegate,
title: 'GoRouter Example',
),
);
key.currentContext!.push(
location,
extra: extra,
);
expect(router.myLocation, location);
expect(router.extra, extra);
});

testWidgets('calls [pushNamed] on closest GoRouter', (tester) async {
final router = GoRouterPushNamedSpy(routes: routes);
await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: router.routeInformationParser,
routerDelegate: router.routerDelegate,
title: 'GoRouter Example',
),
);
key.currentContext!.pushNamed(
name,
params: params,
queryParams: queryParams,
extra: extra,
);
expect(router.name, name);
expect(router.params, params);
expect(router.queryParams, queryParams);
expect(router.extra, extra);
});

testWidgets('calls [pop] on closest GoRouter', (tester) async {
final router = GoRouterPopSpy(routes: routes);
await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: router.routeInformationParser,
routerDelegate: router.routerDelegate,
title: 'GoRouter Example',
),
);
key.currentContext!.pop();
expect(router.popped, true);
});
});
}

class GoRouterNamedLocationSpy extends GoRouter {
GoRouterNamedLocationSpy({required List<GoRoute> routes})
: super(routes: routes);

String? name;
Map<String, String>? params;
Map<String, String>? queryParams;

@override
String namedLocation(
String name, {
Map<String, String> params = const {},
Map<String, String> queryParams = const {},
}) {
this.name = name;
this.params = params;
this.queryParams = queryParams;
return '';
}
}

class GoRouterGoSpy extends GoRouter {
GoRouterGoSpy({required List<GoRoute> routes}) : super(routes: routes);

String? myLocation;
Object? extra;

@override
void go(String location, {Object? extra}) {
myLocation = location;
this.extra = extra;
}
}

class GoRouterGoNamedSpy extends GoRouter {
GoRouterGoNamedSpy({required List<GoRoute> routes}) : super(routes: routes);

String? name;
Map<String, String>? params;
Map<String, String>? queryParams;
Object? extra;

@override
void goNamed(
String name, {
Map<String, String> params = const {},
Map<String, String> queryParams = const {},
Object? extra,
}) {
this.name = name;
this.params = params;
this.queryParams = queryParams;
this.extra = extra;
}
}

class GoRouterPushSpy extends GoRouter {
GoRouterPushSpy({required List<GoRoute> routes}) : super(routes: routes);

String? myLocation;
Object? extra;

@override
void push(String location, {Object? extra}) {
myLocation = location;
this.extra = extra;
}
}

class GoRouterPushNamedSpy extends GoRouter {
GoRouterPushNamedSpy({required List<GoRoute> routes}) : super(routes: routes);

String? name;
Map<String, String>? params;
Map<String, String>? queryParams;
Object? extra;

@override
void pushNamed(
String name, {
Map<String, String> params = const {},
Map<String, String> queryParams = const {},
Object? extra,
}) {
this.name = name;
this.params = params;
this.queryParams = queryParams;
this.extra = extra;
}
}

class GoRouterPopSpy extends GoRouter {
GoRouterPopSpy({required List<GoRoute> routes}) : super(routes: routes);

bool popped = false;

@override
void pop() {
popped = true;
}
}

class MockGoRouterRefreshStream extends GoRouterRefreshStream {
Expand Down Expand Up @@ -1563,3 +1796,15 @@ class DummyBuildContext implements BuildContext {
@override
Widget get widget => throw UnimplementedError();
}

class DummyStatefulWidget extends StatefulWidget {
const DummyStatefulWidget({Key? key}) : super(key: key);

@override
State<DummyStatefulWidget> createState() => _DummyStatefulWidgetState();
}

class _DummyStatefulWidgetState extends State<DummyStatefulWidget> {
@override
Widget build(BuildContext context) => Container();
}
53 changes: 53 additions & 0 deletions go_router/test/src/custom_transition_page_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';

import '../go_router_test.dart';

void main() {
testWidgets('CustomTransitionPage builds its child using transitionsBuilder',
(tester) async {
const child = HomeScreen();
final transition = CustomTransitionPage<void>(
transitionsBuilder: expectAsync4((_, __, ___, child) => child),
child: child,
);
final router = GoRouter(
routes: [
GoRoute(
path: '/',
pageBuilder: (_, __) => transition,
),
],
);
await tester.pumpWidget(
MaterialApp.router(
routeInformationParser: router.routeInformationParser,
routerDelegate: router.routerDelegate,
title: 'GoRouter Example',
),
);
expect(find.byWidget(child), findsOneWidget);
});

test('NoTransitionPage does not apply any transition', () {
const homeScreen = HomeScreen();
const page = NoTransitionPage<void>(child: homeScreen);
const primaryAnimation = AlwaysStoppedAnimation<double>(0);
const secondaryAnimation = AlwaysStoppedAnimation<double>(1);
final widget = page.transitionsBuilder(
DummyBuildContext(),
primaryAnimation,
secondaryAnimation,
homeScreen,
);
expect(widget, homeScreen);
});
}

class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) => Container();
}
30 changes: 30 additions & 0 deletions go_router/test/src/go_route_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';
import 'package:go_router/src/go_router_delegate.dart';

import '../go_router_test.dart';

GoRouterDelegate createGoRouterDelegate() => GoRouter(
initialLocation: '/',
routes: [
GoRoute(path: '/', builder: (_, __) => Container()),
],
).routerDelegate;

void main() {
test('throws when a builder is not set', () {
final delegate = createGoRouterDelegate();
final route = GoRoute(path: '/');
void build() => route.builder(
DummyBuildContext(),
GoRouterState(
delegate,
location: '/foo',
subloc: '/bar',
name: 'baz',
),
);
expect(build, throwsException);
});
}
Loading