From 0ac904e08f222cf50c9a9148bc974ba1e545543b Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Tue, 7 Jun 2022 15:21:59 +0530 Subject: [PATCH] added tests for user_details_bloc --- test/application/auth/auth_bloc_tests.dart | 113 ------------------ .../user_details/user_details_bloc_test.dart | 38 ++++++ .../user_details/user_details_fixtures.dart | 17 +++ 3 files changed, 55 insertions(+), 113 deletions(-) delete mode 100644 test/application/auth/auth_bloc_tests.dart create mode 100644 test/application/user_details/user_details_bloc_test.dart create mode 100644 test/application/user_details/user_details_fixtures.dart diff --git a/test/application/auth/auth_bloc_tests.dart b/test/application/auth/auth_bloc_tests.dart deleted file mode 100644 index d65efcd2..00000000 --- a/test/application/auth/auth_bloc_tests.dart +++ /dev/null @@ -1,113 +0,0 @@ -import 'package:bloc_test/bloc_test.dart'; -import 'package:collaction_app/application/auth/auth_bloc.dart'; -import 'package:collaction_app/domain/auth/auth_failures.dart'; -import 'package:collaction_app/domain/auth/auth_success.dart'; -import 'package:collaction_app/domain/auth/i_auth_repository.dart'; -import 'package:collaction_app/domain/user/i_user_repository.dart'; -import 'package:dartz/dartz.dart'; -import 'package:mocktail/mocktail.dart'; -// ignore: depend_on_referenced_packages -import 'package:test/test.dart'; - -class MockAuthRepository extends Mock implements IAuthRepository {} - -void main() { - group('Authentication BLoC', () { - test('Initial auth state', () { - final bloc = AuthBloc(MockAuthRepository()); - expect(bloc.state, const AuthState.initial()); - }); - - const verificationId = 'verificationId'; - const smsCode = '123456'; - - registerFallbackValue( - const Credential(verificationId: verificationId, smsCode: smsCode), - ); - - final userRepository = MockAuthRepository(); - - { - when( - () => userRepository.verifyPhone( - phoneNumber: any(named: 'phoneNumber'), - ), - ).thenAnswer( - (_) => Stream.fromIterable([ - right( - const AuthSuccess.codeSent( - credential: Credential( - verificationId: verificationId, - ), - ), - ), - ]), - ); - - when( - () => userRepository.signInWithPhone( - authCredentials: any(named: 'authCredentials'), - ), - ).thenAnswer((_) => Future.value(right(true))); - - blocTest( - '"Happy path" transition coverage', - build: () => AuthBloc(userRepository), - act: (AuthBloc bloc) { - bloc.add(const AuthEvent.verifyPhone('+1234567890')); - }, - expect: () => [ - const AuthState.awaitingVerification(), - const AuthState.smsCodeSent(), - ], - ); - - blocTest( - 'SMS Submission', - build: () => AuthBloc(userRepository), - act: (AuthBloc bloc) async { - bloc.add(const AuthEvent.verifyPhone('+1234567890')); - await Future.delayed(const Duration(seconds: 2)); - bloc.add(const AuthEvent.signInWithPhone(smsCode)); - }, - expect: () => [ - const AuthState.awaitingVerification(), - const AuthState.smsCodeSent(), - const AuthState.signingInUser(), - const AuthState.loggedIn(isNewUser: true), - ], - ); - } - - { - final userRepository = MockAuthRepository(); - const error = AuthFailure.verificationFailed(); - when( - () => userRepository.verifyPhone( - phoneNumber: any(named: 'phoneNumber'), - ), - ).thenAnswer((_) => Stream.fromIterable([left(error)])); - - blocTest( - 'Error auth states', - build: () => AuthBloc(userRepository), - act: (AuthBloc bloc) { - bloc.add(const AuthEvent.verifyPhone('+1234567890')); - }, - expect: () => [ - const AuthState.awaitingVerification(), - const AuthState.authError(error) - ], - ); - } - - blocTest( - 'Reset to initial auth state', - build: () => AuthBloc(MockAuthRepository()), - act: (AuthBloc bloc) { - bloc.add(const AuthEvent.reset()); - }, - expect: () => [const AuthState.initial()], - ); - }); -} diff --git a/test/application/user_details/user_details_bloc_test.dart b/test/application/user_details/user_details_bloc_test.dart new file mode 100644 index 00000000..00fa17db --- /dev/null +++ b/test/application/user_details/user_details_bloc_test.dart @@ -0,0 +1,38 @@ +import 'dart:async'; + +import 'package:bloc_test/bloc_test.dart'; +import 'package:collaction_app/application/user_details/user_details_bloc.dart'; +import 'package:collaction_app/domain/user/user.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; + +import 'user_details_fixtures.dart'; + +void main() { + group('test User Details BLoC', () { + final UserDetailsBloc tUserBloc = UserDetailsBloc(tUserRepo); + + test('testing Initial UserDetails BLoC State', () { + expect(tUserBloc.state, const UserDetailsState.initial()); + }); + + when(() => tUserRepo.observeUser()).thenAnswer((_) { + final x = StreamController(); + x.add(tUser); + print('x.stream is User? ${x.stream.distinct() is Stream}'); + return x.stream.distinct(); + }); + + blocTest( + 'testing fetchDetails event', + build: () => tUserBloc, + act: (UserDetailsBloc bloc) { + bloc.add(const UserDetailsEvent.fetchDetails()); + }, + expect: () => [ + const UserDetailsState.fetchingDetails(), + const UserDetailsState.fetchingDetailsFailed(), + ], + ); + }); +} diff --git a/test/application/user_details/user_details_fixtures.dart b/test/application/user_details/user_details_fixtures.dart new file mode 100644 index 00000000..ceedac16 --- /dev/null +++ b/test/application/user_details/user_details_fixtures.dart @@ -0,0 +1,17 @@ +import 'package:collaction_app/domain/user/i_user_repository.dart'; +import 'package:collaction_app/domain/user/user.dart'; + +import 'package:mocktail/mocktail.dart'; + +class MockUserRepository extends Mock implements IUserRepository {} + +final IUserRepository tUserRepo = MockUserRepository(); + +// ignore: avoid_positional_boolean_parameters +Future testGetAnonymousTokenId([bool forceRefresh = false]) => + Future.value(null); + +const User tUser = User( + id: 'tId', + getIdToken: testGetAnonymousTokenId, +);