feat(dependencies): add rxdart package and improve string extension methods for better handling of empty strings and gender parsing

This commit is contained in:
mso7nueh
2025-12-04 15:45:54 +03:00
parent 0cd8e662d9
commit 953230b387
100 changed files with 7202 additions and 14 deletions

View 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);
});
});
}

View File

@@ -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);
});
});
}

View File

@@ -0,0 +1,256 @@
import 'package:eatvio_app/features/diets/models/diet_likes_model.dart';
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
import 'package:eatvio_app/features/diets/services/diet_service.dart';
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'diet_repository_test.mocks.dart';
@GenerateNiceMocks([MockSpec<DietService>()])
void main() {
group('DietRepository', () {
late DietRepository repository;
late MockDietService mockService;
setUp(() {
mockService = MockDietService();
repository = DietRepository(dietsService: mockService);
});
test('likeDiet - успешный лайк', () async {
const dietId = 1;
const expectedLikes = DietLikesModel(
totalLikes: 10,
isLiked: true,
);
when(mockService.likeDiet(id: dietId))
.thenAnswer((_) async => expectedLikes);
final result = await repository.likeDiet(dietId);
expect(result, equals(expectedLikes));
verify(mockService.likeDiet(id: dietId)).called(1);
});
test('getDiet - успешное получение диеты по id', () async {
const dietId = 1;
const expectedDiet = Diet(
id: dietId,
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(mockService.getDiet(id: dietId))
.thenAnswer((_) async => expectedDiet);
final result = await repository.getDiet(dietId);
expect(result, equals(expectedDiet));
expect(result.id, dietId);
verify(mockService.getDiet(id: dietId)).called(1);
});
test('getDietBySlug - успешное получение диеты по slug', () async {
const slug = 'test-diet';
const expectedDiet = Diet(
id: 1,
slug: slug,
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(mockService.getDietBySlug(slug: slug))
.thenAnswer((_) async => expectedDiet);
final result = await repository.getDietBySlug(slug);
expect(result, equals(expectedDiet));
expect(result.slug, slug);
verify(mockService.getDietBySlug(slug: slug)).called(1);
});
test('addToFavorite - успешное добавление в избранное', () async {
const dietId = 1;
const expectedDiet = Diet(
id: dietId,
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(mockService.addToFavorite(id: dietId))
.thenAnswer((_) async => expectedDiet);
final result = await repository.addToFavorite(dietId);
expect(result, equals(expectedDiet));
expect(result.isFavorite, isTrue);
verify(mockService.addToFavorite(id: dietId)).called(1);
});
test('removeFromFavorite - успешное удаление из избранного', () async {
const dietId = 1;
const expectedDiet = Diet(
id: dietId,
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(mockService.removeFromFavorite(id: dietId))
.thenAnswer((_) async => expectedDiet);
final result = await repository.removeFromFavorite(dietId);
expect(result, equals(expectedDiet));
expect(result.isFavorite, isFalse);
verify(mockService.removeFromFavorite(id: dietId)).called(1);
});
test('startDiet - успешный старт диеты', () async {
const dietId = 1;
const date = '2024-01-01';
when(mockService.startDiet(id: dietId, body: {'date': date}))
.thenAnswer((_) async => Future.value());
await repository.startDiet(dietId, date);
verify(mockService.startDiet(id: dietId, body: {'date': date})).called(1);
});
test('cancelDiet - успешная отмена диеты', () async {
const dietId = 1;
when(mockService.cancelDiet(id: dietId))
.thenAnswer((_) async => Future.value());
await repository.cancelDiet(dietId);
verify(mockService.cancelDiet(id: dietId)).called(1);
});
test('deleteDiet - успешное удаление диеты', () async {
const dietId = 1;
when(mockService.deleteDiet(id: dietId))
.thenAnswer((_) async => Future.value());
await repository.deleteDiet(dietId);
verify(mockService.deleteDiet(id: dietId)).called(1);
});
test('emitLikesStateToFeed - отправляет состояние лайков', () async {
const state = LikesState(
wasLikedByMe: true,
totalLikes: 10,
index: 0,
);
expectLater(repository.dietLikeState, emits(state));
repository.emitLikesStateToFeed(state);
});
test('emitFavoriteStateToFeed - отправляет состояние избранного', () async {
const state = FavoriteState(
isFavorite: true,
index: 0,
);
expectLater(repository.dietFavoriteState, emits(state));
repository.emitFavoriteStateToFeed(state);
});
test('emitCommentsStateToFeed - отправляет состояние комментариев', () async {
const state = CommentsState(
index: 0,
totalComments: 5,
);
expectLater(repository.dietCommentsState, emits(state));
repository.emitCommentsStateToFeed(state);
});
});
}