Skip to content

Commit

Permalink
Revisited Tests
Browse files Browse the repository at this point in the history
Updated tests:
- copy_resolve_bloc_test.dart
- pictogram_bloc_test.dart
- environment_test.dart
- giraf_app_bar_widget_test.dart
  • Loading branch information
BicaniWolfie committed Nov 24, 2023
1 parent 438bf2e commit 9b211ae
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 142 deletions.
18 changes: 13 additions & 5 deletions test/blocs/copy_resolve_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,35 @@ 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');
bloc = CopyResolveBloc(api);
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();
});
}));
}
}
29 changes: 20 additions & 9 deletions test/blocs/pictogram_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import 'package:weekplanner/blocs/pictogram_bloc.dart';
class MockPictogramApi extends Mock implements PictogramApi {
@override
Stream<PictogramModel> 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;
}
}
Expand All @@ -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<List<PictogramModel>>
.seeded(<PictogramModel>[]));
// 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);
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();
Expand Down
22 changes: 20 additions & 2 deletions test/providers/environment_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>('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<String>('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<bool>('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<String>('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<String>('PASSWORD'), equals('password'));
});
}
Loading

0 comments on commit 9b211ae

Please sign in to comment.