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,192 @@
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/features/diets/presentation/cubit/about_diet_cubit.dart';
import 'package:eatvio_app/features/diets/presentation/cubit/about_diet_state.dart';
import 'package:eatvio_app/features/diets/presentation/pages/about_diet_screen.dart';
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:eatvio_app/core/comments/comments_cubit/comments_cubit.dart';
import 'package:eatvio_app/core/comments/comments_cubit/comments_state.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/feed_cubit/feed_cubit.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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 'package:network_image_mock/network_image_mock.dart';
import 'about_diet_screen_smoke_test.mocks.dart';
@GenerateNiceMocks([MockSpec<AboutDietCubit>(), MockSpec<FeedCubit>(), MockSpec<CommentsCubit>()])
void main() {
group('AboutDietScreen 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'));
});
void registerMocks(AboutDietCubit dietCubit, CommentsCubit commentsCubit) {
sl.registerLazySingleton<AboutDietCubit>(() => dietCubit);
sl.registerLazySingleton<CommentsCubit>(() => commentsCubit);
}
const testDiet = Diet(
id: 1,
slug: 'test-diet',
name: 'Test Diet',
image: 'https://example.com/image.jpg',
duration: 7,
state: DietState.active,
type: DietType(veg: true, light: false, carb: false, meat: false),
details: 'Test details',
dailySchedule: {},
author: Author(
authorSlug: 'author',
authorName: 'Author Name',
authorProfileIcon: 'https://example.com/icon.jpg',
authorIsMe: false,
isEntitySubscribedByMe: null,
authorPlaceInRating: null,
),
totalLikes: 10,
totalViews: 100,
totalComments: 5,
wasLikedByMe: false,
isFavorite: false,
dailyNutrition: [],
);
Widget wrap(AboutDietCubit cubit, FeedCubit feedCubit, CommentsCubit commentsCubit, Widget child) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: MultiBlocProvider(
providers: [
BlocProvider<AboutDietCubit>.value(value: cubit),
BlocProvider<FeedCubit>.value(value: feedCubit),
BlocProvider<CommentsCubit>.value(value: commentsCubit),
],
child: child,
),
);
}
testWidgets('renders loading state', (tester) async {
final cubit = MockAboutDietCubit();
final feedCubit = MockFeedCubit();
final commentsCubit = MockCommentsCubit();
when(cubit.state).thenReturn(const AboutDietState.loading());
when(cubit.stream).thenAnswer((_) => const Stream<AboutDietState>.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
when(feedCubit.stream).thenAnswer((_) => const Stream.empty());
when(feedCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.state).thenReturn(const CommentsState());
when(commentsCubit.stream).thenAnswer((_) => const Stream<CommentsState>.empty());
when(commentsCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.getComments(any, any, any)).thenAnswer((_) async => Future<void>.value());
registerMocks(cubit, commentsCubit);
await tester.pumpWidget(wrap(cubit, feedCubit, commentsCubit, const AboutDietScreen(slug: 'test', id: null, index: null, navigateToComments: false)));
await tester.pump();
expect(find.byType(AboutDietScreen), findsOneWidget);
});
testWidgets('renders loaded state with diet not started', (tester) async {
final cubit = MockAboutDietCubit();
final feedCubit = MockFeedCubit();
final commentsCubit = MockCommentsCubit();
const state = AboutDietState.loaded(diet: testDiet, isStarted: false, hasStartedDiet: false, selectedIndexes: []);
when(cubit.state).thenReturn(state);
when(cubit.stream).thenAnswer((_) => const Stream<AboutDietState>.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
when(feedCubit.viewDiet(any)).thenAnswer((_) async => Future<void>.value());
when(feedCubit.stream).thenAnswer((_) => const Stream.empty());
when(feedCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.state).thenReturn(const CommentsState());
when(commentsCubit.stream).thenAnswer((_) => const Stream<CommentsState>.empty());
when(commentsCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.getComments(any, any, any)).thenAnswer((_) async => Future<void>.value());
registerMocks(cubit, commentsCubit);
await mockNetworkImagesFor(() async {
await tester.pumpWidget(wrap(cubit, feedCubit, commentsCubit, const AboutDietScreen(slug: 'test', id: null, index: null, navigateToComments: false)));
await tester.pump();
await tester.pump(const Duration(milliseconds: 600));
});
expect(find.byType(AboutDietScreen), findsOneWidget);
expect(find.text('Test Diet'), findsOneWidget);
expect(find.text(S.current.startDiet), findsOneWidget);
});
testWidgets('renders loaded state with diet started', (tester) async {
final cubit = MockAboutDietCubit();
final feedCubit = MockFeedCubit();
final commentsCubit = MockCommentsCubit();
const state = AboutDietState.loaded(diet: testDiet, isStarted: true, hasStartedDiet: true, selectedIndexes: []);
when(cubit.state).thenReturn(state);
when(cubit.stream).thenAnswer((_) => const Stream<AboutDietState>.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
when(feedCubit.viewDiet(any)).thenAnswer((_) async => Future<void>.value());
when(feedCubit.stream).thenAnswer((_) => const Stream.empty());
when(feedCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.state).thenReturn(const CommentsState());
when(commentsCubit.stream).thenAnswer((_) => const Stream<CommentsState>.empty());
when(commentsCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.getComments(any, any, any)).thenAnswer((_) async => Future<void>.value());
registerMocks(cubit, commentsCubit);
await mockNetworkImagesFor(() async {
await tester.pumpWidget(wrap(cubit, feedCubit, commentsCubit, const AboutDietScreen(slug: 'test', id: null, index: null, navigateToComments: false)));
await tester.pump();
await tester.pump(const Duration(milliseconds: 600));
});
expect(find.byType(AboutDietScreen), findsOneWidget);
expect(find.text('Отменить диету'), findsOneWidget);
});
testWidgets('renders error state', (tester) async {
final cubit = MockAboutDietCubit();
final feedCubit = MockFeedCubit();
final commentsCubit = MockCommentsCubit();
const state = AboutDietState.error(message: 'Error loading diet');
when(cubit.state).thenReturn(state);
when(cubit.stream).thenAnswer((_) => const Stream<AboutDietState>.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
when(feedCubit.stream).thenAnswer((_) => const Stream.empty());
when(feedCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.state).thenReturn(const CommentsState());
when(commentsCubit.stream).thenAnswer((_) => const Stream<CommentsState>.empty());
when(commentsCubit.close()).thenAnswer((_) async => Future<void>.value());
registerMocks(cubit, commentsCubit);
await tester.pumpWidget(wrap(cubit, feedCubit, commentsCubit, const AboutDietScreen(slug: 'test', id: null, index: null, navigateToComments: false)));
await tester.pump();
expect(find.byType(AboutDietScreen), findsOneWidget);
});
});
}