fix: plans - like and bookmark in consultant profile (#582)

- decouple PlanWidget from feed PlansCubit via owner-provided callbacks
- add like/favorite actions to profile PlansCubit: slug lookup, optimistic
  update, rollback of the affected plan, per-slug debounce, isClosed guards
- pass slug through like/favorite/comments events; guard out-of-range index
  in feed plans handlers and deep-link about_plan sync
- capture post like intent before await in profile blog post widget
- replace null force-unwraps in profile screen, guard indexOf == -1
- tests: profile plan like/bookmark, slug sync, bounds guards, #700 regression
This commit is contained in:
2026-11-20 16:30:13 +03:00
parent 1e8c1ab2eb
commit 0df474b2b5
22 changed files with 2139 additions and 137 deletions

View File

@@ -0,0 +1,211 @@
import 'package:eatvio_app/core/app_config.dart';
import 'package:eatvio_app/core/app_initializer.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/chat/repositories/chat_repository.dart';
import 'package:eatvio_app/features/consultant/become_consultant/models/consultation_request_model.dart';
import 'package:eatvio_app/features/consultant/become_consultant/repositories/consultant_repository.dart';
import 'package:eatvio_app/features/insights/repositories/insights_repository.dart';
import 'package:eatvio_app/features/profile/models/profile_model.dart';
import 'package:eatvio_app/features/profile/presentation/bloc/plans_cubit/plans_cubit.dart'
as profile_plans;
import 'package:eatvio_app/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart';
import 'package:eatvio_app/features/profile/presentation/pages/profile_screen.dart';
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
import 'package:eatvio_app/features/plans/repositories/plan_repository.dart';
import 'package:eatvio_app/features/plans/repositories/plans_repository.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:material_ui/material_ui.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:network_image_mock/network_image_mock.dart';
import '../../../../fixtures/doubles/insights_doubles.dart';
import 'profile_screen_consultant_null_links_test.mocks.dart';
@GenerateNiceMocks([
MockSpec<ProfileRepository>(),
MockSpec<ChatRepository>(),
MockSpec<ConsultantRepository>(),
MockSpec<PlansRepository>(),
MockSpec<PlanRepository>(),
])
void main() {
group('ProfileScreen — consultant without social links', () {
late MockProfileRepository profileRepository;
late MockChatRepository chatRepository;
late MockConsultantRepository consultantRepository;
late MockPlansRepository plansRepository;
late MockPlanRepository planRepository;
setUpAll(() async {
dotenv.loadFromString(
envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost',
);
});
setUp(() async {
await sl.reset();
sl.registerLazySingleton<AppConfig>(
() =>
AppConfig(buildType: AppBuildType.tests)
..setBaseUrl('http://localhost'),
);
profileRepository = MockProfileRepository();
chatRepository = MockChatRepository();
consultantRepository = MockConsultantRepository();
plansRepository = MockPlansRepository();
planRepository = MockPlanRepository();
when(
planRepository.planLikeState,
).thenAnswer((_) => const Stream.empty());
when(
planRepository.planFavoriteState,
).thenAnswer((_) => const Stream.empty());
when(
planRepository.planCommentsState,
).thenAnswer((_) => const Stream.empty());
sl.registerLazySingleton<ProfileRepository>(() => profileRepository);
sl.registerLazySingleton<InsightsRepository>(
() => FakeInsightsRepository(),
);
sl.registerLazySingleton<ChatRepository>(() => chatRepository);
sl.registerLazySingleton<ConsultantRepository>(
() => consultantRepository,
);
sl.registerLazySingleton<PlansRepository>(() => plansRepository);
sl.registerLazySingleton<PlanRepository>(() => planRepository);
sl.registerFactory<ProfileCubit>(
() => ProfileCubit(
profileRepository: sl(),
chatRepository: sl(),
consultantRepository: sl(),
),
);
sl.registerFactory<profile_plans.PlansCubit>(
() => profile_plans.PlansCubit(
plansRepository: sl(),
planRepository: sl(),
),
);
});
tearDown(() async {
await sl.reset();
});
Widget wrap(Widget child) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
...GlobalMaterialLocalizations.delegates,
],
supportedLocales: const [Locale('ru')],
home: Scaffold(body: child),
);
}
ProfileModel profileOf(String slug, {List<String> roles = const []}) {
return ProfileModel(
id: 1,
name: 'Test User',
email: 'test@example.com',
phoneConfirmationState: 'confirmed',
slug: slug,
isPublicProfile: true,
isBlockedByMe: false,
isBlockedByAuthor: false,
isBlockedByUser: false,
isEntitySubscribedByMe: false,
profilePhotoUrl: '',
roles: roles,
badges: const [],
subscriptions: const [],
subscribers: const [],
);
}
const details = ProfileDetailsModel(
firstName: 'Test',
lastName: 'User',
birthdate: '1990-01-01',
gender: 'male',
country: 'Russia',
city: 'Moscow',
lang: 'ru',
weight: 70.0,
height: 175,
chestCircumference: 100,
waistCircumference: 80,
hipCircumference: 90,
bloodPressureUp: 120,
bloodPressureDown: 80,
hasHypo: false,
hasHyper: false,
aboutAlergy: '',
aboutTakingPils: '',
aboutSportHobbies: '',
favoriteFoods: '',
unfavoriteFoods: '',
additionalInfo: '',
isMoodFillable: true,
isBedtimeFillable: true,
activityCoefficient: 1.5,
);
const consultantInfo = ConsultationRequestModel(
ratingPlace: 5,
educationModel: EducationModel(
hasMedicalEducation: 0,
completedCourses: null,
vkLink: null,
instaLink: null,
fbLink: null,
tgLink: null,
personalSiteLink: null,
requestState: ModerationStatus.approved,
),
photos: [],
);
testWidgets(
'a consultant with null social links renders without an ErrorWidget',
(tester) async {
when(profileRepository.getProfileBySlug(any)).thenAnswer(
(_) async => profileOf('other-consultant', roles: ['consultant']),
);
when(
profileRepository.getMyProfile(),
).thenAnswer((_) async => profileOf('me'));
when(
profileRepository.getProfileDetailsBySlug(any),
).thenAnswer((_) async => details);
when(
profileRepository.getBlogPostsByAuthorSlug(slug: anyNamed('slug')),
).thenAnswer((_) async => []);
when(
consultantRepository.getConsultantRequestStatus(any),
).thenAnswer((_) async => consultantInfo);
when(plansRepository.getPlansBySlug(any)).thenAnswer((_) async => []);
await mockNetworkImagesFor(() async {
await tester.pumpWidget(
wrap(const ProfileScreen(slug: 'other-consultant')),
);
await tester.pumpAndSettle();
});
expect(tester.takeException(), isNull);
expect(find.byType(ErrorWidget), findsNothing);
expect(find.byType(ProfileScreen), findsOneWidget);
},
);
});
}

