feat(auth): tests Issue-761

This commit is contained in:
mso7nueh
2025-11-19 04:47:30 +03:00
parent db5dddff86
commit 6214d19a14
2 changed files with 551 additions and 0 deletions

View File

@@ -0,0 +1,138 @@
import 'package:eatvio_app/features/auth/models/token_model.dart';
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_cubit.dart';
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_state.dart';
import 'package:eatvio_app/features/auth/repositories/auth_repository.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'auth_cubit_test.mocks.dart';
@GenerateNiceMocks([MockSpec<AuthRepository>()])
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('AuthCubit', () {
AuthCubit? authCubit;
late MockAuthRepository mockAuthRepository;
const testToken = TokenModel(token: 'test_token_12345');
setUp(() async {
mockAuthRepository = MockAuthRepository();
SharedPreferences.setMockInitialValues({});
// Mock Fluttertoast plugin
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
const MethodChannel('PonnamKarthik/fluttertoast'),
(MethodCall methodCall) async {
return null;
},
);
});
tearDown(() async {
if (authCubit != null && !authCubit!.isClosed) {
await Future.delayed(const Duration(milliseconds: 100));
authCubit!.close();
authCubit = null;
}
// Clear mock handlers
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
const MethodChannel('PonnamKarthik/fluttertoast'),
null,
);
});
test('начальное состояние - AuthState', () {
authCubit = AuthCubit(authRepository: mockAuthRepository);
expect(authCubit!.state, const AuthState());
});
test('getTokenByCredentials - успешная авторизация', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenAnswer((_) async => testToken);
final result = await authCubit!.getTokenByCredentials('test@example.com', 'password123');
expect(result, true);
await Future.delayed(const Duration(milliseconds: 100));
expect(authCubit!.state, isA<AuthStateLoaded>());
final loadedState = authCubit!.state as AuthStateLoaded;
expect(loadedState.token, 'test_token_12345');
verify(mockAuthRepository.getTokenByCredentials('test@example.com', 'password123')).called(1);
});
test('getTokenByCredentials - переход в состояние loading', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenAnswer((_) async => Future.delayed(const Duration(milliseconds: 50)).then((_) => testToken));
final future = authCubit!.getTokenByCredentials('test@example.com', 'password123');
await Future.delayed(const Duration(milliseconds: 10));
expect(authCubit!.state, isA<AuthStateLoading>());
await future;
});
test('getTokenByCredentials - обработка ошибки авторизации', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenThrow(Exception('Неверный адрес электронной почты или пароль'));
final result = await authCubit!.getTokenByCredentials('wrong@example.com', 'wrongpassword');
expect(result, false);
await Future.delayed(const Duration(milliseconds: 100));
expect(authCubit!.state, isA<AuthStateError>());
final errorState = authCubit!.state as AuthStateError;
expect(errorState.errorMessage, contains('Exception'));
verify(mockAuthRepository.getTokenByCredentials('wrong@example.com', 'wrongpassword')).called(1);
});
test('getTokenByCredentials - обработка различных типов ошибок', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenThrow(FormatException('Invalid credentials format'));
final result = await authCubit!.getTokenByCredentials('invalid', 'pass');
expect(result, false);
await Future.delayed(const Duration(milliseconds: 100));
expect(authCubit!.state, isA<AuthStateError>());
final errorState = authCubit!.state as AuthStateError;
expect(errorState.errorMessage, contains('FormatException'));
});
test('getTokenByCredentials - проверка вызова с правильными параметрами', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenAnswer((_) async => testToken);
await authCubit!.getTokenByCredentials('user@test.com', 'securePassword123');
verify(mockAuthRepository.getTokenByCredentials('user@test.com', 'securePassword123')).called(1);
verifyNever(mockAuthRepository.getTokenByCredentials('other@test.com', 'otherPassword'));
});
test('getTokenByCredentials - множественные вызовы', () async {
authCubit = AuthCubit(authRepository: mockAuthRepository);
when(mockAuthRepository.getTokenByCredentials(any, any))
.thenAnswer((_) async => testToken);
await authCubit!.getTokenByCredentials('first@test.com', 'password1');
await Future.delayed(const Duration(milliseconds: 100));
await authCubit!.getTokenByCredentials('second@test.com', 'password2');
verify(mockAuthRepository.getTokenByCredentials('first@test.com', 'password1')).called(1);
verify(mockAuthRepository.getTokenByCredentials('second@test.com', 'password2')).called(1);
});
});
}