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

@@ -3,6 +3,7 @@ import 'package:eatvio_app/features/diets/presentation/cubit/about_diet_cubit.da
import 'package:eatvio_app/features/diets/presentation/cubit/about_diet_state.dart';
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:eatvio_app/features/profile/models/chosen_diets_pagination_model.dart';
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
@@ -10,10 +11,7 @@ import 'package:mockito/mockito.dart';
import 'about_diet_cubit_test.mocks.dart';
@GenerateNiceMocks([
MockSpec<DietRepository>(),
MockSpec<ProfileRepository>(),
])
@GenerateNiceMocks([MockSpec<DietRepository>(), MockSpec<ProfileRepository>()])
void main() {
group('AboutDietCubit', () {
AboutDietCubit? aboutDietCubit;
@@ -33,19 +31,13 @@ void main() {
});
test('начальное состояние - initial', () {
aboutDietCubit = AboutDietCubit(
dietsRepository: mockDietRepository,
profileRepository: mockProfileRepository,
);
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
expect(aboutDietCubit!.state, isA<AboutDietState>());
});
test('getDiet - успешная загрузка диеты по slug', () async {
aboutDietCubit = AboutDietCubit(
dietsRepository: mockDietRepository,
profileRepository: mockProfileRepository,
);
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
const testDiet = Diet(
id: 1,
@@ -73,8 +65,7 @@ void main() {
dailyNutrition: [],
);
when(mockDietRepository.getDietBySlug('test-diet'))
.thenAnswer((_) async => testDiet);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
await aboutDietCubit!.getDiet(slug: 'test-diet');
@@ -84,10 +75,7 @@ void main() {
});
test('getDiet - успешная загрузка диеты по id', () async {
aboutDietCubit = AboutDietCubit(
dietsRepository: mockDietRepository,
profileRepository: mockProfileRepository,
);
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
const testDiet = Diet(
id: 1,
@@ -115,8 +103,7 @@ void main() {
dailyNutrition: [],
);
when(mockDietRepository.getDiet(1))
.thenAnswer((_) async => testDiet);
when(mockDietRepository.getDiet(1)).thenAnswer((_) async => testDiet);
await aboutDietCubit!.getDiet(id: 1);
@@ -124,13 +111,9 @@ void main() {
});
test('getDiet - обработка ошибки', () async {
aboutDietCubit = AboutDietCubit(
dietsRepository: mockDietRepository,
profileRepository: mockProfileRepository,
);
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
when(mockDietRepository.getDietBySlug(any))
.thenThrow(Exception('Ошибка загрузки'));
when(mockDietRepository.getDietBySlug(any)).thenThrow(Exception('Ошибка загрузки'));
await aboutDietCubit!.getDiet(slug: 'test-diet');
@@ -138,10 +121,7 @@ void main() {
});
test('likeDiet - лайк диеты', () async {
aboutDietCubit = AboutDietCubit(
dietsRepository: mockDietRepository,
profileRepository: mockProfileRepository,
);
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
const testDiet = Diet(
id: 1,
@@ -169,13 +149,8 @@ void main() {
dailyNutrition: [],
);
when(mockDietRepository.getDietBySlug('test-diet'))
.thenAnswer((_) async => testDiet);
when(mockDietRepository.likeDiet(1))
.thenAnswer((_) async => const DietLikesModel(
totalLikes: 11,
isLiked: true,
));
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
when(mockDietRepository.likeDiet(1)).thenAnswer((_) async => const DietLikesModel(totalLikes: 11, isLiked: true));
await aboutDietCubit!.getDiet(slug: 'test-diet');
await aboutDietCubit!.likeDiet(1, null);
@@ -185,6 +160,545 @@ void main() {
final loadedState = aboutDietCubit!.state as DietStateLoaded;
expect(loadedState.diet.wasLikedByMe, isTrue);
});
test('initDietPage - успешная инициализация страницы диеты по slug', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: [],
);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
when(mockProfileRepository.getChosenDiets()).thenAnswer((_) async => []);
await aboutDietCubit!.initDietPage(slug: 'test-diet');
expect(aboutDietCubit!.state, isA<DietStateLoaded>());
final loadedState = aboutDietCubit!.state as DietStateLoaded;
expect(loadedState.diet.id, 1);
});
test('initDietPage - устанавливает isCurrentDietStarted когда диета уже начата', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: [],
);
final chosenDiets = [ChosenDietModel(chosenDietID: 1, chosenPlanId: 1, state: 'started', startDate: DateTime(2024, 1, 1), dietId: 1)];
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
when(mockProfileRepository.getChosenDiets()).thenAnswer((_) async => chosenDiets);
await aboutDietCubit!.initDietPage(slug: 'test-diet');
expect(aboutDietCubit!.isCurrentDietStarted, true);
expect(aboutDietCubit!.hasStartedDiet, true);
});
test('addToFavorite - добавляет диету в избранное', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: [],
);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
const updatedDiet = 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: true,
dailyNutrition: [],
);
when(mockDietRepository.addToFavorite(1)).thenAnswer((_) async => updatedDiet);
await aboutDietCubit!.getDiet(slug: 'test-diet');
await aboutDietCubit!.addToFavorite(1, null);
expect(aboutDietCubit!.state, isA<DietStateLoaded>());
final loadedState = aboutDietCubit!.state as DietStateLoaded;
expect(loadedState.diet.isFavorite, true);
await Future.delayed(const Duration(milliseconds: 1200));
verify(mockDietRepository.addToFavorite(1)).called(1);
});
test('removeFromFavorite - удаляет диету из избранного', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: true,
dailyNutrition: [],
);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
const updatedDiet = 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: [],
);
when(mockDietRepository.removeFromFavorite(1)).thenAnswer((_) async => updatedDiet);
await aboutDietCubit!.getDiet(slug: 'test-diet');
await aboutDietCubit!.removeFromFavorite(1, null);
expect(aboutDietCubit!.state, isA<DietStateLoaded>());
final loadedState = aboutDietCubit!.state as DietStateLoaded;
expect(loadedState.diet.isFavorite, false);
await Future.delayed(const Duration(milliseconds: 1200));
verify(mockDietRepository.removeFromFavorite(1)).called(1);
});
test('startDiet - успешно начинает диету', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: [],
);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
when(mockDietRepository.startDiet(1, any)).thenAnswer((_) async => Future<void>.value());
await aboutDietCubit!.getDiet(slug: 'test-diet');
final result = await aboutDietCubit!.startDiet(DateTime(2024, 1, 1));
expect(result, true);
verify(mockDietRepository.startDiet(1, any)).called(1);
});
test('startDiet - возвращает false при ошибке', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: [],
);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
when(mockDietRepository.startDiet(1, any)).thenThrow(Exception('Ошибка'));
await aboutDietCubit!.getDiet(slug: 'test-diet');
final result = await aboutDietCubit!.startDiet(DateTime(2024, 1, 1));
expect(result, false);
});
test('cancelDiet - успешно отменяет диету', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: [],
);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
when(mockDietRepository.cancelDiet(1)).thenAnswer((_) async => Future<void>.value());
await aboutDietCubit!.getDiet(slug: 'test-diet');
final result = await aboutDietCubit!.cancelDiet();
expect(result, true);
verify(mockDietRepository.cancelDiet(1)).called(1);
});
test('cancelDiet - возвращает false при ошибке', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: [],
);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
when(mockDietRepository.cancelDiet(1)).thenThrow(Exception('Ошибка'));
await aboutDietCubit!.getDiet(slug: 'test-diet');
final result = await aboutDietCubit!.cancelDiet();
expect(result, false);
});
test('clearStartedDietsState - очищает состояние начатых диет', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: [],
);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
await aboutDietCubit!.getDiet(slug: 'test-diet');
aboutDietCubit!.clearStartedDietsState();
expect(aboutDietCubit!.state, isA<DietStateLoaded>());
});
test('emitCurrentStartedDietsState - устанавливает состояние начатой диеты', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: [],
);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
await aboutDietCubit!.getDiet(slug: 'test-diet');
aboutDietCubit!.emitCurrentStartedDietsState();
expect(aboutDietCubit!.state, isA<DietStateLoaded>());
final loadedState = aboutDietCubit!.state as DietStateLoaded;
expect(loadedState.isStarted, true);
});
test('getDietBySlug - возвращает диету по slug', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: [],
);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
final result = await aboutDietCubit!.getDietBySlug('test-diet');
expect(result, isNotNull);
expect(result!.id, 1);
});
test('getDietBySlug - возвращает null при ошибке', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
when(mockDietRepository.getDietBySlug('test-diet')).thenThrow(Exception('Ошибка'));
final result = await aboutDietCubit!.getDietBySlug('test-diet');
expect(result, isNull);
});
test('setSelectedIndex - устанавливает выбранный индекс', () async {
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
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: {
1: {
1: {1: DailySchedule(name: 'Test', portion: Portion.grams, quantity: 100)},
},
},
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: [],
);
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
when(mockProfileRepository.getChosenDiets()).thenAnswer((_) async => []);
await aboutDietCubit!.initDietPage(slug: 'test-diet');
aboutDietCubit!.setSelectedIndex(dayNumber: 0, index: 2);
expect(aboutDietCubit!.state, isA<DietStateLoaded>());
final loadedState = aboutDietCubit!.state as DietStateLoaded;
expect(loadedState.selectedIndexes[0], 2);
});
});
}