Files
eatvio_app/test/features/complaining/presentation/cubit/complaining_cubit_test.dart

279 lines
8.9 KiB
Dart

import 'dart:async';
import 'package:eatvio_app/core/enums/complained_content.dart';
import 'package:eatvio_app/features/complaining/models/complain_model.dart';
import 'package:eatvio_app/features/complaining/presentation/complaining_bottom_sheet.dart';
import 'package:eatvio_app/features/complaining/presentation/cubit/complaining_cubit.dart';
import 'package:eatvio_app/features/complaining/presentation/cubit/complaining_state.dart';
import 'package:eatvio_app/features/complaining/repositories/complaining_repository.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
class MockComplainingRepository extends Mock implements ComplainingRepository {}
void main() {
group('ComplainingCubit', () {
late ComplainingCubit cubit;
late MockComplainingRepository mockRepository;
setUpAll(() {
registerFallbackValue(const ComplainModel());
});
setUp(() {
mockRepository = MockComplainingRepository();
cubit = ComplainingCubit(repository: mockRepository);
});
tearDown(() {
cubit.close();
});
test('начальное состояние - ComplainingState', () {
expect(cubit.state, isA<ComplainingState>());
});
test('setContentTypeAndIdentifier устанавливает тип и идентификатор', () {
cubit.setContentTypeAndIdentifier(
contentType: ComplainedContent.diet,
id: 1,
);
expect(cubit.state.contentType, ComplainedContent.diet);
expect(cubit.state.contentId, 1);
});
test('setContentTypeAndIdentifier устанавливает slug', () {
cubit.setContentTypeAndIdentifier(
contentType: ComplainedContent.plan,
slug: 'test-slug',
);
expect(cubit.state.contentType, ComplainedContent.plan);
expect(cubit.state.contentSlug, 'test-slug');
});
test('setCategory устанавливает категорию', () {
cubit.setCategory(ComplainType.spam);
expect(cubit.state.category, ComplainType.spam);
});
test('setTags устанавливает теги', () {
final tags = ['tag1', 'tag2'];
cubit.setTags(tags);
expect(cubit.state.complainInfo.tags, tags);
});
test('toggleDescription переключает описание', () {
// Включает описание
cubit.toggleDescription();
expect(cubit.state.complainInfo.description, '');
// Выключает описание
cubit.toggleDescription();
expect(cubit.state.complainInfo.description, null);
});
test('setDescription устанавливает описание', () {
const description = 'Test description';
cubit.setDescription(description);
expect(cubit.state.complainInfo.description, description);
});
test('complain - отправляет жалобу на diet', () async {
const id = 1;
when(() => mockRepository.sendDietComplain(
id: id,
category: any(named: 'category'),
body: any(named: 'body'),
)).thenAnswer((_) async => {});
cubit.setContentTypeAndIdentifier(contentType: ComplainedContent.diet, id: id);
cubit.setCategory(ComplainType.spam);
cubit.setTags(['tag1']);
await cubit.complain();
expect(cubit.state.wasSentSuccessfully, true);
expect(cubit.state.isLoading, false);
verify(() => mockRepository.sendDietComplain(
id: id,
category: any(named: 'category'),
body: any(named: 'body'),
)).called(1);
});
test('complain - ставит isLoading на время запроса', () async {
const id = 1;
final completer = Completer<void>();
when(() => mockRepository.sendDietComplain(
id: id,
category: any(named: 'category'),
body: any(named: 'body'),
)).thenAnswer((_) => completer.future);
cubit.setContentTypeAndIdentifier(contentType: ComplainedContent.diet, id: id);
cubit.setCategory(ComplainType.spam);
final future = cubit.complain();
expect(cubit.state.isLoading, true);
expect(cubit.state.wasSentSuccessfully, isNull);
completer.complete();
await future;
expect(cubit.state.isLoading, false);
expect(cubit.state.wasSentSuccessfully, true);
});
test('complain - возвращает false при ошибке', () async {
const id = 1;
when(() => mockRepository.sendDietComplain(
id: id,
category: any(named: 'category'),
body: any(named: 'body'),
)).thenThrow(Exception('Error'));
cubit.setContentTypeAndIdentifier(contentType: ComplainedContent.diet, id: id);
cubit.setCategory(ComplainType.spam);
await cubit.complain();
expect(cubit.state.wasSentSuccessfully, false);
expect(cubit.state.isLoading, false);
});
test('complain - отправляет жалобу на plan', () async {
const slug = 'test-plan-slug';
when(() => mockRepository.sendPlanComplain(
slug: slug,
category: any(named: 'category'),
body: any(named: 'body'),
)).thenAnswer((_) async => {});
cubit.setContentTypeAndIdentifier(contentType: ComplainedContent.plan, slug: slug);
cubit.setCategory(ComplainType.spam);
await cubit.complain();
expect(cubit.state.wasSentSuccessfully, true);
verify(() => mockRepository.sendPlanComplain(
slug: slug,
category: any(named: 'category'),
body: any(named: 'body'),
)).called(1);
});
test('complain - отправляет жалобу на recipe', () async {
const slug = 'test-recipe-slug';
when(() => mockRepository.sendRecipeComplain(
slug: slug,
category: any(named: 'category'),
body: any(named: 'body'),
)).thenAnswer((_) async => {});
cubit.setContentTypeAndIdentifier(contentType: ComplainedContent.recipe, slug: slug);
cubit.setCategory(ComplainType.spam);
await cubit.complain();
expect(cubit.state.wasSentSuccessfully, true);
verify(() => mockRepository.sendRecipeComplain(
slug: slug,
category: any(named: 'category'),
body: any(named: 'body'),
)).called(1);
});
test('complain - отправляет жалобу на profile', () async {
const slug = 'test-profile-slug';
when(() => mockRepository.sendProfileComplain(
slug: slug,
category: any(named: 'category'),
body: any(named: 'body'),
)).thenAnswer((_) async => {});
cubit.setContentTypeAndIdentifier(contentType: ComplainedContent.profile, slug: slug);
cubit.setCategory(ComplainType.spam);
await cubit.complain();
expect(cubit.state.wasSentSuccessfully, true);
verify(() => mockRepository.sendProfileComplain(
slug: slug,
category: any(named: 'category'),
body: any(named: 'body'),
)).called(1);
});
test('complain - отправляет жалобу на posts', () async {
const id = 1;
when(() => mockRepository.sendBlogPostsComplain(
id: id,
category: any(named: 'category'),
body: any(named: 'body'),
)).thenAnswer((_) async => {});
cubit.setContentTypeAndIdentifier(contentType: ComplainedContent.posts, id: id);
cubit.setCategory(ComplainType.spam);
await cubit.complain();
expect(cubit.state.wasSentSuccessfully, true);
verify(() => mockRepository.sendBlogPostsComplain(
id: id,
category: any(named: 'category'),
body: any(named: 'body'),
)).called(1);
});
test('complain - отправляет жалобу на comment', () async {
const id = 1;
when(() => mockRepository.sendCommentComplain(
id: id,
category: any(named: 'category'),
body: any(named: 'body'),
)).thenAnswer((_) async => {});
cubit.setContentTypeAndIdentifier(contentType: ComplainedContent.comment, id: id);
cubit.setCategory(ComplainType.spam);
await cubit.complain();
expect(cubit.state.wasSentSuccessfully, true);
verify(() => mockRepository.sendCommentComplain(
id: id,
category: any(named: 'category'),
body: any(named: 'body'),
)).called(1);
});
test('complain - возвращает false если category null', () async {
cubit.setContentTypeAndIdentifier(contentType: ComplainedContent.diet, id: 1);
await cubit.complain();
expect(cubit.state.wasSentSuccessfully, false);
});
test('complain - возвращает false если contentType null', () async {
cubit.setCategory(ComplainType.spam);
await cubit.complain();
expect(cubit.state.wasSentSuccessfully, false);
});
});
}