Skip to content

Commit

Permalink
test files reviewed
Browse files Browse the repository at this point in the history
  • Loading branch information
omegaMindCoder619 committed Nov 29, 2023
1 parent f704223 commit 3f7ce19
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion test/blocs/choose_citizen_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<GirafUserModel> me() {
Expand All @@ -30,6 +31,7 @@ class MockUserApi extends Mock implements UserApi {
}

void main() {
//Setting up the environment
ChooseCitizenBloc bloc;
Api api;
setUp(() {
Expand All @@ -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<DisplayNameModel> 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');
Expand Down
14 changes: 14 additions & 0 deletions test/widgets/citizen_avatar_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand All @@ -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);
});
}
4 changes: 4 additions & 0 deletions test/widgets/giraf_title_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
8 changes: 8 additions & 0 deletions test/widgets/loading_spinner_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -27,29 +28,36 @@ class MockScreen extends StatelessWidget {
);
}

//Function for showing the loading spinner
void loadingSpinner(BuildContext context) {
showLoadingSpinner(context, true);
}
}

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);
});
}

0 comments on commit 3f7ce19

Please sign in to comment.