forked from eatvio/eatvio_app
feat(dependencies): add rxdart package and improve string extension methods for better handling of empty strings and gender parsing
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:eatvio_app/features/profile/models/profile_model.dart';
|
||||
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
||||
import 'package:eatvio_app/features/registration/presentation/bloc/verify_email_cubit/verify_email_cubit.dart';
|
||||
import 'package:eatvio_app/features/registration/presentation/bloc/verify_email_cubit/verify_email_state.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import 'verify_email_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<ProfileRepository>()])
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('VerifyEmailCubit', () {
|
||||
VerifyEmailCubit? cubit;
|
||||
late MockProfileRepository mockRepository;
|
||||
|
||||
setUp(() {
|
||||
mockRepository = MockProfileRepository();
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (cubit != null && !cubit!.isClosed) {
|
||||
await cubit!.close();
|
||||
cubit = null;
|
||||
}
|
||||
});
|
||||
|
||||
test('начальное состояние - loading', () {
|
||||
cubit = VerifyEmailCubit(profileRepository: mockRepository);
|
||||
|
||||
expect(cubit!.state, isA<VerifyEmailStateLoading>());
|
||||
});
|
||||
|
||||
test('sendEmailCode - успешная отправка кода', () async {
|
||||
cubit = VerifyEmailCubit(profileRepository: mockRepository);
|
||||
|
||||
const testProfile = ProfileModel(
|
||||
id: 1,
|
||||
slug: 'test-user',
|
||||
name: 'Test User',
|
||||
email: 'test@example.com',
|
||||
phoneConfirmationState: 'confirmed',
|
||||
isPublicProfile: true,
|
||||
isBlockedByMe: false,
|
||||
isBlockedByAuthor: false,
|
||||
isBlockedByUser: false,
|
||||
isEntitySubscribedByMe: false,
|
||||
profilePhotoUrl: 'https://example.com/icon.jpg',
|
||||
roles: [],
|
||||
badges: [],
|
||||
subscriptions: [],
|
||||
subscribers: [],
|
||||
);
|
||||
|
||||
when(mockRepository.getMyProfile())
|
||||
.thenAnswer((_) async => testProfile);
|
||||
when(mockRepository.sendEmailCode())
|
||||
.thenAnswer((_) async => {});
|
||||
|
||||
await cubit!.sendEmailCode();
|
||||
|
||||
expect(cubit!.state, isA<VerifyEmailStateLoaded>());
|
||||
final loadedState = cubit!.state as VerifyEmailStateLoaded;
|
||||
expect(loadedState.email, 'test@example.com');
|
||||
});
|
||||
|
||||
test('tryVerify - успешная верификация', () async {
|
||||
cubit = VerifyEmailCubit(profileRepository: mockRepository);
|
||||
|
||||
const testProfile = ProfileModel(
|
||||
id: 1,
|
||||
slug: 'test-user',
|
||||
name: 'Test User',
|
||||
email: 'test@example.com',
|
||||
phoneConfirmationState: 'confirmed',
|
||||
isPublicProfile: true,
|
||||
isBlockedByMe: false,
|
||||
isBlockedByAuthor: false,
|
||||
isBlockedByUser: false,
|
||||
isEntitySubscribedByMe: false,
|
||||
profilePhotoUrl: 'https://example.com/icon.jpg',
|
||||
roles: [],
|
||||
badges: [],
|
||||
subscriptions: [],
|
||||
subscribers: [],
|
||||
);
|
||||
|
||||
when(mockRepository.getMyProfile())
|
||||
.thenAnswer((_) async => testProfile);
|
||||
when(mockRepository.sendEmailCode())
|
||||
.thenAnswer((_) async => {});
|
||||
when(mockRepository.tryVerifyEmailByCode('123456'))
|
||||
.thenAnswer((_) async => true);
|
||||
|
||||
await cubit!.sendEmailCode();
|
||||
|
||||
// Используем runZonedGuarded для перехвата асинхронных ошибок AppMetrica
|
||||
await runZonedGuarded(() async {
|
||||
await cubit!.tryVerify('123456');
|
||||
// Даем время для завершения асинхронных операций AppMetrica
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
}, (error, stack) {
|
||||
// Игнорируем ошибки AppMetrica в тестах
|
||||
});
|
||||
|
||||
expect(cubit!.state, isA<VerifyEmailStateVerifySuccess>());
|
||||
});
|
||||
|
||||
test('tryVerify - неверный код', () async {
|
||||
cubit = VerifyEmailCubit(profileRepository: mockRepository);
|
||||
|
||||
const testProfile = ProfileModel(
|
||||
id: 1,
|
||||
slug: 'test-user',
|
||||
name: 'Test User',
|
||||
email: 'test@example.com',
|
||||
phoneConfirmationState: 'confirmed',
|
||||
isPublicProfile: true,
|
||||
isBlockedByMe: false,
|
||||
isBlockedByAuthor: false,
|
||||
isBlockedByUser: false,
|
||||
isEntitySubscribedByMe: false,
|
||||
profilePhotoUrl: 'https://example.com/icon.jpg',
|
||||
roles: [],
|
||||
badges: [],
|
||||
subscriptions: [],
|
||||
subscribers: [],
|
||||
);
|
||||
|
||||
when(mockRepository.getMyProfile())
|
||||
.thenAnswer((_) async => testProfile);
|
||||
when(mockRepository.sendEmailCode())
|
||||
.thenAnswer((_) async => {});
|
||||
when(mockRepository.tryVerifyEmailByCode('wrong'))
|
||||
.thenAnswer((_) async => false);
|
||||
|
||||
await cubit!.sendEmailCode();
|
||||
await cubit!.tryVerify('wrong');
|
||||
|
||||
expect(cubit!.state, isA<VerifyEmailStateVerifyError>());
|
||||
});
|
||||
|
||||
test('changeEmail - успешная смена email', () async {
|
||||
cubit = VerifyEmailCubit(profileRepository: mockRepository);
|
||||
|
||||
const testProfile = ProfileModel(
|
||||
id: 1,
|
||||
slug: 'test-user',
|
||||
name: 'Test User',
|
||||
email: 'test@example.com',
|
||||
phoneConfirmationState: 'confirmed',
|
||||
isPublicProfile: true,
|
||||
isBlockedByMe: false,
|
||||
isBlockedByAuthor: false,
|
||||
isBlockedByUser: false,
|
||||
isEntitySubscribedByMe: false,
|
||||
profilePhotoUrl: 'https://example.com/icon.jpg',
|
||||
roles: [],
|
||||
badges: [],
|
||||
subscriptions: [],
|
||||
subscribers: [],
|
||||
);
|
||||
|
||||
when(mockRepository.getMyProfile())
|
||||
.thenAnswer((_) async => testProfile);
|
||||
when(mockRepository.sendEmailCode(newEmail: 'new@example.com'))
|
||||
.thenAnswer((_) async => {});
|
||||
|
||||
await cubit!.changeEmail('new@example.com');
|
||||
|
||||
expect(cubit!.state, isA<VerifyEmailStateLoaded>());
|
||||
final loadedState = cubit!.state as VerifyEmailStateLoaded;
|
||||
expect(loadedState.email, 'new@example.com');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user