View File

@@ -0,0 +1,272 @@
import 'package:eatvio_app/core/app_config.dart';
import 'package:eatvio_app/core/app_initializer.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/widgets/bookmark_button.dart';
import 'package:eatvio_app/features/chat/repositories/chat_repository.dart';
import 'package:eatvio_app/features/consultant/become_consultant/models/consultation_request_model.dart';
import 'package:eatvio_app/features/consultant/become_consultant/repositories/consultant_repository.dart';
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
import 'package:eatvio_app/features/insights/repositories/insights_repository.dart';
import 'package:eatvio_app/features/profile/models/profile_model.dart';
import 'package:eatvio_app/features/profile/presentation/bloc/plans_cubit/plans_cubit.dart'
as profile_plans;
import 'package:eatvio_app/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart';
import 'package:eatvio_app/features/profile/presentation/pages/profile_screen.dart';
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
import 'package:eatvio_app/features/plans/repositories/plan_repository.dart';
import 'package:eatvio_app/features/plans/repositories/plans_repository.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:material_ui/material_ui.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_portal/flutter_portal.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:network_image_mock/network_image_mock.dart';
import '../../../../fixtures/doubles/insights_doubles.dart';
import '../../../../fixtures/fixture_reader.dart';
import 'profile_screen_plan_like_test.mocks.dart';
@GenerateNiceMocks([
MockSpec<ProfileRepository>(),
MockSpec<ChatRepository>(),
MockSpec<ConsultantRepository>(),
MockSpec<PlansRepository>(),
MockSpec<PlanRepository>(),
])
void main() {
group('ProfileScreen — план консультанта', () {
late MockProfileRepository profileRepository;
late MockChatRepository chatRepository;
late MockConsultantRepository consultantRepository;
late MockPlansRepository plansRepository;
late MockPlanRepository planRepository;
setUpAll(() async {
dotenv.loadFromString(
envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost',
);
});
setUp(() async {
await sl.reset();
sl.registerLazySingleton<AppConfig>(
() =>
AppConfig(buildType: AppBuildType.tests)
..setBaseUrl('http://localhost'),
);
profileRepository = MockProfileRepository();
chatRepository = MockChatRepository();
consultantRepository = MockConsultantRepository();
plansRepository = MockPlansRepository();
planRepository = MockPlanRepository();
when(
planRepository.planLikeState,
).thenAnswer((_) => const Stream.empty());
when(
planRepository.planFavoriteState,
).thenAnswer((_) => const Stream.empty());
when(
planRepository.planCommentsState,
).thenAnswer((_) => const Stream.empty());
when(planRepository.likePlan(any)).thenAnswer((_) async {});
when(planRepository.addToFavorite(any)).thenAnswer((_) async {});
when(planRepository.removeFromFavorite(any)).thenAnswer((_) async {});
sl.registerLazySingleton<ProfileRepository>(() => profileRepository);
sl.registerLazySingleton<InsightsRepository>(
() => FakeInsightsRepository(),
);
sl.registerLazySingleton<ChatRepository>(() => chatRepository);
sl.registerLazySingleton<ConsultantRepository>(
() => consultantRepository,
);
sl.registerLazySingleton<PlansRepository>(() => plansRepository);
sl.registerLazySingleton<PlanRepository>(() => planRepository);
sl.registerFactory<ProfileCubit>(
() => ProfileCubit(
profileRepository: sl(),
chatRepository: sl(),
consultantRepository: sl(),
),
);
sl.registerFactory<profile_plans.PlansCubit>(
() => profile_plans.PlansCubit(
plansRepository: sl(),
planRepository: sl(),
),
);
});
tearDown(() async {
await sl.reset();
});
Widget wrap(Widget child) {
return Portal(
child: MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
...GlobalMaterialLocalizations.delegates,
],
supportedLocales: const [Locale('ru')],
home: Scaffold(body: child),
),
);
}
ProfileModel profileOf(String slug, {List<String> roles = const []}) {
return ProfileModel(
id: 1,
name: 'Test User',
email: 'test@example.com',
phoneConfirmationState: 'confirmed',
slug: slug,
isPublicProfile: true,
isBlockedByMe: false,
isBlockedByAuthor: false,
isBlockedByUser: false,
isEntitySubscribedByMe: false,
profilePhotoUrl: '',
roles: roles,
badges: const [],
subscriptions: const [],
subscribers: const [],
);
}
const details = ProfileDetailsModel(
firstName: 'Test',
lastName: 'User',
birthdate: '1990-01-01',
gender: 'male',
country: 'Russia',
city: 'Moscow',
lang: 'ru',
weight: 70.0,
height: 175,
chestCircumference: 100,
waistCircumference: 80,
hipCircumference: 90,
bloodPressureUp: 120,
bloodPressureDown: 80,
hasHypo: false,
hasHyper: false,
aboutAlergy: '',
aboutTakingPils: '',
aboutSportHobbies: '',
favoriteFoods: '',
unfavoriteFoods: '',
additionalInfo: '',
isMoodFillable: true,
isBedtimeFillable: true,
activityCoefficient: 1.5,
);
const consultantInfo = ConsultationRequestModel(
ratingPlace: 5,
educationModel: EducationModel(
hasMedicalEducation: 0,
completedCourses: null,
vkLink: '',
instaLink: '',
fbLink: '',
tgLink: '',
personalSiteLink: '',
requestState: ModerationStatus.approved,
),
photos: [],
);
PlanModel activePlan() {
return PlanModel.fromJson(
FixtureReader.readAsMap('plans/plan_response.json'),
).copyWith(
state: 'active',
wasLikedByMe: false,
isFavorite: false,
totalLikes: 10,
);
}
Future<void> pumpConsultantProfile(WidgetTester tester) async {
when(profileRepository.getProfileBySlug(any)).thenAnswer(
(_) async => profileOf('other-consultant', roles: ['consultant']),
);
when(
profileRepository.getMyProfile(),
).thenAnswer((_) async => profileOf('me'));
when(
profileRepository.getProfileDetailsBySlug(any),
).thenAnswer((_) async => details);
when(
profileRepository.getBlogPostsByAuthorSlug(slug: anyNamed('slug')),
).thenAnswer((_) async => []);
when(
consultantRepository.getConsultantRequestStatus(any),
).thenAnswer((_) async => consultantInfo);
when(
plansRepository.getPlansBySlug(any),
).thenAnswer((_) async => [activePlan()]);
await mockNetworkImagesFor(() async {
await tester.pumpWidget(
wrap(const ProfileScreen(slug: 'other-consultant')),
);
await tester.pumpAndSettle();
// ExpandablePageView measures its height in a post-frame callback,
// so the plan card appears only after an extra frame.
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
});
}
testWidgets(
'tapping like on a consultant plan in someone else profile updates like state',
(tester) async {
await pumpConsultantProfile(tester);
final likeButton = find.byIcon(Icons.favorite_border_rounded);
expect(likeButton, findsOneWidget);
await tester.ensureVisible(likeButton);
await tester.pump();
await tester.tap(likeButton);
await tester.pump();
await tester.pump(const Duration(milliseconds: 300));
expect(tester.takeException(), isNull);
expect(find.byIcon(Icons.favorite_rounded), findsOneWidget);
expect(find.text('11'), findsOneWidget);
},
);
testWidgets(
'tapping bookmark on a consultant plan in someone else profile updates favorite state',
(tester) async {
await pumpConsultantProfile(tester);
final bookmark = find.descendant(
of: find.byType(BookmarkButton),
matching: find.byIcon(Icons.bookmark_border_rounded),
);
expect(bookmark, findsOneWidget);
await tester.ensureVisible(bookmark);
await tester.pump();
await tester.tap(bookmark);
await tester.pump();
await tester.pump(const Duration(milliseconds: 300));
expect(tester.takeException(), isNull);
expect(find.byIcon(Icons.bookmark), findsOneWidget);
},
);
});
}

