feat(tests): updated tests coverage to 66.5%

This commit is contained in:
misha
2026-01-14 18:47:11 +03:00
parent 497ff19cfa
commit 13ccf56df0
102 changed files with 10146 additions and 379 deletions

View File

@@ -0,0 +1,188 @@
import 'package:eatvio_app/core/prefs.dart';
import 'package:eatvio_app/features/registration/models/location_model.dart';
import 'package:eatvio_app/features/registration/models/user_registration_model.dart';
import 'package:eatvio_app/features/registration/presentation/bloc/registration_cubit/registration_cubit.dart';
import 'package:eatvio_app/features/registration/presentation/bloc/registration_cubit/registration_state.dart';
import 'package:eatvio_app/features/registration/repositories/registration_repository.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
class MockRegistrationRepository extends Mock implements RegistrationRepository {}
class MockPrefs extends Mock implements Prefs {}
class FakeUserRegistrationSheet extends Fake implements UserRegistrationSheet {}
void main() {
setUpAll(() {
registerFallbackValue(FakeUserRegistrationSheet());
});
group('RegistrationCubit', () {
late RegistrationCubit cubit;
late MockRegistrationRepository mockRepository;
late MockPrefs mockPrefs;
setUp(() {
mockRepository = MockRegistrationRepository();
mockPrefs = MockPrefs();
cubit = RegistrationCubit(registrationRepository: mockRepository, prefs: mockPrefs);
});
tearDown(() {
cubit.close();
});
test('initial state has empty sheet', () {
expect(cubit.state.sheet.firstName, '');
expect(cubit.state.sheet.lastName, '');
expect(cubit.state.sheet.email, '');
expect(cubit.state.sheet.password, '');
expect(cubit.state.error, isNull);
});
test('updateSheet updates state with new sheet', () {
const newSheet = UserRegistrationSheet(
firstName: 'John',
lastName: 'Doe',
email: 'john@example.com',
password: 'password123',
passwordConfirmation: 'password123',
birthdate: '1990-01-01',
gender: 'male',
location: 'New York',
responseBody: '{}',
lang: 'English',
weight: 75,
height: 180,
chestCircumference: 100,
waistCircumference: 80,
hipCircumference: 90,
bloodPressureUp: 120,
bloodPressureDown: 80,
hasHypo: 0,
hasHyper: 0,
aboutAllergy: '',
aboutTakingPils: '',
aboutSportHobbies: '',
favoriteFoods: '',
unfavoriteFoods: '',
additionalInfo: '',
phone: '+1234567890',
);
cubit.updateSheet(newSheet);
expect(cubit.state.sheet.firstName, 'John');
expect(cubit.state.sheet.lastName, 'Doe');
expect(cubit.state.sheet.email, 'john@example.com');
});
test('updateSheet emits new state', () {
const newSheet = UserRegistrationSheet(
firstName: 'Jane',
lastName: 'Smith',
email: 'jane@example.com',
password: 'password123',
passwordConfirmation: 'password123',
birthdate: '1995-05-15',
gender: 'female',
location: 'London',
responseBody: '{}',
lang: 'English',
weight: 60,
height: 165,
chestCircumference: 90,
waistCircumference: 70,
hipCircumference: 85,
bloodPressureUp: 110,
bloodPressureDown: 70,
hasHypo: 0,
hasHyper: 0,
aboutAllergy: '',
aboutTakingPils: '',
aboutSportHobbies: '',
favoriteFoods: '',
unfavoriteFoods: '',
additionalInfo: '',
phone: '+9876543210',
);
expectLater(cubit.stream, emits(isA<RegistrationState>()));
cubit.updateSheet(newSheet);
});
test('getLocationsByQuery returns list of locations', () async {
const query = 'Moscow';
const expectedLocations = ['Moscow, Russia', 'Moscow, USA'];
const locationModel = LocationModel(locations: expectedLocations, responseBody: '{"locations": ["Moscow, Russia", "Moscow, USA"]}');
when(() => mockRepository.getLocationsListByQuery(query)).thenAnswer((_) async => locationModel);
final result = await cubit.getLocationsByQuery(query);
expect(result, expectedLocations);
expect(cubit.state.sheet.responseBody, locationModel.responseBody);
verify(() => mockRepository.getLocationsListByQuery(query)).called(1);
});
test('getLocationsByQuery updates state with responseBody', () async {
const query = 'London';
const expectedLocations = ['London, UK'];
const locationModel = LocationModel(locations: expectedLocations, responseBody: '{"locations": ["London, UK"]}');
when(() => mockRepository.getLocationsListByQuery(query)).thenAnswer((_) async => locationModel);
await cubit.getLocationsByQuery(query);
expect(cubit.state.sheet.responseBody, '{"locations": ["London, UK"]}');
});
test('getLocationsByQuery rethrows exception on error', () async {
const query = 'Invalid';
final exception = Exception('Network error');
when(() => mockRepository.getLocationsListByQuery(query)).thenThrow(exception);
expect(() => cubit.getLocationsByQuery(query), throwsException);
});
test('getLocationsByCoordinates returns list of locations', () async {
const latitude = '55.7558';
const longitude = '37.6173';
const expectedLocations = ['Moscow, Russia'];
const locationModel = LocationModel(locations: expectedLocations, responseBody: '{"locations": ["Moscow, Russia"]}');
when(() => mockRepository.getLocationsListByCoordinates(latitude, longitude)).thenAnswer((_) async => locationModel);
final result = await cubit.getLocationsByCoordinates(latitude, longitude);
expect(result, expectedLocations);
expect(cubit.state.sheet.responseBody, locationModel.responseBody);
verify(() => mockRepository.getLocationsListByCoordinates(latitude, longitude)).called(1);
});
test('getLocationsByCoordinates updates state with responseBody', () async {
const latitude = '51.5074';
const longitude = '-0.1278';
const expectedLocations = ['London, UK'];
const locationModel = LocationModel(locations: expectedLocations, responseBody: '{"locations": ["London, UK"]}');
when(() => mockRepository.getLocationsListByCoordinates(latitude, longitude)).thenAnswer((_) async => locationModel);
await cubit.getLocationsByCoordinates(latitude, longitude);
expect(cubit.state.sheet.responseBody, '{"locations": ["London, UK"]}');
});
test('getLocationsByCoordinates rethrows exception on error', () async {
const latitude = 'invalid';
const longitude = 'invalid';
final exception = Exception('Invalid coordinates');
when(() => mockRepository.getLocationsListByCoordinates(latitude, longitude)).thenThrow(exception);
expect(() => cubit.getLocationsByCoordinates(latitude, longitude), throwsException);
});
});
}

