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,151 @@
import 'package:eatvio_app/features/feed/models/recipes_model.dart';
import 'package:eatvio_app/features/recipe/models/my_recipe_model.dart';
import 'package:eatvio_app/features/recipe/services/my_recipes_service.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'my_recipes_service_test.mocks.dart';
@GenerateNiceMocks([MockSpec<MyRecipesService>()])
void main() {
group('MyRecipesService', () {
late MockMyRecipesService mockMyRecipesService;
setUp(() {
mockMyRecipesService = MockMyRecipesService();
});
test('getMyRecipes returns pagination with recipes', () async {
final expected = MyRecipePaginationModel(
data: [
MyRecipeModel(
id: 1,
image: 'https://someurl.com/img.jpg',
state: 'published',
cookingTime: 30,
activeCookingTime: 20,
description: 'Test recipe description',
steps: [
const Step(image: 'https://someurl.com/step1.jpg', description: 'Step 1 description'),
const Step(image: 'https://someurl.com/step2.jpg', description: 'Step 2 description'),
],
videoUrl: 'https://someurl.com/video.mp4',
title: 'Test Recipe',
slug: 'test-recipe',
calories: 250.0,
likesCount: 5,
viewsCount: 10,
),
],
);
when(mockMyRecipesService.getMyRecipes()).thenAnswer((_) async => expected);
final result = await mockMyRecipesService.getMyRecipes();
expect(result, isA<MyRecipePaginationModel>());
expect(result.data.length, 1);
expect(result.data.first.title, 'Test Recipe');
expect(result.data.first.id, 1);
verify(mockMyRecipesService.getMyRecipes()).called(1);
});
test('getMyRecipes returns pagination with multiple recipes', () async {
final expected = MyRecipePaginationModel(
data: [
MyRecipeModel(
id: 1,
image: 'https://someurl.com/img1.jpg',
state: 'published',
cookingTime: 30,
activeCookingTime: 20,
description: 'First recipe description',
steps: [
const Step(image: 'https://someurl.com/step1.jpg', description: 'Step 1'),
],
videoUrl: 'https://someurl.com/video1.mp4',
title: 'First Recipe',
slug: 'first-recipe',
calories: 250.0,
likesCount: 5,
viewsCount: 10,
),
MyRecipeModel(
id: 2,
image: 'https://someurl.com/img2.jpg',
state: 'draft',
cookingTime: 45,
activeCookingTime: 30,
description: 'Second recipe description',
steps: null,
videoUrl: null,
title: 'Second Recipe',
slug: 'second-recipe',
calories: 350.0,
likesCount: 8,
viewsCount: 15,
),
],
);
when(mockMyRecipesService.getMyRecipes()).thenAnswer((_) async => expected);
final result = await mockMyRecipesService.getMyRecipes();
expect(result, isA<MyRecipePaginationModel>());
expect(result.data.length, 2);
expect(result.data.first.title, 'First Recipe');
expect(result.data.last.title, 'Second Recipe');
verify(mockMyRecipesService.getMyRecipes()).called(1);
});
test('getMyRecipes returns empty pagination', () async {
final expected = MyRecipePaginationModel(
data: [],
);
when(mockMyRecipesService.getMyRecipes()).thenAnswer((_) async => expected);
final result = await mockMyRecipesService.getMyRecipes();
expect(result, isA<MyRecipePaginationModel>());
expect(result.data.length, 0);
verify(mockMyRecipesService.getMyRecipes()).called(1);
});
test('getMyRecipes returns recipe with null steps and videoUrl', () async {
final expected = MyRecipePaginationModel(
data: [
MyRecipeModel(
id: 3,
image: 'https://someurl.com/img3.jpg',
state: 'draft',
cookingTime: 15,
activeCookingTime: 10,
description: 'Simple recipe without steps',
steps: null,
videoUrl: null,
title: 'Simple Recipe',
slug: 'simple-recipe',
calories: 150.0,
likesCount: 2,
viewsCount: 5,
),
],
);
when(mockMyRecipesService.getMyRecipes()).thenAnswer((_) async => expected);
final result = await mockMyRecipesService.getMyRecipes();
expect(result, isA<MyRecipePaginationModel>());
expect(result.data.length, 1);
expect(result.data.first.steps, isNull);
expect(result.data.first.videoUrl, isNull);
expect(result.data.first.state, 'draft');
verify(mockMyRecipesService.getMyRecipes()).called(1);
});
});
}