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:
@@ -0,0 +1,127 @@
|
||||
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/repositories/my_recipes_repository.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_repository_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<MyRecipesService>()])
|
||||
void main() {
|
||||
group('MyRecipesRepository', () {
|
||||
late MyRecipesRepository myRecipesRepository;
|
||||
late MockMyRecipesService mockMyRecipesService;
|
||||
|
||||
setUp(() {
|
||||
mockMyRecipesService = MockMyRecipesService();
|
||||
myRecipesRepository = MyRecipesRepository(myRecipesService: mockMyRecipesService);
|
||||
});
|
||||
|
||||
test('getMyRecipes returns list of recipes', () async {
|
||||
final expectedPagination = 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'),
|
||||
],
|
||||
videoUrl: 'https://someurl.com/video.mp4',
|
||||
title: 'Test Recipe',
|
||||
slug: 'test-recipe',
|
||||
calories: 250.0,
|
||||
likesCount: 5,
|
||||
viewsCount: 10,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
when(mockMyRecipesService.getMyRecipes()).thenAnswer((_) async => expectedPagination);
|
||||
|
||||
final result = await myRecipesRepository.getMyRecipes();
|
||||
|
||||
expect(result, isA<List<MyRecipeModel>>());
|
||||
expect(result.length, 1);
|
||||
expect(result.first.title, 'Test Recipe');
|
||||
expect(result.first.id, 1);
|
||||
verify(mockMyRecipesService.getMyRecipes()).called(1);
|
||||
});
|
||||
|
||||
test('getMyRecipes returns empty list', () async {
|
||||
final expectedPagination = MyRecipePaginationModel(
|
||||
data: [],
|
||||
);
|
||||
|
||||
when(mockMyRecipesService.getMyRecipes()).thenAnswer((_) async => expectedPagination);
|
||||
|
||||
final result = await myRecipesRepository.getMyRecipes();
|
||||
|
||||
expect(result, isA<List<MyRecipeModel>>());
|
||||
expect(result.length, 0);
|
||||
verify(mockMyRecipesService.getMyRecipes()).called(1);
|
||||
});
|
||||
|
||||
test('getMyRecipes returns multiple recipes', () async {
|
||||
final expectedPagination = MyRecipePaginationModel(
|
||||
data: [
|
||||
MyRecipeModel(
|
||||
id: 1,
|
||||
image: 'https://someurl.com/img1.jpg',
|
||||
state: 'published',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 20,
|
||||
description: 'First recipe',
|
||||
steps: null,
|
||||
videoUrl: null,
|
||||
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',
|
||||
steps: [
|
||||
const Step(image: 'https://someurl.com/step1.jpg', description: 'Step 1'),
|
||||
],
|
||||
videoUrl: 'https://someurl.com/video.mp4',
|
||||
title: 'Second Recipe',
|
||||
slug: 'second-recipe',
|
||||
calories: 350.0,
|
||||
likesCount: 8,
|
||||
viewsCount: 15,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
when(mockMyRecipesService.getMyRecipes()).thenAnswer((_) async => expectedPagination);
|
||||
|
||||
final result = await myRecipesRepository.getMyRecipes();
|
||||
|
||||
expect(result, isA<List<MyRecipeModel>>());
|
||||
expect(result.length, 2);
|
||||
expect(result.first.title, 'First Recipe');
|
||||
expect(result.last.title, 'Second Recipe');
|
||||
verify(mockMyRecipesService.getMyRecipes()).called(1);
|
||||
});
|
||||
|
||||
test('getMyRecipes rethrows exception on error', () async {
|
||||
when(mockMyRecipesService.getMyRecipes()).thenThrow(Exception('Network error'));
|
||||
|
||||
expect(() => myRecipesRepository.getMyRecipes(), throwsException);
|
||||
verify(mockMyRecipesService.getMyRecipes()).called(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user