forked from eatvio/eatvio_app
feat(tests): added new tests and updated fixtures
This commit is contained in:
@@ -7,6 +7,8 @@ import 'package:eatvio_app/core/theme/theme_helper.dart';
|
||||
import 'package:eatvio_app/features/blog_post/presentation/cubit/blog_post_cubit.dart';
|
||||
import 'package:eatvio_app/features/blog_post/presentation/cubit/blog_post_state.dart';
|
||||
import 'package:eatvio_app/features/blog_post/presentation/pages/blog_post_screen.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/presentation/bloc/feed_cubit/feed_cubit.dart';
|
||||
import 'package:eatvio_app/generated/l10n.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -159,5 +161,49 @@ void main() {
|
||||
expect(find.byType(BlogPostScreen), findsOneWidget);
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('renders loaded state with blog post model', (tester) async {
|
||||
await mockNetworkImagesFor(() async {
|
||||
final blogPostCubit = MockBlogPostCubit();
|
||||
final feedCubit = MockFeedCubit();
|
||||
final commentsCubit = MockCommentsCubit();
|
||||
final blogPostModel = FeedBlogPostModel(
|
||||
id: 1,
|
||||
title: 'Test Post Title',
|
||||
description: 'Test post 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: '',
|
||||
createdAt: DateTime(2024, 1, 15),
|
||||
updatedAt: DateTime(2024, 1, 15),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
wasLikedByMe: false,
|
||||
);
|
||||
final state = BlogPostState(isLoading: false, blogPostModel: blogPostModel, error: null);
|
||||
|
||||
when(blogPostCubit.state).thenReturn(state);
|
||||
when(blogPostCubit.stream).thenAnswer((_) => Stream.value(state));
|
||||
when(blogPostCubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(feedCubit.viewPost(any)).thenAnswer((_) async => Future<void>.value());
|
||||
when(feedCubit.stream).thenAnswer((_) => const Stream.empty());
|
||||
when(feedCubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(commentsCubit.state).thenReturn(const CommentsState());
|
||||
when(commentsCubit.stream).thenAnswer((_) => const Stream<CommentsState>.empty());
|
||||
when(commentsCubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(commentsCubit.getComments(any, any, any)).thenAnswer((_) async => Future<void>.value());
|
||||
|
||||
registerMocks(blogPostCubit, commentsCubit);
|
||||
|
||||
await tester.pumpWidget(wrap(blogPostCubit, feedCubit, commentsCubit, const BlogPostScreen(id: 1)));
|
||||
await tester.pump();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(BlogPostScreen), findsOneWidget);
|
||||
expect(find.text('Test Post Title'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -67,5 +67,39 @@ void main() {
|
||||
|
||||
expect(find.byType(CreateDietScreenContent), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('renders create diet screen with loading state', (tester) async {
|
||||
final cubit = MockCreateDietCubit();
|
||||
final state = CreateDietState.initial().copyWith(isLoading: true);
|
||||
|
||||
when(cubit.state).thenReturn(state);
|
||||
when(cubit.stream).thenAnswer((_) => Stream.value(state));
|
||||
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(cubit.setInitialData(any)).thenAnswer((_) async => Future<void>.value());
|
||||
|
||||
sl.registerLazySingleton<CreateDietCubit>(() => cubit);
|
||||
|
||||
await tester.pumpWidget(wrap(const CreateDietScreenContent(), cubit));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.byType(CreateDietScreenContent), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('renders create diet screen with diet publishing state', (tester) async {
|
||||
final cubit = MockCreateDietCubit();
|
||||
final state = CreateDietState.initial().copyWith(isDietPublishing: true);
|
||||
|
||||
when(cubit.state).thenReturn(state);
|
||||
when(cubit.stream).thenAnswer((_) => Stream.value(state));
|
||||
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(cubit.setInitialData(any)).thenAnswer((_) async => Future<void>.value());
|
||||
|
||||
sl.registerLazySingleton<CreateDietCubit>(() => cubit);
|
||||
|
||||
await tester.pumpWidget(wrap(const CreateDietScreenContent(), cubit));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.byType(CreateDietScreenContent), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:eatvio_app/features/create_diet/repositories/create_diet_reposit
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/glossary/models/glossary_model.dart';
|
||||
import 'package:eatvio_app/features/glossary/repositories/glossary_repository.dart';
|
||||
import 'package:eatvio_app/features/profile/models/profile_model.dart';
|
||||
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
@@ -236,6 +237,51 @@ void main() {
|
||||
expect(result, null);
|
||||
});
|
||||
|
||||
test('getDietForPreview returns Diet when form is valid', () async {
|
||||
const profile = ProfileModel(
|
||||
id: 1,
|
||||
name: 'Test User',
|
||||
email: 'test@example.com',
|
||||
phoneConfirmationState: 'confirmed',
|
||||
slug: 'test-user',
|
||||
isPublicProfile: true,
|
||||
isBlockedByMe: false,
|
||||
isBlockedByAuthor: false,
|
||||
isBlockedByUser: false,
|
||||
isEntitySubscribedByMe: false,
|
||||
profilePhotoUrl: 'https://example.com/photo.jpg',
|
||||
roles: [],
|
||||
badges: [],
|
||||
subscriptions: [],
|
||||
subscribers: [],
|
||||
);
|
||||
when(profileRepository.getMyProfile()).thenAnswer((_) async => profile);
|
||||
|
||||
cubit.setName('Test Diet');
|
||||
cubit.setDetails('Test Description');
|
||||
cubit.setDuration(0);
|
||||
cubit.setDietState(DietState.active);
|
||||
const portion = DailySchedule(
|
||||
name: 'Apple',
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
cubit.setImage(null, imageUrl: 'https://example.com/cover.jpg');
|
||||
|
||||
final result = await cubit.getDietForPreview();
|
||||
|
||||
expect(result, isNotNull);
|
||||
expect(result!.name, 'Test Diet');
|
||||
expect(result.details, 'Test Description');
|
||||
expect(result.duration, 7);
|
||||
expect(result.author.authorName, 'Test User');
|
||||
expect(result.author.authorSlug, 'test-user');
|
||||
verify(profileRepository.getMyProfile()).called(1);
|
||||
});
|
||||
|
||||
test('createDiet returns validation error when form is invalid', () async {
|
||||
final result = await cubit.createDiet();
|
||||
expect(result, 'validation');
|
||||
@@ -257,7 +303,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
// Set image with non-existent file to trigger error in compression
|
||||
@@ -280,7 +326,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
final image = XFile('test_image.jpg');
|
||||
@@ -302,7 +348,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
final image = XFile('test_image.jpg');
|
||||
@@ -321,7 +367,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
@@ -336,7 +382,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
@@ -405,7 +451,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
final image = XFile('test_image.jpg');
|
||||
@@ -427,7 +473,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
final image = XFile('test_image.jpg');
|
||||
@@ -448,7 +494,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
final image = XFile('test_image.jpg');
|
||||
@@ -469,7 +515,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
final image = XFile('test_image.jpg');
|
||||
@@ -491,7 +537,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
|
||||
@@ -522,7 +568,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
@@ -546,7 +592,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
);
|
||||
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
|
||||
@@ -63,7 +63,7 @@ void main() {
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
await tester.pumpWidget(wrap(DietPreviewScreen(diet: testDiet)));
|
||||
await tester.pumpWidget(wrap(DietPreviewScreen(diet: testDiet, isImageInBase64: true)));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(DietPreviewScreen), findsOneWidget);
|
||||
@@ -103,7 +103,7 @@ void main() {
|
||||
dailyNutrition: [const DailyNutrition(energy: 2000.0, proteins: 100.0, fats: 50.0, carbohydrates: 200.0)],
|
||||
);
|
||||
|
||||
await tester.pumpWidget(wrap(DietPreviewScreen(diet: testDiet)));
|
||||
await tester.pumpWidget(wrap(DietPreviewScreen(diet: testDiet, isImageInBase64: true)));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(DietPreviewScreen), findsOneWidget);
|
||||
@@ -131,7 +131,7 @@ void main() {
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
await tester.pumpWidget(wrap(DietPreviewScreen(diet: testDiet)));
|
||||
await tester.pumpWidget(wrap(DietPreviewScreen(diet: testDiet, isImageInBase64: true)));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Test Diet Name'), findsOneWidget);
|
||||
|
||||
@@ -196,7 +196,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: null,
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
),
|
||||
],
|
||||
],
|
||||
@@ -236,7 +236,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: null,
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
),
|
||||
],
|
||||
],
|
||||
@@ -299,7 +299,7 @@ void main() {
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: null,
|
||||
productNutritionalValueId: '1',
|
||||
productNutritionalValueId: 1,
|
||||
),
|
||||
],
|
||||
],
|
||||
|
||||
@@ -103,7 +103,7 @@ void main() {
|
||||
dietData.first['name'] = 'Another Diet';
|
||||
dietData.first['image'] = 'https://someurl.com/img2.jpg';
|
||||
dietData.first['duration'] = duration;
|
||||
dietData.first['state'] = 'draft';
|
||||
dietData.first['state'] = 'drafted';
|
||||
dietData.first['type'] = {'light': false, 'veg': true, 'carb': false, 'meat': false};
|
||||
dietData.first['description_md'] = '';
|
||||
dietData.first['author'] = {'author_slug': 'author-slug', 'author_name': 'Author Name', 'author_profile_icon': '', 'author_is_me': true, 'is_entity_subscribed_by_me': null, 'author_place_in_rating': null};
|
||||
|
||||
@@ -182,5 +182,34 @@ void main() {
|
||||
|
||||
expect(find.byType(AboutPlanScreen), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('renders with navigateToComments true', (tester) async {
|
||||
final planCubit = MockAboutPlanCubit();
|
||||
final feedCubit = MockFeedCubit();
|
||||
final commentsCubit = MockCommentsCubit();
|
||||
const state = AboutPlanState(isLoading: false, planModel: testPlan, chosenOptionIndex: 0, error: null);
|
||||
|
||||
when(planCubit.state).thenReturn(state);
|
||||
when(planCubit.stream).thenAnswer((_) => const Stream<AboutPlanState>.empty());
|
||||
when(planCubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(feedCubit.viewPlan(any)).thenAnswer((_) async => Future<void>.value());
|
||||
when(feedCubit.stream).thenAnswer((_) => const Stream.empty());
|
||||
when(feedCubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(commentsCubit.state).thenReturn(const CommentsState());
|
||||
when(commentsCubit.stream).thenAnswer((_) => const Stream<CommentsState>.empty());
|
||||
when(commentsCubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(commentsCubit.getComments(any, any, any)).thenAnswer((_) async => Future<void>.value());
|
||||
|
||||
registerMocks(planCubit, commentsCubit);
|
||||
|
||||
await mockNetworkImagesFor(() async {
|
||||
await tester.pumpWidget(wrap(planCubit, feedCubit, commentsCubit, const AboutPlanScreen(slug: 'test', navigateToComments: true, index: 1)));
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
});
|
||||
|
||||
expect(find.byType(AboutPlanScreen), findsOneWidget);
|
||||
expect(find.text('Test Plan'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -193,5 +193,36 @@ void main() {
|
||||
|
||||
expect(find.byType(AboutRecipeScreen), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('renders with navigateToComments true', (tester) async {
|
||||
final recipeCubit = MockAboutRecipeCubit();
|
||||
final feedCubit = MockFeedCubit();
|
||||
final commentsCubit = MockCommentsCubit();
|
||||
final state = AboutRecipeState.loaded(recipe: testRecipe);
|
||||
|
||||
when(recipeCubit.state).thenReturn(state);
|
||||
when(recipeCubit.stream).thenAnswer((_) => const Stream<AboutRecipeState>.empty());
|
||||
when(recipeCubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(feedCubit.viewRecipe(any)).thenAnswer((_) async => Future<void>.value());
|
||||
when(feedCubit.stream).thenAnswer((_) => const Stream.empty());
|
||||
when(feedCubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(commentsCubit.state).thenReturn(const CommentsState());
|
||||
when(commentsCubit.stream).thenAnswer((_) => const Stream<CommentsState>.empty());
|
||||
when(commentsCubit.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(commentsCubit.getComments(any, any, any)).thenAnswer((_) async => Future<void>.value());
|
||||
when(commentsCubit.setReplyId(any)).thenReturn(null);
|
||||
|
||||
registerMocks(recipeCubit, commentsCubit);
|
||||
|
||||
await mockNetworkImagesFor(() async {
|
||||
await tester.pumpWidget(
|
||||
wrap(recipeCubit, feedCubit, commentsCubit, const AboutRecipeScreen(recipeSlug: 'test', index: 2, navigateToComments: true)),
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 600));
|
||||
});
|
||||
|
||||
expect(find.byType(AboutRecipeScreen), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user