chore(version): update app version to 1.3.5+85 and improve test coverage reporting in CI

This commit is contained in:
mso7nueh
2025-12-08 11:19:08 +03:00
parent abe4985f0e
commit c2a506cdbf
148 changed files with 12395 additions and 1889 deletions

View File

@@ -6,414 +6,150 @@ import 'package:eatvio_app/features/feed/models/comment_model.dart';
import 'package:eatvio_app/features/feed/presentation/widgets/comments_widget.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:mocktail/mocktail.dart';
import 'comments_cubit_test.mocks.dart';
class MockFeedRepository extends Mock implements FeedRepository {}
class MockCommentsRepository extends Mock implements CommentsRepository {}
@GenerateNiceMocks([
MockSpec<FeedRepository>(),
MockSpec<CommentsRepository>(),
])
void main() {
group('CommentsCubit', () {
CommentsCubit? commentsCubit;
late CommentsCubit cubit;
late MockFeedRepository mockFeedRepository;
late MockCommentsRepository mockCommentsRepository;
late CommentModel testCommentModel;
setUp(() {
mockFeedRepository = MockFeedRepository();
mockCommentsRepository = MockCommentsRepository();
testCommentModel = const CommentModel(
data: [],
links: Links(first: '', last: '', prev: null, next: null),
meta: Meta(
currentPage: 1,
from: null,
lastPage: 1,
links: [],
path: null,
perPage: 10,
to: null,
total: 0,
totalMinPrice: null,
totalMaxPrice: null,
),
cubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
});
tearDown(() async {
if (commentsCubit != null && !commentsCubit!.isClosed) {
await Future.delayed(const Duration(milliseconds: 100));
commentsCubit!.close();
commentsCubit = null;
}
tearDown(() {
cubit.close();
});
test('начальное состояние - CommentsState', () {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
expect(commentsCubit!.state, const CommentsState());
expect(cubit.state, isA<CommentsState>());
});
test('getComments - успешная загрузка комментариев для blogPost', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
const id = '1';
const mockComment = CommentModel(
data: [],
links: Links(first: '', last: '', prev: null, next: null),
meta: Meta(currentPage: 1, from: null, lastPage: 1, links: [], perPage: 10, to: null, total: 0),
);
const postId = '1';
when(mockFeedRepository.getBlogPostComments(id: postId))
.thenAnswer((_) async => testCommentModel);
when(() => mockFeedRepository.getBlogPostComments(id: id))
.thenAnswer((_) async => mockComment);
await commentsCubit!.getComments(postId, CommentSourceType.blogPost, true);
await cubit.getComments(id, CommentSourceType.blogPost, true);
await Future.delayed(const Duration(milliseconds: 100));
expect(commentsCubit!.state, isA<CommentsStateLoaded>());
final loadedState = commentsCubit!.state as CommentsStateLoaded;
expect(loadedState.comment, testCommentModel);
verify(mockFeedRepository.getBlogPostComments(id: postId)).called(1);
});
test('getComments - успешная загрузка комментариев для diet', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
const dietId = '1';
when(mockFeedRepository.getDietsComments(id: dietId))
.thenAnswer((_) async => testCommentModel);
await commentsCubit!.getComments(dietId, CommentSourceType.diet, true);
await Future.delayed(const Duration(milliseconds: 100));
expect(commentsCubit!.state, isA<CommentsStateLoaded>());
verify(mockFeedRepository.getDietsComments(id: dietId)).called(1);
});
test('getComments - успешная загрузка комментариев для recipe', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
const recipeId = 'recipe-slug';
when(mockFeedRepository.getRecipesComments(id: recipeId))
.thenAnswer((_) async => testCommentModel);
await commentsCubit!.getComments(recipeId, CommentSourceType.recipe, true);
await Future.delayed(const Duration(milliseconds: 100));
expect(commentsCubit!.state, isA<CommentsStateLoaded>());
verify(mockFeedRepository.getRecipesComments(id: recipeId)).called(1);
});
test('getComments - успешная загрузка комментариев для plan', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
const planSlug = 'plan-slug';
when(mockFeedRepository.getPlansComments(slug: planSlug))
.thenAnswer((_) async => testCommentModel);
await commentsCubit!.getComments(planSlug, CommentSourceType.plan, true);
await Future.delayed(const Duration(milliseconds: 100));
expect(commentsCubit!.state, isA<CommentsStateLoaded>());
verify(mockFeedRepository.getPlansComments(slug: planSlug)).called(1);
});
test('getComments - переход в состояние loading при первом запросе', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
const postId = '1';
when(mockFeedRepository.getBlogPostComments(id: postId))
.thenAnswer((_) async {
await Future.delayed(const Duration(milliseconds: 50));
return testCommentModel;
});
final future = commentsCubit!.getComments(postId, CommentSourceType.blogPost, true);
await Future.delayed(const Duration(milliseconds: 10));
expect(commentsCubit!.state, const CommentsState.loading());
await future;
expect(cubit.state, isA<CommentsStateLoaded>());
final loadedState = cubit.state as CommentsStateLoaded;
expect(loadedState.comment, mockComment);
});
test('getComments - обработка ошибки', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
const postId = '1';
const id = '1';
when(mockFeedRepository.getBlogPostComments(id: postId))
.thenThrow(Exception('Ошибка загрузки'));
when(() => mockFeedRepository.getBlogPostComments(id: id))
.thenThrow(Exception('Error'));
await commentsCubit!.getComments(postId, CommentSourceType.blogPost, true);
await cubit.getComments(id, CommentSourceType.blogPost, true);
await Future.delayed(const Duration(milliseconds: 100));
expect(commentsCubit!.state, isA<CommentsStateError>());
final errorState = commentsCubit!.state as CommentsStateError;
expect(errorState.errorMessage, contains('Exception'));
expect(cubit.state, isA<CommentsStateError>());
final errorState = cubit.state as CommentsStateError;
expect(errorState.errorMessage, contains('Error'));
});
test('sendComment - успешная отправка комментария для blogPost', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
test('sendComment - отправка комментария для blogPost', () async {
const id = '1';
const commentText = 'Test comment';
const mockComment = CommentModel(
data: [],
links: Links(first: '', last: '', prev: null, next: null),
meta: Meta(currentPage: 1, from: null, lastPage: 1, links: [], perPage: 10, to: null, total: 0),
);
const postId = '1';
const commentText = 'Тестовый комментарий';
// Устанавливаем начальное состояние
commentsCubit!.emit(CommentsState.loaded(comment: testCommentModel));
when(() => mockFeedRepository.sendBlogPostComment(id: id, commentText: commentText, parentCommentId: null))
.thenAnswer((_) async => {});
when(() => mockFeedRepository.getBlogPostComments(id: id))
.thenAnswer((_) async => mockComment);
when(mockFeedRepository.sendBlogPostComment(
id: postId,
commentText: commentText,
parentCommentId: null,
)).thenAnswer((_) async => Future.value());
when(mockFeedRepository.getBlogPostComments(id: postId))
.thenAnswer((_) async => testCommentModel);
await commentsCubit!.sendComment(
id: postId,
await cubit.sendComment(
id: id,
commentText: commentText,
commentSourceType: CommentSourceType.blogPost,
);
verify(mockFeedRepository.sendBlogPostComment(
id: postId,
commentText: commentText,
parentCommentId: null,
)).called(1);
verify(mockFeedRepository.getBlogPostComments(id: postId)).called(1);
verify(() => mockFeedRepository.sendBlogPostComment(id: id, commentText: commentText, parentCommentId: null)).called(1);
verify(() => mockFeedRepository.getBlogPostComments(id: id)).called(1);
});
test('sendComment - обработка ошибки при отправке', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
test('deleteComment - успешное удаление', () async {
final comment = Comment(
id: 1,
authorName: 'Test',
authorSlug: 'test',
authorProfileIcon: '',
body: 'Test',
childrenComments: [],
createdAt: DateTime.now(),
);
const postId = '1';
const commentText = 'Тестовый комментарий';
commentsCubit!.emit(CommentsState.loaded(comment: testCommentModel));
when(mockFeedRepository.sendBlogPostComment(
id: postId,
commentText: commentText,
parentCommentId: null,
)).thenThrow(Exception('Ошибка отправки'));
await commentsCubit!.sendComment(
id: postId,
commentText: commentText,
commentSourceType: CommentSourceType.blogPost,
final mockComment = CommentModel(
data: [comment],
links: const Links(first: '', last: '', prev: null, next: null),
meta: const Meta(currentPage: 1, from: null, lastPage: 1, links: [], perPage: 10, to: null, total: 0),
);
await Future.delayed(const Duration(milliseconds: 100));
expect(commentsCubit!.state, isA<CommentsStateError>());
});
// Сначала загружаем комментарии
when(() => mockFeedRepository.getBlogPostComments(id: any(named: 'id')))
.thenAnswer((_) async => mockComment);
await cubit.getComments('1', CommentSourceType.blogPost);
test('deleteComment - успешное удаление комментария', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
const commentId = 1;
// Затем удаляем
when(() => mockCommentsRepository.deleteComment(id: 1))
.thenAnswer((_) async => {});
final commentWithData = testCommentModel.copyWith(
data: [
Comment(
id: commentId,
body: 'Комментарий для удаления',
authorName: 'Автор',
authorSlug: 'author-slug',
authorProfileIcon: 'https://example.com/photo.jpg',
createdAt: DateTime.now(),
childrenComments: [],
),
],
);
commentsCubit!.emit(CommentsState.loaded(comment: commentWithData));
when(mockCommentsRepository.deleteComment(id: commentId))
.thenAnswer((_) async => Future.value());
final result = await commentsCubit!.deleteComment(id: commentId);
final result = await cubit.deleteComment(id: 1);
expect(result, true);
expect(commentsCubit!.state, isA<CommentsStateLoaded>());
final loadedState = commentsCubit!.state as CommentsStateLoaded;
expect(loadedState.comment.data, isEmpty);
verify(mockCommentsRepository.deleteComment(id: commentId)).called(1);
verify(() => mockCommentsRepository.deleteComment(id: 1)).called(1);
});
test('deleteComment - возвращает false если состояние не loaded', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
test('updateComment - успешное обновление', () async {
final comment = Comment(
id: 1,
authorName: 'Test',
authorSlug: 'test',
authorProfileIcon: '',
body: 'Old text',
childrenComments: [],
createdAt: DateTime.now(),
);
const commentId = 1;
commentsCubit!.emit(const CommentsState());
final result = await commentsCubit!.deleteComment(id: commentId);
expect(result, false);
verifyNever(mockCommentsRepository.deleteComment(id: commentId));
});
test('deleteComment - обработка ошибки при удалении', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
const commentId = 1;
final commentWithData = testCommentModel.copyWith(
data: [
Comment(
id: commentId,
body: 'Комментарий',
authorName: 'Автор',
authorSlug: 'author-slug',
authorProfileIcon: 'https://example.com/photo.jpg',
createdAt: DateTime.now(),
childrenComments: [],
),
],
final mockComment = CommentModel(
data: [comment],
links: const Links(first: '', last: '', prev: null, next: null),
meta: const Meta(currentPage: 1, from: null, lastPage: 1, links: [], perPage: 10, to: null, total: 0),
);
commentsCubit!.emit(CommentsState.loaded(comment: commentWithData));
// Сначала загружаем комментарии
when(() => mockFeedRepository.getBlogPostComments(id: any(named: 'id')))
.thenAnswer((_) async => mockComment);
await cubit.getComments('1', CommentSourceType.blogPost);
when(mockCommentsRepository.deleteComment(id: commentId))
.thenThrow(Exception('Ошибка удаления'));
// Затем обновляем
when(() => mockCommentsRepository.updateComment(id: 1, body: 'New text'))
.thenAnswer((_) async => {});
final result = await commentsCubit!.deleteComment(id: commentId);
expect(result, false);
// Состояние должно вернуться к исходному
final loadedState = commentsCubit!.state as CommentsStateLoaded;
expect(loadedState.comment.data.length, 1);
});
test('updateComment - успешное обновление комментария', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
const commentId = 1;
const newBody = 'Обновлённый комментарий';
final commentWithData = testCommentModel.copyWith(
data: [
Comment(
id: commentId,
body: 'Старый комментарий',
authorName: 'Автор',
authorSlug: 'author-slug',
authorProfileIcon: 'https://example.com/photo.jpg',
createdAt: DateTime.now(),
childrenComments: [],
),
],
);
commentsCubit!.emit(CommentsState.loaded(comment: commentWithData));
when(mockCommentsRepository.updateComment(id: commentId, body: newBody))
.thenAnswer((_) async => Future.value());
final result = await commentsCubit!.updateComment(id: commentId, body: newBody);
final result = await cubit.updateComment(id: 1, body: 'New text');
expect(result, true);
expect(commentsCubit!.state, isA<CommentsStateLoaded>());
final loadedState = commentsCubit!.state as CommentsStateLoaded;
expect(loadedState.comment.data.first.body, newBody);
verify(mockCommentsRepository.updateComment(id: commentId, body: newBody)).called(1);
});
test('updateComment - обработка ошибки при обновлении', () async {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
const commentId = 1;
const newBody = 'Обновлённый комментарий';
const oldBody = 'Старый комментарий';
final commentWithData = testCommentModel.copyWith(
data: [
Comment(
id: commentId,
body: oldBody,
authorName: 'Автор',
authorSlug: 'author-slug',
authorProfileIcon: 'https://example.com/photo.jpg',
createdAt: DateTime.now(),
childrenComments: [],
),
],
);
commentsCubit!.emit(CommentsState.loaded(comment: commentWithData));
when(mockCommentsRepository.updateComment(id: commentId, body: newBody))
.thenThrow(Exception('Ошибка обновления'));
final result = await commentsCubit!.updateComment(id: commentId, body: newBody);
expect(result, false);
// Состояние должно вернуться к исходному
final loadedState = commentsCubit!.state as CommentsStateLoaded;
expect(loadedState.comment.data.first.body, oldBody);
});
test('setReplyId - устанавливает ID ответа', () {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
const replyId = 5;
commentsCubit!.emit(CommentsState.loaded(comment: testCommentModel));
commentsCubit!.setReplyId(replyId);
expect(commentsCubit!.state, isA<CommentsStateLoaded>());
final loadedState = commentsCubit!.state as CommentsStateLoaded;
expect(loadedState.replyId, replyId);
});
test('setReplyId - сбрасывает ID ответа', () {
commentsCubit = CommentsCubit(
feedRepository: mockFeedRepository,
commentsRepository: mockCommentsRepository,
);
commentsCubit!.emit(CommentsState.loaded(comment: testCommentModel, replyId: 5));
commentsCubit!.setReplyId(null);
expect(commentsCubit!.state, isA<CommentsStateLoaded>());
final loadedState = commentsCubit!.state as CommentsStateLoaded;
expect(loadedState.replyId, isNull);
verify(() => mockCommentsRepository.updateComment(id: 1, body: 'New text')).called(1);
});
});
}

