forked from eatvio/eatvio_app
feat(test): update oauth handling and improve preferences management
This commit is contained in:
154
test/core/views/cubit/views_cubit_test.dart
Normal file
154
test/core/views/cubit/views_cubit_test.dart
Normal file
@@ -0,0 +1,154 @@
|
||||
import 'package:eatvio_app/core/views/cubit/views_cubit.dart';
|
||||
import 'package:eatvio_app/core/views/cubit/views_state.dart';
|
||||
import 'package:eatvio_app/core/views/repository/views_repository.dart';
|
||||
import 'package:eatvio_app/features/feed/presentation/pages/feed_screen.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import 'views_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<ViewsRepository>()])
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('ViewsCubit', () {
|
||||
late ViewsCubit cubit;
|
||||
late MockViewsRepository mockViewsRepository;
|
||||
|
||||
setUp(() {
|
||||
mockViewsRepository = MockViewsRepository();
|
||||
cubit = ViewsCubit(viewsRepository: mockViewsRepository);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (!cubit.isClosed) {
|
||||
await cubit.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('начальное состояние - ViewsCubitState', () {
|
||||
expect(cubit.state, isA<ViewsCubitState>());
|
||||
});
|
||||
|
||||
test('handleVisibility - добавляет ID при visibleFraction >= 0.5', () async {
|
||||
when(mockViewsRepository.viewContent(viewModel: anyNamed('viewModel'))).thenAnswer((_) async => Future.value());
|
||||
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 1, type: ViewableContentType.posts);
|
||||
|
||||
// Ждем выполнения таймера (1 секунда)
|
||||
await Future.delayed(const Duration(milliseconds: 1100));
|
||||
|
||||
expect(cubit.postsIds, contains(1));
|
||||
});
|
||||
|
||||
test('handleVisibility - не добавляет ID при visibleFraction < 0.5', () async {
|
||||
cubit.handleVisibility(visibleFraction: 0.3, id: 1, type: ViewableContentType.posts);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(cubit.postsIds, isEmpty);
|
||||
});
|
||||
|
||||
test('handleVisibility - отменяет таймер при уменьшении visibleFraction', () async {
|
||||
when(mockViewsRepository.viewContent(viewModel: anyNamed('viewModel'))).thenAnswer((_) async => Future.value());
|
||||
|
||||
// Сначала видимость >= 0.5
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 1, type: ViewableContentType.posts);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
|
||||
// Затем видимость < 0.5
|
||||
cubit.handleVisibility(visibleFraction: 0.3, id: 1, type: ViewableContentType.posts);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 700));
|
||||
|
||||
// ID не должен быть добавлен, так как таймер был отменен
|
||||
expect(cubit.postsIds, isEmpty);
|
||||
});
|
||||
|
||||
test('handleVisibility - не добавляет уже отправленный ID', () async {
|
||||
when(mockViewsRepository.viewContent(viewModel: anyNamed('viewModel'))).thenAnswer((_) async => Future.value());
|
||||
|
||||
// Добавляем и отправляем ID
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 1, type: ViewableContentType.posts);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 1100));
|
||||
await Future.delayed(const Duration(milliseconds: 4100)); // Ждем debounce и отправку
|
||||
|
||||
// Пытаемся добавить тот же ID снова
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 1, type: ViewableContentType.posts);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
// Таймер не должен быть создан, так как ID уже отправлен
|
||||
expect(cubit.postsIds, isEmpty);
|
||||
});
|
||||
|
||||
test('handleVisibility - работает для разных типов контента', () async {
|
||||
when(mockViewsRepository.viewContent(viewModel: anyNamed('viewModel'))).thenAnswer((_) async => Future.value());
|
||||
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 1, type: ViewableContentType.posts);
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 2, type: ViewableContentType.plans);
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 3, type: ViewableContentType.diets);
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 4, type: ViewableContentType.recipes);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 1100));
|
||||
|
||||
expect(cubit.postsIds, contains(1));
|
||||
expect(cubit.plansIds, contains(2));
|
||||
expect(cubit.dietsIds, contains(3));
|
||||
expect(cubit.recipesIds, contains(4));
|
||||
});
|
||||
|
||||
test('_sendViews - отправляет просмотры через repository', () async {
|
||||
when(mockViewsRepository.viewContent(viewModel: anyNamed('viewModel'))).thenAnswer((_) async => Future.value());
|
||||
|
||||
// Добавляем ID для разных типов
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 1, type: ViewableContentType.posts);
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 2, type: ViewableContentType.plans);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 1100));
|
||||
await Future.delayed(const Duration(milliseconds: 4100)); // Ждем debounce и отправку
|
||||
|
||||
// Проверяем, что repository был вызван минимум 2 раза (для posts и plans)
|
||||
verify(mockViewsRepository.viewContent(viewModel: anyNamed('viewModel'))).called(greaterThanOrEqualTo(2));
|
||||
});
|
||||
|
||||
test('close - отправляет оставшиеся просмотры и освобождает ресурсы', () async {
|
||||
when(mockViewsRepository.viewContent(viewModel: anyNamed('viewModel'))).thenAnswer((_) async => Future.value());
|
||||
|
||||
// Добавляем ID
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 1, type: ViewableContentType.posts);
|
||||
await Future.delayed(const Duration(milliseconds: 1100));
|
||||
|
||||
// Очищаем предыдущие взаимодействия перед проверкой
|
||||
clearInteractions(mockViewsRepository);
|
||||
|
||||
// Закрываем cubit
|
||||
await cubit.close();
|
||||
|
||||
// Проверяем, что repository был вызван для отправки оставшихся просмотров
|
||||
verify(mockViewsRepository.viewContent(viewModel: anyNamed('viewModel'))).called(greaterThanOrEqualTo(1));
|
||||
expect(cubit.isClosed, isTrue);
|
||||
});
|
||||
|
||||
test('_sendViews - не отправляет уже отправленные ID', () async {
|
||||
when(mockViewsRepository.viewContent(viewModel: anyNamed('viewModel'))).thenAnswer((_) async => Future.value());
|
||||
|
||||
// Добавляем и отправляем ID
|
||||
cubit.handleVisibility(visibleFraction: 0.6, id: 1, type: ViewableContentType.posts);
|
||||
await Future.delayed(const Duration(milliseconds: 1100));
|
||||
await Future.delayed(const Duration(milliseconds: 4100));
|
||||
|
||||
// Очищаем моки
|
||||
clearInteractions(mockViewsRepository);
|
||||
|
||||
// Закрываем cubit
|
||||
await cubit.close();
|
||||
|
||||
// Repository не должен быть вызван, так как все ID уже отправлены
|
||||
verifyNever(mockViewsRepository.viewContent(viewModel: anyNamed('viewModel')));
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user