Skip to content

Commit

Permalink
chore: add tests and workflow for exampl
Browse files Browse the repository at this point in the history
  • Loading branch information
renancaraujo committed Sep 19, 2023
1 parent 5f6e806 commit 4e4404e
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 4 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: example

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
semantic-pull-request:
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/semantic_pull_request.yml@v1

build:
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/flutter_package.yml@v1
with:
flutter_channel: stable

spell-check:
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/spell_check.yml@v1
with:
includes: |
**/*.md
modified_files_only: false
13 changes: 9 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class MyApp extends StatelessWidget {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Formz Example')),
body: const Padding(
padding: EdgeInsets.all(24),
body: Padding(
padding: const EdgeInsets.all(24),
child: SingleChildScrollView(child: MyForm()),
),
),
Expand All @@ -23,7 +23,9 @@ class MyApp extends StatelessWidget {
}

class MyForm extends StatefulWidget {
const MyForm({super.key});
MyForm({super.key, Random? seed}) : seed = seed ?? Random();

final Random seed;

@override
State<MyForm> createState() => _MyFormState();
Expand Down Expand Up @@ -89,7 +91,7 @@ class _MyFormState extends State<MyForm> {

Future<void> _submitForm() async {
await Future<void>.delayed(const Duration(seconds: 1));
if (Random().nextInt(2) == 0) throw Exception();
if (widget.seed.nextInt(2) == 0) throw Exception();
}

void _resetForm() {
Expand Down Expand Up @@ -123,6 +125,7 @@ class _MyFormState extends State<MyForm> {
child: Column(
children: [
TextFormField(
key: const Key('myForm_emailInput'),
controller: _emailController,
decoration: const InputDecoration(
icon: Icon(Icons.email),
Expand All @@ -134,6 +137,7 @@ class _MyFormState extends State<MyForm> {
textInputAction: TextInputAction.next,
),
TextFormField(
key: const Key('myForm_passwordInput'),
controller: _passwordController,
decoration: const InputDecoration(
icon: Icon(Icons.lock),
Expand All @@ -153,6 +157,7 @@ class _MyFormState extends State<MyForm> {
const CircularProgressIndicator()
else
ElevatedButton(
key: const Key('myForm_submit'),
onPressed: _onSubmit,
child: const Text('Submit'),
),
Expand Down
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies:
sdk: flutter
formz:
path: ../
mocktail: ^1.0.0

dev_dependencies:
flutter_test:
Expand Down
152 changes: 152 additions & 0 deletions example/test/main_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import 'dart:math';

import 'package:example/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockRandom extends Mock implements Random {}


final _seed = MockRandom();

void main() {

group('$MyApp', () {
testWidgets('render example', (tester) async {
await tester.pumpWidget(const MyApp());
expect(find.text('Formz Example'), findsOneWidget);
});
});

group('$MyForm', () {

setUp(() {
when(()=> _seed.nextInt(any())).thenReturn(1);
});

testWidgets('submits valid values', (tester) async {
await tester.pumpMyForm();

await tester.enterText(
find.byKey(const Key('myForm_emailInput')),
'[email protected]',
);
await tester.pumpAndSettle();

await tester.enterText(
find.byKey(const Key('myForm_passwordInput')),
'123password',
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('myForm_submit')));
await tester.pumpAndSettle();
await tester.pumpAndSettle();
expect(find.text('Submitted successfully! 🎉'), findsOneWidget);
});

group('invalid values', () {
testWidgets('invalid email', (tester) async {
await tester.pumpMyForm();

await tester.enterText(
find.byKey(const Key('myForm_emailInput')),
'emailexample.com',

Check warning on line 54 in example/test/main_test.dart

View workflow job for this annotation

GitHub Actions / spell-check / build

Unknown word (emailexample)
);
await tester.pumpAndSettle();

await tester.enterText(
find.byKey(const Key('myForm_passwordInput')),
'123password',
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('myForm_submit')));
await tester.pumpAndSettle();
await tester.pumpAndSettle();

expect(
find.text('Please ensure the email entered is valid'),
findsOneWidget,
);
});

testWidgets('empty email', (tester) async {
await tester.pumpMyForm();

await tester.enterText(
find.byKey(const Key('myForm_emailInput')),
'',
);
await tester.pumpAndSettle();

await tester.enterText(
find.byKey(const Key('myForm_passwordInput')),
'123password',
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('myForm_submit')));
await tester.pumpAndSettle();
await tester.pumpAndSettle();

expect(
find.text('Please ensure the email entered is valid'),
findsOneWidget,
);
});

testWidgets('invalid password', (tester) async {
await tester.pumpMyForm();

await tester.enterText(
find.byKey(const Key('myForm_emailInput')),
'[email protected]',
);
await tester.pumpAndSettle();

await tester.enterText(
find.byKey(const Key('myForm_passwordInput')),
'12345678',
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('myForm_submit')));
await tester.pumpAndSettle();
await tester.pumpAndSettle();

expect(
find.text('''Password must be at least 8 characters and contain at least one letter and number'''),
findsOneWidget,
);
});

testWidgets('empty password', (tester) async {
await tester.pumpMyForm();

await tester.enterText(
find.byKey(const Key('myForm_emailInput')),
'[email protected]',
);
await tester.pumpAndSettle();

await tester.enterText(
find.byKey(const Key('myForm_passwordInput')),
'',
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('myForm_submit')));
await tester.pumpAndSettle();
await tester.pumpAndSettle();

expect(
find.text('''Password must be at least 8 characters and contain at least one letter and number'''),
findsOneWidget,
);
});
});
});
}

extension on WidgetTester {
Future<void> pumpMyForm() async {
await pumpWidget(MaterialApp(home: Scaffold(body: MyForm(seed: _seed))));
}
}

0 comments on commit 4e4404e

Please sign in to comment.