Skip to content

Commit

Permalink
fix(tests): go_router tests
Browse files Browse the repository at this point in the history
Signed-off-by: Isaac Obella <wizlif@users.noreply.github.com>
  • Loading branch information
wizlif committed May 24, 2023
1 parent 69fddb1 commit 202379b
Show file tree
Hide file tree
Showing 26 changed files with 190 additions and 230 deletions.
1 change: 0 additions & 1 deletion lib/presentation/auth/unauthenticated_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

Expand Down
1 change: 0 additions & 1 deletion lib/presentation/auth/widgets/profile_photo.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:io';


import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
Expand Down
3 changes: 0 additions & 3 deletions lib/presentation/profile/widget/signup_cta.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import '../../../core/core.dart';
import '../../../domain/user/user.dart';

import '../../routes/app_routes.dart';
import '../../shared_widgets/pill_button.dart';
import '../../themes/constants.dart';
Expand Down
43 changes: 15 additions & 28 deletions lib/presentation/routes/app_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import '../shared_widgets/web_view_page.dart';
part 'app_pages.dart';

class AppRouter {
// final ISettingsRepository settingsRepository;
final GlobalKey<NavigatorState> _rootNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'root');

Expand Down Expand Up @@ -55,21 +54,20 @@ class AppRouter {
]),
StatefulShellBranch(routes: [
GoRoute(
path: AppPage.demoPage.path,
pageBuilder: (_, __) => NoTransitionPage(child: DemoPage()),
routes: [
GoRoute(
path: 'components',
parentNavigatorKey: _rootNavigatorKey,
builder: (_, __) => const ComponentsDemoPage(),
),
GoRoute(
path: 'comments',
parentNavigatorKey: _rootNavigatorKey,
builder: (_, __) => const CrowdActionCommentsPage(),
),
]
)
path: AppPage.demoPage.path,
pageBuilder: (_, __) => NoTransitionPage(child: DemoPage()),
routes: [
GoRoute(
path: 'components',
parentNavigatorKey: _rootNavigatorKey,
builder: (_, __) => const ComponentsDemoPage(),
),
GoRoute(
path: 'comments',
parentNavigatorKey: _rootNavigatorKey,
builder: (_, __) => const CrowdActionCommentsPage(),
),
])
])
],
),
Expand All @@ -84,7 +82,6 @@ class AppRouter {
path: '${AppPage.crowdActionDetails.path}/:id',
parentNavigatorKey: _rootNavigatorKey,
builder: (BuildContext context, GoRouterState state) {

return CrowdActionDetailsPage(
crowdActionId: state.pathParameters['id'],
);
Expand Down Expand Up @@ -148,17 +145,7 @@ class AppRouter {
],
debugLogDiagnostics: true,
initialLocation: AppPage.home.path,
// redirect: (context, state) async {
// final isNotOnBoarded = state.location != AppPage.onBoarding.path &&
// !(await settingsRepository.getWasUserOnboarded());
//
// if (isNotOnBoarded) {
// return AppPage.onBoarding.path;
// }
//
// return null;
// },
);

AppRouter(/*this.settingsRepository*/);
AppRouter();
}
20 changes: 10 additions & 10 deletions test/presentation/auth/profile_photo_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:io';

import 'package:auto_route/auto_route.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:collaction_app/application/auth/auth_bloc.dart';
import 'package:collaction_app/application/user/avatar/avatar_bloc.dart';
Expand All @@ -12,6 +11,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';
import 'package:go_router/go_router.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mocktail/mocktail.dart';

Expand All @@ -23,7 +23,7 @@ import '../shared_widgets/photo_selector_test.dart';
part 'profile_photo_test.ext.dart';

