forked from eatvio/eatvio_app
Merge branch 'issue-1122' of ssh://10.55.5.123:222/misha/eatvio_app into issue-1122
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
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';
|
||||
@@ -15,6 +17,8 @@ import 'package:eatvio_app/features/diary/models/diary_day_model.dart';
|
||||
import 'package:eatvio_app/features/diary/models/nutrition_model.dart';
|
||||
import 'package:eatvio_app/core/widgets/data_error_widget.dart';
|
||||
import 'package:eatvio_app/features/diary/presentation/widgets/meal_widget.dart';
|
||||
import 'package:eatvio_app/features/insights/presentation/widgets/insights_card.dart';
|
||||
import 'package:eatvio_app/features/insights/repositories/insights_repository.dart';
|
||||
import 'package:eatvio_app/generated/l10n.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
@@ -22,6 +26,7 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import '../../../../fixtures/doubles/insights_doubles.dart';
|
||||
import 'diary_screen_smoke_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([
|
||||
@@ -43,6 +48,9 @@ void main() {
|
||||
AppConfig(buildType: AppBuildType.tests)
|
||||
..setBaseUrl('http://localhost'),
|
||||
);
|
||||
sl.registerLazySingleton<InsightsRepository>(
|
||||
() => FakeInsightsRepository(),
|
||||
);
|
||||
});
|
||||
|
||||
Widget wrap(Widget child) {
|
||||
@@ -60,11 +68,13 @@ void main() {
|
||||
bool isDiaryFinished = false,
|
||||
bool isDiaryCommented = false,
|
||||
int closed = 0,
|
||||
String? ownerSlug,
|
||||
String wakeup = '07:00',
|
||||
}) {
|
||||
final diary = DiaryDayEntity(
|
||||
id: 1,
|
||||
date: date,
|
||||
wakeup: '07:00',
|
||||
wakeup: wakeup,
|
||||
retire: '23:00',
|
||||
closed: closed,
|
||||
comment: '',
|
||||
@@ -87,6 +97,7 @@ void main() {
|
||||
chosenPlanId: hasChosenPlan ? 10 : null,
|
||||
dayOfChosenDiet: null,
|
||||
diet: null,
|
||||
ownerSlug: ownerSlug,
|
||||
);
|
||||
const nutrition = NutritionDataModel(
|
||||
dailyNutritionIntake: NutritionModel(
|
||||
@@ -377,6 +388,241 @@ void main() {
|
||||
expect(find.byType(Text), findsWidgets);
|
||||
});
|
||||
|
||||
Future<void> pumpDiaryWithInsights(
|
||||
WidgetTester tester, {
|
||||
required bool isConsultantView,
|
||||
String? ownerSlug,
|
||||
String? diaryOwnerSlug,
|
||||
Stream<DiaryState>? diaryStates,
|
||||
required FakeInsightsRepository insightsRepository,
|
||||
}) async {
|
||||
final diaryCubit = MockDiaryCubit();
|
||||
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.close()).thenAnswer((_) async => Future<void>.value());
|
||||
when(
|
||||
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.close()).thenAnswer((_) async => Future<void>.value());
|
||||
|
||||
sl.registerFactory<DiaryCubit>(() => diaryCubit);
|
||||
sl.registerFactory<ConsultantCommentsCubit>(() => commentsCubit);
|
||||
sl.unregister<InsightsRepository>();
|
||||
sl.registerLazySingleton<InsightsRepository>(() => insightsRepository);
|
||||
|
||||
await tester.pumpWidget(
|
||||
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();
|
||||
|
||||
await pumpDiaryWithInsights(
|
||||
tester,
|
||||
isConsultantView: false,
|
||||
insightsRepository: insightsRepository,
|
||||
);
|
||||
|
||||
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();
|
||||
|
||||
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');
|
||||
},
|
||||
);
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
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 {
|
||||
final insightsRepository = FakeInsightsRepository();
|
||||
|
||||
await pumpDiaryWithInsights(
|
||||
tester,
|
||||
isConsultantView: true,
|
||||
ownerSlug: 'from-route',
|
||||
diaryOwnerSlug: 'from-diary',
|
||||
insightsRepository: insightsRepository,
|
||||
);
|
||||
|
||||
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);
|
||||
|
||||
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));
|
||||
|
||||
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);
|
||||
|
||||
await pumpDiaryWithInsights(
|
||||
tester,
|
||||
isConsultantView: false,
|
||||
diaryStates: states.stream,
|
||||
insightsRepository: insightsRepository,
|
||||
);
|
||||
|
||||
states.add(loadedState(hasChosenPlan: false));
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
|
||||
expect(insightsRepository.serverRequests, hasLength(1));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('consultant view does not refresh insights on diary changes', (
|
||||
tester,
|
||||
) async {
|
||||
final insightsRepository = FakeInsightsRepository();
|
||||
final states = StreamController<DiaryState>.broadcast();
|
||||
addTearDown(states.close);
|
||||
|
||||
await pumpDiaryWithInsights(
|
||||
tester,
|
||||
isConsultantView: true,
|
||||
ownerSlug: 'client',
|
||||
diaryStates: states.stream,
|
||||
insightsRepository: insightsRepository,
|
||||
);
|
||||
|
||||
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 {
|
||||
final insightsRepository = FakeInsightsRepository();
|
||||
|
||||
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(),
|
||||
);
|
||||
|
||||
await pumpDiaryWithInsights(
|
||||
tester,
|
||||
isConsultantView: true,
|
||||
ownerSlug: 'client',
|
||||
insightsRepository: insightsRepository,
|
||||
);
|
||||
|
||||
expect(find.byKey(const Key('insights_card')), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('shows add button for a finished day inside the edit window', (
|
||||
tester,
|
||||
) async {
|
||||
|
||||
Reference in New Issue
Block a user