Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/TestsRevisited' into Issue#980
Browse files Browse the repository at this point in the history
# Conflicts:
#	test/blocs/weekplans_bloc_test.dart
  • Loading branch information
Frederik Bendix committed Dec 16, 2023
2 parents 3f80b6d + 35436b5 commit 17ccabc
Show file tree
Hide file tree
Showing 25 changed files with 701 additions and 267 deletions.
2 changes: 1 addition & 1 deletion lib/main.dart
Expand Up @@ -21,7 +21,7 @@ void main() {
* Use the "environments.local.json" for running against your local web-api
* For IOS users: change the SERVER_HOST in the environment.local file to "http://localhost:5000"
*/
environment.setFile('assets/environments.dev.json').whenComplete(() {
environment.setFile('assets/environments.local.json').whenComplete(() {
_runApp();
});
}
Expand Down
10 changes: 9 additions & 1 deletion test/blocs/choose_citizen_bloc_test.dart
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.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,7 +31,9 @@ class MockUserApi extends Mock implements UserApi {
}

void main() {
//Setting up the environment
late ChooseCitizenBloc bloc;

Api api;
setUp(() {
api = Api('any');
Expand All @@ -40,11 +43,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
9 changes: 9 additions & 0 deletions test/blocs/copy_resolve_bloc_test.dart
Expand Up @@ -26,13 +26,22 @@ 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();
});
}));
Expand Down
17 changes: 11 additions & 6 deletions test/blocs/copy_weekplan_bloc_test.dart
Expand Up @@ -65,17 +65,20 @@ void main() {
});

test('toggleMarkedUserModel', async((DoneFn done) {
//Toggles the user
bloc.toggleMarkedUserModel(user);

//Creates listener, which listens for activities on markedUserModels.
// Expects that the user is part of the activated toggleMarkedUserModel
bloc.markedUserModels.listen((List<DisplayNameModel> response) {
expect(response.contains(user), true);
});
done();
}));