View File

@@ -0,0 +1,100 @@
import 'package:eatvio_app/core/comments/comments_cubit/comments_state.dart';
import 'package:eatvio_app/core/models/pagination_model.dart';
import 'package:eatvio_app/features/feed/models/comment_model.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('CommentsState', () {
test('creates initial state', () {
const state = CommentsState();
expect(state, isA<CommentsState>());
});
test('creates loading state', () {
const state = CommentsState.loading();
expect(state, isA<CommentsStateLoading>());
});
test('creates loaded state with comment', () {
final comment = CommentModel(
data: [],
links: Links(first: '', last: '', prev: null, next: null),
meta: Meta(
currentPage: 1,
from: null,
lastPage: 1,
links: [],
path: null,
perPage: 20,
to: null,
total: 0,
),
);
final state = CommentsState.loaded(comment: comment);
expect(state, isA<CommentsStateLoaded>());
expect((state as CommentsStateLoaded).comment, comment);
});
test('creates loaded state with replyId', () {
final comment = CommentModel(
data: [],
links: Links(first: '', last: '', prev: null, next: null),
meta: Meta(
currentPage: 1,
from: null,
lastPage: 1,
links: [],
path: null,
perPage: 20,
to: null,
total: 0,
),
);
final state = CommentsState.loaded(
comment: comment,
replyId: 123,
);
expect((state as CommentsStateLoaded).replyId, 123);
});
test('creates loaded state with editedCommentId', () {
final comment = CommentModel(
data: [],
links: Links(first: '', last: '', prev: null, next: null),
meta: Meta(
currentPage: 1,
from: null,
lastPage: 1,
links: [],
path: null,
perPage: 20,
to: null,
total: 0,
),
);
final state = CommentsState.loaded(
comment: comment,
editedCommentId: 456,
);
expect((state as CommentsStateLoaded).editedCommentId, 456);
});
test('creates error state', () {
const state = CommentsState.error(
errorMessage: 'Test error',
);
expect(state, isA<CommentsStateError>());
expect((state as CommentsStateError).errorMessage, 'Test error');
});
});
}