forked from eatvio/eatvio_app
feat(tests): fixed feed tests failing and added tests for my_diets, my_plans and recipes
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
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/custom_outlined_button.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/my_plans/presentation/cubit/my_plans_cubit.dart';
|
||||
import 'package:eatvio_app/features/my_plans/presentation/cubit/my_plans_state.dart';
|
||||
import 'package:eatvio_app/features/my_plans/presentation/widgets/my_plan_widget.dart';
|
||||
import 'package:eatvio_app/features/my_plans/repositories/my_diets_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 'basic_widget_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<MyPlansRepository>()])
|
||||
void main() {
|
||||
group('MyPlans Basic Widget Tests', () {
|
||||
late MockMyPlansRepository mockMyPlansRepository;
|
||||
late MyPlansCubit myPlansCubit;
|
||||
|
||||
final testPlan = PlanModel(
|
||||
id: 1,
|
||||
slug: 'test-plan',
|
||||
name: 'Test Plan',
|
||||
image: 'test-image.jpg',
|
||||
duration: 30,
|
||||
state: 'active',
|
||||
type: const PlanType(fit: true, slim: false, behavior: false, wellness: false),
|
||||
details: 'Test plan description',
|
||||
packetsCount: 1,
|
||||
author: const Author(authorSlug: '', authorName: '', authorProfileIcon: '', authorIsMe: false),
|
||||
totalLikes: 1,
|
||||
totalViews: 1,
|
||||
totalComments: 1,
|
||||
isFavorite: false,
|
||||
minPriceOfOptions: 100.0,
|
||||
maxPriceOfOptions: 500.0,
|
||||
wasLikedByMe: false,
|
||||
packet1Options: null,
|
||||
);
|
||||
|
||||
setUp(() async {
|
||||
await sl.reset();
|
||||
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
|
||||
mockMyPlansRepository = MockMyPlansRepository();
|
||||
myPlansCubit = MyPlansCubit(myPlansRepository: mockMyPlansRepository);
|
||||
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
if (!myPlansCubit.isClosed) {
|
||||
myPlansCubit.close();
|
||||
}
|
||||
});
|
||||
|
||||
Widget createTestWidget(Widget child, {bool wrapInScaffold = true}) {
|
||||
final widget = BlocProvider<MyPlansCubit>(
|
||||
create: (context) => myPlansCubit,
|
||||
child: Portal(
|
||||
child: wrapInScaffold ? Scaffold(body: SingleChildScrollView(child: child)) : SizedBox(width: 800, height: 600, child: child),
|
||||
),
|
||||
);
|
||||
return MaterialApp(
|
||||
theme: ThemeHelper().themeData(),
|
||||
localizationsDelegates: const [
|
||||
S.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
supportedLocales: const [Locale('ru')],
|
||||
home: widget,
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('should create MyPlansCubit successfully', (WidgetTester tester) async {
|
||||
// Arrange & Act
|
||||
final cubit = MyPlansCubit(myPlansRepository: mockMyPlansRepository);
|
||||
|
||||
// Assert
|
||||
expect(cubit, isA<MyPlansCubit>());
|
||||
expect(cubit.state.activePlans, isEmpty);
|
||||
|
||||
cubit.close();
|
||||
});
|
||||
|
||||
testWidgets('should display MyPlanWidget with plan name', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
myPlansCubit.emit(MyPlansState(selectedStateIndex: 0));
|
||||
final widget = MyPlanWidget(plan: testPlan);
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(createTestWidget(widget));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
|
||||
// Assert
|
||||
expect(find.text('Test Plan'), findsOneWidget);
|
||||
expect(find.byType(MyPlanWidget), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('should display MyPlanWidget with description when details are present', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
myPlansCubit.emit(MyPlansState(selectedStateIndex: 0));
|
||||
final widget = MyPlanWidget(plan: testPlan);
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(createTestWidget(widget));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
|
||||
// Assert
|
||||
expect(find.text('Test Plan'), findsOneWidget);
|
||||
expect(find.byType(MyPlanWidget), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('should display MyPlanWidget with price', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
myPlansCubit.emit(MyPlansState(selectedStateIndex: 0));
|
||||
final widget = MyPlanWidget(plan: testPlan);
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(createTestWidget(widget));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
|
||||
// Assert
|
||||
expect(find.text('Test Plan'), findsOneWidget);
|
||||
expect(find.byType(MyPlanWidget), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('should display MyPlanWidget buttons', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
myPlansCubit.emit(MyPlansState(selectedStateIndex: 0));
|
||||
final widget = MyPlanWidget(plan: testPlan);
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(createTestWidget(widget));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
|
||||
// Assert
|
||||
expect(find.byType(MyPlanWidget), findsOneWidget);
|
||||
expect(find.byType(CustomOutlinedButton), findsWidgets);
|
||||
});
|
||||
|
||||
testWidgets('should display MyPlanWidget structure correctly', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
myPlansCubit.emit(MyPlansState(selectedStateIndex: 0));
|
||||
final widget = MyPlanWidget(plan: testPlan);
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(createTestWidget(widget));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
|
||||
// Assert
|
||||
expect(find.byType(MyPlanWidget), findsOneWidget);
|
||||
expect(find.text('Test Plan'), findsOneWidget);
|
||||
expect(find.byType(CustomOutlinedButton), findsWidgets);
|
||||
});
|
||||
|
||||
testWidgets('should display MyPlanWidget with empty details', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
myPlansCubit.emit(MyPlansState(selectedStateIndex: 0));
|
||||
final planWithoutDetails = testPlan.copyWith(details: '');
|
||||
final widget = MyPlanWidget(plan: planWithoutDetails);
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(createTestWidget(widget));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
|
||||
// Assert
|
||||
expect(find.text('Test Plan'), findsOneWidget);
|
||||
expect(find.byType(MyPlanWidget), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user