forked from eatvio/eatvio_app
feat(tests): updated tests coverage to 66.5%
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:easy_debounce/easy_debounce.dart';
|
||||
import 'package:eatvio_app/core/models/pagination_model.dart';
|
||||
import 'package:eatvio_app/features/diets/models/diet_likes_model.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/feed/presentation/bloc/diets_cubit/diets_cubit.dart';
|
||||
@@ -172,6 +173,411 @@ void main() {
|
||||
expect(dietsCubit!.state.errorMessage, isNotNull);
|
||||
expect(dietsCubit!.state.isLoading, isFalse);
|
||||
});
|
||||
|
||||
test('loadNextPage - загружает следующую страницу', () async {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
const testDiet1 = Diet(
|
||||
id: 1,
|
||||
slug: 'test-diet-1',
|
||||
name: 'Test Diet 1',
|
||||
image: 'https://example.com/image1.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Test details 1',
|
||||
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: [],
|
||||
);
|
||||
|
||||
const testDiet2 = Diet(
|
||||
id: 2,
|
||||
slug: 'test-diet-2',
|
||||
name: 'Test Diet 2',
|
||||
image: 'https://example.com/image2.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(veg: false, light: true, carb: false, meat: false),
|
||||
details: 'Test details 2',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false, isEntitySubscribedByMe: null, authorPlaceInRating: null),
|
||||
totalLikes: 20,
|
||||
totalViews: 200,
|
||||
totalComments: 10,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
const firstPagePagination = DietsPagination(
|
||||
data: [testDiet1],
|
||||
links: Links(first: 'https://example.com?page=1', last: 'https://example.com?page=2', prev: null, next: 'https://example.com?page=2'),
|
||||
meta: Meta(currentPage: 1, from: 1, lastPage: 2, links: [], perPage: 10, to: 1, total: 2),
|
||||
);
|
||||
|
||||
const secondPagePagination = DietsPagination(
|
||||
data: [testDiet2],
|
||||
links: Links(first: 'https://example.com?page=1', last: 'https://example.com?page=2', prev: 'https://example.com?page=1', next: null),
|
||||
meta: Meta(currentPage: 2, from: 2, lastPage: 2, links: [], perPage: 10, to: 2, total: 2),
|
||||
);
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenAnswer((_) async => firstPagePagination);
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
page: 2,
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenAnswer((_) async => secondPagePagination);
|
||||
|
||||
await dietsCubit!.loadNextPage();
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
|
||||
expect(dietsCubit!.state.dietsList.length, 2);
|
||||
expect(dietsCubit!.state.dietsList[0].id, 1);
|
||||
expect(dietsCubit!.state.dietsList[1].id, 2);
|
||||
});
|
||||
|
||||
test('loadNextPage - не загружает если уже загружается', () async {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
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: [],
|
||||
);
|
||||
|
||||
const firstPagePagination = DietsPagination(
|
||||
data: [testDiet],
|
||||
links: Links(first: 'https://example.com?page=1', last: 'https://example.com?page=2', prev: null, next: 'https://example.com?page=2'),
|
||||
meta: Meta(currentPage: 1, from: 1, lastPage: 2, links: [], perPage: 10, to: 1, total: 2),
|
||||
);
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenAnswer((_) async => firstPagePagination);
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
page: 2,
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenAnswer((_) async {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
return firstPagePagination;
|
||||
});
|
||||
|
||||
final firstLoad = dietsCubit!.loadNextPage();
|
||||
await dietsCubit!.loadNextPage();
|
||||
await firstLoad;
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
verify(mockFeedRepository.getDiets(page: 2, query: anyNamed('query'), types: anyNamed('types'), duration: anyNamed('duration'), sorting: anyNamed('sorting'))).called(1);
|
||||
});
|
||||
|
||||
test('likeDiet - добавляет лайк', () async {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
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: [],
|
||||
);
|
||||
|
||||
const dietsPagination = DietsPagination(
|
||||
data: [testDiet],
|
||||
links: Links(first: 'https://example.com?page=1', last: 'https://example.com?page=1', prev: null, next: null),
|
||||
meta: Meta(currentPage: 1, from: 1, lastPage: 1, links: [], perPage: 10, to: 1, total: 1),
|
||||
);
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenAnswer((_) async => dietsPagination);
|
||||
|
||||
when(mockDietRepository.likeDiet(1)).thenAnswer((_) async => const DietLikesModel(totalLikes: 11, isLiked: true));
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
await dietsCubit!.likeDiet(1, 0);
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(dietsCubit!.state.dietsList[0].wasLikedByMe, true);
|
||||
expect(dietsCubit!.state.dietsList[0].totalLikes, 11);
|
||||
});
|
||||
|
||||
test('likeDiet - убирает лайк', () async {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
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: true,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
const dietsPagination = DietsPagination(
|
||||
data: [testDiet],
|
||||
links: Links(first: 'https://example.com?page=1', last: 'https://example.com?page=1', prev: null, next: null),
|
||||
meta: Meta(currentPage: 1, from: 1, lastPage: 1, links: [], perPage: 10, to: 1, total: 1),
|
||||
);
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenAnswer((_) async => dietsPagination);
|
||||
|
||||
when(mockDietRepository.likeDiet(1)).thenAnswer((_) async => const DietLikesModel(totalLikes: 9, isLiked: false));
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
await dietsCubit!.likeDiet(1, 0);
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(dietsCubit!.state.dietsList[0].wasLikedByMe, false);
|
||||
expect(dietsCubit!.state.dietsList[0].totalLikes, 9);
|
||||
});
|
||||
|
||||
test('addDietToFavorite - добавляет диету в избранное', () async {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
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: [],
|
||||
);
|
||||
|
||||
const dietsPagination = DietsPagination(
|
||||
data: [testDiet],
|
||||
links: Links(first: 'https://example.com?page=1', last: 'https://example.com?page=1', prev: null, next: null),
|
||||
meta: Meta(currentPage: 1, from: 1, lastPage: 1, links: [], perPage: 10, to: 1, total: 1),
|
||||
);
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenAnswer((_) async => dietsPagination);
|
||||
|
||||
when(mockDietRepository.addToFavorite(1)).thenAnswer((_) async => testDiet.copyWith(isFavorite: true));
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
await dietsCubit!.addDietToFavorite(1, 0);
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
|
||||
expect(dietsCubit!.state.dietsList[0].isFavorite, true);
|
||||
verify(mockDietRepository.addToFavorite(1)).called(1);
|
||||
});
|
||||
|
||||
test('removeDietFromFavorite - убирает диету из избранного', () async {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
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: [],
|
||||
);
|
||||
|
||||
const dietsPagination = DietsPagination(
|
||||
data: [testDiet],
|
||||
links: Links(first: 'https://example.com?page=1', last: 'https://example.com?page=1', prev: null, next: null),
|
||||
meta: Meta(currentPage: 1, from: 1, lastPage: 1, links: [], perPage: 10, to: 1, total: 1),
|
||||
);
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenAnswer((_) async => dietsPagination);
|
||||
|
||||
when(mockDietRepository.removeFromFavorite(1)).thenAnswer((_) async => testDiet.copyWith(isFavorite: false));
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
await dietsCubit!.removeDietFromFavorite(1, 0);
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(dietsCubit!.state.dietsList[0].isFavorite, false);
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
verify(mockDietRepository.removeFromFavorite(1)).called(1);
|
||||
});
|
||||
|
||||
test('deleteDiet - удаляет диету', () async {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
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: [],
|
||||
);
|
||||
|
||||
const dietsPagination = DietsPagination(
|
||||
data: [testDiet],
|
||||
links: Links(first: 'https://example.com?page=1', last: 'https://example.com?page=1', prev: null, next: null),
|
||||
meta: Meta(currentPage: 1, from: 1, lastPage: 1, links: [], perPage: 10, to: 1, total: 1),
|
||||
);
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenAnswer((_) async => dietsPagination);
|
||||
|
||||
when(mockDietRepository.deleteDiet(1)).thenAnswer((_) async => {});
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
final result = await dietsCubit!.deleteDiet(1);
|
||||
|
||||
expect(result, true);
|
||||
verify(mockDietRepository.deleteDiet(1)).called(1);
|
||||
});
|
||||
|
||||
test('deleteDiet - возвращает false при ошибке', () async {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
when(mockDietRepository.deleteDiet(1)).thenThrow(Exception('Ошибка удаления'));
|
||||
|
||||
final result = await dietsCubit!.deleteDiet(1);
|
||||
|
||||
expect(result, false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -56,6 +56,57 @@ void main() {
|
||||
expect(state.dietsList.length, 1);
|
||||
expect(state.isLoading, isFalse);
|
||||
});
|
||||
|
||||
test('copyWith creates new instance with updated values', () {
|
||||
const original = DietsState(
|
||||
filterState: DietsFilterState(
|
||||
durationIndex: 0,
|
||||
sortingIndex: 0,
|
||||
typeIndex: 0,
|
||||
),
|
||||
dietsList: [],
|
||||
isLoading: true,
|
||||
errorMessage: null,
|
||||
);
|
||||
|
||||
final updated = original.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: 'Test error',
|
||||
);
|
||||
|
||||
expect(updated.isLoading, isFalse);
|
||||
expect(updated.errorMessage, 'Test error');
|
||||
expect(updated.dietsList, original.dietsList);
|
||||
expect(updated.filterState, original.filterState);
|
||||
});
|
||||
});
|
||||
|
||||
group('DietsFilterState', () {
|
||||
test('creates DietsFilterState with values', () {
|
||||
const state = DietsFilterState(
|
||||
sortingIndex: 1,
|
||||
durationIndex: 2,
|
||||
typeIndex: 3,
|
||||
);
|
||||
|
||||
expect(state.sortingIndex, 1);
|
||||
expect(state.durationIndex, 2);
|
||||
expect(state.typeIndex, 3);
|
||||
});
|
||||
|
||||
test('copyWith creates new instance with updated values', () {
|
||||
const original = DietsFilterState(
|
||||
sortingIndex: 0,
|
||||
durationIndex: 0,
|
||||
typeIndex: 0,
|
||||
);
|
||||
|
||||
final updated = original.copyWith(sortingIndex: 1);
|
||||
|
||||
expect(updated.sortingIndex, 1);
|
||||
expect(updated.durationIndex, original.durationIndex);
|
||||
expect(updated.typeIndex, original.typeIndex);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user