test(
'Test whether the copyToCitizens method '
'copies the weekplan to the citizens', async((DoneFn done) {

test('Test whether the copyToCitizens method '
'copies the weekplan to the citizens', async((DoneFn done) {
// Creates 10 different users, marks them, and listens for
// them to be marked. Ensures that all users are marked.
for (int i = 0; i < 10; i++) {
final DisplayNameModel user = DisplayNameModel(
displayName: 'Hans', role: Role.Citizen.toString(), id: i.toString());
Expand All @@ -84,9 +87,11 @@ void main() {
expect(markedUsers.contains(user), true);
});
}

// Copies one weekplan to a user.
bloc.copyWeekplan(<WeekModel>[weekplan1], user, false);

// Creates Listener for marked users and checks if the right user
// has the right weekplan.
bloc.markedUserModels.listen((List<DisplayNameModel> markedUsers) {
for (DisplayNameModel user in markedUsers) {
expect(map.containsKey(user.id), true);
Expand Down
4 changes: 3 additions & 1 deletion test/blocs/edit_weekplan_bloc_test.dart
Expand Up @@ -15,11 +15,12 @@ import 'package:weekplanner/di.dart';

class MockWeekApi extends Mock implements WeekApi {}

//test functionality for editing a weekplan bloc
void main() {
Api api = Api('any');
EditWeekplanBloc bloc = EditWeekplanBloc(api);
WeekplansBloc mockWeekplanSelector = WeekplansBloc(api);

//Create mockup pictogram
final PictogramModel mockThumbnail = PictogramModel(
id: 1,
lastEdit: null,
Expand All @@ -38,6 +39,7 @@ void main() {
weekNumber: 1,
weekYear: DateTime.now().year + 1);

//set up test, defines what values to return on api calls
setUp(() {
api = Api('any');
api.week = MockWeekApi();
Expand Down
4 changes: 4 additions & 0 deletions test/blocs/new_citizen_bloc_test.dart
Expand Up @@ -8,6 +8,9 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:weekplanner/blocs/new_citizen_bloc.dart';

//tests that features relevant to creating a citizen are functional

//creates an example user
class MockUserApi extends Mock implements UserApi {
@override
Stream<GirafUserModel> me() {
Expand Down Expand Up @@ -46,6 +49,7 @@ void main() {
bloc = NewCitizenBloc(api);
bloc.initialize();

//sets api calls to return correct user data
when(() => api.account.register(any(), any(), any(), any(),
departmentId: any(named: 'departmentId'),
role: any(named: 'role'))).thenAnswer((_) {
Expand Down
2 changes: 2 additions & 0 deletions test/blocs/new_pictogram_password_bloc_test.dart
Expand Up @@ -9,6 +9,8 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:weekplanner/blocs/new_pictogram_password_bloc.dart';

//tests if a new pictogram password can be properly created

class MockUserApi extends Mock implements UserApi {
@override
Stream<GirafUserModel> me() {
Expand Down
7 changes: 5 additions & 2 deletions test/blocs/new_weekplan_bloc_test.dart
Expand Up @@ -16,13 +16,15 @@ class MockWeekApi extends Mock implements WeekApi {}

class MockWeekModel extends Fake implements WeekModel {}

//tests for various functions related to creating new weekplans
void main() {
setUpAll(() {
registerFallbackValue(MockWeekModel());
});

Api api = Api('baseUrl');
NewWeekplanBloc bloc = NewWeekplanBloc(api);
//create a mock week for use in the tests
final PictogramModel mockThumbnail = PictogramModel(
id: 1,
lastEdit: null,
Expand All @@ -42,9 +44,10 @@ void main() {
WeekplansBloc mockWeekplanSelector = WeekplansBloc(api);

setUp(() {
api = Api('any');
api = Api('baseUrl');
api.week = MockWeekApi();

// Setup api listeners to return the correct values
// from the mock week created earlier
when(() => api.week.update(any(), any(), any(), any())).thenAnswer((_) {
return Stream<WeekModel>.value(mockWeek);
});
Expand Down
18 changes: 17 additions & 1 deletion test/blocs/pictogram_bloc_test.dart
Expand Up @@ -33,29 +33,45 @@ void main() {
const String query = 'Kat';
int count = 0;

// 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<List<PictogramModel>>.seeded(
<PictogramModel>[]));

// 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<PictogramModel>? 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);
done();
break;
case 1:
// 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);
}));

/*Closes streams for the pictogram bloc
test does not return anything on its own (see lack of expect function)
but the dispose function should throw an error if something goes wrong within
itself. It may however still be relevant to keep this test as it ensures the
streams are actually closed at the end of the test file.
*/
test('Should dispose stream', async((DoneFn done) {
bloc.pictograms.listen((_) {}, onDone: done);
bloc.dispose();
Expand Down
12 changes: 11 additions & 1 deletion test/blocs/settings_bloc_test.dart
Expand Up @@ -87,17 +87,25 @@ void main() {
});

test('Can load settings from username model', async((DoneFn done) {
// Creates listener, fires then SettingsModel is loaded
//Checks that the response is not null, that the response is equal to the
// current setting and verifies that the getSettings method is used.

settingsBloc.settings.listen((SettingsModel? response) {
expect(response, isNotNull);
expect(response!.toJson(), equals(settings.toJson()));
verify(() => api.user.getSettings(any()));
done();
});

//Loads the setting for user
settingsBloc.loadSettings(user);
}));

test('Can update settings', async((DoneFn done) {
// Creates listener for SettingModel, fires when a setting is loaded.
//Checks that the loaded setting is not null, and equal to the
// updatedSettings

settingsBloc.settings.listen((SettingsModel? loadedSettings) {
expect(loadedSettings, isNotNull);
expect(loadedSettings!.toJson(), equals(updatedSettings.toJson()));
Expand All @@ -109,7 +117,9 @@ void main() {
}));

test('Should dispose stream', async((DoneFn done) {
//Creates listener for settings, uses done when fired
settingsBloc.settings.listen((_) {}, onDone: done);
//Disposes the settingsBloc and triggers the listener
settingsBloc.dispose();
}));
}
19 changes: 14 additions & 5 deletions test/blocs/toolbar_bloc_test.dart
Expand Up @@ -22,38 +22,47 @@ void main() {
di.registerDependency<ToolbarBloc>(() => bloc);
});

test('Should insert log out icon when none are defined', async((DoneFn done) {
test('Should insert log out icon when none are defined',
async((DoneFn done) {
// Creates listener for visibleButtons, using a list of IconButtons
//When fired, it expects that the list contains one element
bloc.visibleButtons.skip(1).listen((List<IconButton> response) {
expect(response.length, 1);
done();
});
//Updates the icons on bloc with null and null.
bloc.updateIcons(null, null);
}));

test('Defined icon is added to stream', async((DoneFn done) {
// Creates a map method Icon setting AppBarIcon.undo to null.
final Map<AppBarIcon, VoidCallback> icons = <AppBarIcon, VoidCallback>{
AppBarIcon.undo: () {}
};

// Creates listener for visibleButtons to a list of IconButton
//When fired, the response is expected to have one element.
bloc.visibleButtons.skip(1).listen((List<IconButton> response) {
expect(response.length, 1);
done();
});

//Updates bloc with updateIcons and fires the listener.
bloc.updateIcons(icons, null);
}));

test('Defined icons are added to stream', async((DoneFn done) {
// Creates map method called icons, setting AppBarIcon.undo and .search
// to null
final Map<AppBarIcon, VoidCallback> icons = <AppBarIcon, VoidCallback>{
AppBarIcon.undo: () {},
AppBarIcon.search: () {}
};

// Creates Listener on visibleButtons to a list of IconButtons
// Expects the response be to have two elements
bloc.visibleButtons.skip(1).listen((List<IconButton> response) {
expect(response.length, 2);
done();
});

//Updates the bloc with updateIcons carrying the icon, firing the listener
bloc.updateIcons(icons, null);
}));
}

0 comments on commit 17ccabc

Please sign in to comment.