From 9b41f0e72ffcd18eefeddaa984c5c233b0914e41 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Wed, 2 Dec 2020 13:42:12 +0100 Subject: [PATCH] Add auth screen widget test --- lib/screens/auth_screen.dart | 13 ++++++--- test/auth_screen_test.dart | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 test/auth_screen_test.dart diff --git a/lib/screens/auth_screen.dart b/lib/screens/auth_screen.dart index 898d9e70..4b5ca7b5 100644 --- a/lib/screens/auth_screen.dart +++ b/lib/screens/auth_screen.dart @@ -62,7 +62,7 @@ class AuthScreen extends StatelessWidget { ), ), Flexible( - flex: deviceSize.width > 600 ? 2 : 1, + //flex: deviceSize.width > 600 ? 2 : 1, child: AuthCard(), ), ], @@ -195,6 +195,7 @@ class _AuthCardState extends State { child: Column( children: [ TextFormField( + key: Key('inputUsername'), decoration: InputDecoration(labelText: 'Username'), controller: _usernameController, textInputAction: TextInputAction.next, @@ -211,6 +212,7 @@ class _AuthCardState extends State { ), if (_authMode == AuthMode.Signup) TextFormField( + key: Key('inputEmail'), decoration: InputDecoration(labelText: 'E-Mail'), controller: _emailController, keyboardType: TextInputType.emailAddress, @@ -226,6 +228,7 @@ class _AuthCardState extends State { }, ), TextFormField( + key: Key('inputPassword'), decoration: InputDecoration(labelText: 'Password'), obscureText: true, controller: _passwordController, @@ -244,6 +247,7 @@ class _AuthCardState extends State { ), if (_authMode == AuthMode.Signup) TextFormField( + key: Key('inputPassword2'), decoration: InputDecoration(labelText: 'Confirm Password'), controller: _password2Controller, enabled: _authMode == AuthMode.Signup, @@ -258,6 +262,7 @@ class _AuthCardState extends State { : null, ), TextFormField( + key: Key('inputServer'), decoration: InputDecoration(labelText: 'Server URL'), controller: _serverUrlController, validator: (value) { @@ -277,11 +282,13 @@ class _AuthCardState extends State { CircularProgressIndicator() else ElevatedButton( - child: Text(_authMode == AuthMode.Login ? 'LOGIN' : 'SIGN UP'), + key: Key('actionButton'), + child: Text(_authMode == AuthMode.Login ? 'LOGIN' : 'REGISTER'), onPressed: _submit, ), TextButton( - child: Text('${_authMode == AuthMode.Login ? 'SIGNUP' : 'LOGIN'} INSTEAD'), + key: Key('toggleActionButton'), + child: Text('${_authMode == AuthMode.Login ? 'REGISTER' : 'LOGIN'} INSTEAD'), onPressed: _switchAuthMode, ), ], diff --git a/test/auth_screen_test.dart b/test/auth_screen_test.dart new file mode 100644 index 00000000..1cf96cb9 --- /dev/null +++ b/test/auth_screen_test.dart @@ -0,0 +1,51 @@ +// This is a basic Flutter widget test. +// +// To perform an interaction with a widget in your test, use the WidgetTester +// utility that Flutter provides. For example, you can send tap and scroll +// gestures. You can also use WidgetTester to find child widgets in the widget +// tree, read text, and verify that the values of widget properties are correct. + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:wger/screens/auth_screen.dart'; + +void main() { + testWidgets('Test the widgets on the auth screen, login mode', (WidgetTester tester) async { + // Wrap screen in material app so that the media query gets a context + await tester.pumpWidget(MaterialApp(home: AuthScreen())); + expect(find.text('WGER'), findsOneWidget); + + // Verify that the correct buttons and input fields are shown: login + expect(find.text('REGISTER INSTEAD'), findsOneWidget); + expect(find.text('LOGIN INSTEAD'), findsNothing); + + // Check that the correct widgets are shown + expect(find.byKey(Key('inputUsername')), findsOneWidget); + expect(find.byKey(Key('inputEmail')), findsNothing); + expect(find.byKey(Key('inputPassword')), findsOneWidget); + expect(find.byKey(Key('inputServer')), findsOneWidget); + expect(find.byKey(Key('inputPassword2')), findsNothing); + expect(find.byKey(Key('actionButton')), findsOneWidget); + expect(find.byKey(Key('toggleActionButton')), findsOneWidget); + }); + + testWidgets('Test the widgets on the auth screen, registration', (WidgetTester tester) async { + // Wrap screen in material app so that the media query gets a context + await tester.pumpWidget(MaterialApp(home: AuthScreen())); + await tester.tap(find.byKey(Key('toggleActionButton'))); + + // Rebuild the widget after the state has changed. + await tester.pump(); + expect(find.text('REGISTER INSTEAD'), findsNothing); + expect(find.text('LOGIN INSTEAD'), findsOneWidget); + + // Check that the correct widgets are shown + expect(find.byKey(Key('inputUsername')), findsOneWidget); + expect(find.byKey(Key('inputEmail')), findsOneWidget); + expect(find.byKey(Key('inputPassword')), findsOneWidget); + expect(find.byKey(Key('inputServer')), findsOneWidget); + expect(find.byKey(Key('inputPassword2')), findsOneWidget); + expect(find.byKey(Key('actionButton')), findsOneWidget); + expect(find.byKey(Key('toggleActionButton')), findsOneWidget); + }); +}