Skip to content

Commit

Permalink
Merge pull request #905 from Prime-Holding/tests/profile-page-tests
Browse files Browse the repository at this point in the history
Add profile page tests
  • Loading branch information
JvnSlv authored Dec 4, 2024
2 parents 6ee4b63 + 0bd208d commit 6138e71
Showing 1 changed file with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{{> licence.dart }}

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:rx_bloc/rx_bloc.dart';
import 'package:rx_bloc_test/rx_bloc_test.dart';
import 'package:{{project_name}}/base/common_services/push_notifications_service.dart';
Expand All @@ -14,11 +17,14 @@ import 'profile_test.mocks.dart';
void main() {
late PushNotificationsService _notificationService;

void _defineWhen(/*value*/) {
/*
//Sample mock during a test case
when(repository.fetchPage()).thenAnswer((_) async => value);
*/
void defineWhen({
bool? areNotificationsEnabled,
Future<void> Function(Invocation)? syncNotificationSettings,
}) {
when(_notificationService.areNotificationsEnabled())
.thenAnswer((_) => Future.value(areNotificationsEnabled));
when(_notificationService.syncNotificationSettings())
.thenAnswer(syncNotificationSettings ?? (_) => Future.value());
}

ProfileBloc profileBloc() => ProfileBloc(
Expand All @@ -32,46 +38,69 @@ void main() {
rxBlocTest<ProfileBlocType, Result<bool>>(
'test profile_bloc_dart state areNotificationsEnabled',
build: () async {
_defineWhen();
defineWhen(
areNotificationsEnabled: true,
);
return profileBloc();
},
act: (bloc) async {},
act: (bloc) async {
bloc.events.loadNotificationsSettings();
},
state: (bloc) => bloc.states.areNotificationsEnabled,
expect: []);
expect: [
Result.loading(),
Result.success(true),
]);
});

group('test profile_bloc_dart state syncNotificationsStatus', () {
rxBlocTest<ProfileBlocType, Result<bool>>(
'test profile_bloc_dart state syncNotificationsStatus',
build: () async {
_defineWhen();
defineWhen();
return profileBloc();
},
act: (bloc) async {},
act: (bloc) async {
bloc.events.setNotifications(true);
},
state: (bloc) => bloc.states.syncNotificationsStatus,
expect: []);
expect: [
Result.loading(),
Result.success(true),
]);
});

group('test profile_bloc_dart state isLoading', () {
rxBlocTest<ProfileBlocType, bool>('test profile_bloc_dart state isLoading',
build: () async {
_defineWhen();
defineWhen();
return profileBloc();
},
act: (bloc) async {},
state: (bloc) => bloc.states.isLoading,
expect: []);
expect: [
false,
true,
false,
]);
});

group('test profile_bloc_dart state errors', () {
rxBlocTest<ProfileBlocType, ErrorModel>(
'test profile_bloc_dart state errors',
build: () async {
_defineWhen();
defineWhen(
syncNotificationSettings: (_) =>
Future.error(UnknownErrorModel()));

return profileBloc();
},
act: (bloc) async {},
act: (bloc) async {
bloc.events.loadNotificationsSettings();
},
state: (bloc) => bloc.states.errors,
expect: []);
expect: [
isA<UnknownErrorModel>(),
]);
});
}

0 comments on commit 6138e71

Please sign in to comment.