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:
@@ -1,5 +1,10 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:eatvio_app/core/error_reporting/error_reporter.dart';
|
||||
import 'package:eatvio_app/core/locator_service.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/plans/repositories/plan_repository.dart';
|
||||
import 'package:eatvio_app/features/plans/repositories/plans_repository.dart';
|
||||
import 'package:eatvio_app/features/profile/presentation/bloc/plans_cubit/plans_cubit.dart';
|
||||
import 'package:eatvio_app/features/profile/presentation/bloc/plans_cubit/plans_state.dart';
|
||||
@@ -9,14 +14,34 @@ import 'package:mockito/mockito.dart';
|
||||
|
||||
import 'plans_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<PlansRepository>()])
|
||||
class _RecordingErrorReporter implements ErrorReporter {
|
||||
final List<Object> errors = [];
|
||||
|
||||
@override
|
||||
Future<void> report(
|
||||
Object error,
|
||||
StackTrace? stackTrace, {
|
||||
String? origin,
|
||||
Map<String, Object?>? extras,
|
||||
}) async {
|
||||
errors.add(error);
|
||||
}
|
||||
}
|
||||
|
||||
@GenerateNiceMocks([MockSpec<PlansRepository>(), MockSpec<PlanRepository>()])
|
||||
void main() {
|
||||
group('PlansCubit', () {
|
||||
PlansCubit? cubit;
|
||||
late MockPlansRepository mockRepository;
|
||||
late MockPlanRepository mockPlanRepository;
|
||||
|
||||
setUp(() {
|
||||
setUp(() async {
|
||||
await sl.reset();
|
||||
mockRepository = MockPlansRepository();
|
||||
mockPlanRepository = MockPlanRepository();
|
||||
when(mockPlanRepository.planLikeState).thenAnswer((_) => const Stream.empty());
|
||||
when(mockPlanRepository.planFavoriteState).thenAnswer((_) => const Stream.empty());
|
||||
when(mockPlanRepository.planCommentsState).thenAnswer((_) => const Stream.empty());
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
@@ -24,16 +49,23 @@ void main() {
|
||||
await cubit!.close();
|
||||
cubit = null;
|
||||
}
|
||||
await sl.reset();
|
||||
});
|
||||
|
||||
test('начальное состояние - пустое', () {
|
||||
cubit = PlansCubit(plansRepository: mockRepository);
|
||||
cubit = PlansCubit(
|
||||
plansRepository: mockRepository,
|
||||
planRepository: mockPlanRepository,
|
||||
);
|
||||
|
||||
expect(cubit!.state, isA<PlansState>());
|
||||
});
|
||||
|
||||
test('getPlans - успешная загрузка планов', () async {
|
||||
cubit = PlansCubit(plansRepository: mockRepository);
|
||||
cubit = PlansCubit(
|
||||
plansRepository: mockRepository,
|
||||
planRepository: mockPlanRepository,
|
||||
);
|
||||
|
||||
final testPlans = [
|
||||
const PlanModel(
|
||||
@@ -75,7 +107,10 @@ void main() {
|
||||
});
|
||||
|
||||
test('getPlans - обработка ошибки', () async {
|
||||
cubit = PlansCubit(plansRepository: mockRepository);
|
||||
cubit = PlansCubit(
|
||||
plansRepository: mockRepository,
|
||||
planRepository: mockPlanRepository,
|
||||
);
|
||||
|
||||
when(mockRepository.getPlansBySlug(any))
|
||||
.thenThrow(Exception('Ошибка загрузки'));
|
||||
@@ -84,6 +119,33 @@ void main() {
|
||||
|
||||
expect(cubit!.state, isA<PlansStateError>());
|
||||
});
|
||||
|
||||
test('getPlans - close during request does not emit loaded or report an error', () async {
|
||||
final reporter = _RecordingErrorReporter();
|
||||
sl.registerSingleton<ErrorReporter>(reporter);
|
||||
|
||||
cubit = PlansCubit(
|
||||
plansRepository: mockRepository,
|
||||
planRepository: mockPlanRepository,
|
||||
);
|
||||
|
||||
final response = Completer<List<PlanModel>>();
|
||||
when(mockRepository.getPlansBySlug('test-user'))
|
||||
.thenAnswer((_) => response.future);
|
||||
|
||||
final emitted = <PlansState>[];
|
||||
final subscription = cubit!.stream.listen(emitted.add);
|
||||
|
||||
final request = cubit!.getPlans('test-user');
|
||||
await cubit!.close();
|
||||
response.complete(const <PlanModel>[]);
|
||||
await request;
|
||||
await subscription.cancel();
|
||||
|
||||
expect(emitted.whereType<PlansStateLoaded>(), isEmpty);
|
||||
expect(emitted.whereType<PlansStateError>(), isEmpty);
|
||||
expect(reporter.errors, isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,363 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:easy_debounce/easy_debounce.dart';
|
||||
import 'package:eatvio_app/core/error_reporting/error_reporter.dart';
|
||||
import 'package:eatvio_app/core/locator_service.dart';
|
||||
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
|
||||
import 'package:eatvio_app/features/feed/models/plan_pagination_model.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/features/profile/presentation/bloc/plans_cubit/plans_cubit.dart';
|
||||
import 'package:eatvio_app/features/profile/presentation/bloc/plans_cubit/plans_state.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
import '../../../../../fixtures/fixture_reader.dart';
|
||||
import 'profile_plans_cubit_like_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<PlansRepository>(), MockSpec<PlanRepository>()])
|
||||
class _RecordingReporter implements ErrorReporter {
|
||||
final List<Object> errors = [];
|
||||
Object? lastError;
|
||||
StackTrace? lastStack;
|
||||
String? lastOrigin;
|
||||
|
||||
@override
|
||||
Future<void> report(
|
||||
Object error,
|
||||
StackTrace? stackTrace, {
|
||||
String? origin,
|
||||
Map<String, Object?>? extras,
|
||||
}) async {
|
||||
errors.add(error);
|
||||
lastError = error;
|
||||
lastStack = stackTrace;
|
||||
lastOrigin = origin;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('profile PlansCubit — like/favorite', () {
|
||||
late MockPlansRepository plansRepository;
|
||||
late MockPlanRepository planRepository;
|
||||
late PlansCubit cubit;
|
||||
late _RecordingReporter reporter;
|
||||
late List<MethodCall> toastCalls;
|
||||
|
||||
const slugA = 'plan-a';
|
||||
const slugB = 'plan-b';
|
||||
|
||||
PlanModel plan({
|
||||
String slug = slugA,
|
||||
bool wasLikedByMe = false,
|
||||
bool isFavorite = false,
|
||||
int totalLikes = 10,
|
||||
}) {
|
||||
return PlanModel.fromJson(
|
||||
FixtureReader.readAsMap('plans/plan_response.json'),
|
||||
).copyWith(
|
||||
slug: slug,
|
||||
state: 'active',
|
||||
wasLikedByMe: wasLikedByMe,
|
||||
isFavorite: isFavorite,
|
||||
totalLikes: totalLikes,
|
||||
);
|
||||
}
|
||||
|
||||
setUpAll(() {
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(
|
||||
const MethodChannel('PonnamKarthik/fluttertoast'),
|
||||
(MethodCall methodCall) async {
|
||||
toastCalls.add(methodCall);
|
||||
return null;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
setUp(() async {
|
||||
await sl.reset();
|
||||
reporter = _RecordingReporter();
|
||||
sl.registerSingleton<ErrorReporter>(reporter);
|
||||
toastCalls = <MethodCall>[];
|
||||
|
||||
plansRepository = MockPlansRepository();
|
||||
planRepository = MockPlanRepository();
|
||||
|
||||
when(
|
||||
planRepository.planLikeState,
|
||||
).thenAnswer((_) => BehaviorSubject<LikesState>());
|
||||
when(
|
||||
planRepository.planFavoriteState,
|
||||
).thenAnswer((_) => BehaviorSubject<FavoriteState>());
|
||||
when(
|
||||
planRepository.planCommentsState,
|
||||
).thenAnswer((_) => BehaviorSubject<CommentsState>());
|
||||
|
||||
cubit = PlansCubit(
|
||||
plansRepository: plansRepository,
|
||||
planRepository: planRepository,
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
EasyDebounce.cancel('profile_like_plan_$slugA');
|
||||
EasyDebounce.cancel('profile_like_plan_$slugB');
|
||||
EasyDebounce.cancel('profile_favorite_$slugA');
|
||||
EasyDebounce.cancel('profile_favorite_$slugB');
|
||||
if (!cubit.isClosed) {
|
||||
await cubit.close();
|
||||
}
|
||||
await sl.reset();
|
||||
});
|
||||
|
||||
Future<void> load(List<PlanModel> plans) async {
|
||||
when(plansRepository.getPlansBySlug(any)).thenAnswer((_) async => plans);
|
||||
await cubit.getPlans('consultant');
|
||||
expect(cubit.state, isA<PlansStateLoaded>());
|
||||
}
|
||||
|
||||
PlansStateLoaded loaded() => cubit.state as PlansStateLoaded;
|
||||
|
||||
test(
|
||||
'likePlan adds a like optimistically and calls the repository once',
|
||||
() async {
|
||||
await load([plan()]);
|
||||
|
||||
await cubit.likePlan(slugA);
|
||||
|
||||
expect(loaded().plansList.single.wasLikedByMe, isTrue);
|
||||
expect(loaded().plansList.single.totalLikes, 11);
|
||||
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
verify(planRepository.likePlan(slugA)).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
test('likePlan removes an existing like optimistically', () async {
|
||||
await load([plan(wasLikedByMe: true, totalLikes: 10)]);
|
||||
|
||||
await cubit.likePlan(slugA);
|
||||
|
||||
expect(loaded().plansList.single.wasLikedByMe, isFalse);
|
||||
expect(loaded().plansList.single.totalLikes, 9);
|
||||
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
verify(planRepository.likePlan(slugA)).called(1);
|
||||
});
|
||||
|
||||
test(
|
||||
'likePlan rolls back the optimistic change when the repository fails',
|
||||
() async {
|
||||
when(planRepository.likePlan(any)).thenThrow(Exception('network'));
|
||||
await load([plan()]);
|
||||
|
||||
await cubit.likePlan(slugA);
|
||||
expect(loaded().plansList.single.wasLikedByMe, isTrue);
|
||||
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
|
||||
expect(loaded().plansList.single.wasLikedByMe, isFalse);
|
||||
expect(loaded().plansList.single.totalLikes, 10);
|
||||
verify(planRepository.likePlan(slugA)).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'likePlan does nothing when the slug is not in the loaded list',
|
||||
() async {
|
||||
await load([plan()]);
|
||||
final before = loaded().plansList;
|
||||
|
||||
await cubit.likePlan('missing-plan');
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
|
||||
expect(loaded().plansList, before);
|
||||
verifyNever(planRepository.likePlan(any));
|
||||
},
|
||||
);
|
||||
|
||||
test('likePlan does nothing when state is not loaded', () async {
|
||||
await cubit.likePlan(slugA);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
|
||||
expect(cubit.state, isNot(isA<PlansStateLoaded>()));
|
||||
verifyNever(planRepository.likePlan(any));
|
||||
});
|
||||
|
||||
test(
|
||||
'likePlan debounces repeated taps on the same slug into one request',
|
||||
() async {
|
||||
await load([plan()]);
|
||||
|
||||
await cubit.likePlan(slugA);
|
||||
await cubit.likePlan(slugA);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
|
||||
verify(planRepository.likePlan(slugA)).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
test('likePlan keeps a separate debounce per slug', () async {
|
||||
await load([plan(slug: slugA), plan(slug: slugB)]);
|
||||
|
||||
await cubit.likePlan(slugA);
|
||||
await cubit.likePlan(slugB);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
|
||||
verify(planRepository.likePlan(slugA)).called(1);
|
||||
verify(planRepository.likePlan(slugB)).called(1);
|
||||
});
|
||||
|
||||
test(
|
||||
'addPlanToFavorite sets favorite optimistically and calls the repository',
|
||||
() async {
|
||||
await load([plan()]);
|
||||
|
||||
await cubit.addPlanToFavorite(slugA);
|
||||
|
||||
expect(loaded().plansList.single.isFavorite, isTrue);
|
||||
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
verify(planRepository.addToFavorite(slugA)).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
test('addPlanToFavorite rolls back when the repository fails', () async {
|
||||
when(planRepository.addToFavorite(any)).thenThrow(Exception('network'));
|
||||
await load([plan()]);
|
||||
|
||||
await cubit.addPlanToFavorite(slugA);
|
||||
expect(loaded().plansList.single.isFavorite, isTrue);
|
||||
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
|
||||
expect(loaded().plansList.single.isFavorite, isFalse);
|
||||
verify(planRepository.addToFavorite(slugA)).called(1);
|
||||
});
|
||||
|
||||
test(
|
||||
'removePlanFromFavorite clears favorite optimistically and calls the repository',
|
||||
() async {
|
||||
await load([plan(isFavorite: true)]);
|
||||
|
||||
await cubit.removePlanFromFavorite(slugA);
|
||||
|
||||
expect(loaded().plansList.single.isFavorite, isFalse);
|
||||
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
verify(planRepository.removeFromFavorite(slugA)).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'favorite actions do nothing when the slug is not in the loaded list',
|
||||
() async {
|
||||
await load([plan()]);
|
||||
final before = loaded().plansList;
|
||||
|
||||
await cubit.addPlanToFavorite('missing-plan');
|
||||
await cubit.removePlanFromFavorite('missing-plan');
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
|
||||
expect(loaded().plansList, before);
|
||||
verifyNever(planRepository.addToFavorite(any));
|
||||
verifyNever(planRepository.removeFromFavorite(any));
|
||||
},
|
||||
);
|
||||
|
||||
test('close cancels a pending like request', () async {
|
||||
await load([plan()]);
|
||||
|
||||
await cubit.likePlan(slugA);
|
||||
await cubit.close();
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
|
||||
verifyNever(planRepository.likePlan(any));
|
||||
});
|
||||
|
||||
test('a debounced failure after close does not emit or throw', () async {
|
||||
when(planRepository.likePlan(any)).thenAnswer((_) async {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 50));
|
||||
throw Exception('network');
|
||||
});
|
||||
await load([plan()]);
|
||||
|
||||
await cubit.likePlan(slugA);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 300));
|
||||
await cubit.close();
|
||||
await Future<void>.delayed(const Duration(milliseconds: 300));
|
||||
|
||||
expect(cubit.isClosed, isTrue);
|
||||
});
|
||||
|
||||
test(
|
||||
'a like that fails after close neither emits after close nor leaks a zone error',
|
||||
() async {
|
||||
await load([plan()]);
|
||||
|
||||
final zoneErrors = <Object>[];
|
||||
|
||||
await runZonedGuarded(() async {
|
||||
final pending = Completer<void>();
|
||||
when(planRepository.likePlan(any)).thenAnswer((_) => pending.future);
|
||||
|
||||
await cubit.likePlan(slugA);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 250));
|
||||
verify(planRepository.likePlan(slugA)).called(1);
|
||||
await cubit.close();
|
||||
pending.completeError(Exception('network'));
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
}, (error, stackTrace) {
|
||||
zoneErrors.add(error);
|
||||
});
|
||||
|
||||
expect(cubit.isClosed, isTrue);
|
||||
expect(
|
||||
zoneErrors.whereType<StateError>().where(
|
||||
(error) => error.message.contains('Cannot emit new states after calling close'),
|
||||
),
|
||||
isEmpty,
|
||||
);
|
||||
expect(zoneErrors, isEmpty, reason: 'errors in zone: $zoneErrors');
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'addPlanToFavorite rolls back and shows an error toast when the repository fails',
|
||||
() async {
|
||||
when(planRepository.addToFavorite(any)).thenThrow(Exception('network'));
|
||||
await load([plan()]);
|
||||
|
||||
await cubit.addPlanToFavorite(slugA);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
|
||||
expect(loaded().plansList.single.isFavorite, isFalse);
|
||||
|
||||
final showToastCalls = toastCalls.where((call) => call.method == 'showToast').toList();
|
||||
expect(showToastCalls, isNotEmpty);
|
||||
final arguments = showToastCalls.single.arguments as Map;
|
||||
expect(arguments['msg'], contains('Произошла ошибка'));
|
||||
},
|
||||
);
|
||||
|
||||
test('likePlan reports the failure through ErrorReporter', () async {
|
||||
final error = Exception('network');
|
||||
when(planRepository.likePlan(any)).thenThrow(error);
|
||||
await load([plan()]);
|
||||
|
||||
await cubit.likePlan(slugA);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1200));
|
||||
|
||||
expect(reporter.errors, isNotEmpty);
|
||||
expect(reporter.lastError, same(error));
|
||||
expect(reporter.lastOrigin, 'PlansCubit');
|
||||
expect(reporter.lastOrigin, isNotEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
import 'package:easy_debounce/easy_debounce.dart';
|
||||
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
|
||||
import 'package:eatvio_app/features/feed/models/plan_pagination_model.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/features/profile/presentation/bloc/plans_cubit/plans_cubit.dart';
|
||||
import 'package:eatvio_app/features/profile/presentation/bloc/plans_cubit/plans_state.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
import '../../../../../fixtures/fixture_reader.dart';
|
||||
import 'profile_plans_cubit_slug_sync_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<PlansRepository>(), MockSpec<PlanRepository>()])
|
||||
void main() {
|
||||
group('profile PlansCubit — slug-based stream sync', () {
|
||||
late MockPlansRepository plansRepository;
|
||||
late MockPlanRepository planRepository;
|
||||
late BehaviorSubject<LikesState> likeState;
|
||||
late BehaviorSubject<FavoriteState> favoriteState;
|
||||
late BehaviorSubject<CommentsState> commentsState;
|
||||
late PlansCubit cubit;
|
||||
|
||||
PlanModel plan({required String slug, int totalLikes = 10}) {
|
||||
return PlanModel.fromJson(
|
||||
FixtureReader.readAsMap('plans/plan_response.json'),
|
||||
).copyWith(
|
||||
slug: slug,
|
||||
state: 'active',
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
totalLikes: totalLikes,
|
||||
);
|
||||
}
|
||||
|
||||
setUp(() async {
|
||||
plansRepository = MockPlansRepository();
|
||||
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);
|
||||
|
||||
cubit = PlansCubit(
|
||||
plansRepository: plansRepository,
|
||||
planRepository: planRepository,
|
||||
);
|
||||
|
||||
when(
|
||||
plansRepository.getPlansBySlug(any),
|
||||
).thenAnswer((_) async => [plan(slug: 'plan-a'), plan(slug: 'plan-b')]);
|
||||
await cubit.getPlans('consultant');
|
||||
expect(cubit.state, isA<PlansStateLoaded>());
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
EasyDebounce.cancel('profile_like_plan_plan-a');
|
||||
EasyDebounce.cancel('profile_like_plan_plan-b');
|
||||
if (!cubit.isClosed) {
|
||||
await cubit.close();
|
||||
}
|
||||
await likeState.close();
|
||||
await favoriteState.close();
|
||||
await commentsState.close();
|
||||
});
|
||||
|
||||
PlansStateLoaded loaded() => cubit.state as PlansStateLoaded;
|
||||
|
||||
test('a like event with a slug updates only the matching plan', () async {
|
||||
likeState.add(
|
||||
const LikesState(
|
||||
slug: 'plan-b',
|
||||
totalLikes: 42,
|
||||
wasLikedByMe: true,
|
||||
index: 0,
|
||||
),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(loaded().plansList[0].wasLikedByMe, isFalse);
|
||||
expect(loaded().plansList[0].totalLikes, 10);
|
||||
expect(loaded().plansList[1].wasLikedByMe, isTrue);
|
||||
expect(loaded().plansList[1].totalLikes, 42);
|
||||
});
|
||||
|
||||
test('a like event with an unknown slug is a no-op', () async {
|
||||
final before = loaded().plansList;
|
||||
|
||||
likeState.add(
|
||||
const LikesState(
|
||||
slug: 'unknown-plan',
|
||||
totalLikes: 42,
|
||||
wasLikedByMe: true,
|
||||
index: 0,
|
||||
),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(loaded().plansList, before);
|
||||
});
|
||||
|
||||
test('a favorite event with a slug updates only the matching plan', () async {
|
||||
favoriteState.add(
|
||||
const FavoriteState(slug: 'plan-b', isFavorite: true, index: 0),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(loaded().plansList[0].isFavorite, isFalse);
|
||||
expect(loaded().plansList[1].isFavorite, isTrue);
|
||||
});
|
||||
|
||||
test('a comments event with a slug updates only the matching plan', () async {
|
||||
commentsState.add(
|
||||
const CommentsState(slug: 'plan-b', totalComments: 7, index: 0),
|
||||
);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(loaded().plansList[0].totalComments, isNot(7));
|
||||
expect(loaded().plansList[1].totalComments, 7);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user