forked from eatvio/eatvio_app
feat(tests): test coverage is 70.5
This commit is contained in:
@@ -965,5 +965,116 @@ void main() {
|
||||
|
||||
verify(mockDiaryRepository.deletePhoto(photo: photo, diaryId: 1, mealId: 1)).called(1);
|
||||
});
|
||||
|
||||
test('setSelectedDay - возвращает true даже если getDiary возвращает null', () async {
|
||||
diaryCubit = DiaryCubit(
|
||||
diaryRepository: mockDiaryRepository,
|
||||
glossaryRepository: mockGlossaryRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
dietRepository: mockDietRepository,
|
||||
chatRepository: mockChatRepository,
|
||||
settingsRepository: mockSettingsRepository,
|
||||
);
|
||||
when(mockDiaryRepository.createDiaryDay(day: anyNamed('day'))).thenThrow(
|
||||
DioException(
|
||||
requestOptions: RequestOptions(path: ''),
|
||||
response: Response(requestOptions: RequestOptions(path: ''), statusCode: 500),
|
||||
),
|
||||
);
|
||||
|
||||
final result = await diaryCubit!.setSelectedDay(DateTime(2024, 1, 20));
|
||||
|
||||
// fetchDiaryV2 возвращает true когда getDiary() == null, так как эмитит состояние ошибки
|
||||
expect(result, true);
|
||||
expect(diaryCubit!.state, isA<DiaryStateError>());
|
||||
});
|
||||
|
||||
test('fetchDiaryV2 - обрабатывает случай когда diaryEntity == null и нет диеты', () async {
|
||||
diaryCubit = DiaryCubit(
|
||||
diaryRepository: mockDiaryRepository,
|
||||
glossaryRepository: mockGlossaryRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
dietRepository: mockDietRepository,
|
||||
chatRepository: mockChatRepository,
|
||||
settingsRepository: mockSettingsRepository,
|
||||
);
|
||||
// toEntity() возвращает null когда id == null
|
||||
const diaryDayWithoutEntity = DiaryDayModel(
|
||||
id: null,
|
||||
date: '2024-01-15',
|
||||
closed: 0,
|
||||
comment: '',
|
||||
typeOfDiary: TypeOfDiaryModel(hasChosenDiet: false, hasChosenPlan: false, diaryState: null),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: null,
|
||||
diet: null,
|
||||
);
|
||||
when(mockDiaryRepository.createDiaryDay(day: anyNamed('day'))).thenAnswer((_) async => Future.value());
|
||||
when(mockDiaryRepository.getDiaryDayV2(day: anyNamed('day'))).thenAnswer((_) async => diaryDayWithoutEntity);
|
||||
|
||||
final result = await diaryCubit!.fetchDiaryV2(needLoading: false);
|
||||
|
||||
// Если diaryEntity == null (id == null) и нет диеты, fetchDiaryV2 возвращает false
|
||||
// и эмитит предыдущее состояние (loading)
|
||||
expect(result, false);
|
||||
expect(diaryCubit!.state, const DiaryState.loading());
|
||||
});
|
||||
|
||||
test('fetchDiaryV2 - обрабатывает случай когда diaryEntity == null но есть диета', () async {
|
||||
diaryCubit = DiaryCubit(
|
||||
diaryRepository: mockDiaryRepository,
|
||||
glossaryRepository: mockGlossaryRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
dietRepository: mockDietRepository,
|
||||
chatRepository: mockChatRepository,
|
||||
settingsRepository: mockSettingsRepository,
|
||||
);
|
||||
// id должен быть null, чтобы toEntity() вернул null и код пошел по ветке setDiaryForDietOnly
|
||||
const diaryDayWithDiet = DiaryDayModel(
|
||||
id: null,
|
||||
date: '2024-01-15',
|
||||
closed: 0,
|
||||
comment: '',
|
||||
typeOfDiary: TypeOfDiaryModel(hasChosenDiet: true, hasChosenPlan: false, diaryState: null),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: 1,
|
||||
diet: DiaryDietModel(id: 1, duration: 7, name: 'Test Diet'),
|
||||
);
|
||||
when(mockDiaryRepository.createDiaryDay(day: anyNamed('day'))).thenAnswer((_) async => Future.value());
|
||||
when(mockDiaryRepository.getDiaryDayV2(day: anyNamed('day'))).thenAnswer((_) async => diaryDayWithDiet);
|
||||
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: {
|
||||
1: {
|
||||
1: {1: DailySchedule(name: 'Яблоко', portion: Portion.grams, quantity: 1)},
|
||||
},
|
||||
},
|
||||
author: Author(authorSlug: 'test', authorName: 'Test', authorProfileIcon: '', authorIsMe: false),
|
||||
totalLikes: 0,
|
||||
totalViews: 0,
|
||||
totalComments: 0,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
),
|
||||
);
|
||||
|
||||
final result = await diaryCubit!.fetchDiaryV2(needLoading: false);
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(result, true);
|
||||
// setDiaryForDietOnly сначала эмитит loading, затем loadedTomorrowWithDiet
|
||||
expect(diaryCubit!.state, isA<DiaryStateLoadedTomorrowWithDiet>());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user