|
| 1 | +import 'package:firebase_auth/firebase_auth.dart'; |
| 2 | +import 'package:firebase_ui_auth/firebase_ui_auth.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:mockito/mockito.dart'; |
| 6 | + |
| 7 | +import '../test_utils.dart'; |
| 8 | + |
| 9 | +class MockFirebaseAuth extends Mock implements FirebaseAuth { |
| 10 | + @override |
| 11 | + Future<void> sendPasswordResetEmail({ |
| 12 | + String? email, |
| 13 | + ActionCodeSettings? actionCodeSettings, |
| 14 | + }) { |
| 15 | + return super.noSuchMethod( |
| 16 | + Invocation.method( |
| 17 | + #sendPasswordResetEmail, |
| 18 | + [], |
| 19 | + { |
| 20 | + #email: email, |
| 21 | + #actionCodeSettings: actionCodeSettings, |
| 22 | + }, |
| 23 | + ), |
| 24 | + returnValue: Future.value(), |
| 25 | + returnValueForMissingStub: Future.value(), |
| 26 | + ); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +void main() { |
| 31 | + group('Forgot password view', () { |
| 32 | + late Widget widget; |
| 33 | + late MockFirebaseAuth auth; |
| 34 | + |
| 35 | + setUpAll(() { |
| 36 | + auth = MockFirebaseAuth(); |
| 37 | + |
| 38 | + widget = TestMaterialApp( |
| 39 | + child: ForgotPasswordView(auth: auth), |
| 40 | + ); |
| 41 | + |
| 42 | + when(auth.sendPasswordResetEmail(email: 'invalid@email')).thenThrow( |
| 43 | + FirebaseAuthException( |
| 44 | + message: 'invalid-email', |
| 45 | + code: 'invalid-email', |
| 46 | + ), |
| 47 | + ); |
| 48 | + |
| 49 | + when(auth. sendPasswordResetEmail(email : '[email protected]')). thenAnswer( |
| 50 | + (_) => Future.value(), |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + testWidgets('shows error if sendPasswordResetEmail failed', (tester) async { |
| 55 | + await tester.pumpWidget(widget); |
| 56 | + |
| 57 | + final input = find.byType(TextField); |
| 58 | + await tester.enterText(input, 'invalid@email'); |
| 59 | + |
| 60 | + await tester.testTextInput.receiveAction(TextInputAction.done); |
| 61 | + await tester.pumpAndSettle(); |
| 62 | + |
| 63 | + expect(find.text('invalid-email'), findsOneWidget); |
| 64 | + }); |
| 65 | + |
| 66 | + testWidgets( |
| 67 | + "doesn't show ana error on next successful attempt after error", |
| 68 | + (tester) async { |
| 69 | + await tester.pumpWidget(widget); |
| 70 | + |
| 71 | + final input = find.byType(TextField); |
| 72 | + await tester. enterText(input, '[email protected]'); |
| 73 | + |
| 74 | + await tester.testTextInput.receiveAction(TextInputAction.done); |
| 75 | + await tester.pumpAndSettle(); |
| 76 | + |
| 77 | + expect(find.byType(ErrorText), findsNothing); |
| 78 | + }, |
| 79 | + ); |
| 80 | + }); |
| 81 | +} |
0 commit comments