forked from eatvio/eatvio_app
feat(tests): fixed feed tests failing and added tests for my_diets, my_plans and recipes
This commit is contained in:
165
test/features/my_plans/services/my_plans_service_test.dart
Normal file
165
test/features/my_plans/services/my_plans_service_test.dart
Normal file
@@ -0,0 +1,165 @@
|
||||
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/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_service_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<MyPlansService>()])
|
||||
void main() {
|
||||
group('MyPlansService', () {
|
||||
late MockMyPlansService mockMyPlansService;
|
||||
|
||||
setUp(() {
|
||||
mockMyPlansService = 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 mockMyPlansService.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 minimal parameters', () async {
|
||||
const page = 1;
|
||||
const expected = PlanPaginationModel(
|
||||
data: [
|
||||
PlanModel(
|
||||
id: 2,
|
||||
slug: 'another-plan',
|
||||
name: 'Another Plan',
|
||||
image: 'https://someurl.com/img2.jpg',
|
||||
duration: 14,
|
||||
state: 'draft',
|
||||
type: PlanType(fit: false, slim: true, behavior: false, wellness: false),
|
||||
details: '',
|
||||
packetsCount: 2,
|
||||
author: 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: 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(mockMyPlansService.getPlans(page: page, query: null, types: null, duration: null, minPrice: null, maxPrice: null)).thenAnswer((_) async => expected);
|
||||
|
||||
final result = await mockMyPlansService.getPlans(page: page, query: null, types: null, duration: null, minPrice: null, maxPrice: null);
|
||||
|
||||
expect(result, isA<PlanPaginationModel>());
|
||||
expect(result.data.length, 1);
|
||||
expect(result.data.first.id, 2);
|
||||
verify(mockMyPlansService.getPlans(page: page, 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;
|
||||
const expected = PlanPaginationModel(
|
||||
data: [],
|
||||
links: Links(first: '', last: '', prev: null, next: null),
|
||||
meta: 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 mockMyPlansService.getPlans(page: page, query: query, types: null, duration: duration, minPrice: null, maxPrice: null);
|
||||
|
||||
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 mockMyPlansService.deletePlan(slug: slug);
|
||||
|
||||
verify(mockMyPlansService.deletePlan(slug: slug)).called(1);
|
||||
});
|
||||
|
||||
test('deletePlan deletes plan with different slug', () async {
|
||||
const slug = 'another-plan-slug';
|
||||
|
||||
when(mockMyPlansService.deletePlan(slug: slug)).thenAnswer((_) async => Future.value());
|
||||
|
||||
await mockMyPlansService.deletePlan(slug: 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 mockMyPlansService.unpublish(slug: slug);
|
||||
|
||||
verify(mockMyPlansService.unpublish(slug: slug)).called(1);
|
||||
});
|
||||
|
||||
test('unpublish unpublishes plan with different slug', () async {
|
||||
const slug = 'published-plan-slug';
|
||||
|
||||
when(mockMyPlansService.unpublish(slug: slug)).thenAnswer((_) async => Future.value());
|
||||
|
||||
await mockMyPlansService.unpublish(slug: slug);
|
||||
|
||||
verify(mockMyPlansService.unpublish(slug: slug)).called(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user