View File

@@ -12,6 +12,7 @@ import 'package:eatvio_app/features/profile/presentation/bloc/plans_cubit/plans_
import 'package:eatvio_app/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart';
import 'package:eatvio_app/features/profile/presentation/pages/profile_screen.dart';
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
import 'package:eatvio_app/features/plans/repositories/plan_repository.dart';
import 'package:eatvio_app/features/plans/repositories/plans_repository.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:material_ui/material_ui.dart';
@@ -29,6 +30,7 @@ import 'profile_screen_smoke_test.mocks.dart';
MockSpec<ChatRepository>(),
MockSpec<ConsultantRepository>(),
MockSpec<PlansRepository>(),
MockSpec<PlanRepository>(),
])
void main() {
group('ProfileScreen smoke', () {
@@ -36,6 +38,7 @@ void main() {
late MockChatRepository chatRepository;
late MockConsultantRepository consultantRepository;
late MockPlansRepository plansRepository;
late MockPlanRepository planRepository;
setUpAll(() async {
dotenv.loadFromString(
@@ -55,6 +58,11 @@ void main() {
chatRepository = MockChatRepository();
consultantRepository = MockConsultantRepository();
plansRepository = MockPlansRepository();
planRepository = MockPlanRepository();
when(planRepository.planLikeState).thenAnswer((_) => const Stream.empty());
when(planRepository.planFavoriteState).thenAnswer((_) => const Stream.empty());
when(planRepository.planCommentsState).thenAnswer((_) => const Stream.empty());
sl.registerLazySingleton<ProfileRepository>(() => profileRepository);
sl.registerLazySingleton<InsightsRepository>(
@@ -65,6 +73,7 @@ void main() {
() => consultantRepository,
);
sl.registerLazySingleton<PlansRepository>(() => plansRepository);
sl.registerLazySingleton<PlanRepository>(() => planRepository);
sl.registerFactory<ProfileCubit>(
() => ProfileCubit(
@@ -74,7 +83,10 @@ void main() {
),
);
sl.registerFactory<profile_plans.PlansCubit>(
() => profile_plans.PlansCubit(plansRepository: sl()),
() => profile_plans.PlansCubit(
plansRepository: sl(),
planRepository: sl(),
),
);
});