feat(tests): fixed feed tests failing and added tests for my_diets, my_plans and recipes

This commit is contained in:
misha
2025-11-28 00:46:25 +03:00
parent 626f74d20d
commit 8917f54e7b
14 changed files with 2807 additions and 103 deletions

View File

@@ -0,0 +1,236 @@
import 'package:eatvio_app/core/models/pagination_model.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/my_plans/repositories/my_diets_repository.dart';
import 'package:eatvio_app/features/my_plans/services/my_plans_service.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'my_plans_repository_test.mocks.dart';
@GenerateNiceMocks([MockSpec<MyPlansService>()])
void main() {
group('MyPlansRepository', () {
late MyPlansRepository myPlansRepository;
late MockMyPlansService mockMyPlansService;
setUp(() {
mockMyPlansService = MockMyPlansService();
myPlansRepository = MyPlansRepository(myPlansService: mockMyPlansService);
});
test('getPlans returns pagination with all parameters', () async {
const page = 1;
const query = 'test plan';
const types = 'fit,slim';
const duration = 30;
const minPrice = 100;
const maxPrice = 500;
final expected = PlanPaginationModel(
data: [
PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: 'https://someurl.com/img.jpg',
duration: duration,
state: 'active',
type: const PlanType(fit: true, slim: false, behavior: false, wellness: false),
details: 'Test plan description',
packetsCount: 1,
author: const Author(authorSlug: '', authorName: '', authorProfileIcon: '', authorIsMe: false),
totalLikes: 1,
totalViews: 1,
totalComments: 1,
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(mockMyPlansService.getPlans(
page: page,
query: query,
types: types,
duration: duration,
minPrice: minPrice,
maxPrice: maxPrice,
)).thenAnswer((_) async => expected);
final result = await myPlansRepository.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.name, 'Test Plan');
verify(mockMyPlansService.getPlans(
page: page,
query: query,
types: types,
duration: duration,
minPrice: minPrice,
maxPrice: maxPrice,
)).called(1);
});
test('getPlans returns pagination with default parameters', () async {
final expected = PlanPaginationModel(
data: [
PlanModel(
id: 2,
slug: 'another-plan',
name: 'Another Plan',
image: 'https://someurl.com/img2.jpg',
duration: 14,
state: 'draft',
type: const PlanType(fit: false, slim: true, behavior: false, wellness: false),
details: '',
packetsCount: 2,
author: const Author(authorSlug: 'author-slug', authorName: 'Author Name', authorProfileIcon: '', authorIsMe: true),
totalLikes: 5,
totalViews: 10,
totalComments: 2,
isFavorite: true,
minPriceOfOptions: 200.0,
maxPriceOfOptions: 600.0,
wasLikedByMe: true,
),
],
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(mockMyPlansService.getPlans(
page: 1,
query: null,
types: null,
duration: null,
minPrice: null,
maxPrice: null,
)).thenAnswer((_) async => expected);
final result = await myPlansRepository.getPlans();
expect(result, isA<PlanPaginationModel>());
expect(result.data.length, 1);
expect(result.data.first.id, 2);
verify(mockMyPlansService.getPlans(
page: 1,
query: null,
types: null,
duration: null,
minPrice: null,
maxPrice: null,
)).called(1);
});
test('getPlans returns pagination with partial parameters', () async {
const page = 2;
const query = 'wellness';
const duration = 21;
final expected = PlanPaginationModel(
data: [],
links: const Links(first: '', last: '', prev: null, next: null),
meta: const Meta(currentPage: 2, lastPage: 2, total: 0, perPage: 10, from: null, links: [Link(url: null, label: null, active: null)], to: null),
);
when(mockMyPlansService.getPlans(
page: page,
query: query,
types: null,
duration: duration,
minPrice: null,
maxPrice: null,
)).thenAnswer((_) async => expected);
final result = await myPlansRepository.getPlans(
page: page,
query: query,
duration: duration,
);
expect(result, isA<PlanPaginationModel>());
expect(result.data.length, 0);
expect(result.meta.currentPage, 2);
verify(mockMyPlansService.getPlans(
page: page,
query: query,
types: null,
duration: duration,
minPrice: null,
maxPrice: null,
)).called(1);
});
test('deletePlan deletes plan by slug', () async {
const slug = 'test-plan-slug';
when(mockMyPlansService.deletePlan(slug: slug)).thenAnswer((_) async => Future.value());
await myPlansRepository.deletePlan(slug);
verify(mockMyPlansService.deletePlan(slug: slug)).called(1);
});
test('unpublish unpublishes plan by slug', () async {
const slug = 'draft-plan-slug';
when(mockMyPlansService.unpublish(slug: slug)).thenAnswer((_) async => Future.value());
await myPlansRepository.unpublish(slug);
verify(mockMyPlansService.unpublish(slug: slug)).called(1);
});
test('deletePlan rethrows exception on error', () async {
const slug = 'test-plan-slug';
when(mockMyPlansService.deletePlan(slug: slug)).thenThrow(Exception('Network error'));
expect(() => myPlansRepository.deletePlan(slug), throwsException);
verify(mockMyPlansService.deletePlan(slug: slug)).called(1);
});
test('unpublish rethrows exception on error', () async {
const slug = 'draft-plan-slug';
when(mockMyPlansService.unpublish(slug: slug)).thenThrow(Exception('Network error'));
expect(() => myPlansRepository.unpublish(slug), throwsException);
verify(mockMyPlansService.unpublish(slug: slug)).called(1);
});
test('getPlans rethrows exception on error', () async {
when(mockMyPlansService.getPlans(
page: 1,
query: null,
types: null,
duration: null,
minPrice: null,
maxPrice: null,
)).thenThrow(Exception('Network error'));
expect(() => myPlansRepository.getPlans(), throwsException);
verify(mockMyPlansService.getPlans(
page: 1,
query: null,
types: null,
duration: null,
minPrice: null,
maxPrice: null,
)).called(1);
});
});
}