|
| 1 | +import 'package:common/core/failure/failure.dart'; |
| 2 | +import 'package:common/core/result_type.dart'; |
| 3 | +import 'package:domain/bloc/auth/auth_cubit.dart'; |
| 4 | +import 'package:domain/repositories/auth_repository.dart'; |
| 5 | +import 'package:domain/services/AuthService.dart'; |
| 6 | +import 'package:flutter_test/flutter_test.dart'; |
| 7 | +import 'package:mocktail/mocktail.dart'; |
| 8 | + |
| 9 | +class MockAuthRepository extends Mock implements AuthRepository {} |
| 10 | + |
| 11 | +class MockSessionCubit extends Mock implements AuthCubit {} |
| 12 | + |
| 13 | +void main() { |
| 14 | + late AuthRepository mockAuthRepository; |
| 15 | + late AuthCubit mockSessionCubit; |
| 16 | + late AuthService authService; |
| 17 | + |
| 18 | + const username = 'testuser'; |
| 19 | + const password = 'password123'; |
| 20 | + final failure = Exception('Invalid credentials'); |
| 21 | + |
| 22 | + setUp(() { |
| 23 | + mockAuthRepository = MockAuthRepository(); |
| 24 | + mockSessionCubit = MockSessionCubit(); |
| 25 | + authService = AuthService(mockAuthRepository, mockSessionCubit); |
| 26 | + }); |
| 27 | + |
| 28 | + group('AuthService', () { |
| 29 | + test('logInWithCredentials calls isLogin on success', () async { |
| 30 | + when(() => mockAuthRepository.login(username, password)) |
| 31 | + .thenAnswer((_) async => TSuccess(null)); |
| 32 | + when(() => mockSessionCubit.isLoading()).thenReturn(null); |
| 33 | + when(() => mockSessionCubit.isLogin()).thenReturn(null); |
| 34 | + |
| 35 | + await authService.logInWithCredentials(username, password); |
| 36 | + |
| 37 | + verify(() => mockSessionCubit.isLoading()).called(1); |
| 38 | + verify(() => mockSessionCubit.isLogin()).called(1); |
| 39 | + verifyNever(() => mockSessionCubit.isError(any())); |
| 40 | + }); |
| 41 | + |
| 42 | + test('logInWithCredentials calls isError on failure', () async { |
| 43 | + when(() => mockAuthRepository.login(username, password)) |
| 44 | + .thenAnswer((_) async => TError(failure)); |
| 45 | + when(() => mockSessionCubit.isLoading()).thenReturn(null); |
| 46 | + when(() => mockSessionCubit.isError(failure)).thenReturn(null); |
| 47 | + |
| 48 | + await authService.logInWithCredentials(username, password); |
| 49 | + |
| 50 | + verify(() => mockSessionCubit.isLoading()).called(1); |
| 51 | + verify(() => mockSessionCubit.isError(failure)).called(1); |
| 52 | + verifyNever(() => mockSessionCubit.isLogin()); |
| 53 | + }); |
| 54 | + |
| 55 | + test('onValidate calls isLogin when user is logged in', () { |
| 56 | + when(() => mockAuthRepository.isLoggedIn()).thenReturn(true); |
| 57 | + when(() => mockSessionCubit.isLogin()).thenReturn(null); |
| 58 | + |
| 59 | + authService.onValidate(); |
| 60 | + |
| 61 | + verify(() => mockAuthRepository.isLoggedIn()).called(1); |
| 62 | + verify(() => mockSessionCubit.isLogin()).called(1); |
| 63 | + verifyNever(() => mockSessionCubit.isLogOut()); |
| 64 | + }); |
| 65 | + |
| 66 | + test('onValidate calls isLogOut when user is not logged in', () { |
| 67 | + when(() => mockAuthRepository.isLoggedIn()).thenReturn(false); |
| 68 | + when(() => mockSessionCubit.isLogOut()).thenReturn(null); |
| 69 | + |
| 70 | + authService.onValidate(); |
| 71 | + |
| 72 | + verify(() => mockAuthRepository.isLoggedIn()).called(1); |
| 73 | + verify(() => mockSessionCubit.isLogOut()).called(1); |
| 74 | + verifyNever(() => mockSessionCubit.isLogin()); |
| 75 | + }); |
| 76 | + |
| 77 | + test('onLogout calls logout and then isLogOut', () async { |
| 78 | + when(() => mockAuthRepository.logout()).thenAnswer((_) async => {}); |
| 79 | + when(() => mockSessionCubit.isLogOut()).thenReturn(null); |
| 80 | + |
| 81 | + await authService.onLogout(); |
| 82 | + |
| 83 | + verify(() => mockAuthRepository.logout()).called(1); |
| 84 | + verify(() => mockSessionCubit.isLogOut()).called(1); |
| 85 | + }); |
| 86 | + }); |
| 87 | +} |
0 commit comments