|
| 1 | +import 'dart:math'; |
| 2 | + |
| 3 | +import 'package:example/main.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:mocktail/mocktail.dart'; |
| 7 | + |
| 8 | +class MockRandom extends Mock implements Random {} |
| 9 | + |
| 10 | + |
| 11 | +final _seed = MockRandom(); |
| 12 | + |
| 13 | +void main() { |
| 14 | + |
| 15 | + group('$MyApp', () { |
| 16 | + testWidgets('render example', (tester) async { |
| 17 | + await tester.pumpWidget(const MyApp()); |
| 18 | + expect(find.text('Formz Example'), findsOneWidget); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + group('$MyForm', () { |
| 23 | + |
| 24 | + setUp(() { |
| 25 | + when(()=> _seed.nextInt(any())).thenReturn(1); |
| 26 | + }); |
| 27 | + |
| 28 | + testWidgets('submits valid values', (tester) async { |
| 29 | + await tester.pumpMyForm(); |
| 30 | + |
| 31 | + await tester.enterText( |
| 32 | + find.byKey(const Key('myForm_emailInput')), |
| 33 | + 'email@example.com', |
| 34 | + ); |
| 35 | + await tester.pumpAndSettle(); |
| 36 | + |
| 37 | + await tester.enterText( |
| 38 | + find.byKey(const Key('myForm_passwordInput')), |
| 39 | + '123password', |
| 40 | + ); |
| 41 | + await tester.pumpAndSettle(); |
| 42 | + await tester.tap(find.byKey(const Key('myForm_submit'))); |
| 43 | + await tester.pumpAndSettle(); |
| 44 | + await tester.pumpAndSettle(); |
| 45 | + expect(find.text('Submitted successfully! 🎉'), findsOneWidget); |
| 46 | + }); |
| 47 | + |
| 48 | + group('invalid values', () { |
| 49 | + testWidgets('invalid email', (tester) async { |
| 50 | + await tester.pumpMyForm(); |
| 51 | + |
| 52 | + await tester.enterText( |
| 53 | + find.byKey(const Key('myForm_emailInput')), |
| 54 | + 'example.com', |
| 55 | + ); |
| 56 | + await tester.pumpAndSettle(); |
| 57 | + |
| 58 | + await tester.enterText( |
| 59 | + find.byKey(const Key('myForm_passwordInput')), |
| 60 | + '123password', |
| 61 | + ); |
| 62 | + await tester.pumpAndSettle(); |
| 63 | + await tester.tap(find.byKey(const Key('myForm_submit'))); |
| 64 | + await tester.pumpAndSettle(); |
| 65 | + await tester.pumpAndSettle(); |
| 66 | + |
| 67 | + expect( |
| 68 | + find.text('Please ensure the email entered is valid'), |
| 69 | + findsOneWidget, |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + testWidgets('empty email', (tester) async { |
| 74 | + await tester.pumpMyForm(); |
| 75 | + |
| 76 | + await tester.enterText( |
| 77 | + find.byKey(const Key('myForm_emailInput')), |
| 78 | + '', |
| 79 | + ); |
| 80 | + await tester.pumpAndSettle(); |
| 81 | + |
| 82 | + await tester.enterText( |
| 83 | + find.byKey(const Key('myForm_passwordInput')), |
| 84 | + '123password', |
| 85 | + ); |
| 86 | + await tester.pumpAndSettle(); |
| 87 | + await tester.tap(find.byKey(const Key('myForm_submit'))); |
| 88 | + await tester.pumpAndSettle(); |
| 89 | + await tester.pumpAndSettle(); |
| 90 | + |
| 91 | + expect( |
| 92 | + find.text('Please ensure the email entered is valid'), |
| 93 | + findsOneWidget, |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + testWidgets('invalid password', (tester) async { |
| 98 | + await tester.pumpMyForm(); |
| 99 | + |
| 100 | + await tester.enterText( |
| 101 | + find.byKey(const Key('myForm_emailInput')), |
| 102 | + 'email@example.com', |
| 103 | + ); |
| 104 | + await tester.pumpAndSettle(); |
| 105 | + |
| 106 | + await tester.enterText( |
| 107 | + find.byKey(const Key('myForm_passwordInput')), |
| 108 | + '12345678', |
| 109 | + ); |
| 110 | + await tester.pumpAndSettle(); |
| 111 | + await tester.tap(find.byKey(const Key('myForm_submit'))); |
| 112 | + await tester.pumpAndSettle(); |
| 113 | + await tester.pumpAndSettle(); |
| 114 | + |
| 115 | + expect( |
| 116 | + find.text('''Password must be at least 8 characters and contain at least one letter and number'''), |
| 117 | + findsOneWidget, |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + testWidgets('empty password', (tester) async { |
| 122 | + await tester.pumpMyForm(); |
| 123 | + |
| 124 | + await tester.enterText( |
| 125 | + find.byKey(const Key('myForm_emailInput')), |
| 126 | + 'email@example.com', |
| 127 | + ); |
| 128 | + await tester.pumpAndSettle(); |
| 129 | + |
| 130 | + await tester.enterText( |
| 131 | + find.byKey(const Key('myForm_passwordInput')), |
| 132 | + '', |
| 133 | + ); |
| 134 | + await tester.pumpAndSettle(); |
| 135 | + await tester.tap(find.byKey(const Key('myForm_submit'))); |
| 136 | + await tester.pumpAndSettle(); |
| 137 | + await tester.pumpAndSettle(); |
| 138 | + |
| 139 | + expect( |
| 140 | + find.text('''Password must be at least 8 characters and contain at least one letter and number'''), |
| 141 | + findsOneWidget, |
| 142 | + ); |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | +} |
| 147 | + |
| 148 | +extension on WidgetTester { |
| 149 | + Future<void> pumpMyForm() async { |
| 150 | + await pumpWidget(MaterialApp(home: Scaffold(body: MyForm(seed: _seed)))); |
| 151 | + } |
| 152 | +} |
0 commit comments