forked from eatvio/eatvio_app
feat(tests): tests coverage 67.2
This commit is contained in:
@@ -578,6 +578,233 @@ void main() {
|
||||
|
||||
expect(result, false);
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
page: 2,
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenThrow(Exception('Ошибка загрузки'));
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
await dietsCubit!.loadNextPage();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
expect(dietsCubit!.state.errorMessage, isNotNull);
|
||||
});
|
||||
|
||||
test('loadNextPage - не загружает если lastPage == currentPage', () 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);
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
await dietsCubit!.loadNextPage();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
// Не должно вызывать getDiets для страницы 2
|
||||
verifyNever(mockFeedRepository.getDiets(page: 2, query: anyNamed('query'), types: anyNamed('types'), duration: anyNamed('duration'), sorting: anyNamed('sorting')));
|
||||
});
|
||||
|
||||
test('loadDiets - загружает диеты с query', () 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);
|
||||
|
||||
await dietsCubit!.loadDiets(query: 'test');
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
verify(mockFeedRepository.getDiets(
|
||||
query: 'test',
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).called(1);
|
||||
});
|
||||
|
||||
test('close - отменяет подписки', () async {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
final likesStateSubject = BehaviorSubject<LikesState>();
|
||||
final favoriteStateSubject = BehaviorSubject<FavoriteState>();
|
||||
final commentsStateSubject = BehaviorSubject<CommentsState>();
|
||||
|
||||
when(mockDietRepository.dietLikeState).thenAnswer((_) => likesStateSubject.stream);
|
||||
when(mockDietRepository.dietFavoriteState).thenAnswer((_) => favoriteStateSubject.stream);
|
||||
when(mockDietRepository.dietCommentsState).thenAnswer((_) => commentsStateSubject.stream);
|
||||
|
||||
await dietsCubit!.close();
|
||||
|
||||
expect(likesStateSubject.isClosed, false); // Stream не закрывается, но подписка отменяется
|
||||
expect(dietsCubit!.isClosed, true);
|
||||
|
||||
await likesStateSubject.close();
|
||||
await favoriteStateSubject.close();
|
||||
await commentsStateSubject.close();
|
||||
});
|
||||
|
||||
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)).thenThrow(Exception('Ошибка'));
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
await dietsCubit!.likeDiet(1, 0);
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
|
||||
// После ошибки состояние должно вернуться к исходному
|
||||
expect(dietsCubit!.state.dietsList[0].wasLikedByMe, false);
|
||||
expect(dietsCubit!.state.dietsList[0].totalLikes, 10);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user