|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_template/core/common/network_exceptions.dart'; |
| 3 | +import 'package:flutter_template/core/di/di_provider.dart'; |
| 4 | +import 'package:flutter_template/core/model/service/auth_models.dart'; |
| 5 | +import 'package:flutter_template/core/model/user.dart'; |
| 6 | +import 'package:flutter_template/main.dart' as app; |
| 7 | +import 'package:flutter_template/ui/extensions/context_extensions.dart'; |
| 8 | +import 'package:flutter_template/ui/signin/signin_screen.dart'; |
| 9 | +import 'package:flutter_template/ui/welcome/welcome_screen.dart'; |
| 10 | +import 'package:integration_test/integration_test.dart'; |
| 11 | +import 'package:flutter_test/flutter_test.dart'; |
| 12 | +import 'package:mocktail/mocktail.dart'; |
| 13 | + |
| 14 | +import '../common/general_helpers.dart'; |
| 15 | +import '../common/repository_mocks.dart'; |
| 16 | + |
| 17 | +void main() { |
| 18 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 19 | + late MockAuthRemoteSource mockAuthRemoteSource; |
| 20 | + late MockProjectRemoteSource mockProjectRemoteSource; |
| 21 | + |
| 22 | + setUp(() async { |
| 23 | + mockAuthRemoteSource = MockAuthRemoteSource(); |
| 24 | + mockProjectRemoteSource = MockProjectRemoteSource(); |
| 25 | + await commonSetup( |
| 26 | + mockAuthRemoteSource: mockAuthRemoteSource, |
| 27 | + mockProjectRemoteSource: mockProjectRemoteSource, |
| 28 | + ); |
| 29 | + }); |
| 30 | + |
| 31 | + tearDown(() { |
| 32 | + DiProvider.instance.reset(); |
| 33 | + }); |
| 34 | + group('SignIn screen tests', () { |
| 35 | + const email = "[email protected]"; |
| 36 | + const password = "test"; |
| 37 | + |
| 38 | + testWidgets('SignIn with wrong credentials', (WidgetTester tester) async { |
| 39 | + when(() => mockAuthRemoteSource.signIn(email, password)).thenAnswer( |
| 40 | + (_) => Future.error( |
| 41 | + const NetworkException.unauthorizedRequest("Unauthorized"), |
| 42 | + ), |
| 43 | + ); |
| 44 | + |
| 45 | + await tester.pumpWidget(const app.MyApp()); |
| 46 | + await tester.pumpAndSettle(); |
| 47 | + |
| 48 | + final context = tester.contextOfType<SignInScreen>(); |
| 49 | + await tester.enterText( |
| 50 | + find.widgetWithText(TextField, context.localizations.mail), |
| 51 | + email, |
| 52 | + ); |
| 53 | + await tester.enterText( |
| 54 | + find.widgetWithText(TextField, context.localizations.password), |
| 55 | + password, |
| 56 | + ); |
| 57 | + await tester.tap( |
| 58 | + find.widgetWithText(TextButton, context.localizations.sign_in), |
| 59 | + ); |
| 60 | + await tester.pumpAndSettle(); |
| 61 | + expect(find.byType(AlertDialog), findsOneWidget); |
| 62 | + }); |
| 63 | + testWidgets('SignIn with correct credentials', (WidgetTester tester) async { |
| 64 | + when(() => mockAuthRemoteSource.signIn(email, password)).thenAnswer( |
| 65 | + (_) => Future.value( |
| 66 | + SignInResponse( |
| 67 | + accessToken: "testing-token", |
| 68 | + user : User(email : "[email protected]"), |
| 69 | + ), |
| 70 | + ), |
| 71 | + ); |
| 72 | + when(() => mockProjectRemoteSource.getProjects()) |
| 73 | + .thenAnswer((_) => Future.value([])); |
| 74 | + |
| 75 | + await tester.pumpWidget(const app.MyApp()); |
| 76 | + await tester.pumpAndSettle(); |
| 77 | + |
| 78 | + final context = tester.contextOfType<SignInScreen>(); |
| 79 | + await tester.enterText( |
| 80 | + find.widgetWithText(TextField, context.localizations.mail), |
| 81 | + email, |
| 82 | + ); |
| 83 | + await tester.enterText( |
| 84 | + find.widgetWithText(TextField, context.localizations.password), |
| 85 | + password, |
| 86 | + ); |
| 87 | + await tester |
| 88 | + .tap(find.widgetWithText(TextButton, context.localizations.sign_in)); |
| 89 | + await tester.pumpAndSettle(); |
| 90 | + expect(find.byType(WelcomeScreen), findsOneWidget); |
| 91 | + }); |
| 92 | + }); |
| 93 | +} |
0 commit comments