void main() {
late StackRouter stackRouter;
late GoRouter goRouter;

late AvatarBloc avatarBloc;
late AuthBloc authBloc;
Expand All @@ -34,7 +34,7 @@ void main() {
MethodChannel('plugins.hunghd.vn/image_cropper');

setUpAll(() {
stackRouter = RouteHelpers.setUpRouterStubs();
goRouter = RouteHelpers.setUpRouterStubs();

// Avatar Bloc
avatarBloc = MockAvatarBloc();
Expand Down Expand Up @@ -72,7 +72,7 @@ void main() {

group('SelectProfilePhoto tests:', () {
testWidgets('can render', (WidgetTester tester) async {
await tester.pumpSelectProfilePhoto(authBloc, stackRouter, onSkip: () {});
await tester.pumpSelectProfilePhoto(authBloc, goRouter, onSkip: () {});
await tester.pumpAndSettle();

expect(find.byType(SelectProfilePhoto), findsOneWidget);
Expand All @@ -91,7 +91,7 @@ void main() {
bool onSkipCalled = false;
await tester.pumpSelectProfilePhoto(
authBloc,
stackRouter,
goRouter,
onSkip: () => onSkipCalled = true,
);
await tester.tap(find.text('Maybe later'));
Expand All @@ -102,7 +102,7 @@ void main() {

testWidgets('open PhotoSelection, select photo, adds event to AuthBloc',
(WidgetTester tester) async {
await tester.pumpSelectProfilePhoto(authBloc, stackRouter, onSkip: () {});
await tester.pumpSelectProfilePhoto(authBloc, goRouter, onSkip: () {});

expect(
tester.firstWidget<PillButton>(find.byType(PillButton)).isEnabled,
Expand All @@ -129,21 +129,21 @@ void main() {
verify(() => authBloc.add(any())).called(1);
});

testWidgets('stackRouter pops on uploadSuccess event',
testWidgets('goRouter pops on uploadSuccess event',
(WidgetTester tester) async {
whenListen(
avatarBloc,
Stream.fromIterable([AvatarState.uploadSuccess()]),
);
await tester.pumpSelectProfilePhoto(authBloc, stackRouter, onSkip: () {});
verify(() => stackRouter.pop()).called(1);
await tester.pumpSelectProfilePhoto(authBloc, goRouter, onSkip: () {});
verify(() => goRouter.pop()).called(1);
});

testWidgets('PillButton isLoading', (WidgetTester tester) async {
when(() => authBloc.state).thenAnswer(
(_) => AuthState.awaitingPhotoUpdate(),
);
await tester.pumpSelectProfilePhoto(authBloc, stackRouter, onSkip: () {});
await tester.pumpSelectProfilePhoto(authBloc, goRouter, onSkip: () {});
expect(
tester.firstWidget<PillButton>(find.byType(PillButton)).isLoading,
true,
Expand Down
8 changes: 5 additions & 3 deletions test/presentation/auth/profile_photo_test.ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ part of 'profile_photo_test.dart';

extension WidgetX on WidgetTester {
Future<void> pumpSelectProfilePhoto(
AuthBloc authBloc, StackRouter stackRouter,
{required Function() onSkip}) async {
AuthBloc authBloc,
GoRouter router, {
required Function() onSkip,
}) async {
await pumpWidget(
BlocProvider<AuthBloc>(
create: (context) => authBloc,
Expand All @@ -13,7 +15,7 @@ extension WidgetX on WidgetTester {
onSkip: onSkip,
),
),
).withRouterScope(stackRouter),
).withRouterScope(router),
),
);
}
Expand Down
23 changes: 10 additions & 13 deletions test/presentation/auth/verified_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:auto_route/auto_route.dart';
import 'package:collaction_app/application/user/profile/profile_bloc.dart';
import 'package:collaction_app/presentation/auth/widgets/verified.dart';
import 'package:collaction_app/presentation/routes/app_routes.dart';
import 'package:collaction_app/presentation/shared_widgets/pill_button.dart';
import 'package:collaction_app/presentation/routes/app_routes.gr.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';
import 'package:go_router/go_router.dart';
import 'package:mocktail/mocktail.dart';

import '../../application/user/profile/profile_bloc.mocks.dart';
Expand All @@ -15,15 +15,12 @@ import '../router.mocks.dart';
part 'verified_test.ext.dart';

void main() {
late StackRouter stackRouter;
late GoRouter goRouter;

late ProfileBloc profileBloc;

setUpAll(() {
stackRouter = RouteHelpers.setUpRouterStubs();
when(() => stackRouter.replaceAll([const HomeRoute()])).thenAnswer(
(_) async {},
);
goRouter = RouteHelpers.setUpRouterStubs();

// Profile Bloc
profileBloc = MockProfileBloc();
Expand All @@ -37,7 +34,7 @@ void main() {

group('VerifiedPage tests:', () {
testWidgets('can render', (WidgetTester tester) async {
await tester.pumpVerifiedPage(stackRouter);
await tester.pumpVerifiedPage(goRouter);
await tester.pump();

expect(find.byType(VerifiedPage), findsOneWidget);
Expand All @@ -47,26 +44,26 @@ void main() {
when(() => profileBloc.state).thenAnswer(
(_) => ProfileState(userProfile: testUserProfile),
);
await tester.pumpVerifiedPage(stackRouter);
await tester.pumpVerifiedPage(goRouter);
await tester.pump();

expect(find.text(testUserProfile.profile.fullName), findsOneWidget);
});

testWidgets('PillButton redirects to home', (WidgetTester tester) async {
await tester.pumpVerifiedPage(stackRouter);
await tester.pumpVerifiedPage(goRouter);
await tester.tap(find.byType(PillButton));
await tester.pump();

verify(() => stackRouter.replaceAll([const HomeRoute()])).called(1);
verify(() => goRouter.go(AppPage.home.path)).called(1);
});

testWidgets('TextButton redirects to home', (WidgetTester tester) async {
await tester.pumpVerifiedPage(stackRouter);
await tester.pumpVerifiedPage(goRouter);
await tester.tap(find.byType(TextButton));
await tester.pump();

verify(() => stackRouter.replaceAll([const HomeRoute()])).called(1);
verify(() => goRouter.go(AppPage.home.path)).called(1);
});
});
}
4 changes: 2 additions & 2 deletions test/presentation/auth/verified_test.ext.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
part of 'verified_test.dart';

extension WidgetX on WidgetTester {
Future<void> pumpVerifiedPage(StackRouter stackRouter) async {
Future<void> pumpVerifiedPage(GoRouter goRouter) async {
await pumpWidget(
MaterialApp(
home: Scaffold(
body: VerifiedPage(),
),
).withRouterScope(stackRouter),
).withRouterScope(goRouter),
);
}
}
Loading

0 comments on commit 202379b

Please sign in to comment.