forked from eatvio/eatvio_app
fix: review fixes
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:eatvio_app/features/chat/repositories/chat_repository.dart';
|
||||
import 'package:eatvio_app/features/consultations/models/consultation_model.dart';
|
||||
@@ -1424,6 +1426,27 @@ void main() {
|
||||
name: 'Овсянка',
|
||||
portion: Portion.grams,
|
||||
quantity: 150,
|
||||
productNutritionalValueId: 42,
|
||||
dailyNutrition: DailyNutrition(
|
||||
energy: 195,
|
||||
proteins: 7,
|
||||
fats: 4,
|
||||
carbohydrates: 32,
|
||||
),
|
||||
),
|
||||
},
|
||||
2: {
|
||||
1: DailySchedule(
|
||||
name: 'Гречка',
|
||||
portion: Portion.pcs,
|
||||
quantity: 2,
|
||||
productNutritionalValueId: 7,
|
||||
dailyNutrition: DailyNutrition(
|
||||
energy: 220,
|
||||
proteins: 8,
|
||||
fats: 3,
|
||||
carbohydrates: 40,
|
||||
),
|
||||
),
|
||||
},
|
||||
},
|
||||
@@ -1448,8 +1471,267 @@ void main() {
|
||||
expect(result, true);
|
||||
expect(diaryCubit!.state, isA<DiaryStateLoadedTomorrowWithDiet>());
|
||||
final loaded = diaryCubit!.state as DiaryStateLoadedTomorrowWithDiet;
|
||||
expect(loaded.diary.meals.first.portions.single.productName, 'Овсянка');
|
||||
expect(loaded.diary.meals, hasLength(5));
|
||||
final oatmeal = loaded.diary.meals[0];
|
||||
expect(oatmeal.typeNumber, 1);
|
||||
expect(oatmeal.portions.single.productName, 'Овсянка');
|
||||
expect(oatmeal.portions.single.productNutritionalValueId, 42);
|
||||
expect(
|
||||
oatmeal.portions.single.productCaloriesInfo?.calories,
|
||||
closeTo(195 * 100 / 150, 0.001),
|
||||
);
|
||||
final buckwheat = loaded.diary.meals[1];
|
||||
expect(buckwheat.typeNumber, 2);
|
||||
expect(buckwheat.portions.single.productName, 'Гречка');
|
||||
expect(buckwheat.portions.single.productNutritionalValueId, 7);
|
||||
expect(buckwheat.portions.single.productCaloriesInfo?.energyOnPortion, 220);
|
||||
},
|
||||
);
|
||||
|
||||
test('fetchDiaryV2 - пустая диета не оставляет экран в загрузке', () async {
|
||||
diaryCubit = DiaryCubit(
|
||||
diaryRepository: mockDiaryRepository,
|
||||
glossaryRepository: mockGlossaryRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
dietRepository: mockDietRepository,
|
||||
chatRepository: mockChatRepository,
|
||||
settingsRepository: mockSettingsRepository,
|
||||
);
|
||||
when(
|
||||
mockDiaryRepository.createDiaryDay(day: anyNamed('day')),
|
||||
).thenAnswer((_) async {});
|
||||
when(
|
||||
mockDiaryRepository.getDiaryDayV2(day: anyNamed('day')),
|
||||
).thenAnswer((_) async => testDiaryDay);
|
||||
when(
|
||||
mockDiaryRepository.getNutritionData(any),
|
||||
).thenAnswer((_) async => testNutrition);
|
||||
when(mockSettingsRepository.getProfileSettings()).thenAnswer(
|
||||
(_) async => const ProfileSettingsModel(
|
||||
isMoodFillable: true,
|
||||
isBedtimeFillable: true,
|
||||
isPublicProfile: true,
|
||||
newUpdatesNotifications: null,
|
||||
diaryReminderNotifications: null,
|
||||
),
|
||||
);
|
||||
|
||||
expect(await diaryCubit!.setSelectedDay(DateTime(2024, 1, 15)), true);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 50));
|
||||
expect(diaryCubit!.state, isA<DiaryStateLoaded>());
|
||||
|
||||
when(mockDiaryRepository.getDiaryDayV2(day: anyNamed('day'))).thenAnswer(
|
||||
(_) async => const DiaryDayModel(
|
||||
id: null,
|
||||
date: '2024-01-20',
|
||||
closed: 0,
|
||||
comment: '',
|
||||
typeOfDiary: TypeOfDiaryModel(
|
||||
hasChosenDiet: true,
|
||||
hasChosenPlan: false,
|
||||
diaryState: null,
|
||||
),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: null,
|
||||
diet: null,
|
||||
),
|
||||
);
|
||||
|
||||
final result = await diaryCubit!.setSelectedDay(DateTime(2024, 1, 20));
|
||||
|
||||
expect(result, false);
|
||||
expect(diaryCubit!.state, isA<DiaryStateLoaded>());
|
||||
expect(diaryCubit!.selectedDay, DateTime(2024, 1, 15));
|
||||
});
|
||||
|
||||
test('setSelectedDay - ошибка диеты оставляет выбранный день для повтора', () async {
|
||||
diaryCubit = DiaryCubit(
|
||||
diaryRepository: mockDiaryRepository,
|
||||
glossaryRepository: mockGlossaryRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
dietRepository: mockDietRepository,
|
||||
chatRepository: mockChatRepository,
|
||||
settingsRepository: mockSettingsRepository,
|
||||
);
|
||||
const preview = DiaryDayModel(
|
||||
id: null,
|
||||
date: '2024-01-20',
|
||||
closed: 0,
|
||||
comment: '',
|
||||
typeOfDiary: TypeOfDiaryModel(
|
||||
hasChosenDiet: true,
|
||||
hasChosenPlan: false,
|
||||
diaryState: DiaryStateEnum.init,
|
||||
),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: 2,
|
||||
diet: DiaryDietModel(id: 1, duration: 7, name: 'Test Diet'),
|
||||
);
|
||||
when(
|
||||
mockDiaryRepository.createDiaryDay(day: anyNamed('day')),
|
||||
).thenAnswer((_) async {});
|
||||
when(
|
||||
mockDiaryRepository.getDiaryDayV2(day: anyNamed('day')),
|
||||
).thenAnswer((_) async => preview);
|
||||
when(mockDietRepository.getDiet(1)).thenThrow(Exception('network'));
|
||||
|
||||
final day = DateTime(2024, 1, 20);
|
||||
final result = await diaryCubit!.setSelectedDay(day);
|
||||
|
||||
expect(result, false);
|
||||
expect(diaryCubit!.state, isA<DiaryStateError>());
|
||||
expect(diaryCubit!.selectedDay, day);
|
||||
|
||||
when(mockDietRepository.getDiet(1)).thenAnswer(
|
||||
(_) async => const Diet(
|
||||
id: 1,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: '',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(),
|
||||
details: '',
|
||||
dailySchedule: {
|
||||
2: {
|
||||
1: {
|
||||
1: DailySchedule(
|
||||
name: 'Овсянка',
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
author: Author(
|
||||
authorSlug: 'test',
|
||||
authorName: 'Test',
|
||||
authorProfileIcon: '',
|
||||
authorIsMe: false,
|
||||
),
|
||||
totalLikes: 0,
|
||||
totalViews: 0,
|
||||
totalComments: 0,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
),
|
||||
);
|
||||
|
||||
final retry = await diaryCubit!.fetchDiaryV2(needLoading: true);
|
||||
|
||||
expect(retry, true);
|
||||
final loaded = diaryCubit!.state as DiaryStateLoadedTomorrowWithDiet;
|
||||
expect(loaded.diary.meals.first.portions.single.productName, 'Овсянка');
|
||||
expect(diaryCubit!.selectedDay, day);
|
||||
});
|
||||
|
||||
test('setSelectedDay - поздний ответ не затирает более новый день', () async {
|
||||
diaryCubit = DiaryCubit(
|
||||
diaryRepository: mockDiaryRepository,
|
||||
glossaryRepository: mockGlossaryRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
dietRepository: mockDietRepository,
|
||||
chatRepository: mockChatRepository,
|
||||
settingsRepository: mockSettingsRepository,
|
||||
);
|
||||
DiaryDayModel preview(String date, int dietId) {
|
||||
return DiaryDayModel(
|
||||
id: null,
|
||||
date: date,
|
||||
closed: 0,
|
||||
comment: '',
|
||||
typeOfDiary: const TypeOfDiaryModel(
|
||||
hasChosenDiet: true,
|
||||
hasChosenPlan: false,
|
||||
diaryState: DiaryStateEnum.init,
|
||||
),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: 1,
|
||||
diet: DiaryDietModel(id: dietId, duration: 7, name: 'Diet $dietId'),
|
||||
);
|
||||
}
|
||||
|
||||
Diet diet(int id, String product) {
|
||||
return Diet(
|
||||
id: id,
|
||||
slug: 'diet-$id',
|
||||
name: 'Diet $id',
|
||||
image: '',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: const DietType(),
|
||||
details: '',
|
||||
dailySchedule: {
|
||||
1: {
|
||||
1: {
|
||||
1: DailySchedule(
|
||||
name: product,
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
author: const Author(
|
||||
authorSlug: 'test',
|
||||
authorName: 'Test',
|
||||
authorProfileIcon: '',
|
||||
authorIsMe: false,
|
||||
),
|
||||
totalLikes: 0,
|
||||
totalViews: 0,
|
||||
totalComments: 0,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
}
|
||||
|
||||
when(
|
||||
mockDiaryRepository.createDiaryDay(day: anyNamed('day')),
|
||||
).thenAnswer((_) async {});
|
||||
when(mockDiaryRepository.getDiaryDayV2(day: anyNamed('day'))).thenAnswer((
|
||||
invocation,
|
||||
) async {
|
||||
final day = invocation.namedArguments[const Symbol('day')] as String;
|
||||
if (day.startsWith('2024-01-20')) {
|
||||
return preview('2024-01-20', 1);
|
||||
}
|
||||
return preview('2024-01-21', 2);
|
||||
});
|
||||
final firstDiet = Completer<Diet>();
|
||||
final secondDiet = Completer<Diet>();
|
||||
var dietCalls = 0;
|
||||
when(mockDietRepository.getDiet(any)).thenAnswer((invocation) {
|
||||
dietCalls++;
|
||||
final id = invocation.positionalArguments.first as int;
|
||||
return (id == 1 ? firstDiet : secondDiet).future;
|
||||
});
|
||||
|
||||
Future<void> waitForDietCalls(int count) async {
|
||||
for (var i = 0; i < 50 && dietCalls < count; i++) {
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
}
|
||||
expect(dietCalls, greaterThanOrEqualTo(count));
|
||||
}
|
||||
|
||||
final firstSelection = diaryCubit!.setSelectedDay(DateTime(2024, 1, 20));
|
||||
await waitForDietCalls(1);
|
||||
final secondSelection = diaryCubit!.setSelectedDay(DateTime(2024, 1, 21));
|
||||
await waitForDietCalls(2);
|
||||
|
||||
secondDiet.complete(diet(2, 'Новый'));
|
||||
await secondSelection;
|
||||
firstDiet.complete(diet(1, 'Старый'));
|
||||
await firstSelection;
|
||||
|
||||
final loaded = diaryCubit!.state as DiaryStateLoadedTomorrowWithDiet;
|
||||
expect(loaded.diary.meals.first.portions.single.productName, 'Новый');
|
||||
expect(diaryCubit!.selectedDay, DateTime(2024, 1, 21));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user