feat(test): repostory, cubit and widget tests for feed

This commit is contained in:
misha
2025-11-25 11:13:37 +03:00
parent b83d91e7f2
commit c6c42b0525
5 changed files with 15123 additions and 11671 deletions

View File

@@ -0,0 +1,609 @@
import 'package:eatvio_app/core/models/pagination_model.dart';
import 'package:eatvio_app/features/feed/models/comment_model.dart';
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:eatvio_app/features/feed/models/feed_blog_post_model.dart';
import 'package:eatvio_app/features/feed/models/feed_blog_post_settings_model.dart';
import 'package:eatvio_app/features/feed/models/feed_consultant_model.dart';
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
import 'package:eatvio_app/features/feed/models/recipes_model.dart';
import 'package:eatvio_app/features/feed/models/recipes_pagination_model.dart';
import 'package:eatvio_app/features/feed/models/view_model.dart';
import 'package:eatvio_app/features/feed/repositories/feed_repository.dart';
import 'package:eatvio_app/features/feed/services/feed_service.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'feed_repository_test.mocks.dart';
@GenerateNiceMocks([MockSpec<FeedService>()])
void main() {
group('FeedRepository', () {
late FeedRepository feedRepository;
late MockFeedService mockFeedService;
setUp(() {
mockFeedService = MockFeedService();
feedRepository = FeedRepository(feedService: mockFeedService);
});
test('getFeedBlogPosts - возвращает список постов блога', () async {
const page = 1;
const query = 'test query';
final expectedSettings = FeedBlogPostSettingsModel(
feedBlogPostList: [
FeedBlogPostModel(
id: 1,
title: 'Test Post',
description: 'Test description',
image: 'https://example.com/image.jpg',
state: 'published',
author: const Author(authorSlug: 'author-1', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false),
videoUrl: 'https://example.com/video.mp4',
createdAt: DateTime(2024, 1, 15),
updatedAt: DateTime(2024, 1, 15),
totalLikes: 10,
totalViews: 100,
totalComments: 5,
wasLikedByMe: false,
),
],
meta: const Meta(currentPage: 1, lastPage: 1, total: 1, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(mockFeedService.getBlogPostList(page: page, query: query)).thenAnswer((_) async => expectedSettings);
final result = await feedRepository.getFeedBlogPosts(query: query, page: page);
expect(result, isA<FeedBlogPostSettingsModel>());
expect(result.feedBlogPostList.length, 1);
expect(result.feedBlogPostList.first.id, 1);
verify(mockFeedService.getBlogPostList(page: page, query: query)).called(1);
});
test('getFeedBlogPosts - возвращает список постов без query', () async {
const page = 1;
const expectedSettings = FeedBlogPostSettingsModel(
feedBlogPostList: [],
meta: Meta(currentPage: 1, lastPage: 1, total: 0, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(mockFeedService.getBlogPostList(page: page, query: null)).thenAnswer((_) async => expectedSettings);
final result = await feedRepository.getFeedBlogPosts(page: page);
expect(result, isA<FeedBlogPostSettingsModel>());
expect(result.feedBlogPostList.isEmpty, true);
verify(mockFeedService.getBlogPostList(page: page, query: null)).called(1);
});
test('getBlogPost - возвращает пост блога по ID', () async {
const id = 1;
final expectedPost = FeedBlogPostModel(
id: id,
title: 'Test Post',
description: 'Test description',
image: 'https://example.com/image.jpg',
state: 'published',
author: const Author(authorSlug: 'author-1', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false),
videoUrl: 'https://example.com/video.mp4',
createdAt: DateTime(2024, 1, 15),
updatedAt: DateTime(2024, 1, 15),
totalLikes: 10,
totalViews: 100,
totalComments: 5,
wasLikedByMe: false,
);
when(mockFeedService.getBlogPost(postId: id)).thenAnswer((_) async => expectedPost);
final result = await feedRepository.getBlogPost(id: id);
expect(result, isA<FeedBlogPostModel>());
expect(result.id, id);
verify(mockFeedService.getBlogPost(postId: id)).called(1);
});
test('getRecipes - возвращает список рецептов', () async {
const page = 1;
const query = 'pasta';
final tags = [1, 2, 3];
final expectedRecipes = [
Recipe(
id: 1,
image: 'https://example.com/recipe.jpg',
state: 'published',
cookingTime: 30,
activeCookingTime: 20,
ingredients: [const Ingredient(name: 'Pasta', unit: 'g', portion: '200g', productId: 1)],
description: 'Test recipe description',
steps: [const Step(description: 'Step 1', image: 'https://example.com/step1.jpg')],
videoUrl: 'https://example.com/video.mp4',
createdAt: DateTime(2024, 1, 15),
updatedAt: DateTime(2024, 1, 15),
title: 'Pasta Recipe',
type: 'recipe',
slug: 'pasta-recipe',
commentsCount: 5,
wasLikedByMe: false,
isFavorite: false,
calories: 350.0,
servings: 4,
likesCount: 20,
viewsCount: 150,
tags: [const Tag(name: 'Italian')],
author: const Author(authorSlug: 'chef-1', authorName: 'Chef Name', authorProfileIcon: 'https://example.com/chef.jpg', authorIsMe: false),
),
];
final expectedPagination = RecipesPagination(recipesList: expectedRecipes);
when(mockFeedService.getRecipes(query: query, tags: tags, page: page)).thenAnswer((_) async => expectedPagination);
final result = await feedRepository.getRecipes(query: query, tags: tags, page: page);
expect(result, isA<List<Recipe>>());
expect(result.length, 1);
expect(result.first.id, 1);
verify(mockFeedService.getRecipes(query: query, tags: tags, page: page)).called(1);
});
test('getRecipes - возвращает список рецептов без query и tags', () async {
const page = 1;
final expectedRecipes = <Recipe>[];
final expectedPagination = RecipesPagination(recipesList: expectedRecipes);
when(mockFeedService.getRecipes(query: null, tags: null, page: page)).thenAnswer((_) async => expectedPagination);
final result = await feedRepository.getRecipes(page: page);
expect(result, isA<List<Recipe>>());
expect(result.isEmpty, true);
verify(mockFeedService.getRecipes(query: null, tags: null, page: page)).called(1);
});
test('getDiets - возвращает список диет', () async {
const page = 1;
const query = 'keto';
const types = 'keto';
const duration = 30;
const sorting = 'popular';
const expectedDiets = DietsPagination(
data: [
Diet(
id: 1,
slug: 'keto-diet',
name: 'Keto Diet',
image: 'https://example.com/keto.jpg',
duration: duration,
state: DietState.active,
type: DietType(light: false, veg: false, carb: false, meat: true),
details: 'Keto diet description',
dailySchedule: {},
author: Author(
authorSlug: 'nutritionist-1',
authorName: 'Nutritionist Name',
authorProfileIcon: 'https://example.com/nutritionist.jpg',
authorIsMe: false,
),
totalLikes: 50,
totalViews: 500,
totalComments: 25,
wasLikedByMe: false,
isFavorite: false,
dailyNutrition: [],
),
],
links: Links(first: '', last: '', prev: null, next: null),
meta: Meta(currentPage: 1, lastPage: 1, total: 1, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(mockFeedService.getDiets(page: page, query: query, types: types, duration: duration, sorting: sorting)).thenAnswer((_) async => expectedDiets);
final result = await feedRepository.getDiets(page: page, query: query, types: types, duration: duration, sorting: sorting);
expect(result, isA<DietsPagination>());
expect(result.data.length, 1);
expect(result.data.first.id, 1);
verify(mockFeedService.getDiets(page: page, query: query, types: types, duration: duration, sorting: sorting)).called(1);
});
test('getDiets - возвращает список диет без параметров', () async {
const page = 1;
const expectedDiets = DietsPagination(
data: [],
links: Links(first: '', last: '', prev: null, next: null),
meta: Meta(currentPage: 1, lastPage: 1, total: 0, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(mockFeedService.getDiets(page: page, query: null, types: null, duration: null, sorting: null)).thenAnswer((_) async => expectedDiets);
final result = await feedRepository.getDiets(page: page);
expect(result, isA<DietsPagination>());
expect(result.data.isEmpty, true);
verify(mockFeedService.getDiets(page: page, query: null, types: null, duration: null, sorting: null)).called(1);
});
test('getPlans - возвращает список планов', () async {
const page = 1;
const query = 'weight loss';
const types = 'slim';
const duration = 30;
const minPrice = 1000;
const maxPrice = 5000;
final expectedPlans = PlanPaginationModel(
data: [
PlanModel(
id: 1,
slug: 'weight-loss-plan',
name: 'Weight Loss Plan',
image: 'https://example.com/plan.jpg',
duration: duration,
state: 'active',
type: const PlanType(fit: false, slim: true, behavior: false, wellness: false),
details: 'Weight loss plan description',
packetsCount: 30,
author: const Author(
authorSlug: 'consultant-1',
authorName: 'Consultant Name',
authorProfileIcon: 'https://example.com/consultant.jpg',
authorIsMe: false,
),
totalLikes: 100,
totalViews: 1000,
totalComments: 50,
isFavorite: false,
minPriceOfOptions: minPrice.toDouble(),
maxPriceOfOptions: maxPrice.toDouble(),
wasLikedByMe: false,
),
],
links: const Links(first: '', last: '', prev: null, next: null),
meta: const Meta(currentPage: 1, lastPage: 1, total: 1, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(
mockFeedService.getPlans(page: page, query: query, types: types, duration: duration, minPrice: minPrice, maxPrice: maxPrice),
).thenAnswer((_) async => expectedPlans);
final result = await feedRepository.getPlans(page: page, query: query, types: types, duration: duration, minPrice: minPrice, maxPrice: maxPrice);
expect(result, isA<PlanPaginationModel>());
expect(result.data.length, 1);
expect(result.data.first.id, 1);
verify(mockFeedService.getPlans(page: page, query: query, types: types, duration: duration, minPrice: minPrice, maxPrice: maxPrice)).called(1);
});
test('getPlans - возвращает список планов без параметров', () async {
const page = 1;
const expectedPlans = PlanPaginationModel(
data: [],
links: Links(first: '', last: '', prev: null, next: null),
meta: Meta(currentPage: 1, lastPage: 1, total: 0, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(
mockFeedService.getPlans(page: page, query: null, types: null, duration: null, minPrice: null, maxPrice: null),
).thenAnswer((_) async => expectedPlans);
final result = await feedRepository.getPlans(page: page);
expect(result, isA<PlanPaginationModel>());
expect(result.data.isEmpty, true);
verify(mockFeedService.getPlans(page: page, query: null, types: null, duration: null, minPrice: null, maxPrice: null)).called(1);
});
test('getBlogPostComments - возвращает комментарии поста блога', () async {
const id = '1';
final expectedComments = CommentModel(
data: [
Comment(
id: 1,
authorName: 'User 1',
authorSlug: 'user-1',
authorProfileIcon: 'https://example.com/user1.jpg',
body: 'Great post!',
childrenComments: [],
createdAt: DateTime(2024, 1, 15),
),
],
links: const Links(first: '', last: '', prev: null, next: null),
meta: const Meta(currentPage: 1, lastPage: 1, total: 1, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(mockFeedService.getBlogPostComments(id: id)).thenAnswer((_) async => expectedComments);
final result = await feedRepository.getBlogPostComments(id: id);
expect(result, isA<CommentModel>());
expect(result.data.length, 1);
expect(result.data.first.id, 1);
verify(mockFeedService.getBlogPostComments(id: id)).called(1);
});
test('sendBlogPostComment - отправляет комментарий к посту блога', () async {
const id = '1';
const commentText = 'Nice post!';
const parentCommentId = 5;
when(mockFeedService.sendBlogPostComment(id: id, body: commentText, parentCommentId: parentCommentId)).thenAnswer((_) async => Future.value());
await feedRepository.sendBlogPostComment(id: id, commentText: commentText, parentCommentId: parentCommentId);
verify(mockFeedService.sendBlogPostComment(id: id, body: commentText, parentCommentId: parentCommentId)).called(1);
});
test('sendBlogPostComment - отправляет комментарий без parentCommentId', () async {
const id = '1';
const commentText = 'Nice post!';
when(mockFeedService.sendBlogPostComment(id: id, body: commentText, parentCommentId: null)).thenAnswer((_) async => Future.value());
await feedRepository.sendBlogPostComment(id: id, commentText: commentText);
verify(mockFeedService.sendBlogPostComment(id: id, body: commentText, parentCommentId: null)).called(1);
});
test('getDietsComments - возвращает комментарии диеты', () async {
const id = '5';
final expectedComments = CommentModel(
data: [
Comment(
id: 2,
authorName: 'User 2',
authorSlug: 'user-2',
authorProfileIcon: 'https://example.com/user2.jpg',
body: 'Great diet!',
childrenComments: [],
createdAt: DateTime(2024, 1, 15),
),
],
links: const Links(first: '', last: '', prev: null, next: null),
meta: const Meta(currentPage: 1, lastPage: 1, total: 1, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(mockFeedService.getDietsComments(id: id)).thenAnswer((_) async => expectedComments);
final result = await feedRepository.getDietsComments(id: id);
expect(result, isA<CommentModel>());
expect(result.data.length, 1);
verify(mockFeedService.getDietsComments(id: id)).called(1);
});
test('sendDietsComment - отправляет комментарий к диете', () async {
const id = '5';
const commentText = 'Nice diet!';
const parentCommentId = 10;
when(mockFeedService.sendDietsComment(id: id, body: commentText, parentCommentId: parentCommentId)).thenAnswer((_) async => Future.value());
await feedRepository.sendDietsComment(id: id, commentText: commentText, parentCommentId: parentCommentId);
verify(mockFeedService.sendDietsComment(id: id, body: commentText, parentCommentId: parentCommentId)).called(1);
});
test('getRecipesComments - возвращает комментарии рецепта', () async {
const id = '33';
final expectedComments = CommentModel(
data: [
Comment(
id: 3,
authorName: 'User 3',
authorSlug: 'user-3',
authorProfileIcon: 'https://example.com/user3.jpg',
body: 'Looks tasty!',
childrenComments: [],
createdAt: DateTime(2024, 1, 15),
),
],
links: const Links(first: '', last: '', prev: null, next: null),
meta: const Meta(currentPage: 1, lastPage: 1, total: 1, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(mockFeedService.getRecipesComments(id: id)).thenAnswer((_) async => expectedComments);
final result = await feedRepository.getRecipesComments(id: id);
expect(result, isA<CommentModel>());
expect(result.data.length, 1);
verify(mockFeedService.getRecipesComments(id: id)).called(1);
});
test('sendRecipesComment - отправляет комментарий к рецепту', () async {
const id = '33';
const commentText = 'Tasty recipe!';
const parentCommentId = 15;
when(mockFeedService.sendRecipesComment(id: id, body: commentText, parentCommentId: parentCommentId)).thenAnswer((_) async => Future.value());
await feedRepository.sendRecipesComment(id: id, commentText: commentText, parentCommentId: parentCommentId);
verify(mockFeedService.sendRecipesComment(id: id, body: commentText, parentCommentId: parentCommentId)).called(1);
});
test('getPlansComments - возвращает комментарии плана', () async {
const slug = 'plan-1';
final expectedComments = CommentModel(
data: [
Comment(
id: 4,
authorName: 'User 4',
authorSlug: 'user-4',
authorProfileIcon: 'https://example.com/user4.jpg',
body: 'Super plan!',
childrenComments: [],
createdAt: DateTime(2024, 1, 15),
),
],
links: const Links(first: '', last: '', prev: null, next: null),
meta: const Meta(currentPage: 1, lastPage: 1, total: 1, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(mockFeedService.getPlansComments(slug: slug)).thenAnswer((_) async => expectedComments);
final result = await feedRepository.getPlansComments(slug: slug);
expect(result, isA<CommentModel>());
expect(result.data.length, 1);
verify(mockFeedService.getPlansComments(slug: slug)).called(1);
});
test('sendPlansComment - отправляет комментарий к плану', () async {
const slug = 'plan-1';
const commentText = 'Great plan!';
const parentCommentId = 20;
when(mockFeedService.sendPlansComment(slug: slug, body: commentText, parentCommentId: parentCommentId)).thenAnswer((_) async => Future.value());
await feedRepository.sendPlansComment(slug: slug, commentText: commentText, parentCommentId: parentCommentId);
verify(mockFeedService.sendPlansComment(slug: slug, body: commentText, parentCommentId: parentCommentId)).called(1);
});
test('likeBlogPost - лайкает пост блога', () async {
const id = 1;
when(mockFeedService.likeBlogPost(id: id)).thenAnswer((_) async => Future.value());
await feedRepository.likeBlogPost(id: id);
verify(mockFeedService.likeBlogPost(id: id)).called(1);
});
test('unlikedBlogPost - убирает лайк с поста блога', () async {
const id = 1;
when(mockFeedService.unlikedBlogPost(id: id)).thenAnswer((_) async => Future.value());
await feedRepository.unlikedBlogPost(id: id);
verify(mockFeedService.unlikedBlogPost(id: id)).called(1);
});
test('getConsultants - возвращает список консультантов', () async {
const name = 'Anna';
final expectedConsultants = [
const FeedConsultantModel(
slug: 'consultant-1',
name: 'Anna Ivanova',
description: 'Test consultant description',
totalScores: 5,
photo: 'https://example.com/consultant1.jpg',
badges: [],
),
];
final expectedPagination = FeedConsultantPaginationModel(
data: expectedConsultants,
links: const Links(first: '', last: '', prev: null, next: null),
meta: const Meta(currentPage: 1, lastPage: 1, total: 1, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(mockFeedService.getConsultants(name: name)).thenAnswer((_) async => expectedPagination);
final result = await feedRepository.getConsultants(name);
expect(result, isA<List<FeedConsultantModel>>());
expect(result.length, 1);
expect(result.first.name, 'Anna Ivanova');
verify(mockFeedService.getConsultants(name: name)).called(1);
});
test('getConsultants - возвращает список консультантов без имени', () async {
final expectedConsultants = <FeedConsultantModel>[];
final expectedPagination = FeedConsultantPaginationModel(
data: expectedConsultants,
links: const Links(first: '', last: '', prev: null, next: null),
meta: const Meta(currentPage: 1, lastPage: 1, total: 0, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(mockFeedService.getConsultants(name: null)).thenAnswer((_) async => expectedPagination);
final result = await feedRepository.getConsultants();
expect(result, isA<List<FeedConsultantModel>>());
expect(result.isEmpty, true);
verify(mockFeedService.getConsultants(name: null)).called(1);
});
test('viewPost - отправляет просмотр поста', () async {
const id = 1;
when(mockFeedService.viewPost(id: id)).thenAnswer((_) async => Future.value());
await feedRepository.viewPost(id);
verify(mockFeedService.viewPost(id: id)).called(1);
});
test('viewRecipe - отправляет просмотр рецепта', () async {
const slug = 'recipe-1';
when(mockFeedService.viewRecipe(slug: slug)).thenAnswer((_) async => Future.value());
await feedRepository.viewRecipe(slug);
verify(mockFeedService.viewRecipe(slug: slug)).called(1);
});
test('viewDiet - отправляет просмотр диеты', () async {
const id = 2;
when(mockFeedService.viewDiet(id: id)).thenAnswer((_) async => Future.value());
await feedRepository.viewDiet(id);
verify(mockFeedService.viewDiet(id: id)).called(1);
});
test('viewPlan - отправляет просмотр плана', () async {
const slug = 'plan-7';
when(mockFeedService.viewPlan(slug: slug)).thenAnswer((_) async => Future.value());
await feedRepository.viewPlan(slug);
verify(mockFeedService.viewPlan(slug: slug)).called(1);
});
test('viewContent - отправляет просмотр контента', () async {
final viewModel = ViewModel(type: 'recipe', slugs: ['recipe-1']);
when(mockFeedService.viewContent(viewModel: viewModel)).thenAnswer((_) async => Future.value());
await feedRepository.viewContent(viewModel: viewModel);
verify(mockFeedService.viewContent(viewModel: viewModel)).called(1);
});
test('viewContent - отправляет просмотр контента с ids', () async {
final viewModel = ViewModel(type: 'diet', ids: [1, 2, 3]);
when(mockFeedService.viewContent(viewModel: viewModel)).thenAnswer((_) async => Future.value());
await feedRepository.viewContent(viewModel: viewModel);
verify(mockFeedService.viewContent(viewModel: viewModel)).called(1);
});
test('getFeedBlogPosts - пробрасывает исключение при ошибке', () async {
const page = 1;
final exception = Exception('Network error');
when(mockFeedService.getBlogPostList(page: page, query: null)).thenThrow(exception);
expect(() => feedRepository.getFeedBlogPosts(page: page), throwsA(isA<Exception>()));
});
test('getBlogPost - пробрасывает исключение при ошибке', () async {
const id = 1;
final exception = Exception('Not found');
when(mockFeedService.getBlogPost(postId: id)).thenThrow(exception);
expect(() => feedRepository.getBlogPost(id: id), throwsA(isA<Exception>()));
});
});
}