forked from eatvio/eatvio_app
Merge resolve-conflicts into main
This commit is contained in:
29
test/features/diets/models/diet_likes_model_test.dart
Normal file
29
test/features/diets/models/diet_likes_model_test.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'package:eatvio_app/features/diets/models/diet_likes_model.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('DietLikesModel', () {
|
||||
test('создает DietLikesModel с правильными значениями', () {
|
||||
const model = DietLikesModel(
|
||||
totalLikes: 10,
|
||||
isLiked: true,
|
||||
);
|
||||
|
||||
expect(model.totalLikes, 10);
|
||||
expect(model.isLiked, isTrue);
|
||||
});
|
||||
|
||||
test('fromJson создает DietLikesModel из JSON', () {
|
||||
final json = {
|
||||
'total_likers': 10,
|
||||
'is_liked': true,
|
||||
};
|
||||
|
||||
final model = DietLikesModel.fromJson(json);
|
||||
|
||||
expect(model.totalLikes, 10);
|
||||
expect(model.isLiked, isTrue);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
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';
|
||||
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import 'about_diet_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([
|
||||
MockSpec<DietRepository>(),
|
||||
MockSpec<ProfileRepository>(),
|
||||
])
|
||||
void main() {
|
||||
group('AboutDietCubit', () {
|
||||
AboutDietCubit? aboutDietCubit;
|
||||
late MockDietRepository mockDietRepository;
|
||||
late MockProfileRepository mockProfileRepository;
|
||||
|
||||
setUp(() {
|
||||
mockDietRepository = MockDietRepository();
|
||||
mockProfileRepository = MockProfileRepository();
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (aboutDietCubit != null && !aboutDietCubit!.isClosed) {
|
||||
await aboutDietCubit!.close();
|
||||
aboutDietCubit = null;
|
||||
}
|
||||
});
|
||||
|
||||
test('начальное состояние - initial', () {
|
||||
aboutDietCubit = AboutDietCubit(
|
||||
dietsRepository: mockDietRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
);
|
||||
|
||||
expect(aboutDietCubit!.state, isA<AboutDietState>());
|
||||
});
|
||||
|
||||
test('getDiet - успешная загрузка диеты по slug', () 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');
|
||||
|
||||
expect(aboutDietCubit!.state, isA<DietStateLoaded>());
|
||||
final loadedState = aboutDietCubit!.state as DietStateLoaded;
|
||||
expect(loadedState.diet.id, 1);
|
||||
});
|
||||
|
||||
test('getDiet - успешная загрузка диеты по id', () 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.getDiet(1))
|
||||
.thenAnswer((_) async => testDiet);
|
||||
|
||||
await aboutDietCubit!.getDiet(id: 1);
|
||||
|
||||
expect(aboutDietCubit!.state, isA<DietStateLoaded>());
|
||||
});
|
||||
|
||||
test('getDiet - обработка ошибки', () async {
|
||||
aboutDietCubit = AboutDietCubit(
|
||||
dietsRepository: mockDietRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
);
|
||||
|
||||
when(mockDietRepository.getDietBySlug(any))
|
||||
.thenThrow(Exception('Ошибка загрузки'));
|
||||
|
||||
await aboutDietCubit!.getDiet(slug: 'test-diet');
|
||||
|
||||
expect(aboutDietCubit!.state, isA<DietStateError>());
|
||||
});
|
||||
|
||||
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))
|
||||
.thenAnswer((_) async => const DietLikesModel(
|
||||
totalLikes: 11,
|
||||
isLiked: true,
|
||||
));
|
||||
|
||||
await aboutDietCubit!.getDiet(slug: 'test-diet');
|
||||
await aboutDietCubit!.likeDiet(1, null);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 1200));
|
||||
expect(aboutDietCubit!.state, isA<DietStateLoaded>());
|
||||
final loadedState = aboutDietCubit!.state as DietStateLoaded;
|
||||
expect(loadedState.diet.wasLikedByMe, isTrue);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -11,196 +11,245 @@ import 'diet_repository_test.mocks.dart';
|
||||
@GenerateNiceMocks([MockSpec<DietService>()])
|
||||
void main() {
|
||||
group('DietRepository', () {
|
||||
late DietRepository dietRepository;
|
||||
late MockDietService mockDietService;
|
||||
late DietRepository repository;
|
||||
late MockDietService mockService;
|
||||
|
||||
setUp(() {
|
||||
mockDietService = MockDietService();
|
||||
dietRepository = DietRepository(dietsService: mockDietService);
|
||||
mockService = MockDietService();
|
||||
repository = DietRepository(dietsService: mockService);
|
||||
});
|
||||
|
||||
test('likeDiet returns DietLikesModel', () async {
|
||||
const id = 1;
|
||||
const expected = DietLikesModel(totalLikes: 10, isLiked: true);
|
||||
test('likeDiet - успешный лайк', () async {
|
||||
const dietId = 1;
|
||||
const expectedLikes = DietLikesModel(
|
||||
totalLikes: 10,
|
||||
isLiked: true,
|
||||
);
|
||||
|
||||
when(mockDietService.likeDiet(id: id)).thenAnswer((_) async => expected);
|
||||
when(mockService.likeDiet(id: dietId))
|
||||
.thenAnswer((_) async => expectedLikes);
|
||||
|
||||
final result = await dietRepository.likeDiet(id);
|
||||
final result = await repository.likeDiet(dietId);
|
||||
|
||||
expect(result, isA<DietLikesModel>());
|
||||
expect(result.totalLikes, 10);
|
||||
expect(result.isLiked, true);
|
||||
verify(mockDietService.likeDiet(id: id)).called(1);
|
||||
expect(result, equals(expectedLikes));
|
||||
verify(mockService.likeDiet(id: dietId)).called(1);
|
||||
});
|
||||
|
||||
test('getDiet returns Diet by id', () async {
|
||||
const id = 1;
|
||||
const expected = Diet(
|
||||
id: id,
|
||||
test('getDiet - успешное получение диеты по id', () async {
|
||||
const dietId = 1;
|
||||
const expectedDiet = Diet(
|
||||
id: dietId,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: 'https://someurl.com/img.jpg',
|
||||
duration: 30,
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(light: true, veg: false, carb: false, meat: false),
|
||||
details: 'Test diet description',
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Test details',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: '', authorName: '', authorProfileIcon: '', authorIsMe: false),
|
||||
totalLikes: 1,
|
||||
totalViews: 1,
|
||||
totalComments: 1,
|
||||
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(mockDietService.getDiet(id: id)).thenAnswer((_) async => expected);
|
||||
when(mockService.getDiet(id: dietId))
|
||||
.thenAnswer((_) async => expectedDiet);
|
||||
|
||||
final result = await dietRepository.getDiet(id);
|
||||
final result = await repository.getDiet(dietId);
|
||||
|
||||
expect(result, isA<Diet>());
|
||||
expect(result.id, id);
|
||||
expect(result.name, 'Test Diet');
|
||||
verify(mockDietService.getDiet(id: id)).called(1);
|
||||
expect(result, equals(expectedDiet));
|
||||
expect(result.id, dietId);
|
||||
verify(mockService.getDiet(id: dietId)).called(1);
|
||||
});
|
||||
|
||||
test('getDietBySlug returns Diet by slug', () async {
|
||||
const slug = 'test-diet-slug';
|
||||
const expected = Diet(
|
||||
test('getDietBySlug - успешное получение диеты по slug', () async {
|
||||
const slug = 'test-diet';
|
||||
const expectedDiet = Diet(
|
||||
id: 1,
|
||||
slug: slug,
|
||||
name: 'Test Diet',
|
||||
image: 'https://someurl.com/img.jpg',
|
||||
duration: 30,
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(light: true, veg: false, carb: false, meat: false),
|
||||
details: 'Test diet description',
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Test details',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: '', authorName: '', authorProfileIcon: '', authorIsMe: false),
|
||||
totalLikes: 1,
|
||||
totalViews: 1,
|
||||
totalComments: 1,
|
||||
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(mockDietService.getDietBySlug(slug: slug)).thenAnswer((_) async => expected);
|
||||
when(mockService.getDietBySlug(slug: slug))
|
||||
.thenAnswer((_) async => expectedDiet);
|
||||
|
||||
final result = await dietRepository.getDietBySlug(slug);
|
||||
final result = await repository.getDietBySlug(slug);
|
||||
|
||||
expect(result, isA<Diet>());
|
||||
expect(result, equals(expectedDiet));
|
||||
expect(result.slug, slug);
|
||||
verify(mockDietService.getDietBySlug(slug: slug)).called(1);
|
||||
verify(mockService.getDietBySlug(slug: slug)).called(1);
|
||||
});
|
||||
|
||||
test('addToFavorite returns Diet', () async {
|
||||
const id = 1;
|
||||
const expected = Diet(
|
||||
id: id,
|
||||
test('addToFavorite - успешное добавление в избранное', () async {
|
||||
const dietId = 1;
|
||||
const expectedDiet = Diet(
|
||||
id: dietId,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: 'https://someurl.com/img.jpg',
|
||||
duration: 30,
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(light: true, veg: false, carb: false, meat: false),
|
||||
details: 'Test diet description',
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Test details',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: '', authorName: '', authorProfileIcon: '', authorIsMe: false),
|
||||
totalLikes: 1,
|
||||
totalViews: 1,
|
||||
totalComments: 1,
|
||||
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(mockDietService.addToFavorite(id: id)).thenAnswer((_) async => expected);
|
||||
when(mockService.addToFavorite(id: dietId))
|
||||
.thenAnswer((_) async => expectedDiet);
|
||||
|
||||
final result = await dietRepository.addToFavorite(id);
|
||||
final result = await repository.addToFavorite(dietId);
|
||||
|
||||
expect(result, isA<Diet>());
|
||||
expect(result.isFavorite, true);
|
||||
verify(mockDietService.addToFavorite(id: id)).called(1);
|
||||
expect(result, equals(expectedDiet));
|
||||
expect(result.isFavorite, isTrue);
|
||||
verify(mockService.addToFavorite(id: dietId)).called(1);
|
||||
});
|
||||
|
||||
test('removeFromFavorite returns Diet', () async {
|
||||
const id = 1;
|
||||
const expected = Diet(
|
||||
id: id,
|
||||
test('removeFromFavorite - успешное удаление из избранного', () async {
|
||||
const dietId = 1;
|
||||
const expectedDiet = Diet(
|
||||
id: dietId,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: 'https://someurl.com/img.jpg',
|
||||
duration: 30,
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(light: true, veg: false, carb: false, meat: false),
|
||||
details: 'Test diet description',
|
||||
type: DietType(veg: true, light: false, carb: false, meat: false),
|
||||
details: 'Test details',
|
||||
dailySchedule: {},
|
||||
author: Author(authorSlug: '', authorName: '', authorProfileIcon: '', authorIsMe: false),
|
||||
totalLikes: 1,
|
||||
totalViews: 1,
|
||||
totalComments: 1,
|
||||
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(mockDietService.removeFromFavorite(id: id)).thenAnswer((_) async => expected);
|
||||
when(mockService.removeFromFavorite(id: dietId))
|
||||
.thenAnswer((_) async => expectedDiet);
|
||||
|
||||
final result = await dietRepository.removeFromFavorite(id);
|
||||
final result = await repository.removeFromFavorite(dietId);
|
||||
|
||||
expect(result, isA<Diet>());
|
||||
expect(result.isFavorite, false);
|
||||
verify(mockDietService.removeFromFavorite(id: id)).called(1);
|
||||
expect(result, equals(expectedDiet));
|
||||
expect(result.isFavorite, isFalse);
|
||||
verify(mockService.removeFromFavorite(id: dietId)).called(1);
|
||||
});
|
||||
|
||||
test('startDiet starts diet with date', () async {
|
||||
const id = 1;
|
||||
test('startDiet - успешный старт диеты', () async {
|
||||
const dietId = 1;
|
||||
const date = '2024-01-01';
|
||||
|
||||
when(mockDietService.startDiet(id: id, body: {'date': date})).thenAnswer((_) async => Future.value());
|
||||
when(mockService.startDiet(id: dietId, body: {'date': date}))
|
||||
.thenAnswer((_) async => Future.value());
|
||||
|
||||
await dietRepository.startDiet(id, date);
|
||||
await repository.startDiet(dietId, date);
|
||||
|
||||
verify(mockDietService.startDiet(id: id, body: {'date': date})).called(1);
|
||||
verify(mockService.startDiet(id: dietId, body: {'date': date})).called(1);
|
||||
});
|
||||
|
||||
test('cancelDiet cancels diet by id', () async {
|
||||
const id = 1;
|
||||
test('cancelDiet - успешная отмена диеты', () async {
|
||||
const dietId = 1;
|
||||
|
||||
when(mockDietService.cancelDiet(id: id)).thenAnswer((_) async => Future.value());
|
||||
when(mockService.cancelDiet(id: dietId))
|
||||
.thenAnswer((_) async => Future.value());
|
||||
|
||||
await dietRepository.cancelDiet(id);
|
||||
await repository.cancelDiet(dietId);
|
||||
|
||||
verify(mockDietService.cancelDiet(id: id)).called(1);
|
||||
verify(mockService.cancelDiet(id: dietId)).called(1);
|
||||
});
|
||||
|
||||
test('deleteDiet deletes diet by id', () async {
|
||||
const id = 1;
|
||||
test('deleteDiet - успешное удаление диеты', () async {
|
||||
const dietId = 1;
|
||||
|
||||
when(mockDietService.deleteDiet(id: id)).thenAnswer((_) async => Future.value());
|
||||
when(mockService.deleteDiet(id: dietId))
|
||||
.thenAnswer((_) async => Future.value());
|
||||
|
||||
await dietRepository.deleteDiet(id);
|
||||
await repository.deleteDiet(dietId);
|
||||
|
||||
verify(mockDietService.deleteDiet(id: id)).called(1);
|
||||
verify(mockService.deleteDiet(id: dietId)).called(1);
|
||||
});
|
||||
|
||||
test('likeDiet rethrows exception on error', () async {
|
||||
const id = 1;
|
||||
test('emitLikesStateToFeed - отправляет состояние лайков', () async {
|
||||
const state = LikesState(
|
||||
wasLikedByMe: true,
|
||||
totalLikes: 10,
|
||||
index: 0,
|
||||
);
|
||||
|
||||
when(mockDietService.likeDiet(id: id)).thenThrow(Exception('Network error'));
|
||||
|
||||
expect(() => dietRepository.likeDiet(id), throwsException);
|
||||
verify(mockDietService.likeDiet(id: id)).called(1);
|
||||
expectLater(repository.dietLikeState, emits(state));
|
||||
repository.emitLikesStateToFeed(state);
|
||||
});
|
||||
|
||||
test('getDiet rethrows exception on error', () async {
|
||||
const id = 1;
|
||||
test('emitFavoriteStateToFeed - отправляет состояние избранного', () async {
|
||||
const state = FavoriteState(
|
||||
isFavorite: true,
|
||||
index: 0,
|
||||
);
|
||||
|
||||
when(mockDietService.getDiet(id: id)).thenThrow(Exception('Network error'));
|
||||
expectLater(repository.dietFavoriteState, emits(state));
|
||||
repository.emitFavoriteStateToFeed(state);
|
||||
});
|
||||
|
||||
expect(() => dietRepository.getDiet(id), throwsException);
|
||||
verify(mockDietService.getDiet(id: id)).called(1);
|
||||
test('emitCommentsStateToFeed - отправляет состояние комментариев', () async {
|
||||
const state = CommentsState(
|
||||
index: 0,
|
||||
totalComments: 5,
|
||||
);
|
||||
|
||||
expectLater(repository.dietCommentsState, emits(state));
|
||||
repository.emitCommentsStateToFeed(state);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user