diff --git a/test/blocs/choose_citizen_bloc_test.dart b/test/blocs/choose_citizen_bloc_test.dart index 9e2d2254b..0d32ec29b 100644 --- a/test/blocs/choose_citizen_bloc_test.dart +++ b/test/blocs/choose_citizen_bloc_test.dart @@ -8,6 +8,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; import 'package:weekplanner/blocs/choose_citizen_bloc.dart'; +//Creates a mock for the test class MockUserApi extends Mock implements UserApi { @override Stream me() { @@ -30,6 +31,7 @@ class MockUserApi extends Mock implements UserApi { } void main() { + //Setting up the environment ChooseCitizenBloc bloc; Api api; setUp(() { @@ -40,11 +42,16 @@ void main() { test('Should be able to get UsernameModel from API', async((DoneFn done) { int _count = 0; + //Set up citizen listener bloc.citizen.listen((List response) { + //When "_count" is zero it expects length to be 0, and iterates "_count" if (_count == 0) { expect(response.length, 0); _count++; - } else { + } + //Otherwise it expects the length to be 1, + //and checks if the data matches with the mock made above. + else { expect(response.length, 1); final DisplayNameModel rsp = response[0]; expect(rsp.displayName, 'test1'); diff --git a/test/widgets/citizen_avatar_widget_test.dart b/test/widgets/citizen_avatar_widget_test.dart index 1611c311e..b8c1a795e 100644 --- a/test/widgets/citizen_avatar_widget_test.dart +++ b/test/widgets/citizen_avatar_widget_test.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:weekplanner/widgets/citizen_avatar_widget.dart'; +//Creates a mock for the test class MockScreen extends StatelessWidget { MockScreen({@required this.callback}); @@ -24,44 +25,57 @@ class MockScreen extends StatelessWidget { } void main() { + //Setting up the environment int i = 0; const Key widgetAvatar = Key('WidgetAvatar'); const Key widgetText = Key('WidgetText'); final MockScreen mockScreen = MockScreen(callback: () => ++i); testWidgets('Test if citizen text appears', (WidgetTester tester) async { + //Pumps the mockScreen until there are no more frames scheduled await tester.pumpWidget(MaterialApp(home: mockScreen)); await tester.pumpAndSettle(); + //Checks if it can find the text (widgetText) expect(find.byKey(widgetText), findsOneWidget); }); testWidgets('Test if citizen avatar appears', (WidgetTester tester) async { + //Pumps the mockScreen until there are no more frames scheduled await tester.pumpWidget(MaterialApp(home: mockScreen)); await tester.pumpAndSettle(); + //Checks if it can find the citizen avatar (widgetAvatar) expect(find.byKey(widgetAvatar), findsOneWidget); }); testWidgets('Test if text is the username', (WidgetTester tester) async { + //Pumps the mockScreen until there are no more frames scheduled await tester.pumpWidget(MaterialApp(home: mockScreen)); await tester.pumpAndSettle(); + //Checks if "Testname" is the username expect(find.text('Testname'), findsOneWidget); }); testWidgets('Test if callback is working on avatar', (WidgetTester tester) async { i = 0; + //Pumps the mockScreen until there are no more frames scheduled await tester.pumpWidget(MaterialApp(home: mockScreen)); await tester.pumpAndSettle(); + //Uses tester.tap to check if callback works when clicking on an avatar await tester.tap(find.byKey(widgetAvatar)); + //If it works "i" would have been iterated to 1 and the assert will be true expect(i, 1); }); testWidgets('Test if callback is working on text', (WidgetTester tester) async { i = 0; + //Pumps the mockScreen until there are no more frames scheduled await tester.pumpWidget(MaterialApp(home: mockScreen)); await tester.pumpAndSettle(); + //Uses tester.tap to check if callback works when clicking on an avatar await tester.tap(find.byKey(widgetText)); + //If it works "i" would have been iterated to 1 and the assert will be true expect(i, 1); }); } diff --git a/test/widgets/giraf_title_header_test.dart b/test/widgets/giraf_title_header_test.dart index b7314f14f..139410667 100644 --- a/test/widgets/giraf_title_header_test.dart +++ b/test/widgets/giraf_title_header_test.dart @@ -5,21 +5,25 @@ import 'package:weekplanner/widgets/giraf_title_header.dart'; void main() { testWidgets('Test that a null title is replaced with empty', (WidgetTester tester) async { + //Pumps a widget containing a GirafTitleHeader with the title "TitleHeader" await tester.pumpWidget(const MaterialApp( home: Scaffold( body: GirafTitleHeader(title: 'TitleHeader'), ))); await tester.pump(); + //Checks if the text is "TitleHeader" expect(find.text('TitleHeader'), findsOneWidget); }); testWidgets('Test that a null title is replaced with empty', (WidgetTester tester) async { + //Pumps a widget containing a GirafTitleHeader with no title assigned" await tester.pumpWidget(const MaterialApp( home: Scaffold( body: GirafTitleHeader(), ))); await tester.pump(); + //Checks if the text is empty expect(find.text(''), findsOneWidget); }); } diff --git a/test/widgets/loading_spinner_widget_test.dart b/test/widgets/loading_spinner_widget_test.dart index 9b2bf96ce..8d7fbbc12 100644 --- a/test/widgets/loading_spinner_widget_test.dart +++ b/test/widgets/loading_spinner_widget_test.dart @@ -3,6 +3,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:weekplanner/routes.dart'; import 'package:weekplanner/widgets/loading_spinner_widget.dart'; +//Creates a mock for the test class MockScreen extends StatelessWidget { @override Widget build(BuildContext context) { @@ -27,6 +28,7 @@ class MockScreen extends StatelessWidget { ); } + //Function for showing the loading spinner void loadingSpinner(BuildContext context) { showLoadingSpinner(context, true); } @@ -34,22 +36,28 @@ class MockScreen extends StatelessWidget { void main() { testWidgets('Test if Loading Spinner is shown', (WidgetTester tester) async { + //Pumps the MockScreen and tries to find the "FirstButton" with tester.tap await tester.pumpWidget(MaterialApp(home: MockScreen())); await tester.tap(find.byKey(const Key('FirstButton'))); await tester.pump(); + //As the "FirstButton" is set to toggle the loading spinner (can be seen in the mock above), + //It is now expected that the loading spinner is being shown expect(find.byType(CircularProgressIndicator), findsOneWidget); }); testWidgets('Test if Loading Spinner is removed again', (WidgetTester tester) async { + //Line 51-54 is the same as above, toggling the loading spinner on await tester.pumpWidget(MaterialApp(home: MockScreen())); await tester.tap(find.byKey(const Key('FirstButton'))); await tester.pump(); expect(find.byType(CircularProgressIndicator), findsOneWidget); await tester.pump(); + //Finds the "SecondButton" which toggles the loading spinner off again await tester.tap(find.byKey(const Key('SecondButton')), warnIfMissed: false); await tester.pump(); + //Checks to see if the loading spinner is removed expect(find.byType(CircularProgressIndicator), findsNothing); }); }