diff --git a/test/blocs/copy_resolve_bloc_test.dart b/test/blocs/copy_resolve_bloc_test.dart index 66f0ce6e1..70868f0bb 100644 --- a/test/blocs/copy_resolve_bloc_test.dart +++ b/test/blocs/copy_resolve_bloc_test.dart @@ -8,11 +8,11 @@ import 'package:weekplanner/blocs/copy_resolve_bloc.dart'; void main() { CopyResolveBloc bloc; Api api; - final WeekModel oldWeekmodel = WeekModel( - name: 'test', weekNumber: 23, weekYear: 2020); + final WeekModel oldWeekmodel = + WeekModel(name: 'test', weekNumber: 23, weekYear: 2020); final DisplayNameModel mockUser = - DisplayNameModel(displayName: 'testName', role: 'testRole', id: 'testId'); + DisplayNameModel(displayName: 'testName', role: 'testRole', id: 'testId'); setUp(() { api = Api('any'); @@ -20,15 +20,23 @@ void main() { bloc.initializeCopyResolverBloc(mockUser, oldWeekmodel); }); - + // Tests functionality for creating a new weekmodel, + // which is used when creating a new week plan. test('Test createNewWeekmodel', async((DoneFn done) { + // Add week 24 to the weekNoController // ignore: invalid_use_of_protected_member bloc.weekNoController.add('24'); + + // Create a listener for the weekNoController to check for any updates. // ignore: invalid_use_of_protected_member bloc.weekNoController.listen((_) { + // Create a new dummy weekmodel based on the old weekmodel. final WeekModel newWeekModel = bloc.createNewWeekmodel(oldWeekmodel); + + // Check if the new weekmodel has the correct weeknumber. expect(newWeekModel.weekNumber == 24, isTrue); + done(); }); })); -} \ No newline at end of file +} diff --git a/test/blocs/pictogram_bloc_test.dart b/test/blocs/pictogram_bloc_test.dart index db5f56000..d421ed48b 100644 --- a/test/blocs/pictogram_bloc_test.dart +++ b/test/blocs/pictogram_bloc_test.dart @@ -11,8 +11,8 @@ import 'package:weekplanner/blocs/pictogram_bloc.dart'; class MockPictogramApi extends Mock implements PictogramApi { @override Stream get(int id) async* { - final PictogramModel mockModel = PictogramModel(id: -1, title: 'test1', - accessLevel: AccessLevel.PUBLIC); + final PictogramModel mockModel = + PictogramModel(id: -1, title: 'test1', accessLevel: AccessLevel.PUBLIC); yield mockModel; } } @@ -33,29 +33,40 @@ void main() { const String query = 'Kat'; int count = 0; - when(pictogramApi.getAll(page: bloc.latestPage, - pageSize: pageSize, query: query)).thenAnswer( - (_) => - rx_dart.BehaviorSubject> - .seeded([])); + // Make a mock call to the api. + // It deposes the result and returns with an empty, seeded list. + when(pictogramApi.getAll( + page: bloc.latestPage, pageSize: pageSize, query: query)) + .thenAnswer((_) => rx_dart.BehaviorSubject>.seeded( + [])); + // Validate behaviour of the stream. bloc.search adds two objects to + // bloc.pictograms: one null and one placeholder PictogramModel. + // The listener below is called every time bloc.pictograms is updated. bloc.pictograms.listen((List response) { switch (count) { case 0: + // If the stream is empty, ie. no results, + // the response should be null, since bloc.search adds a null object + // to the stream first. Otherwise, the test fails. expect(response, isNull); break; case 1: - verify(pictogramApi.getAll(page: bloc.latestPage, - pageSize: pageSize, query: query)); + // If the stream is not empty, the 'getAll' method must have been run. + // 'verify' makes the test fail if 'getAll' was not called. + verify(pictogramApi.getAll( + page: bloc.latestPage, pageSize: pageSize, query: query)); done(); break; } + count++; }); bloc.search(query); })); + // TODO: Is this even a valid test? test('Should dispose stream', async((DoneFn done) { bloc.pictograms.listen((_) {}, onDone: done); bloc.dispose(); diff --git a/test/providers/environment_test.dart b/test/providers/environment_test.dart index 9a59febf2..28fc81cbc 100644 --- a/test/providers/environment_test.dart +++ b/test/providers/environment_test.dart @@ -18,29 +18,47 @@ void main() { '''; test('Should get SERVER_HOST from environment file (DEBUG)', () { + // Parses the variables from the 'debugEnvironments' string. environment.setContent(debugEnvironments); + + // Compare the loaded environment variable "SERVER_HOST" from + // the DEBUG environment to an expected value. + // If "SERVER_HOST" wasn't loaded or did not have the expected value, + // the test fails. Otherwise the test succeeds. expect(environment.getVar('SERVER_HOST'), equals('http://web.giraf.cs.aau.dk:5000')); }); test('Should get SERVER_HOST from environment file (PRODUCTION)', () { + // Parses the variables from the 'prodEnvironments' string. environment.setContent(prodEnvironments); + + // Compare the loaded environment variable "SERVER_HOST" from + // the PRODUCTION environment to an expected value. + // This test does the same as the test above, just ausing the PRODUCTION + // environment instead of the DEBUG environment. expect(environment.getVar('SERVER_HOST'), equals('http://web.giraf.cs.aau.dk:5000')); }); test('Should get DEBUG from environment file (PRODUCTION)', () { + // Parses the variables from the 'prodEnvironments' string. environment.setContent(prodEnvironments); + expect(environment.getVar('DEBUG'), equals(false)); }); - test('Should get USERNAME from environment file (PRODUCTION)', () { + test('Should get USERNAME from environment file (DEBUG)', () { + // Parses the variables from the 'debugEnvironments' string. environment.setContent(debugEnvironments); + expect(environment.getVar('USERNAME'), equals('Graatand')); }); - test('Should get PASSWORD from environment file (PRODUCTION)', () { + test('Should get PASSWORD from environment file (DEBUG)', () { + // Parses the variables from the 'debugEnvironments' string. environment.setContent(debugEnvironments); + expect(environment.getVar('PASSWORD'), equals('password')); }); } diff --git a/test/widgets/giraf_app_bar_widget_test.dart b/test/widgets/giraf_app_bar_widget_test.dart index 836ea38af..1ef446d07 100644 --- a/test/widgets/giraf_app_bar_widget_test.dart +++ b/test/widgets/giraf_app_bar_widget_test.dart @@ -18,8 +18,8 @@ import 'package:weekplanner/widgets/giraf_confirm_dialog.dart'; class MockAuth extends Mock implements AuthBloc { @override Stream get loggedIn => _loggedIn.stream; - final rx_dart.BehaviorSubject _loggedIn = rx_dart.BehaviorSubject - .seeded(true); + final rx_dart.BehaviorSubject _loggedIn = + rx_dart.BehaviorSubject.seeded(true); String loggedInUsername = 'Graatand'; @@ -49,25 +49,23 @@ class MockScreen extends StatelessWidget { appBar: GirafAppBar( title: 'TestTitle', appBarIcons: { - AppBarIcon.logout: null, - AppBarIcon.changeToGuardian: () {}, - })); + AppBarIcon.logout: null, + AppBarIcon.changeToGuardian: () {}, + })); } } - class MockScreenForErrorDialog extends StatelessWidget { @override Widget build(BuildContext context) { final ToolbarBloc bloc = di.get(); return Scaffold( - body: GirafButton( - key: const Key('IconChangeToGuardian'), - onPressed: () { - bloc.createPopupDialog(context).show(); - }, - ) - ); + body: GirafButton( + key: const Key('IconChangeToGuardian'), + onPressed: () { + bloc.createPopupDialog(context).show(); + }, + )); } } @@ -85,136 +83,199 @@ void main() { di.registerDependency(() => bloc); }); - // Used to wrap a widget into a materialapp, otherwise the widget is not - // testable + // Used to wrap a widget into a materialapp, + // otherwise the widget is not testable Widget makeTestableWidget({Widget child}) { return MaterialApp( home: child, ); } + // Used to simulate the widget being built inside the app itself, + // which is done using tester.pumpWidget. Optionmanlly, the function can + // simulate a frame change, which is done using tester.pump. + // This is done through the doUpdate parameter, which is true by default. + Future simulateTestWidget( + {WidgetTester tester, Widget widget, bool doUpdate = true}) async { + await tester.pumpWidget(makeTestableWidget(child: widget)); + if (doUpdate) { + await tester.pump(); + } + } + + // Used to override the authbloc and toolbarbloc, which allows the tests + // to have a 'clean' authbloc and toolbarbloc for each, individual test. void setupAlternativeDependencies() { di.registerDependency(() => MockAuthBloc(api), override: true); di.registerDependency(() => ToolbarBloc(), override: true); } - + // Test that had no documentation. testWidgets('Elements on dialog should be visible', - (WidgetTester tester) async { - // we have to use a diffent authbloc, where everything is not overridden + (WidgetTester tester) async { + // We have to use a diffent authbloc, where everything is not overridden setupAlternativeDependencies(); + + // This part creates a MockScreenForErrorDialog, which is then validated + // using tester.pumpAndSettle. This lets the widget be built and evaluated + // as if it had been built in the app itself. await tester.pumpWidget(MaterialApp(home: MockScreenForErrorDialog())); await tester.pumpAndSettle(); + // This part taps the button with the key 'IconChangeToGuardian', + // which is the button used for chanmging to guardian mode. + // This is then validated using tester.pumpAndSettle, + // which lets the test simulate a button press. await tester.tap(find.byKey(const Key('IconChangeToGuardian'))); await tester.pumpAndSettle(); - expect(find.byKey(const Key('SwitchToGuardianPassword')), - findsOneWidget); - + // These expect statements validate that there exists both a textfield + // for changing the password and a button for submitting the password. + // If either of these are not found, the test fails. + expect(find.byKey(const Key('SwitchToGuardianPassword')), findsOneWidget); expect(find.byKey(const Key('SwitchToGuardianSubmit')), findsOneWidget); }); + // Test that had no documentation. testWidgets('Wrong credentials should show error dialog', - (WidgetTester tester) async { - // we have to use a diffent authbloc, where everything is not overridden + (WidgetTester tester) async { + // We have to use a diffent authbloc, where everything is not overridden. setupAlternativeDependencies(); + + // This part creates a MockScreenForErrorDialog, which is then validated + // using tester.pumpAndSettle. This lets the widget be built and evaluated + // as if it had been built in the app itself. await tester.pumpWidget(MaterialApp(home: MockScreenForErrorDialog())); await tester.pumpAndSettle(); + // This part taps the button with the key 'IconChangeToGuardian', + // which is the button used for chanmging to guardian mode. + // This is then validated using tester.pumpAndSettle, + // which lets the test simulate a button press. await tester.tap(find.byKey(const Key('IconChangeToGuardian'))); await tester.pumpAndSettle(); + // This part enters the text 'abc' into the textfield for changing the + // password. Afterwards, the button for submitting the password is tapped. + // This is then validated using tester.pumpAndSettle. await tester.enterText( find.byKey(const Key('SwitchToGuardianPassword')), 'abc'); - await tester.tap(find.byKey(const Key('SwitchToGuardianSubmit'))); await tester.pumpAndSettle(); - expect(find.byKey(const Key('WrongPasswordDialog')), - findsOneWidget); - + // This part validates that the dialog for wrong password is shown, and thus + // the entered password 'abc' is incorrect. If the dialog is not displayed, + // the test fails. Otherwise, the test passes. + expect(find.byKey(const Key('WrongPasswordDialog')), findsOneWidget); }); testWidgets('Right credentials should not show error dialog', - (WidgetTester tester) async { + (WidgetTester tester) async { setupAlternativeDependencies(); - await tester.pumpWidget(makeTestableWidget( - child: MockScreenForErrorDialog())); + + // This part works the same way as the test above, but instead of just + // initializing the widget, it is wrapped in a MaterialApp object, + // which is done through the makeTestableWidget function. + await tester + .pumpWidget(makeTestableWidget(child: MockScreenForErrorDialog())); await tester.pumpAndSettle(); + // This part taps the button with the key 'IconChangeToGuardian', + // which is the button used for chanmging to guardian mode. + // This is then validated using tester.pumpAndSettle, + // which lets the test simulate a button press. await tester.tap(find.byKey(const Key('IconChangeToGuardian'))); await tester.pumpAndSettle(); + // This part enters the text 'abc' into the textfield for changing the + // password. Afterwards, the button for submitting the password is tapped. + // This is then validated using tester.pumpAndSettle. await tester.enterText( find.byKey(const Key('SwitchToGuardianPassword')), 'password'); await tester.tap(find.byKey(const Key('SwitchToGuardianSubmit'))); + await tester.pumpAndSettle(const Duration(seconds: 2)); - await tester.pumpAndSettle(const Duration(seconds:2)); + // This part validates that the dialog for wrong password is not shown, and + // thus the entered password 'password' is correct. If the dialog is not + // displayed, the test fails. Otherwise, the test passes. + expect(find.byKey(const Key('WrongPasswordDialog')), findsNothing); + }); - expect(find.byKey(const Key('WrongPasswordDialog')), - findsNothing); + ////////////////////////////////////////////////////////////////////////////// - }); + // All of the tests until the next comment like above are tests that validate + // if the various text elements are displayed correctly. + + // To simplify the code, the function 'simulateWidget' is used for + // building a testable widget, and then simulating a frame change. + + // For reference, all of the tests were not documented. testWidgets('Has toolbar with title', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar(title: 'Ugeplan'); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); + // Simulate the widget being built inside the app itself. + await simulateTestWidget( + tester: tester, widget: girafAppBar, doUpdate: false); + // This part validates that the text 'Ugeplan' is displayed in the appbar. expect(find.text('Ugeplan'), findsOneWidget); }); + // As the rest of the tests in this section are very similar, and function + // the same way, only the first test is commented. + testWidgets('Display default icon when given no icons to display', (WidgetTester tester) async { + // Create the GirafAppBar object, which is the widget being tested. final GirafAppBar girafAppBar = GirafAppBar(title: 'Ugeplan', appBarIcons: null); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + // Simulate the widget being built inside the app itself, + // but also simulate a frame change. + await simulateTestWidget(tester: tester, widget: girafAppBar); + expect(find.byTooltip('Log ud'), findsOneWidget); }); testWidgets('Accept button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.accept: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.accept: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Accepter'), findsOneWidget); }); testWidgets('Add button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.add: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.add: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Tilføj'), findsOneWidget); }); testWidgets('Add timer button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.addTimer: null}); + title: 'Ugeplan', + appBarIcons: const { + AppBarIcon.addTimer: null + }); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Tilføj timer'), findsOneWidget); }); testWidgets('Back button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.back: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.back: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Tilbage'), findsOneWidget); }); @@ -223,32 +284,30 @@ void main() { final GirafAppBar girafAppBar = GirafAppBar( title: 'Ugeplan', appBarIcons: const { - AppBarIcon.burgerMenu: null}); + AppBarIcon.burgerMenu: null + }); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Åbn menu'), findsOneWidget); }); testWidgets('Camera button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.camera: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.camera: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Åbn kamera'), findsOneWidget); }); testWidgets('Cancel button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.cancel: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.cancel: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Fortryd'), findsOneWidget); }); @@ -258,10 +317,10 @@ void main() { final GirafAppBar girafAppBar = GirafAppBar( title: 'Ugeplan', appBarIcons: const { - AppBarIcon.changeToCitizen: null}); + AppBarIcon.changeToCitizen: null + }); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Skift til borger tilstand'), findsOneWidget); }); @@ -271,173 +330,194 @@ void main() { final GirafAppBar girafAppBar = GirafAppBar( title: 'Ugeplan', appBarIcons: const { - AppBarIcon.changeToGuardian: null}); + AppBarIcon.changeToGuardian: null + }); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Skift til værge tilstand'), findsOneWidget); }); testWidgets('Copy button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.copy: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.copy: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Kopier'), findsOneWidget); }); testWidgets('Delete button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.delete: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.delete: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Slet'), findsOneWidget); }); testWidgets('Edit button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.edit: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.edit: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Rediger'), findsOneWidget); }); testWidgets('Help button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.help: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.help: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Hjælp'), findsOneWidget); }); testWidgets('Home button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.home: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.home: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Gå til startside'), findsOneWidget); }); testWidgets('Log out button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.logout: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.logout: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Log ud'), findsOneWidget); }); testWidgets('Profile button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.profile: null}); + title: 'Ugeplan', + appBarIcons: const { + AppBarIcon.profile: null + }); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Vis profil'), findsOneWidget); }); testWidgets('Redo button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.redo: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.redo: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Gendan'), findsOneWidget); }); testWidgets('Save button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.save: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.save: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Gem'), findsOneWidget); }); testWidgets('Search button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.search: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.search: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Søg'), findsOneWidget); }); testWidgets('Settings button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.settings: null}); + title: 'Ugeplan', + appBarIcons: const { + AppBarIcon.settings: null + }); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Indstillinger'), findsOneWidget); }); testWidgets('Undo button is displayed', (WidgetTester tester) async { final GirafAppBar girafAppBar = GirafAppBar( - title: 'Ugeplan', appBarIcons: const { - AppBarIcon.undo: null}); + title: 'Ugeplan', + appBarIcons: const {AppBarIcon.undo: null}); - await tester.pumpWidget(makeTestableWidget(child: girafAppBar)); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: girafAppBar); expect(find.byTooltip('Fortryd'), findsOneWidget); }); + ////////////////////////////////////////////////////////////////////////////// + + // Not at all commented testWidgets('GirafConfirmDialog is shown on logout icon press', (WidgetTester tester) async { - await tester.pumpWidget(MaterialApp(home: MockScreen())); - await tester.pump(); + await simulateTestWidget(tester: tester, widget: MockScreen()); + + // Validate that one and only one, + // button tagged with the tooltip 'Log ud' exists. expect(find.byTooltip('Log ud'), findsOneWidget); + + // Look for the button tagged with the tooltip 'Log ud' and tap it. + // Afterwards, simulate a frame change using tester.pump. await tester.tap(find.byTooltip('Log ud')); await tester.pump(); + + // Validate that the confirmation dialog is shown when logging out. expect(find.byType(GirafConfirmDialog), findsOneWidget); }); + // Not at all commented. testWidgets('User is logged out on confirmation in GirafConfirmDialog', (WidgetTester tester) async { final Completer done = Completer(); - await tester.pumpWidget(MaterialApp(home: MockScreen())); - await tester.pump(); + + await simulateTestWidget(tester: tester, widget: MockScreen()); + + // Validate that one and only one, + // button tagged with the tooltip 'Log ud' exists. expect(find.byTooltip('Log ud'), findsOneWidget); + + // Look for the button tagged with the tooltip 'Log ud' and tap it. + // Afterwards, simulate a complete rebuild using tester.pumpAndSettle. + // tester.pumpAndSettle calls tester.pump until there are no more frames. await tester.tap(find.byTooltip('Log ud')); await tester.pumpAndSettle(); + + // Validate that the confirmation dialog is shown when logging out. + // This is done by validating that exactly one GirafConfirmDialog exists, + // and that exactly one button tagged with the key + // 'ConfirmDialogConfirmButton' exists. expect(find.byType(GirafConfirmDialog), findsOneWidget); expect(find.byKey(const Key('ConfirmDialogConfirmButton')), findsOneWidget); + + // Tap the confirmation button and await the stream to update. await tester.tap(find.byKey(const Key('ConfirmDialogConfirmButton'))); + + // Listens for an update to the loggedIn stream. authBloc.loggedIn.listen((bool statusLogout) async { - if (statusLogout == false) { - expect(statusLogout, isFalse); - done.complete(); - } + // If there is a discrepancy between the stream and + // the expected value 'false', the test fails. + expect(statusLogout, isFalse); + done.complete(); }); + + // Wait for the stream above to update. await done.future; }); }