Files
eatvio_app/test/features/plans/presentation/widgets/widgets_test.dart

132 lines
5.3 KiB
Dart

import 'package:eatvio_app/core/app_config.dart';
import 'package:eatvio_app/core/app_initializer.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/widgets/animated_option_button.dart';
import 'package:eatvio_app/features/plans/presentation/cubit/about_plan_cubit.dart';
import 'package:eatvio_app/features/plans/presentation/cubit/about_plan_state.dart';
import 'package:eatvio_app/features/plans/presentation/widgets/options_tab.dart';
import 'package:eatvio_app/features/plans/repositories/plan_repository.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_portal/flutter_portal.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'widgets_test.mocks.dart';
@GenerateNiceMocks([MockSpec<PlanRepository>()])
void main() {
group('Plans Widgets Tests', () {
late MockPlanRepository mockPlanRepository;
late AboutPlanCubit aboutPlanCubit;
setUp(() async {
await sl.reset();
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
mockPlanRepository = MockPlanRepository();
aboutPlanCubit = AboutPlanCubit(planRepository: mockPlanRepository);
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
});
tearDown(() async {
await Future.delayed(const Duration(milliseconds: 100));
if (!aboutPlanCubit.isClosed) {
aboutPlanCubit.close();
}
});
Widget createTestWidget(Widget child) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: Portal(
child: Scaffold(body: SingleChildScrollView(child: child)),
),
);
}
group('OptionsTab', () {
testWidgets('should display OptionsTab with three options', (WidgetTester tester) async {
// Arrange
aboutPlanCubit.emit(AboutPlanState.initial().copyWith(chosenOptionIndex: 0));
const widget = OptionsTab(cost: [100, 200, 300]);
// Act
await tester.pumpWidget(BlocProvider<AboutPlanCubit>(create: (context) => aboutPlanCubit, child: createTestWidget(widget)));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
// Assert
expect(find.byType(OptionsTab), findsOneWidget);
expect(find.byType(AnimatedOptionButton), findsNWidgets(3));
expect(find.text('100₽'), findsOneWidget);
expect(find.text('200₽'), findsOneWidget);
expect(find.text('300₽'), findsOneWidget);
});
testWidgets('should highlight selected option', (WidgetTester tester) async {
// Arrange
aboutPlanCubit.emit(AboutPlanState.initial().copyWith(chosenOptionIndex: 1));
const widget = OptionsTab(cost: [100, 200, 300]);
// Act
await tester.pumpWidget(BlocProvider<AboutPlanCubit>(create: (context) => aboutPlanCubit, child: createTestWidget(widget)));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
// Assert
expect(find.byType(OptionsTab), findsOneWidget);
expect(find.byType(AnimatedOptionButton), findsNWidgets(3));
});
testWidgets('should handle option selection', (WidgetTester tester) async {
// Arrange
aboutPlanCubit.emit(AboutPlanState.initial().copyWith(chosenOptionIndex: 0));
const widget = OptionsTab(cost: [100, 200, 300]);
// Act
await tester.pumpWidget(BlocProvider<AboutPlanCubit>(create: (context) => aboutPlanCubit, child: createTestWidget(widget)));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
// Tap the second option
final buttons = find.byType(AnimatedOptionButton);
expect(buttons, findsNWidgets(3));
await tester.tap(buttons.at(1));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
// Assert
expect(aboutPlanCubit.state.chosenOptionIndex, 1);
});
testWidgets('should display OptionsTab with different costs', (WidgetTester tester) async {
// Arrange
aboutPlanCubit.emit(AboutPlanState.initial().copyWith(chosenOptionIndex: 0));
const widget = OptionsTab(cost: [500, 1000, 1500]);
// Act
await tester.pumpWidget(BlocProvider<AboutPlanCubit>(create: (context) => aboutPlanCubit, child: createTestWidget(widget)));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
// Assert
expect(find.byType(OptionsTab), findsOneWidget);
expect(find.text('500₽'), findsOneWidget);
expect(find.text('1000₽'), findsOneWidget);
expect(find.text('1500₽'), findsOneWidget);
});
});
});
}