6177214e-ce7c-49e3-99de-ff9721b26f63 — Commit bb8d79c6

AuthorNKL<nikolaj.king@gmail.com>
Date2024-10-16 15:00:27 +0200
added unit test to authentication_bloc

Changed files

.../authentication/authentication_repository.dart  |  7 +-
 comwell_key_app/pubspec.yaml                       |  1 +
 .../authentication_bloc_test.dart                  | 94 ++++++++++++++++++++++
 3 files changed, 101 insertions(+), 1 deletion(-)

Diff

diff --git a/comwell_key_app/lib/authentication/authentication_repository.dart b/comwell_key_app/lib/authentication/authentication_repository.dart
index f0971c87..f9d73627 100644
--- a/comwell_key_app/lib/authentication/authentication_repository.dart
+++ b/comwell_key_app/lib/authentication/authentication_repository.dart
@@ -4,9 +4,14 @@ import 'package:comwell_key_app/authentication/enum/authentication_status.dart';
import 'package:comwell_key_app/utils/secure_storage.dart';
class AuthenticationRepository {
- final SecureStorage secureStorage = SecureStorage();
+ late SecureStorage secureStorage;
final _controller = StreamController<AuthenticationStatus>.broadcast();
+ AuthenticationRepository() {
+ secureStorage = SecureStorage();
+ }
+
+
Stream<AuthenticationStatus> get status async* {
await Future<void>.delayed(const Duration(seconds: 1));
//yield AuthenticationStatus.unauthenticated;
diff --git a/comwell_key_app/pubspec.yaml b/comwell_key_app/pubspec.yaml
index 2ba21098..f461b65a 100644
--- a/comwell_key_app/pubspec.yaml
+++ b/comwell_key_app/pubspec.yaml
@@ -45,6 +45,7 @@ dependencies:
wolt_modal_sheet: ^0.9.2
aad_b2c_webview: ^0.0.57
modal_bottom_sheet: ^3.0.0
+ mocktail: ^1.0.4
dev_dependencies:
flutter_test:
diff --git a/comwell_key_app/test/authentication_test/authentication_bloc_test.dart b/comwell_key_app/test/authentication_test/authentication_bloc_test.dart
new file mode 100644
index 00000000..169749f5
--- /dev/null
+++ b/comwell_key_app/test/authentication_test/authentication_bloc_test.dart
@@ -0,0 +1,94 @@
+import 'package:bloc_test/bloc_test.dart';
+import 'package:comwell_key_app/authentication/authentication_repository.dart';
+import 'package:comwell_key_app/authentication/bloc/authentication_bloc.dart';
+import 'package:comwell_key_app/authentication/enum/authentication_status.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:mocktail/mocktail.dart';
+
+class MockAuthenticationRepository extends Mock
+ implements AuthenticationRepository {}
+
+void main() {
+ late AuthenticationRepository authenticationRepository;
+
+ setUp(() {
+ authenticationRepository = MockAuthenticationRepository();
+ });
+
+ AuthenticationBloc buildBloc() {
+ return AuthenticationBloc(
+ authenticationRepository: authenticationRepository,
+ );
+ }
+
+ group('authenticationBloc', () {
+ group('AuthenticationSubscriptionRequested', () {
+ final error = Exception('oops');
+ blocTest<AuthenticationBloc, AuthenticationState>(
+ 'emits [unauthenticated] when status is unauthenticated',
+ setUp: () {
+ when(() => authenticationRepository.status).thenAnswer(
+ (_) => Stream.value(AuthenticationStatus.unauthenticated),
+ );
+ },
+ build: buildBloc,
+ act: (bloc) => bloc.add(AuthenticationSubscriptionRequested()),
+ expect: () => const [AuthenticationState.unauthenticated()],
+ );
+
+ blocTest<AuthenticationBloc, AuthenticationState>(
+ 'emits [authenticated] when status is authenticated',
+ setUp: () {
+ when(() => authenticationRepository.status).thenAnswer(
+ (_) => Stream.value(AuthenticationStatus.authenticated),
+ );
+ },
+ build: buildBloc,
+ act: (bloc) => bloc.add(AuthenticationSubscriptionRequested()),
+ expect: () => const [AuthenticationState.authenticated()],
+ );
+
+ blocTest<AuthenticationBloc, AuthenticationState>(
+ 'adds error when status stream emits an error',
+ setUp: () {
+ when(
+ () => authenticationRepository.status,
+ ).thenAnswer((_) => Stream.error(error));
+ },
+ build: buildBloc,
+ act: (bloc) => bloc.add(AuthenticationSubscriptionRequested()),
+ errors: () => [error],
+ );
+ });
+ });
+ test('initial state is unknown', () {
+ final authenticationBloc = AuthenticationBloc(
+ authenticationRepository: authenticationRepository,
+ );
+ expect(authenticationBloc.state, const AuthenticationState.unknown());
+ });
+
+ test('emits [authenticated] when logIn is called', () {
+ when(() => authenticationRepository.logIn()).thenAnswer((_) async {});
+ when(() => authenticationRepository.status).thenAnswer((_) async* {
+ yield AuthenticationStatus.authenticated;
+ });
+
+ authenticationRepository.logIn();
+
+ expect(authenticationRepository.status,
+ emitsInOrder([AuthenticationStatus.authenticated]));
+ });
+
+ test('emits [unauthenticated] when logOut is called', () {
+ when(() => authenticationRepository.logOut()).thenAnswer((_) async {});
+ when(() => authenticationRepository.status).thenAnswer((_) async* {
+ yield AuthenticationStatus.unauthenticated;
+ });
+
+ authenticationRepository.logOut();
+
+ expect(authenticationRepository.status,
+ emitsInOrder([AuthenticationStatus.unauthenticated]));
+ });
+}