forked from eatvio/eatvio_app
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:
@@ -0,0 +1,256 @@
|
||||
import 'dart:async';
|
||||
|
||||
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/repositories/consultant_repository.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/features/profile/models/profile_model.dart';
|
||||
import 'package:eatvio_app/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart';
|
||||
import 'package:eatvio_app/features/profile/presentation/bloc/profile_cubit/profile_state.dart';
|
||||
import 'package:eatvio_app/features/profile/presentation/widgets/profile_blog_post_widget.dart';
|
||||
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
||||
import 'package:eatvio_app/generated/l10n.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.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/feed_fixtures.dart';
|
||||
import 'profile_blog_post_widget_like_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([
|
||||
MockSpec<ProfileCubit>(),
|
||||
MockSpec<FeedCubit>(),
|
||||
MockSpec<ProfileRepository>(),
|
||||
MockSpec<ChatRepository>(),
|
||||
MockSpec<ConsultantRepository>(),
|
||||
])
|
||||
void main() {
|
||||
group('ProfileBlogPostWidget — like', () {
|
||||
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'),
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await sl.reset();
|
||||
});
|
||||
|
||||
const profile = ProfileModel(
|
||||
id: 1,
|
||||
name: 'User',
|
||||
email: 'u@e.com',
|
||||
phoneConfirmationState: 'confirmed',
|
||||
slug: 'user',
|
||||
isPublicProfile: true,
|
||||
isBlockedByMe: false,
|
||||
isBlockedByAuthor: false,
|
||||
isBlockedByUser: false,
|
||||
isEntitySubscribedByMe: false,
|
||||
profilePhotoUrl: '',
|
||||
roles: [],
|
||||
badges: [],
|
||||
subscriptions: [],
|
||||
subscribers: [],
|
||||
);
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
ProfileState loadedState() {
|
||||
return ProfileState.loaded(
|
||||
profile: profile,
|
||||
profileDetails: details,
|
||||
blogPosts: [
|
||||
FeedFixtures.feedBlogPostModel().copyWith(
|
||||
wasLikedByMe: false,
|
||||
totalLikes: 10,
|
||||
),
|
||||
],
|
||||
isMyProfile: true,
|
||||
certPhotos: const [],
|
||||
);
|
||||
}
|
||||
|
||||
Widget wrap({
|
||||
required ProfileCubit profileCubit,
|
||||
required FeedCubit feedCubit,
|
||||
}) {
|
||||
return Portal(
|
||||
child: MaterialApp(
|
||||
theme: ThemeHelper().themeData(),
|
||||
localizationsDelegates: const [
|
||||
S.delegate,
|
||||
...GlobalMaterialLocalizations.delegates,
|
||||
],
|
||||
supportedLocales: const [Locale('ru')],
|
||||
home: Scaffold(
|
||||
body: MediaQuery(
|
||||
data: const MediaQueryData(size: Size(800, 1600)),
|
||||
child: SingleChildScrollView(
|
||||
child: BlocProvider<FeedCubit>.value(
|
||||
value: feedCubit,
|
||||
child: ProfileBlogPostWidget(
|
||||
index: 0,
|
||||
profileCubit: profileCubit,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets(
|
||||
'passes the new like value to updateFeedBlogPostList before the request completes',
|
||||
(tester) async {
|
||||
final profileCubit = _RecordingProfileCubit(
|
||||
profileRepository: MockProfileRepository(),
|
||||
chatRepository: MockChatRepository(),
|
||||
consultantRepository: MockConsultantRepository(),
|
||||
);
|
||||
addTearDown(profileCubit.close);
|
||||
profileCubit.emit(loadedState());
|
||||
final expectedPost =
|
||||
(profileCubit.state as ProfileStateLoaded).blogPosts.single;
|
||||
|
||||
final feedCubit = MockFeedCubit();
|
||||
final likeCompleter = Completer<void>();
|
||||
when(feedCubit.likePost(any)).thenAnswer((_) => likeCompleter.future);
|
||||
|
||||
await mockNetworkImagesFor(() async {
|
||||
await tester.pumpWidget(
|
||||
wrap(profileCubit: profileCubit, feedCubit: feedCubit),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final likeButton = find.byIcon(Icons.favorite_border_rounded);
|
||||
await tester.ensureVisible(likeButton);
|
||||
await tester.pump();
|
||||
await tester.tap(likeButton);
|
||||
// A frame while the HTTP request is still in flight: the rebuild resets
|
||||
// the mutable isLiked field to post.wasLikedByMe (false).
|
||||
await tester.pump();
|
||||
});
|
||||
|
||||
// The widget must propagate the NEW value synchronously, before the
|
||||
// request completes. A broken implementation that reads the mutable
|
||||
// isLiked field after the await has not called this at all yet.
|
||||
expect(profileCubit.likeUpdates, hasLength(1));
|
||||
expect(profileCubit.likeUpdates.single.post.id, expectedPost.id);
|
||||
expect(profileCubit.likeUpdates.single.wasLikedByMe, isTrue);
|
||||
|
||||
likeCompleter.complete();
|
||||
await tester.pump();
|
||||
|
||||
// Issue #582, P1: exactly once, with the NEW value.
|
||||
expect(profileCubit.likeUpdates, hasLength(1));
|
||||
expect(profileCubit.likeUpdates.single.wasLikedByMe, isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'liking a post stores the new like value and keeps the heart and counter after rebuild',
|
||||
(tester) async {
|
||||
final feedCubit = MockFeedCubit();
|
||||
final likeCompleter = Completer<void>();
|
||||
when(feedCubit.likePost(any)).thenAnswer((_) => likeCompleter.future);
|
||||
|
||||
final profileCubit = ProfileCubit(
|
||||
profileRepository: MockProfileRepository(),
|
||||
chatRepository: MockChatRepository(),
|
||||
consultantRepository: MockConsultantRepository(),
|
||||
);
|
||||
addTearDown(profileCubit.close);
|
||||
profileCubit.emit(loadedState());
|
||||
|
||||
await mockNetworkImagesFor(() async {
|
||||
await tester.pumpWidget(
|
||||
wrap(profileCubit: profileCubit, feedCubit: feedCubit),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final likeButton = find.byIcon(Icons.favorite_border_rounded);
|
||||
await tester.ensureVisible(likeButton);
|
||||
await tester.pump();
|
||||
await tester.tap(likeButton);
|
||||
// A frame while the HTTP request is still in flight: the build resets
|
||||
// isLiked/totalLikes from the post unless the new value was stored.
|
||||
await tester.pump();
|
||||
});
|
||||
|
||||
expect(find.byIcon(Icons.favorite_rounded), findsOneWidget);
|
||||
expect(find.text('11'), findsOneWidget);
|
||||
|
||||
likeCompleter.complete();
|
||||
await tester.pump();
|
||||
|
||||
final state = profileCubit.state as ProfileStateLoaded;
|
||||
expect(state.blogPosts.single.wasLikedByMe, isTrue);
|
||||
expect(state.blogPosts.single.totalLikes, 11);
|
||||
expect(find.byIcon(Icons.favorite_rounded), findsOneWidget);
|
||||
expect(find.text('11'), findsOneWidget);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
class _RecordingProfileCubit extends ProfileCubit {
|
||||
_RecordingProfileCubit({
|
||||
required super.profileRepository,
|
||||
required super.chatRepository,
|
||||
required super.consultantRepository,
|
||||
});
|
||||
|
||||
final List<({FeedBlogPostModel post, bool wasLikedByMe})> likeUpdates = [];
|
||||
|
||||
@override
|
||||
void updateFeedBlogPostList(FeedBlogPostModel post, bool wasLikedByMe) {
|
||||
likeUpdates.add((post: post, wasLikedByMe: wasLikedByMe));
|
||||
super.updateFeedBlogPostList(post, wasLikedByMe);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user