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,194 @@
|
||||
import 'package:easy_debounce/easy_debounce.dart';
|
||||
import 'package:eatvio_app/core/models/pagination_model.dart';
|
||||
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/feed/presentation/bloc/plans_cubit/plans_cubit.dart';
|
||||
import 'package:eatvio_app/features/feed/repositories/feed_repository.dart';
|
||||
import 'package:eatvio_app/features/plans/repositories/plan_repository.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
import 'plans_cubit_slug_sync_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<FeedRepository>(), MockSpec<PlanRepository>()])
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('feed PlansCubit — slug-based stream sync', () {
|
||||
PlanModel plan({required String slug, int totalLikes = 10}) {
|
||||
return PlanModel(
|
||||
id: slug.hashCode,
|
||||
slug: slug,
|
||||
name: slug,
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: 'active',
|
||||
type: const PlanType(
|
||||
fit: true,
|
||||
slim: false,
|
||||
behavior: false,
|
||||
wellness: false,
|
||||
),
|
||||
details: 'details',
|
||||
packetsCount: 1,
|
||||
minPriceOfOptions: 1000.0,
|
||||
maxPriceOfOptions: 2000.0,
|
||||
author: const Author(
|
||||
authorSlug: 'author',
|
||||
authorName: 'Author Name',
|
||||
authorProfileIcon: 'https://example.com/icon.jpg',
|
||||
authorIsMe: false,
|
||||
isEntitySubscribedByMe: null,
|
||||
authorPlaceInRating: null,
|
||||
),
|
||||
totalLikes: totalLikes,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
);
|
||||
}
|
||||
|
||||
PlanPaginationModel pagination(List<PlanModel> plans) {
|
||||
return PlanPaginationModel(
|
||||
data: plans,
|
||||
links: const Links(
|
||||
first: 'https://example.com?page=1',
|
||||
last: 'https://example.com?page=1',
|
||||
prev: null,
|
||||
next: null,
|
||||
),
|
||||
meta: Meta(
|
||||
currentPage: 1,
|
||||
from: 1,
|
||||
lastPage: 1,
|
||||
links: const [],
|
||||
perPage: 10,
|
||||
to: plans.length,
|
||||
total: plans.length,
|
||||
totalMinPrice: 1000,
|
||||
totalMaxPrice: 2000,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
late MockFeedRepository feedRepository;
|
||||
late MockPlanRepository planRepository;
|
||||
late BehaviorSubject<LikesState> likeState;
|
||||
late BehaviorSubject<FavoriteState> favoriteState;
|
||||
late BehaviorSubject<CommentsState> commentsState;
|
||||
late PlansCubit cubit;
|
||||
|
||||
setUp(() async {
|
||||
feedRepository = MockFeedRepository();
|
||||
planRepository = MockPlanRepository();
|
||||
likeState = BehaviorSubject<LikesState>();
|
||||
favoriteState = BehaviorSubject<FavoriteState>();
|
||||
commentsState = BehaviorSubject<CommentsState>();
|
||||
|
||||
when(planRepository.planLikeState).thenAnswer((_) => likeState);
|
||||
when(planRepository.planFavoriteState).thenAnswer((_) => favoriteState);
|
||||
when(planRepository.planCommentsState).thenAnswer((_) => commentsState);
|
||||
|
||||
when(
|
||||
feedRepository.getPlans(
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
minPrice: anyNamed('minPrice'),
|
||||
maxPrice: anyNamed('maxPrice'),
|
||||
),
|
||||
).thenAnswer(
|
||||
(_) async => pagination([
|
||||
plan(slug: 'plan-a'),
|
||||
plan(slug: 'plan-b'),
|
||||
]),
|
||||
);
|
||||
|
||||
cubit = PlansCubit(
|
||||
feedRepository: feedRepository,
|
||||
planRepository: planRepository,
|
||||
);
|
||||
await cubit.loadPlans();
|
||||
expect(cubit.state.plans.length, 2);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
EasyDebounce.cancel('loadPlans');
|
||||
EasyDebounce.cancel('like_plan');
|
||||
EasyDebounce.cancel('add_or_remove_plan_from_favorite');
|
||||
if (!cubit.isClosed) {
|
||||
await cubit.close();
|
||||
}
|
||||
await likeState.close();
|
||||
await favoriteState.close();
|
||||
await commentsState.close();
|
||||
});
|
||||
|
||||
test('a like event with a slug updates the matching plan, not the index', () async {
|
||||
likeState.add(
|
||||
const LikesState(
|
||||
slug: 'plan-b',
|
||||
index: 0,
|
||||
totalLikes: 42,
|
||||
wasLikedByMe: true,
|
||||
),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(cubit.state.plans[0].wasLikedByMe, isFalse);
|
||||
expect(cubit.state.plans[0].totalLikes, 10);
|
||||
expect(cubit.state.plans[1].wasLikedByMe, isTrue);
|
||||
expect(cubit.state.plans[1].totalLikes, 42);
|
||||
});
|
||||
|
||||
test('a like event with an unknown slug is a no-op', () async {
|
||||
final before = cubit.state.plans;
|
||||
|
||||
likeState.add(
|
||||
const LikesState(
|
||||
slug: 'unknown-plan',
|
||||
index: 0,
|
||||
totalLikes: 42,
|
||||
wasLikedByMe: true,
|
||||
),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(cubit.state.plans, before);
|
||||
});
|
||||
|
||||
test('a legacy like event without a slug keeps index behaviour', () async {
|
||||
likeState.add(
|
||||
const LikesState(index: 1, totalLikes: 42, wasLikedByMe: true),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(cubit.state.plans[1].wasLikedByMe, isTrue);
|
||||
expect(cubit.state.plans[1].totalLikes, 42);
|
||||
});
|
||||
|
||||
test('a favorite event with a slug updates the matching plan', () async {
|
||||
favoriteState.add(
|
||||
const FavoriteState(slug: 'plan-b', index: 0, isFavorite: true),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(cubit.state.plans[0].isFavorite, isFalse);
|
||||
expect(cubit.state.plans[1].isFavorite, isTrue);
|
||||
});
|
||||
|
||||
test('a comments event with a slug updates the matching plan', () async {
|
||||
commentsState.add(
|
||||
const CommentsState(slug: 'plan-b', index: 0, totalComments: 7),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(cubit.state.plans[0].totalComments, isNot(7));
|
||||
expect(cubit.state.plans[1].totalComments, 7);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:easy_debounce/easy_debounce.dart';
|
||||
import 'package:eatvio_app/core/models/pagination_model.dart';
|
||||
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/feed/presentation/bloc/plans_cubit/plans_cubit.dart';
|
||||
import 'package:eatvio_app/features/feed/repositories/feed_repository.dart';
|
||||
import 'package:eatvio_app/features/plans/repositories/plan_repository.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
import 'plans_cubit_sync_guard_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<FeedRepository>(), MockSpec<PlanRepository>()])
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('feed PlansCubit — index bounds guard', () {
|
||||
const testPlan = PlanModel(
|
||||
id: 1,
|
||||
slug: 'test-plan',
|
||||
name: 'Test Plan',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: 'active',
|
||||
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
|
||||
details: 'Test details',
|
||||
packetsCount: 3,
|
||||
minPriceOfOptions: 1000.0,
|
||||
maxPriceOfOptions: 2000.0,
|
||||
author: Author(
|
||||
authorSlug: 'author',
|
||||
authorName: 'Author Name',
|
||||
authorProfileIcon: 'https://example.com/icon.jpg',
|
||||
authorIsMe: false,
|
||||
isEntitySubscribedByMe: null,
|
||||
authorPlaceInRating: null,
|
||||
),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
);
|
||||
|
||||
const plansPagination = PlanPaginationModel(
|
||||
data: [testPlan],
|
||||
links: Links(
|
||||
first: 'https://example.com?page=1',
|
||||
last: 'https://example.com?page=1',
|
||||
prev: null,
|
||||
next: null,
|
||||
),
|
||||
meta: Meta(
|
||||
currentPage: 1,
|
||||
from: 1,
|
||||
lastPage: 1,
|
||||
links: [],
|
||||
perPage: 10,
|
||||
to: 1,
|
||||
total: 1,
|
||||
totalMinPrice: 1000,
|
||||
totalMaxPrice: 2000,
|
||||
),
|
||||
);
|
||||
|
||||
late MockFeedRepository feedRepository;
|
||||
late MockPlanRepository planRepository;
|
||||
late BehaviorSubject<LikesState> likeState;
|
||||
late BehaviorSubject<FavoriteState> favoriteState;
|
||||
late BehaviorSubject<CommentsState> commentsState;
|
||||
|
||||
setUp(() {
|
||||
feedRepository = MockFeedRepository();
|
||||
planRepository = MockPlanRepository();
|
||||
likeState = BehaviorSubject<LikesState>();
|
||||
favoriteState = BehaviorSubject<FavoriteState>();
|
||||
commentsState = BehaviorSubject<CommentsState>();
|
||||
|
||||
when(planRepository.planLikeState).thenAnswer((_) => likeState);
|
||||
when(planRepository.planFavoriteState).thenAnswer((_) => favoriteState);
|
||||
when(planRepository.planCommentsState).thenAnswer((_) => commentsState);
|
||||
|
||||
when(
|
||||
feedRepository.getPlans(
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
minPrice: anyNamed('minPrice'),
|
||||
maxPrice: anyNamed('maxPrice'),
|
||||
),
|
||||
).thenAnswer((_) async => plansPagination);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
EasyDebounce.cancel('loadPlans');
|
||||
EasyDebounce.cancel('like_plan');
|
||||
EasyDebounce.cancel('add_or_remove_plan_from_favorite');
|
||||
await likeState.close();
|
||||
await favoriteState.close();
|
||||
await commentsState.close();
|
||||
});
|
||||
|
||||
/// Runs [body] in its own error zone so an unhandled stream-listener error
|
||||
/// is captured instead of failing the test at teardown.
|
||||
Future<List<Object>> errorsFrom(Future<void> Function() body) async {
|
||||
final errors = <Object>[];
|
||||
final done = Completer<void>();
|
||||
runZonedGuarded(
|
||||
() async {
|
||||
try {
|
||||
await body();
|
||||
} finally {
|
||||
done.complete();
|
||||
}
|
||||
},
|
||||
(error, stackTrace) => errors.add(error),
|
||||
);
|
||||
await done.future;
|
||||
return errors;
|
||||
}
|
||||
|
||||
Future<PlansCubit> loadedCubit() async {
|
||||
final cubit = PlansCubit(
|
||||
feedRepository: feedRepository,
|
||||
planRepository: planRepository,
|
||||
);
|
||||
await cubit.loadPlans();
|
||||
expect(cubit.state.plans.length, 1);
|
||||
return cubit;
|
||||
}
|
||||
|
||||
test('an out-of-range like event is ignored instead of throwing', () async {
|
||||
final errors = await errorsFrom(() async {
|
||||
final cubit = await loadedCubit();
|
||||
likeState.add(
|
||||
const LikesState(index: 5, totalLikes: 99, wasLikedByMe: true),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
await cubit.close();
|
||||
});
|
||||
|
||||
expect(errors, isEmpty);
|
||||
});
|
||||
|
||||
test('an out-of-range favorite event is ignored instead of throwing', () async {
|
||||
final errors = await errorsFrom(() async {
|
||||
final cubit = await loadedCubit();
|
||||
favoriteState.add(const FavoriteState(index: 5, isFavorite: true));
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
await cubit.close();
|
||||
});
|
||||
|
||||
expect(errors, isEmpty);
|
||||
});
|
||||
|
||||
test('an out-of-range comments event is ignored instead of throwing', () async {
|
||||
final errors = await errorsFrom(() async {
|
||||
final cubit = await loadedCubit();
|
||||
commentsState.add(const CommentsState(index: 5, totalComments: 99));
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
await cubit.close();
|
||||
});
|
||||
|
||||
expect(errors, isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user