feat(tests): increased tests coverage

This commit is contained in:
misha
2025-12-25 12:56:38 +03:00
parent 5b705dfb72
commit 99659f7ea1
132 changed files with 10304 additions and 6226 deletions

View File

@@ -1,5 +1,8 @@
import 'package:dio/dio.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/plans/enums/chosen_plan_state_enum.dart';
import 'package:eatvio_app/features/plans/models/chosen_plan_model.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/repositories/plan_repository.dart';
@@ -64,8 +67,7 @@ void main() {
isFavorite: false,
);
when(mockPlanRepository.getPlan('test-plan'))
.thenAnswer((_) async => testPlan);
when(mockPlanRepository.getPlan('test-plan')).thenAnswer((_) async => testPlan);
await aboutPlanCubit!.getPlan('test-plan');
@@ -104,8 +106,7 @@ void main() {
isFavorite: false,
);
when(mockPlanRepository.getPlan('test-plan'))
.thenAnswer((_) async => testPlan);
when(mockPlanRepository.getPlan('test-plan')).thenAnswer((_) async => testPlan);
await aboutPlanCubit!.getPlanWithLoading('test-plan');
@@ -151,10 +152,8 @@ void main() {
isFavorite: false,
);
when(mockPlanRepository.getPlan('test-plan'))
.thenAnswer((_) async => testPlan);
when(mockPlanRepository.likePlan('test-plan'))
.thenAnswer((_) async => {});
when(mockPlanRepository.getPlan('test-plan')).thenAnswer((_) async => testPlan);
when(mockPlanRepository.likePlan('test-plan')).thenAnswer((_) async => {});
await aboutPlanCubit!.getPlan('test-plan');
await aboutPlanCubit!.likePlan(index: null, slug: 'test-plan');
@@ -163,6 +162,254 @@ void main() {
expect(aboutPlanCubit!.state.planModel!.wasLikedByMe, isTrue);
expect(aboutPlanCubit!.state.planModel!.totalLikes, 11);
});
test('getPlan - обработка ошибки', () async {
aboutPlanCubit = AboutPlanCubit(planRepository: mockPlanRepository);
when(mockPlanRepository.getPlan('test-plan')).thenThrow(Exception('Ошибка загрузки'));
await aboutPlanCubit!.getPlan('test-plan');
expect(aboutPlanCubit!.state.error, isNotNull);
expect(aboutPlanCubit!.state.isLoading, isFalse);
});
test('getPlanWithLoading - обработка ошибки', () async {
aboutPlanCubit = AboutPlanCubit(planRepository: mockPlanRepository);
when(mockPlanRepository.getPlan('test-plan')).thenThrow(Exception('Ошибка загрузки'));
await aboutPlanCubit!.getPlanWithLoading('test-plan');
expect(aboutPlanCubit!.state.error, isNotNull);
expect(aboutPlanCubit!.state.isLoading, isFalse);
});
test('likePlan - убирает лайк если уже лайкнуто', () async {
aboutPlanCubit = AboutPlanCubit(planRepository: mockPlanRepository);
const testPlan = PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: 'https://example.com/image.jpg',
duration: 7,
state: 'active',
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
details: 'Test description',
packetsCount: 3,
minPriceOfOptions: 1000.0,
maxPriceOfOptions: 2000.0,
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: true,
isFavorite: false,
);
when(mockPlanRepository.getPlan('test-plan')).thenAnswer((_) async => testPlan);
when(mockPlanRepository.likePlan('test-plan')).thenAnswer((_) async => {});
await aboutPlanCubit!.getPlan('test-plan');
await aboutPlanCubit!.likePlan(index: null, slug: 'test-plan');
expect(aboutPlanCubit!.state.planModel!.wasLikedByMe, isFalse);
expect(aboutPlanCubit!.state.planModel!.totalLikes, 9);
await Future.delayed(const Duration(milliseconds: 1200));
verify(mockPlanRepository.likePlan('test-plan')).called(1);
});
test('addToFavorite - добавляет план в избранное', () async {
aboutPlanCubit = AboutPlanCubit(planRepository: mockPlanRepository);
const testPlan = PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: 'https://example.com/image.jpg',
duration: 7,
state: 'active',
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
details: 'Test description',
packetsCount: 3,
minPriceOfOptions: 1000.0,
maxPriceOfOptions: 2000.0,
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,
);
when(mockPlanRepository.getPlan('test-plan')).thenAnswer((_) async => testPlan);
when(mockPlanRepository.addToFavorite('test-plan')).thenAnswer((_) async => Future<void>.value());
await aboutPlanCubit!.getPlan('test-plan');
await aboutPlanCubit!.addToFavorite(slug: 'test-plan', index: null);
expect(aboutPlanCubit!.state.planModel!.isFavorite, true);
await Future.delayed(const Duration(milliseconds: 1200));
verify(mockPlanRepository.addToFavorite('test-plan')).called(1);
});
test('removeFromFavorite - удаляет план из избранного', () async {
aboutPlanCubit = AboutPlanCubit(planRepository: mockPlanRepository);
const testPlan = PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: 'https://example.com/image.jpg',
duration: 7,
state: 'active',
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
details: 'Test description',
packetsCount: 3,
minPriceOfOptions: 1000.0,
maxPriceOfOptions: 2000.0,
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: true,
);
when(mockPlanRepository.getPlan('test-plan')).thenAnswer((_) async => testPlan);
when(mockPlanRepository.removeFromFavorite('test-plan')).thenAnswer((_) async => Future<void>.value());
await aboutPlanCubit!.getPlan('test-plan');
await aboutPlanCubit!.removeFromFavorite(slug: 'test-plan', index: null);
expect(aboutPlanCubit!.state.planModel!.isFavorite, false);
await Future.delayed(const Duration(milliseconds: 1200));
verify(mockPlanRepository.removeFromFavorite('test-plan')).called(1);
});
test('chosePlan - успешно выбирает план', () async {
aboutPlanCubit = AboutPlanCubit(planRepository: mockPlanRepository);
const testPlan = PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: 'https://example.com/image.jpg',
duration: 7,
state: 'active',
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
details: 'Test description',
packetsCount: 3,
minPriceOfOptions: 1000.0,
maxPriceOfOptions: 2000.0,
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,
);
final chosenPlan = ChosenPlanModel(
id: 1,
optionType: 'options1',
startDate: DateTime(2024, 1, 1),
chosenPlanState: ChosenPlanStateEnum.stateStarted,
chatId: 'chat-1',
consultantId: 1,
plan: testPlan,
);
when(mockPlanRepository.getPlan('test-plan')).thenAnswer((_) async => testPlan);
when(mockPlanRepository.chosePlan('test-plan', any)).thenAnswer((_) async => chosenPlan);
await aboutPlanCubit!.getPlan('test-plan');
aboutPlanCubit!.choseOption(0);
final result = await aboutPlanCubit!.chosePlan();
expect(result.$1, isNotNull);
expect(result.$2, isNull);
verify(mockPlanRepository.chosePlan('test-plan', any)).called(1);
});
test('chosePlan - возвращает ошибку hasPlan при 400 статусе', () async {
aboutPlanCubit = AboutPlanCubit(planRepository: mockPlanRepository);
const testPlan = PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: 'https://example.com/image.jpg',
duration: 7,
state: 'active',
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
details: 'Test description',
packetsCount: 3,
minPriceOfOptions: 1000.0,
maxPriceOfOptions: 2000.0,
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,
);
when(mockPlanRepository.getPlan('test-plan')).thenAnswer((_) async => testPlan);
when(mockPlanRepository.chosePlan('test-plan', any)).thenThrow(
DioException(
requestOptions: RequestOptions(path: '/api/plans/chose'),
response: Response(statusCode: 400, requestOptions: RequestOptions(path: '/api/plans/chose')),
),
);
await aboutPlanCubit!.getPlan('test-plan');
aboutPlanCubit!.choseOption(0);
final result = await aboutPlanCubit!.chosePlan();
expect(result.$1, isNull);
expect(result.$2, 'hasPlan');
});
});
}

View File

@@ -0,0 +1,41 @@
import 'package:eatvio_app/core/theme/theme_helper.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:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'options_tab_test.mocks.dart';
@GenerateNiceMocks([MockSpec<AboutPlanCubit>()])
void main() {
group('OptionsTab smoke', () {
Widget wrap(Widget child, AboutPlanCubit cubit) {
return MaterialApp(
theme: ThemeHelper().themeData(),
home: Scaffold(
body: BlocProvider<AboutPlanCubit>.value(value: cubit, child: child),
),
);
}
testWidgets('renders options tab', (tester) async {
final cubit = MockAboutPlanCubit();
final state = AboutPlanState.initial();
when(cubit.state).thenReturn(state);
when(cubit.stream).thenAnswer((_) => Stream.value(state));
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
when(cubit.choseOption(any)).thenReturn(null);
await tester.pumpWidget(wrap(const OptionsTab(cost: [100, 200, 300]), cubit));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
expect(find.byType(OptionsTab), findsOneWidget);
});
});
}