Files
eatvio_app/test/features/profile/presentation/basic_widget_test.dart
2025-11-21 23:31:13 +03:00

448 lines
17 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.
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart';
import 'package:eatvio_app/features/profile/presentation/bloc/profile_cubit/profile_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../../fixtures/doubles/profile_doubles.dart';
import '../../../fixtures/fixture_reader.dart';
void main() {
group('Profile Basic Widget Tests', () {
late FixtureProfileRepository profileRepository;
late FixtureChatRepository chatRepository;
late FixtureConsultantRepository consultantRepository;
late ProfileCubit profileCubit;
late Map<String, dynamic> weightFixture;
late Map<String, dynamic> circumferenceFixture;
setUp(() {
weightFixture = FixtureReader.readAsMap('profile/add_weight_request.json');
circumferenceFixture = FixtureReader.readAsMap('profile/add_circumference_request.json');
profileRepository = FixtureProfileRepository();
chatRepository = FixtureChatRepository();
consultantRepository = FixtureConsultantRepository();
profileCubit = ProfileCubit(profileRepository: profileRepository, chatRepository: chatRepository, consultantRepository: consultantRepository);
});
tearDown(() {
profileCubit.close();
});
Widget createTestWidget(Widget child) {
return MaterialApp(
theme: ThemeHelper().themeData(),
home: BlocProvider<ProfileCubit>(
create: (context) => profileCubit,
child: Scaffold(body: SingleChildScrollView(child: child)),
),
);
}
testWidgets('should create ProfileCubit successfully', (WidgetTester tester) async {
// Arrange & Act
final cubit = ProfileCubit(profileRepository: profileRepository, chatRepository: chatRepository, consultantRepository: consultantRepository);
// Assert
expect(cubit, isA<ProfileCubit>());
expect(cubit.state, isA<ProfileState>());
cubit.close();
});
testWidgets('should display weight input field', (WidgetTester tester) async {
// Arrange
Widget weightWidget = TextFormField(
key: const Key('weightField'),
decoration: const InputDecoration(labelText: 'Вес', hintText: 'Введите вес в кг'),
keyboardType: TextInputType.number,
);
// Act
await tester.pumpWidget(createTestWidget(weightWidget));
await tester.pumpAndSettle();
// Assert
expect(find.byKey(const Key('weightField')), findsOneWidget);
expect(find.text('Вес'), findsOneWidget);
expect(find.text('Введите вес в кг'), findsOneWidget);
});
testWidgets('should update weight when user types in weight field', (WidgetTester tester) async {
// Arrange
Widget weightWidget = TextFormField(
key: const Key('weightField'),
decoration: const InputDecoration(labelText: 'Вес'),
keyboardType: TextInputType.number,
);
await tester.pumpWidget(createTestWidget(weightWidget));
await tester.pumpAndSettle();
final testWeight = weightFixture['weight'] as double;
final testWeightString = testWeight.toString();
// Act
await tester.enterText(find.byKey(const Key('weightField')), testWeightString);
await tester.pump();
// Assert
expect(find.text(testWeightString), findsOneWidget);
});
testWidgets('should display circumference input fields', (WidgetTester tester) async {
// Arrange
Widget circumferenceWidget = Column(
children: [
TextFormField(
key: const Key('chestCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват груди (см)'),
keyboardType: TextInputType.number,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('waistCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват талии (см)'),
keyboardType: TextInputType.number,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('hipCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват бедер (см)'),
keyboardType: TextInputType.number,
),
],
);
// Act
await tester.pumpWidget(createTestWidget(circumferenceWidget));
await tester.pumpAndSettle();
// Assert
expect(find.byKey(const Key('chestCircumferenceField')), findsOneWidget);
expect(find.byKey(const Key('waistCircumferenceField')), findsOneWidget);
expect(find.byKey(const Key('hipCircumferenceField')), findsOneWidget);
expect(find.text('Обхват груди (см)'), findsOneWidget);
expect(find.text('Обхват талии (см)'), findsOneWidget);
expect(find.text('Обхват бедер (см)'), findsOneWidget);
});
testWidgets('should update circumference values when user types', (WidgetTester tester) async {
// Arrange
Widget circumferenceWidget = Column(
children: [
TextFormField(
key: const Key('chestCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват груди (см)'),
keyboardType: TextInputType.number,
),
TextFormField(
key: const Key('waistCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват талии (см)'),
keyboardType: TextInputType.number,
),
TextFormField(
key: const Key('hipCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват бедер (см)'),
keyboardType: TextInputType.number,
),
],
);
await tester.pumpWidget(createTestWidget(circumferenceWidget));
await tester.pumpAndSettle();
final chestValue = (circumferenceFixture['chest_circumference'] as int).toString();
final waistValue = (circumferenceFixture['waist_circumference'] as int).toString();
final hipValue = (circumferenceFixture['hip_circumference'] as int).toString();
// Act
await tester.enterText(find.byKey(const Key('chestCircumferenceField')), chestValue);
await tester.enterText(find.byKey(const Key('waistCircumferenceField')), waistValue);
await tester.enterText(find.byKey(const Key('hipCircumferenceField')), hipValue);
await tester.pump();
// Assert
expect(find.text(chestValue), findsOneWidget);
expect(find.text(waistValue), findsOneWidget);
expect(find.text(hipValue), findsOneWidget);
});
testWidgets('should display save weight button', (WidgetTester tester) async {
// Arrange
Widget buttonWidget = ElevatedButton(key: const Key('saveWeightButton'), onPressed: () {}, child: const Text('Сохранить вес'));
// Act
await tester.pumpWidget(createTestWidget(buttonWidget));
await tester.pumpAndSettle();
// Assert
expect(find.byKey(const Key('saveWeightButton')), findsOneWidget);
expect(find.text('Сохранить вес'), findsOneWidget);
});
testWidgets('should display save circumference button', (WidgetTester tester) async {
// Arrange
Widget buttonWidget = ElevatedButton(key: const Key('saveCircumferenceButton'), onPressed: () {}, child: const Text('Сохранить замеры'));
// Act
await tester.pumpWidget(createTestWidget(buttonWidget));
await tester.pumpAndSettle();
// Assert
expect(find.byKey(const Key('saveCircumferenceButton')), findsOneWidget);
expect(find.text('Сохранить замеры'), findsOneWidget);
});
testWidgets('should handle save weight button tap', (WidgetTester tester) async {
// Arrange
bool buttonPressed = false;
Widget buttonWidget = ElevatedButton(
key: const Key('saveWeightButton'),
onPressed: () {
buttonPressed = true;
},
child: const Text('Сохранить вес'),
);
await tester.pumpWidget(createTestWidget(buttonWidget));
await tester.pumpAndSettle();
// Act
await tester.tap(find.byKey(const Key('saveWeightButton')));
await tester.pump();
// Assert
expect(buttonPressed, isTrue);
});
testWidgets('should handle save circumference button tap', (WidgetTester tester) async {
// Arrange
bool buttonPressed = false;
Widget buttonWidget = ElevatedButton(
key: const Key('saveCircumferenceButton'),
onPressed: () {
buttonPressed = true;
},
child: const Text('Сохранить замеры'),
);
await tester.pumpWidget(createTestWidget(buttonWidget));
await tester.pumpAndSettle();
// Act
await tester.tap(find.byKey(const Key('saveCircumferenceButton')));
await tester.pump();
// Assert
expect(buttonPressed, isTrue);
});
testWidgets('should display profile information fields', (WidgetTester tester) async {
// Arrange
Widget profileWidget = const Column(children: [Text('Имя: Анна'), Text('Email: anna@example.com'), Text('Вес: 65.0 кг'), Text('Рост: 170 см')]);
// Act
await tester.pumpWidget(createTestWidget(profileWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Имя: Анна'), findsOneWidget);
expect(find.text('Email: anna@example.com'), findsOneWidget);
expect(find.text('Вес: 65.0 кг'), findsOneWidget);
expect(find.text('Рост: 170 см'), findsOneWidget);
});
testWidgets('should display weight chart section', (WidgetTester tester) async {
// Arrange
Widget chartWidget = Column(
children: [
const Text('График веса'),
Container(
key: const Key('weightChartContainer'),
height: 200,
color: Colors.grey[200],
child: const Center(child: Text('График веса')),
),
],
);
// Act
await tester.pumpWidget(createTestWidget(chartWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('График веса'), findsNWidgets(2));
expect(find.byKey(const Key('weightChartContainer')), findsOneWidget);
});
testWidgets('should display circumference chart section', (WidgetTester tester) async {
// Arrange
Widget chartWidget = Column(
children: [
const Text('График замеров'),
Container(
key: const Key('circumferenceChartContainer'),
height: 200,
color: Colors.grey[200],
child: const Center(child: Text('График замеров')),
),
],
);
// Act
await tester.pumpWidget(createTestWidget(chartWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('График замеров'), findsNWidgets(2));
expect(find.byKey(const Key('circumferenceChartContainer')), findsOneWidget);
});
testWidgets('should send correct request when adding weight', (WidgetTester tester) async {
// Arrange
final testWeight = weightFixture['weight'] as double;
// Act
await profileCubit.addWeight(weight: testWeight);
await tester.pumpAndSettle();
// Assert
expect(profileRepository.addWeightRequests.length, 1);
final request = profileRepository.addWeightRequests.single;
expect(request['weight'], testWeight);
});
testWidgets('should send correct request when adding circumference', (WidgetTester tester) async {
// Arrange
final chest = circumferenceFixture['chest_circumference'] as int;
final waist = circumferenceFixture['waist_circumference'] as int;
final hip = circumferenceFixture['hip_circumference'] as int;
// Act
await profileCubit.addCircumference(chestCircumference: chest, waistCircumference: waist, hipCircumference: hip);
await tester.pumpAndSettle();
// Assert
expect(profileRepository.addCircumferenceRequests.length, 1);
final request = profileRepository.addCircumferenceRequests.single;
expect(request['chest_circumference'], chest);
expect(request['waist_circumference'], waist);
expect(request['hip_circumference'], hip);
});
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 validate weight input', (WidgetTester tester) async {
// Arrange
Widget validationWidget = TextFormField(
key: const Key('weightField'),
decoration: const InputDecoration(labelText: 'Вес', errorText: 'Введите корректное значение веса'),
);
// Act
await tester.pumpWidget(createTestWidget(validationWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Введите корректное значение веса'), findsOneWidget);
});
testWidgets('should validate circumference inputs', (WidgetTester tester) async {
// Arrange
Widget validationWidget = Column(
children: [
TextFormField(
key: const Key('chestCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват груди (см)', errorText: 'Введите корректное значение'),
),
TextFormField(
key: const Key('waistCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват талии (см)', errorText: 'Введите корректное значение'),
),
TextFormField(
key: const Key('hipCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват бедер (см)', errorText: 'Введите корректное значение'),
),
],
);
// Act
await tester.pumpWidget(createTestWidget(validationWidget));
await tester.pumpAndSettle();
// Assert
expect(find.text('Введите корректное значение'), findsNWidgets(3));
});
testWidgets('should display complete profile form', (WidgetTester tester) async {
// Arrange
Widget completeForm = Column(
children: [
TextFormField(
key: const Key('weightField'),
decoration: const InputDecoration(labelText: 'Вес'),
keyboardType: TextInputType.number,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('chestCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват груди (см)'),
keyboardType: TextInputType.number,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('waistCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват талии (см)'),
keyboardType: TextInputType.number,
),
const SizedBox(height: 16),
TextFormField(
key: const Key('hipCircumferenceField'),
decoration: const InputDecoration(labelText: 'Обхват бедер (см)'),
keyboardType: TextInputType.number,
),
const SizedBox(height: 32),
ElevatedButton(key: const Key('saveWeightButton'), onPressed: () {}, child: const Text('Сохранить вес')),
ElevatedButton(key: const Key('saveCircumferenceButton'), onPressed: () {}, child: const Text('Сохранить замеры')),
],
);
// Act
await tester.pumpWidget(createTestWidget(completeForm));
await tester.pumpAndSettle();
// Assert
expect(find.byKey(const Key('weightField')), findsOneWidget);
expect(find.byKey(const Key('chestCircumferenceField')), findsOneWidget);
expect(find.byKey(const Key('waistCircumferenceField')), findsOneWidget);
expect(find.byKey(const Key('hipCircumferenceField')), findsOneWidget);
expect(find.byKey(const Key('saveWeightButton')), findsOneWidget);
expect(find.byKey(const Key('saveCircumferenceButton')), findsOneWidget);
});
});
}