feat(tests): test coverage 60%

This commit is contained in:
misha
2025-12-23 10:59:56 +03:00
parent 38118055a5
commit 5b705dfb72
67 changed files with 8391 additions and 42 deletions

View File

@@ -0,0 +1,99 @@
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/models/pagination_model.dart';
import 'package:eatvio_app/core/theme/theme_helper.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/my_plans_screen.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_dotenv/flutter_dotenv.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'my_plans_screen_smoke_test.mocks.dart';
@GenerateNiceMocks([MockSpec<MyPlansRepository>()])
void main() {
group('MyPlansScreen smoke', () {
setUpAll(() async {
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
});
setUp(() async {
await sl.reset();
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
});
const mockPlansPagination = PlanPaginationModel(
data: [],
links: Links(first: 'http://localhost/api?page=1', last: 'http://localhost/api?page=1', prev: null, next: null),
meta: Meta(currentPage: 1, from: null, lastPage: 1, links: [], perPage: 10, to: null, total: 0),
);
Widget wrap(Widget child) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: child,
);
}
testWidgets('renders loading state', (tester) async {
final repository = MockMyPlansRepository();
final cubit = MyPlansCubit(myPlansRepository: repository);
when(repository.getPlans(page: 1)).thenAnswer((_) async => mockPlansPagination);
sl.registerLazySingleton<MyPlansRepository>(() => repository);
sl.registerLazySingleton<MyPlansCubit>(() => cubit);
await tester.pumpWidget(wrap(const MyPlansScreen()));
await tester.pump();
expect(find.byType(MyPlansScreen), findsOneWidget);
});
testWidgets('renders loaded state', (tester) async {
final repository = MockMyPlansRepository();
final cubit = MyPlansCubit(myPlansRepository: repository);
when(repository.getPlans(page: 1)).thenAnswer((_) async => mockPlansPagination);
sl.registerLazySingleton<MyPlansRepository>(() => repository);
sl.registerLazySingleton<MyPlansCubit>(() => cubit);
await tester.pumpWidget(wrap(const MyPlansScreen()));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
expect(find.byType(MyPlansScreen), findsOneWidget);
});
testWidgets('renders error state', (tester) async {
final repository = MockMyPlansRepository();
final cubit = MyPlansCubit(myPlansRepository: repository);
when(repository.getPlans(page: 1)).thenThrow(Exception('Error loading plans'));
sl.registerLazySingleton<MyPlansRepository>(() => repository);
sl.registerLazySingleton<MyPlansCubit>(() => cubit);
await tester.pumpWidget(wrap(const MyPlansScreen()));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
expect(find.byType(MyPlansScreen), findsOneWidget);
});
});
}