View File

@@ -176,6 +176,38 @@ void main() {
final loadedState = cubit!.state as VerifyEmailStateLoaded;
expect(loadedState.email, 'new@example.com');
});
test('sendEmailCode - обработка ошибки', () async {
cubit = VerifyEmailCubit(profileRepository: mockRepository);
when(mockRepository.getMyProfile()).thenThrow(Exception('Network error'));
await cubit!.sendEmailCode();
expect(cubit!.state, isA<VerifyEmailStateError>());
final errorState = cubit!.state as VerifyEmailStateError;
expect(errorState.errorMessage, 'Ошибка отправки кода.\nПовторите попытку позже');
});
test('tryVerify - обработка ошибки при верификации', () async {
cubit = VerifyEmailCubit(profileRepository: mockRepository);
when(mockRepository.tryVerifyEmailByCode('123456')).thenThrow(Exception('Network error'));
await cubit!.tryVerify('123456');
expect(cubit!.state, isA<VerifyEmailStateVerifyError>());
});
test('changeEmail - обработка ошибки', () async {
cubit = VerifyEmailCubit(profileRepository: mockRepository);
when(mockRepository.sendEmailCode(newEmail: 'new@example.com')).thenThrow(Exception('Network error'));
await cubit!.changeEmail('new@example.com');
expect(cubit!.state, isA<VerifyEmailStateChangeEmailError>());
});
});
}