Files
eatvio_app/test/features/registration/basic_widget_test.dart

640 lines
24 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ignore_for_file: deprecated_member_use
import 'package:eatvio_app/core/prefs.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/registration/presentation/bloc/registration_cubit/registration_cubit.dart';
import 'package:eatvio_app/features/registration/repositories/registration_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'basic_widget_test.mocks.dart';
@GenerateMocks([RegistrationRepository, Prefs])
void main() {
group('Registration Basic Widget Tests', () {
late MockRegistrationRepository mockRegistrationRepository;
late RegistrationCubit registrationCubit;
setUp(() {
mockRegistrationRepository = MockRegistrationRepository();
registrationCubit = RegistrationCubit(registrationRepository: mockRegistrationRepository, prefs: MockPrefs());
});
tearDown(() {
registrationCubit.close();
});
Widget createTestWidget(Widget child) {
return MaterialApp(
theme: ThemeHelper().themeData(),
home: BlocProvider<RegistrationCubit>(
create: (context) => registrationCubit,
child: Scaffold(body: SingleChildScrollView(child: child)),
),
);
}
testWidgets('should create RegistrationCubit successfully', (WidgetTester tester) async {
// Arrange & Act
final cubit = RegistrationCubit(registrationRepository: mockRegistrationRepository, prefs: MockPrefs());
// Assert
expect(cubit, isA<RegistrationCubit>());
cubit.close();
});
testWidgets('should display basic registration form fields', (WidgetTester tester) async {
// Arrange
Widget formWidget = Column(
children: [
TextFormField(
key: const Key('firstNameField'),
decoration: const InputDecoration(labelText: 'Имя', hintText: 'Введите ваше имя'),
),
const SizedBox(height: 16),
TextFormField(
key: const Key('lastNameField'),
decoration: const InputDecoration(labelText: 'Фамилия', hintText: 'Введите вашу фамилию'),
),
const SizedBox(height: 16),
TextFormField(
key: const Key('emailField'),
decoration: const InputDecoration(labelText: 'Email', hintText: 'example@email.com'),
keyboardType: TextInputType.emailAddress,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('passwordField'),
decoration: const InputDecoration(labelText: 'Пароль', hintText: 'Введите пароль'),
obscureText: true,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('passwordConfirmationField'),
decoration: const InputDecoration(labelText: 'Подтверждение пароля', hintText: 'Повторите пароль'),
obscureText: true,
),
const SizedBox(height: 16),
ElevatedButton(key: const Key('registerButton'), onPressed: () {}, child: const Text('Зарегистрироваться')),
],
);
// Act
await tester.pumpWidget(createTestWidget(formWidget));
await tester.pumpAndSettle();
// Assert
expect(find.byKey(const Key('firstNameField')), findsOneWidget);
expect(find.byKey(const Key('lastNameField')), findsOneWidget);
expect(find.byKey(const Key('emailField')), findsOneWidget);
expect(find.byKey(const Key('passwordField')), findsOneWidget);
expect(find.byKey(const Key('passwordConfirmationField')), findsOneWidget);
expect(find.byKey(const Key('registerButton')), findsOneWidget);
});
testWidgets('should update first name when user types in first name field', (WidgetTester tester) async {
// Arrange
Widget formWidget = TextFormField(
key: const Key('firstNameField'),
decoration: const InputDecoration(labelText: 'Имя', hintText: 'Введите ваше имя'),
);
await tester.pumpWidget(createTestWidget(formWidget));
await tester.pumpAndSettle();
const testFirstName = 'Иван';
// Act
await tester.enterText(find.byKey(const Key('firstNameField')), testFirstName);
await tester.pump();
// Assert
expect(find.text(testFirstName), findsOneWidget);
});
testWidgets('should update last name when user types in last name field', (WidgetTester tester) async {
// Arrange
Widget formWidget = TextFormField(
key: const Key('lastNameField'),
decoration: const InputDecoration(labelText: 'Фамилия', hintText: 'Введите вашу фамилию'),
);
await tester.pumpWidget(createTestWidget(formWidget));
await tester.pumpAndSettle();
const testLastName = 'Иванов';
// Act
await tester.enterText(find.byKey(const Key('lastNameField')), testLastName);
await tester.pump();
// Assert
expect(find.text(testLastName), findsOneWidget);
});
testWidgets('should update email when user types in email field', (WidgetTester tester) async {
// Arrange
Widget formWidget = TextFormField(
key: const Key('emailField'),
decoration: const InputDecoration(labelText: 'Email', hintText: 'example@email.com'),
keyboardType: TextInputType.emailAddress,
);
await tester.pumpWidget(createTestWidget(formWidget));
await tester.pumpAndSettle();
const testEmail = 'test@example.com';
// Act
await tester.enterText(find.byKey(const Key('emailField')), testEmail);
await tester.pump();
// Assert
expect(find.text(testEmail), findsOneWidget);
});
testWidgets('should update password when user types in password field', (WidgetTester tester) async {
// Arrange
Widget formWidget = TextFormField(
key: const Key('passwordField'),
decoration: const InputDecoration(labelText: 'Пароль', hintText: 'Введите пароль'),
obscureText: true,
);
await tester.pumpWidget(createTestWidget(formWidget));
await tester.pumpAndSettle();
const testPassword = 'password123';
// Act
await tester.enterText(find.byKey(const Key('passwordField')), testPassword);
await tester.pump();
// Assert
expect(find.text(testPassword), findsOneWidget);
});
testWidgets('should display gender selection', (WidgetTester tester) async {
// Arrange
Widget genderWidget = Column(
children: [
const Text('Пол'),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: RadioListTile<String>(key: const Key('maleRadio'), title: const Text('Мужской'), value: 'male', groupValue: null, onChanged: (value) {}),
),
Expanded(
child: RadioListTile<String>(
key: const Key('femaleRadio'),
title: const Text('Женский'),
value: 'female',
groupValue: null,
onChanged: (value) {},
),
),
],
),
],
);
// Act
await tester.pumpWidget(createTestWidget(genderWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Пол'), findsOneWidget);
expect(find.byKey(const Key('maleRadio')), findsOneWidget);
expect(find.byKey(const Key('femaleRadio')), findsOneWidget);
});
testWidgets('should handle gender selection', (WidgetTester tester) async {
// Arrange
String? selectedGender;
Widget genderWidget = RadioListTile<String>(
key: const Key('maleRadio'),
title: const Text('Мужской'),
value: 'male',
groupValue: selectedGender,
onChanged: (value) {
selectedGender = value;
},
);
await tester.pumpWidget(createTestWidget(genderWidget));
await tester.pumpAndSettle();
// Act
await tester.tap(find.byKey(const Key('maleRadio')));
await tester.pump();
// Assert
expect(selectedGender, equals('male'));
});
testWidgets('should display date of birth picker', (WidgetTester tester) async {
// Arrange
Widget dateWidget = Column(
children: [
const Text('Дата рождения'),
const SizedBox(height: 8),
TextFormField(
key: const Key('birthDateField'),
decoration: const InputDecoration(labelText: 'Дата рождения', hintText: 'ДД.ММ.ГГГГ'),
readOnly: true,
onTap: () {},
),
],
);
// Act
await tester.pumpWidget(createTestWidget(dateWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Дата рождения'), findsNWidgets(2));
expect(find.byKey(const Key('birthDateField')), findsOneWidget);
});
testWidgets('should display phone number field', (WidgetTester tester) async {
// Arrange
Widget phoneWidget = TextFormField(
key: const Key('phoneField'),
decoration: const InputDecoration(labelText: 'Номер телефона', hintText: '+7 (999) 123-45-67'),
keyboardType: TextInputType.phone,
);
// Act
await tester.pumpWidget(createTestWidget(phoneWidget));
await tester.pumpAndSettle();
// Assert
expect(find.byKey(const Key('phoneField')), findsOneWidget);
expect(find.text('Номер телефона'), findsOneWidget);
});
testWidgets('should display height and weight fields', (WidgetTester tester) async {
// Arrange
Widget physicalWidget = Column(
children: [
TextFormField(
key: const Key('heightField'),
decoration: const InputDecoration(labelText: 'Рост (см)', hintText: '175'),
keyboardType: TextInputType.number,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('weightField'),
decoration: const InputDecoration(labelText: 'Вес (кг)', hintText: '70'),
keyboardType: TextInputType.number,
),
],
);
// Act
await tester.pumpWidget(createTestWidget(physicalWidget));
await tester.pumpAndSettle();
// Assert
expect(find.byKey(const Key('heightField')), findsOneWidget);
expect(find.byKey(const Key('weightField')), findsOneWidget);
expect(find.text('Рост (см)'), findsOneWidget);
expect(find.text('Вес (кг)'), findsOneWidget);
});
testWidgets('should display form labels correctly', (WidgetTester tester) async {
// Arrange
Widget formWidget = const Column(
children: [
Text('Имя'),
Text('Фамилия'),
Text('Email'),
Text('Пароль'),
Text('Подтверждение пароля'),
Text('Пол'),
Text('Дата рождения'),
Text('Номер телефона'),
Text('Рост'),
Text('Вес'),
],
);
// Act
await tester.pumpWidget(createTestWidget(formWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Имя'), findsOneWidget);
expect(find.text('Фамилия'), findsOneWidget);
expect(find.text('Email'), findsOneWidget);
expect(find.text('Пароль'), findsOneWidget);
expect(find.text('Подтверждение пароля'), findsOneWidget);
expect(find.text('Пол'), findsOneWidget);
expect(find.text('Дата рождения'), findsOneWidget);
expect(find.text('Номер телефона'), findsOneWidget);
expect(find.text('Рост'), findsOneWidget);
expect(find.text('Вес'), findsOneWidget);
});
testWidgets('should display placeholder text in fields', (WidgetTester tester) async {
// Arrange
Widget formWidget = Column(
children: [
TextFormField(
key: const Key('firstNameField'),
decoration: const InputDecoration(labelText: 'Имя', hintText: 'Введите ваше имя'),
),
const SizedBox(height: 16),
TextFormField(
key: const Key('emailField'),
decoration: const InputDecoration(labelText: 'Email', hintText: 'example@email.com'),
),
],
);
// Act
await tester.pumpWidget(createTestWidget(formWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Введите ваше имя'), findsOneWidget);
expect(find.text('example@email.com'), findsOneWidget);
});
testWidgets('should display register button', (WidgetTester tester) async {
// Arrange
Widget buttonWidget = ElevatedButton(key: const Key('registerButton'), onPressed: () {}, child: const Text('Зарегистрироваться'));
// Act
await tester.pumpWidget(createTestWidget(buttonWidget));
await tester.pumpAndSettle();
// Assert
expect(find.byKey(const Key('registerButton')), findsOneWidget);
expect(find.text('Зарегистрироваться'), findsOneWidget);
});
testWidgets('should handle register button tap', (WidgetTester tester) async {
// Arrange
bool buttonPressed = false;
Widget buttonWidget = ElevatedButton(
key: const Key('registerButton'),
onPressed: () {
buttonPressed = true;
},
child: const Text('Зарегистрироваться'),
);
await tester.pumpWidget(createTestWidget(buttonWidget));
await tester.pumpAndSettle();
// Act
await tester.tap(find.byKey(const Key('registerButton')));
await tester.pump();
// Assert
expect(buttonPressed, isTrue);
});
testWidgets('should display loading state', (WidgetTester tester) async {
// Arrange
Widget loadingWidget = const Center(child: CircularProgressIndicator());
// Act
await tester.pumpWidget(createTestWidget(loadingWidget));
await tester.pump();
// Assert
expect(find.byType(CircularProgressIndicator), findsOneWidget);
});
testWidgets('should display error message', (WidgetTester tester) async {
// Arrange
Widget errorWidget = const Center(child: Text('Произошла ошибка при регистрации'));
// Act
await tester.pumpWidget(createTestWidget(errorWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Произошла ошибка при регистрации'), findsOneWidget);
});
testWidgets('should display success message', (WidgetTester tester) async {
// Arrange
Widget successWidget = const Center(child: Text('Регистрация прошла успешно!'));
// Act
await tester.pumpWidget(createTestWidget(successWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Регистрация прошла успешно!'), findsOneWidget);
});
testWidgets('should validate required fields', (WidgetTester tester) async {
// Arrange
Widget validationWidget = Column(
children: [
TextFormField(
key: const Key('firstNameField'),
decoration: const InputDecoration(labelText: 'Имя', errorText: 'Имя обязательно'),
),
const SizedBox(height: 16),
TextFormField(
key: const Key('emailField'),
decoration: const InputDecoration(labelText: 'Email', errorText: 'Email обязателен'),
),
const SizedBox(height: 16),
TextFormField(
key: const Key('passwordField'),
decoration: const InputDecoration(labelText: 'Пароль', errorText: 'Пароль обязателен'),
),
],
);
// Act
await tester.pumpWidget(createTestWidget(validationWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Имя обязательно'), findsOneWidget);
expect(find.text('Email обязателен'), findsOneWidget);
expect(find.text('Пароль обязателен'), findsOneWidget);
});
testWidgets('should display complete registration form', (WidgetTester tester) async {
// Arrange
Widget completeForm = Column(
children: [
TextFormField(
key: const Key('firstNameField'),
decoration: const InputDecoration(labelText: 'Имя', hintText: 'Введите ваше имя'),
),
const SizedBox(height: 16),
TextFormField(
key: const Key('lastNameField'),
decoration: const InputDecoration(labelText: 'Фамилия', hintText: 'Введите вашу фамилию'),
),
const SizedBox(height: 16),
TextFormField(
key: const Key('emailField'),
decoration: const InputDecoration(labelText: 'Email', hintText: 'example@email.com'),
keyboardType: TextInputType.emailAddress,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('passwordField'),
decoration: const InputDecoration(labelText: 'Пароль', hintText: 'Введите пароль'),
obscureText: true,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('passwordConfirmationField'),
decoration: const InputDecoration(labelText: 'Подтверждение пароля', hintText: 'Повторите пароль'),
obscureText: true,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('phoneField'),
decoration: const InputDecoration(labelText: 'Номер телефона', hintText: '+7 (999) 123-45-67'),
keyboardType: TextInputType.phone,
),
const SizedBox(height: 32),
ElevatedButton(key: const Key('registerButton'), onPressed: () {}, child: const Text('Зарегистрироваться')),
],
);
// Act
await tester.pumpWidget(createTestWidget(completeForm));
await tester.pumpAndSettle();
// Assert
expect(find.byKey(const Key('firstNameField')), findsOneWidget);
expect(find.byKey(const Key('lastNameField')), findsOneWidget);
expect(find.byKey(const Key('emailField')), findsOneWidget);
expect(find.byKey(const Key('passwordField')), findsOneWidget);
expect(find.byKey(const Key('passwordConfirmationField')), findsOneWidget);
expect(find.byKey(const Key('phoneField')), findsOneWidget);
expect(find.byKey(const Key('registerButton')), findsOneWidget);
});
testWidgets('should display registration steps', (WidgetTester tester) async {
// Arrange
Widget stepsWidget = const Column(
children: [
Text('Шаг 1 из 15'),
SizedBox(height: 8),
LinearProgressIndicator(value: 0.067),
SizedBox(height: 16),
Text('Основная информация'),
SizedBox(height: 8),
Text('Шаг 2 из 15'),
SizedBox(height: 8),
LinearProgressIndicator(value: 0.133),
SizedBox(height: 16),
Text('Личные данные'),
],
);
// Act
await tester.pumpWidget(createTestWidget(stepsWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Шаг 1 из 15'), findsOneWidget);
expect(find.text('Шаг 2 из 15'), findsOneWidget);
expect(find.text('Основная информация'), findsOneWidget);
expect(find.text('Личные данные'), findsOneWidget);
expect(find.byType(LinearProgressIndicator), findsNWidgets(2));
});
testWidgets('should display terms and conditions checkbox', (WidgetTester tester) async {
// Arrange
Widget termsWidget = CheckboxListTile(
key: const Key('termsCheckbox'),
title: const Text('Я согласен с условиями использования'),
subtitle: const Text('Нажимая кнопку "Зарегистрироваться", вы принимаете наши условия'),
value: false,
onChanged: (value) {},
);
// Act
await tester.pumpWidget(createTestWidget(termsWidget));
await tester.pumpAndSettle();
// Assert
expect(find.byKey(const Key('termsCheckbox')), findsOneWidget);
expect(find.text('Я согласен с условиями использования'), findsOneWidget);
expect(find.text('Нажимая кнопку "Зарегистрироваться", вы принимаете наши условия'), findsOneWidget);
});
testWidgets('should handle terms and conditions checkbox', (WidgetTester tester) async {
// Arrange
bool termsAccepted = false;
Widget termsWidget = CheckboxListTile(
key: const Key('termsCheckbox'),
title: const Text('Я согласен с условиями использования'),
value: termsAccepted,
onChanged: (value) {
termsAccepted = value ?? false;
},
);
await tester.pumpWidget(createTestWidget(termsWidget));
await tester.pumpAndSettle();
// Act
await tester.tap(find.byKey(const Key('termsCheckbox')));
await tester.pump();
// Assert
expect(termsAccepted, isTrue);
});
testWidgets('should display additional information fields', (WidgetTester tester) async {
// Arrange
Widget additionalWidget = Column(
children: [
const Text('Дополнительная информация'),
const SizedBox(height: 16),
TextFormField(
key: const Key('allergiesField'),
decoration: const InputDecoration(labelText: 'Аллергии', hintText: 'Укажите ваши аллергии'),
maxLines: 3,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('medicationsField'),
decoration: const InputDecoration(labelText: 'Принимаемые лекарства', hintText: 'Укажите принимаемые лекарства'),
maxLines: 3,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('sportsField'),
decoration: const InputDecoration(labelText: 'Любимые виды спорта', hintText: 'Укажите ваши любимые виды спорта'),
maxLines: 2,
),
],
);
// Act
await tester.pumpWidget(createTestWidget(additionalWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Дополнительная информация'), findsOneWidget);
expect(find.byKey(const Key('allergiesField')), findsOneWidget);
expect(find.byKey(const Key('medicationsField')), findsOneWidget);
expect(find.byKey(const Key('sportsField')), findsOneWidget);
expect(find.text('Аллергии'), findsOneWidget);
expect(find.text('Принимаемые лекарства'), findsOneWidget);
expect(find.text('Любимые виды спорта'), findsOneWidget);
});
});
}