feat(insights): review fixes

This commit is contained in:
misha
2026-11-11 16:05:11 +03:00
parent 5228d43f15
commit db3a32c12e
16 changed files with 694 additions and 232 deletions

View File

@@ -49,7 +49,9 @@ void main() {
AppConfig(buildType: AppBuildType.tests)
..setBaseUrl('http://localhost'),
);
sl.registerLazySingleton<InsightsRepository>(() => FakeInsightsRepository());
sl.registerLazySingleton<InsightsRepository>(
() => FakeInsightsRepository(),
);
});
Widget wrap(Widget child) {
@@ -401,17 +403,31 @@ void main() {
required FakeInsightsRepository insightsRepository,
}) async {
final diaryCubit = MockDiaryCubit();
final state = loadedState(hasChosenPlan: false, ownerSlug: diaryOwnerSlug).copyWith(isConsultantView: isConsultantView);
final state = loadedState(
hasChosenPlan: false,
ownerSlug: diaryOwnerSlug,
).copyWith(isConsultantView: isConsultantView);
when(diaryCubit.state).thenReturn(state);
when(diaryCubit.stream).thenAnswer((_) => diaryStates ?? const Stream<DiaryState>.empty());
when(
diaryCubit.stream,
).thenAnswer((_) => diaryStates ?? const Stream<DiaryState>.empty());
when(diaryCubit.close()).thenAnswer((_) async => Future<void>.value());
when(
diaryCubit.fetchDiaryV2(needLoading: anyNamed('needLoading'), diaryId: anyNamed('diaryId')),
diaryCubit.fetchDiaryV2(
needLoading: anyNamed('needLoading'),
diaryId: anyNamed('diaryId'),
),
).thenAnswer((_) async => true);
final commentsCubit = MockConsultantCommentsCubit();
when(commentsCubit.state).thenReturn(ConsultantCommentsState(commentsModel: ConsultantCommentModel(comment: '')));
when(commentsCubit.stream).thenAnswer((_) => const Stream<ConsultantCommentsState>.empty());
when(commentsCubit.state).thenReturn(
ConsultantCommentsState(
commentsModel: ConsultantCommentModel(comment: ''),
),
);
when(
commentsCubit.stream,
).thenAnswer((_) => const Stream<ConsultantCommentsState>.empty());
when(commentsCubit.close()).thenAnswer((_) async => Future<void>.value());
sl.registerFactory<DiaryCubit>(() => diaryCubit);
@@ -420,43 +436,84 @@ void main() {
sl.registerLazySingleton<InsightsRepository>(() => insightsRepository);
await tester.pumpWidget(
wrap(isConsultantView ? UserDiaryScreen(diaryId: 1, ownerSlug: ownerSlug) : DiaryScreen(date: DateTime(2025, 1, 1))),
wrap(
isConsultantView
? UserDiaryScreen(diaryId: 1, ownerSlug: ownerSlug)
: DiaryScreen(date: DateTime(2025, 1, 1)),
),
);
await tester.pump(const Duration(milliseconds: 200));
}
testWidgets('own diary shows insights card for own data with add button enabled', (tester) async {
final insightsRepository = FakeInsightsRepository();
testWidgets(
'own diary shows insights card for own data with add button enabled',
(tester) async {
final insightsRepository = FakeInsightsRepository();
await pumpDiaryWithInsights(tester, isConsultantView: false, insightsRepository: insightsRepository);
await pumpDiaryWithInsights(
tester,
isConsultantView: false,
insightsRepository: insightsRepository,
);
expect(find.byType(InsightsCard), findsOneWidget);
expect(tester.widget<InsightsCard>(find.byType(InsightsCard)).slug, isNull);
expect(tester.widget<InsightsCard>(find.byType(InsightsCard)).canAdd, isTrue);
expect(insightsRepository.serverRequests.single.slug, isNull);
});
expect(find.byType(InsightsCard), findsOneWidget);
final card = tester.widget<InsightsCard>(find.byType(InsightsCard));
expect(card.slug, isNull);
expect(card.canAdd, isTrue);
expect(card.initialIntraDate, '2025-01-01');
expect(insightsRepository.serverRequests.single.slug, isNull);
expect(
insightsRepository.serverRequests.single.intraDate,
'2025-01-01',
);
},
);
testWidgets('consultant view shows insights card of the client without add button', (tester) async {
final insightsRepository = FakeInsightsRepository();
testWidgets(
'consultant view shows insights card of the client without add button',
(tester) async {
final insightsRepository = FakeInsightsRepository();
await pumpDiaryWithInsights(tester, isConsultantView: true, ownerSlug: 'client', insightsRepository: insightsRepository);
await pumpDiaryWithInsights(
tester,
isConsultantView: true,
ownerSlug: 'client',
insightsRepository: insightsRepository,
);
final card = tester.widget<InsightsCard>(find.byType(InsightsCard));
expect(card.slug, 'client');
expect(card.canAdd, isFalse);
expect(insightsRepository.serverRequests.single.slug, 'client');
});
final card = tester.widget<InsightsCard>(find.byType(InsightsCard));
expect(card.slug, 'client');
expect(card.canAdd, isFalse);
expect(insightsRepository.serverRequests.single.slug, 'client');
},
);
testWidgets('consultant view takes the owner slug from the diary (user.slug)', (tester) async {
final insightsRepository = FakeInsightsRepository();
testWidgets(
'consultant view takes the owner slug from the diary (user.slug)',
(tester) async {
final insightsRepository = FakeInsightsRepository();
await pumpDiaryWithInsights(tester, isConsultantView: true, diaryOwnerSlug: 'client-from-diary', insightsRepository: insightsRepository);
await pumpDiaryWithInsights(
tester,
isConsultantView: true,
diaryOwnerSlug: 'client-from-diary',
insightsRepository: insightsRepository,
);
expect(tester.widget<InsightsCard>(find.byType(InsightsCard)).slug, 'client-from-diary');
expect(insightsRepository.serverRequests.single.slug, 'client-from-diary');
});
expect(
tester.widget<InsightsCard>(find.byType(InsightsCard)).slug,
'client-from-diary',
);
expect(
insightsRepository.serverRequests.single.slug,
'client-from-diary',
);
},
);
testWidgets('diary owner slug wins over the route parameter', (tester) async {
testWidgets('diary owner slug wins over the route parameter', (
tester,
) async {
final insightsRepository = FakeInsightsRepository();
await pumpDiaryWithInsights(
@@ -467,37 +524,58 @@ void main() {
insightsRepository: insightsRepository,
);
expect(tester.widget<InsightsCard>(find.byType(InsightsCard)).slug, 'from-diary');
expect(
tester.widget<InsightsCard>(find.byType(InsightsCard)).slug,
'from-diary',
);
});
testWidgets('own diary refreshes insights when meals or wake-up time change', (tester) async {
final insightsRepository = FakeInsightsRepository();
final states = StreamController<DiaryState>.broadcast();
addTearDown(states.close);
testWidgets(
'own diary refreshes insights when meals or wake-up time change',
(tester) async {
final insightsRepository = FakeInsightsRepository();
final states = StreamController<DiaryState>.broadcast();
addTearDown(states.close);
await pumpDiaryWithInsights(tester, isConsultantView: false, diaryStates: states.stream, insightsRepository: insightsRepository);
expect(insightsRepository.serverRequests, hasLength(1));
await pumpDiaryWithInsights(
tester,
isConsultantView: false,
diaryStates: states.stream,
insightsRepository: insightsRepository,
);
expect(insightsRepository.serverRequests, hasLength(1));
states.add(loadedState(hasChosenPlan: false, wakeup: '06:30'));
await tester.pump(const Duration(milliseconds: 200));
states.add(loadedState(hasChosenPlan: false, wakeup: '06:30'));
await tester.pump(const Duration(milliseconds: 200));
expect(insightsRepository.serverRequests, hasLength(2));
});
expect(insightsRepository.serverRequests, hasLength(2));
},
);
testWidgets('own diary does not refresh insights when nothing relevant changed', (tester) async {
final insightsRepository = FakeInsightsRepository();
final states = StreamController<DiaryState>.broadcast();
addTearDown(states.close);
testWidgets(
'own diary does not refresh insights when nothing relevant changed',
(tester) async {
final insightsRepository = FakeInsightsRepository();
final states = StreamController<DiaryState>.broadcast();
addTearDown(states.close);
await pumpDiaryWithInsights(tester, isConsultantView: false, diaryStates: states.stream, insightsRepository: insightsRepository);
await pumpDiaryWithInsights(
tester,
isConsultantView: false,
diaryStates: states.stream,
insightsRepository: insightsRepository,
);
states.add(loadedState(hasChosenPlan: false));
await tester.pump(const Duration(milliseconds: 200));
states.add(loadedState(hasChosenPlan: false));
await tester.pump(const Duration(milliseconds: 200));
expect(insightsRepository.serverRequests, hasLength(1));
});
expect(insightsRepository.serverRequests, hasLength(1));
},
);
testWidgets('consultant view does not refresh insights on diary changes', (tester) async {
testWidgets('consultant view does not refresh insights on diary changes', (
tester,
) async {
final insightsRepository = FakeInsightsRepository();
final states = StreamController<DiaryState>.broadcast();
addTearDown(states.close);
@@ -510,25 +588,43 @@ void main() {
insightsRepository: insightsRepository,
);
states.add(loadedState(hasChosenPlan: false, wakeup: '06:30').copyWith(isConsultantView: true));
states.add(
loadedState(
hasChosenPlan: false,
wakeup: '06:30',
).copyWith(isConsultantView: true),
);
await tester.pump(const Duration(milliseconds: 200));
expect(insightsRepository.serverRequests, hasLength(1));
});
testWidgets('consultant view without owner slug hides insights card', (tester) async {
testWidgets('consultant view without owner slug hides insights card', (
tester,
) async {
final insightsRepository = FakeInsightsRepository();
await pumpDiaryWithInsights(tester, isConsultantView: true, insightsRepository: insightsRepository);
await pumpDiaryWithInsights(
tester,
isConsultantView: true,
insightsRepository: insightsRepository,
);
expect(find.byType(InsightsCard), findsNothing);
expect(insightsRepository.serverRequests, isEmpty);
});
testWidgets('consultant view hides insights card on 403', (tester) async {
final insightsRepository = FakeInsightsRepository(error: const InsightsForbidden());
final insightsRepository = FakeInsightsRepository(
error: const InsightsForbidden(),
);
await pumpDiaryWithInsights(tester, isConsultantView: true, ownerSlug: 'client', insightsRepository: insightsRepository);
await pumpDiaryWithInsights(
tester,
isConsultantView: true,
ownerSlug: 'client',
insightsRepository: insightsRepository,
);
expect(find.byKey(const Key('insights_card')), findsNothing);
});