forked from eatvio/eatvio_app
feat(dependencies): add rxdart package and improve string extension methods for better handling of empty strings and gender parsing
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
import 'package:eatvio_app/core/models/pagination_model.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/feed/presentation/bloc/diets_cubit/diets_cubit.dart';
|
||||
import 'package:eatvio_app/features/feed/presentation/bloc/diets_cubit/diets_state.dart';
|
||||
import 'package:eatvio_app/features/feed/repositories/feed_repository.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
import 'diets_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([
|
||||
MockSpec<FeedRepository>(),
|
||||
MockSpec<DietRepository>(),
|
||||
])
|
||||
void main() {
|
||||
group('DietsCubit', () {
|
||||
DietsCubit? dietsCubit;
|
||||
late MockFeedRepository mockFeedRepository;
|
||||
late MockDietRepository mockDietRepository;
|
||||
|
||||
setUp(() {
|
||||
mockFeedRepository = MockFeedRepository();
|
||||
mockDietRepository = MockDietRepository();
|
||||
|
||||
// Мокируем streams для подписок
|
||||
when(mockDietRepository.dietLikeState).thenAnswer((_) => BehaviorSubject<LikesState>());
|
||||
when(mockDietRepository.dietFavoriteState).thenAnswer((_) => BehaviorSubject<FavoriteState>());
|
||||
when(mockDietRepository.dietCommentsState).thenAnswer((_) => BehaviorSubject<CommentsState>());
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (dietsCubit != null && !dietsCubit!.isClosed) {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
await dietsCubit!.close();
|
||||
dietsCubit = null;
|
||||
}
|
||||
});
|
||||
|
||||
test('начальное состояние - initial', () {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
expect(dietsCubit!.state, isA<DietsState>());
|
||||
expect(dietsCubit!.state.isLoading, isTrue);
|
||||
expect(dietsCubit!.state.dietsList, isEmpty);
|
||||
expect(dietsCubit!.state.errorMessage, isNull);
|
||||
});
|
||||
|
||||
test('changeTypeFilter - изменяет тип фильтра', () {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
dietsCubit!.changeTypeFilter(1);
|
||||
|
||||
expect(dietsCubit!.state.filterState.typeIndex, 1);
|
||||
});
|
||||
|
||||
test('changeSortingType - изменяет тип сортировки', () {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
dietsCubit!.changeSortingType(2);
|
||||
|
||||
expect(dietsCubit!.state.filterState.sortingIndex, 2);
|
||||
});
|
||||
|
||||
test('changeDurationFilter - изменяет фильтр продолжительности', () {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
dietsCubit!.changeDurationFilter(1);
|
||||
|
||||
expect(dietsCubit!.state.filterState.durationIndex, 1);
|
||||
});
|
||||
|
||||
test('loadDiets - успешная загрузка диет', () async {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
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 dietsPagination = DietsPagination(
|
||||
data: [testDiet],
|
||||
links: Links(
|
||||
first: 'https://example.com?page=1',
|
||||
last: 'https://example.com?page=1',
|
||||
prev: null,
|
||||
next: null,
|
||||
),
|
||||
meta: Meta(
|
||||
currentPage: 1,
|
||||
from: 1,
|
||||
lastPage: 1,
|
||||
links: [],
|
||||
perPage: 10,
|
||||
to: 1,
|
||||
total: 1,
|
||||
),
|
||||
);
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenAnswer((_) async => dietsPagination);
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
expect(dietsCubit!.state.dietsList.length, 1);
|
||||
expect(dietsCubit!.state.dietsList.first.id, 1);
|
||||
expect(dietsCubit!.state.errorMessage, isNull);
|
||||
});
|
||||
|
||||
test('loadDiets - обработка ошибки', () async {
|
||||
dietsCubit = DietsCubit(
|
||||
feedRepository: mockFeedRepository,
|
||||
dietsRepository: mockDietRepository,
|
||||
);
|
||||
|
||||
when(mockFeedRepository.getDiets(
|
||||
query: anyNamed('query'),
|
||||
types: anyNamed('types'),
|
||||
duration: anyNamed('duration'),
|
||||
sorting: anyNamed('sorting'),
|
||||
)).thenThrow(Exception('Ошибка загрузки'));
|
||||
|
||||
await dietsCubit!.loadDiets();
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
expect(dietsCubit!.state.errorMessage, isNotNull);
|
||||
expect(dietsCubit!.state.isLoading, isFalse);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/feed/presentation/bloc/diets_cubit/diets_state.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('DietsState', () {
|
||||
test('initial создает начальное состояние', () {
|
||||
final state = DietsState.initial();
|
||||
|
||||
expect(state.isLoading, isTrue);
|
||||
expect(state.dietsList, isEmpty);
|
||||
expect(state.errorMessage, isNull);
|
||||
expect(state.filterState.durationIndex, 0);
|
||||
expect(state.filterState.sortingIndex, 0);
|
||||
expect(state.filterState.typeIndex, 0);
|
||||
});
|
||||
|
||||
test('создает DietsState с данными', () {
|
||||
const diet = 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 state = DietsState(
|
||||
filterState: DietsFilterState(
|
||||
durationIndex: 1,
|
||||
sortingIndex: 1,
|
||||
typeIndex: 1,
|
||||
),
|
||||
dietsList: [diet],
|
||||
isLoading: false,
|
||||
errorMessage: null,
|
||||
);
|
||||
|
||||
expect(state.dietsList.length, 1);
|
||||
expect(state.isLoading, isFalse);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user