Skip to content

Commit

Permalink
Completed auth flow test
Browse files Browse the repository at this point in the history
  • Loading branch information
bizz84 committed Nov 6, 2024
1 parent 874d1c8 commit 31d8a84
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@ void main() {
final r = Robot(tester);
await r.pumpMyApp();
r.expectFindAllProductCards();
await r.openPopupMenu();
await r.auth.openEmailPasswordSignInScreen();
await r.auth.signInWithEmailAndPassword();
r.expectFindAllProductCards();
await r.openPopupMenu();
await r.auth.openAccountScreen();
await r.auth.tapLogoutButton();
await r.auth.tapDialogLogoutButton();
r.expectFindAllProductCards();
});
}
23 changes: 22 additions & 1 deletion ecommerce_app/test/src/features/authentication/auth_robot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:ecommerce_app/src/features/authentication/data/fake_auth_reposit
import 'package:ecommerce_app/src/features/authentication/presentation/account/account_screen.dart';
import 'package:ecommerce_app/src/features/authentication/presentation/sign_in/email_password_sign_in_screen.dart';
import 'package:ecommerce_app/src/features/authentication/presentation/sign_in/email_password_sign_in_state.dart';
import 'package:ecommerce_app/src/features/products/presentation/home_app_bar/more_menu_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
Expand All @@ -12,6 +13,13 @@ class AuthRobot {
AuthRobot(this.tester);
final WidgetTester tester;

Future<void> openEmailPasswordSignInScreen() async {
final finder = find.byKey(MoreMenuButton.signInKey);
expect(finder, findsOneWidget);
await tester.tap(finder);
await tester.pumpAndSettle();
}

Future<void> pumpEmailPasswordSignInContents({
required FakeAuthRepository authRepository,
required EmailPasswordSignInFormType formType,
Expand All @@ -38,7 +46,7 @@ class AuthRobot {
final primaryButton = find.byType(PrimaryButton);
expect(primaryButton, findsOneWidget);
await tester.tap(primaryButton);
await tester.pump();
await tester.pumpAndSettle();
}

Future<void> enterEmail(String email) async {
Expand All @@ -53,6 +61,19 @@ class AuthRobot {
await tester.enterText(passwordField, password);
}

Future<void> signInWithEmailAndPassword() async {
await enterEmail('[email protected]');
await enterPassword('test1234');
await tapEmailAndPasswordSubmitButton();
}

Future<void> openAccountScreen() async {
final finder = find.byKey(MoreMenuButton.accountKey);
expect(finder, findsOneWidget);
await tester.tap(finder);
await tester.pumpAndSettle();
}

Future<void> pumpAccountScreen({FakeAuthRepository? authRepository}) async {
await tester.pumpWidget(
ProviderScope(
Expand Down
18 changes: 17 additions & 1 deletion ecommerce_app/test/src/robot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import 'package:ecommerce_app/src/app.dart';
import 'package:ecommerce_app/src/constants/test_products.dart';
import 'package:ecommerce_app/src/features/authentication/data/fake_auth_repository.dart';
import 'package:ecommerce_app/src/features/products/data/fake_products_repository.dart';
import 'package:ecommerce_app/src/features/products/presentation/home_app_bar/more_menu_button.dart';
import 'package:ecommerce_app/src/features/products/presentation/products_list/product_card.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';

import 'features/authentication/auth_robot.dart';

class Robot {
Robot(this.tester);
Robot(this.tester) : auth = AuthRobot(tester);
final WidgetTester tester;
final AuthRobot auth;

Future<void> pumpMyApp() async {
// Override repositories
Expand All @@ -30,4 +34,16 @@ class Robot {
final finder = find.byType(ProductCard);
expect(finder, findsNWidgets(kTestProducts.length));
}

Future<void> openPopupMenu() async {
final finder = find.byType(MoreMenuButton);
final matches = finder.evaluate();
// if an item is found, it means that we're running
// on a small window and can tap to reveal the menu
if (matches.isNotEmpty) {
await tester.tap(finder);
await tester.pumpAndSettle();
}
// else no-op, as the items are already visible
}
}

0 comments on commit 31d8a84

Please sign in to comment.