forked from eatvio/eatvio_app
feat(tests): updated tests coverage to 66.5%
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'package:easy_debounce/easy_debounce.dart';
|
||||
import 'package:eatvio_app/features/diets/models/diet_likes_model.dart';
|
||||
import 'package:eatvio_app/features/diets/presentation/cubit/about_diet_cubit.dart';
|
||||
import 'package:eatvio_app/features/diets/presentation/cubit/about_diet_state.dart';
|
||||
@@ -13,6 +14,7 @@ import 'about_diet_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<DietRepository>(), MockSpec<ProfileRepository>()])
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
group('AboutDietCubit', () {
|
||||
AboutDietCubit? aboutDietCubit;
|
||||
late MockDietRepository mockDietRepository;
|
||||
@@ -24,7 +26,10 @@ void main() {
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
EasyDebounce.cancel('like_diet');
|
||||
EasyDebounce.cancel('add_or_remove_diet_to_favorites');
|
||||
if (aboutDietCubit != null && !aboutDietCubit!.isClosed) {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
await aboutDietCubit!.close();
|
||||
aboutDietCubit = null;
|
||||
}
|
||||
@@ -700,5 +705,232 @@ void main() {
|
||||
final loadedState = aboutDietCubit!.state as DietStateLoaded;
|
||||
expect(loadedState.selectedIndexes[0], 2);
|
||||
});
|
||||
|
||||
test('likeDiet - убирает лайк', () async {
|
||||
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
|
||||
|
||||
const testDiet = Diet(
|
||||
id: 1,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Test details',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false, isEntitySubscribedByMe: null, authorPlaceInRating: null),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
wasLikedByMe: true,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
|
||||
when(mockDietRepository.likeDiet(1)).thenAnswer((_) async => const DietLikesModel(totalLikes: 9, isLiked: false));
|
||||
|
||||
await aboutDietCubit!.getDiet(slug: 'test-diet');
|
||||
await aboutDietCubit!.likeDiet(1, null);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
final loadedState = aboutDietCubit!.state as DietStateLoaded;
|
||||
expect(loadedState.diet.wasLikedByMe, false);
|
||||
expect(loadedState.diet.totalLikes, 9);
|
||||
});
|
||||
|
||||
test('likeDiet - обрабатывает ошибку', () async {
|
||||
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
|
||||
|
||||
const testDiet = Diet(
|
||||
id: 1,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Test details',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false, isEntitySubscribedByMe: null, authorPlaceInRating: null),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
|
||||
when(mockDietRepository.likeDiet(1)).thenThrow(Exception('Ошибка'));
|
||||
|
||||
await aboutDietCubit!.getDiet(slug: 'test-diet');
|
||||
await aboutDietCubit!.likeDiet(1, 0);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 1200));
|
||||
final loadedState = aboutDietCubit!.state as DietStateLoaded;
|
||||
expect(loadedState.diet.wasLikedByMe, false);
|
||||
expect(loadedState.diet.totalLikes, 10);
|
||||
});
|
||||
|
||||
test('addToFavorite - обрабатывает ошибку', () async {
|
||||
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
|
||||
|
||||
const testDiet = Diet(
|
||||
id: 1,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Test details',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false, isEntitySubscribedByMe: null, authorPlaceInRating: null),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
|
||||
when(mockDietRepository.addToFavorite(1)).thenThrow(Exception('Ошибка'));
|
||||
|
||||
await aboutDietCubit!.getDiet(slug: 'test-diet');
|
||||
await aboutDietCubit!.addToFavorite(1, 0);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
final loadedState = aboutDietCubit!.state as DietStateLoaded;
|
||||
expect(loadedState.diet.isFavorite, true);
|
||||
});
|
||||
|
||||
test('removeFromFavorite - обрабатывает ошибку', () async {
|
||||
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
|
||||
|
||||
const testDiet = Diet(
|
||||
id: 1,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Test details',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false, isEntitySubscribedByMe: null, authorPlaceInRating: null),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: true,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
|
||||
when(mockDietRepository.removeFromFavorite(1)).thenThrow(Exception('Ошибка'));
|
||||
|
||||
await aboutDietCubit!.getDiet(slug: 'test-diet');
|
||||
await aboutDietCubit!.removeFromFavorite(1, 0);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
final loadedState = aboutDietCubit!.state as DietStateLoaded;
|
||||
expect(loadedState.diet.isFavorite, false);
|
||||
});
|
||||
|
||||
test('initDietPage - обрабатывает ошибку', () async {
|
||||
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
|
||||
|
||||
when(mockDietRepository.getDietBySlug('test-diet')).thenThrow(Exception('Ошибка загрузки'));
|
||||
|
||||
await aboutDietCubit!.initDietPage(slug: 'test-diet');
|
||||
|
||||
expect(aboutDietCubit!.state, isA<DietStateError>());
|
||||
});
|
||||
|
||||
test('initDietPage - устанавливает userStartedDietName когда диета другая', () async {
|
||||
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
|
||||
|
||||
const testDiet = Diet(
|
||||
id: 1,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Test details',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false, isEntitySubscribedByMe: null, authorPlaceInRating: null),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
const otherDiet = Diet(
|
||||
id: 2,
|
||||
slug: 'other-diet',
|
||||
name: 'Other Diet',
|
||||
image: 'https://example.com/image2.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Other details',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false, isEntitySubscribedByMe: null, authorPlaceInRating: null),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
final chosenDiets = [ChosenDietModel(chosenDietID: 1, chosenPlanId: 1, state: 'started', startDate: DateTime(2024, 1, 1), dietId: 2)];
|
||||
|
||||
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
|
||||
when(mockProfileRepository.getChosenDiets()).thenAnswer((_) async => chosenDiets);
|
||||
when(mockDietRepository.getDiet(2)).thenAnswer((_) async => otherDiet);
|
||||
|
||||
await aboutDietCubit!.initDietPage(slug: 'test-diet');
|
||||
|
||||
expect(aboutDietCubit!.isCurrentDietStarted, false);
|
||||
expect(aboutDietCubit!.hasStartedDiet, true);
|
||||
expect(aboutDietCubit!.userStartedDietName, 'Other Diet');
|
||||
});
|
||||
|
||||
test('getDiet - с index параметром', () async {
|
||||
aboutDietCubit = AboutDietCubit(dietsRepository: mockDietRepository, profileRepository: mockProfileRepository);
|
||||
|
||||
const testDiet = Diet(
|
||||
id: 1,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Test details',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false, isEntitySubscribedByMe: null, authorPlaceInRating: null),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
when(mockDietRepository.getDietBySlug('test-diet')).thenAnswer((_) async => testDiet);
|
||||
|
||||
await aboutDietCubit!.getDiet(slug: 'test-diet', index: 0);
|
||||
|
||||
verify(mockDietRepository.emitCommentsStateToFeed(any)).called(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user