forked from eatvio/eatvio_app
test(tests): add comprehensive unit tests for various models and repositories, enhancing test coverage and ensuring correct functionality
This commit is contained in:
@@ -151,5 +151,293 @@ void main() {
|
||||
expect(result, true);
|
||||
verify(() => mockCommentsRepository.updateComment(id: 1, body: 'New text')).called(1);
|
||||
});
|
||||
|
||||
test('setReplyId - устанавливает replyId', () async {
|
||||
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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.getBlogPostComments(id: any(named: 'id')))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
await cubit.getComments('1', CommentSourceType.blogPost);
|
||||
|
||||
cubit.setReplyId(123);
|
||||
|
||||
final state = cubit.state as CommentsStateLoaded;
|
||||
expect(state.replyId, 123);
|
||||
});
|
||||
|
||||
test('setReplyId - устанавливает null replyId', () async {
|
||||
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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.getBlogPostComments(id: any(named: 'id')))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
await cubit.getComments('1', CommentSourceType.blogPost);
|
||||
|
||||
cubit.setReplyId(null);
|
||||
|
||||
final state = cubit.state as CommentsStateLoaded;
|
||||
expect(state.replyId, null);
|
||||
});
|
||||
|
||||
test('getComments - успешная загрузка комментариев для diet', () async {
|
||||
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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.getDietsComments(id: id))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
|
||||
await cubit.getComments(id, CommentSourceType.diet, true);
|
||||
|
||||
expect(cubit.state, isA<CommentsStateLoaded>());
|
||||
final loadedState = cubit.state as CommentsStateLoaded;
|
||||
expect(loadedState.comment, mockComment);
|
||||
});
|
||||
|
||||
test('getComments - успешная загрузка комментариев для recipe', () async {
|
||||
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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.getRecipesComments(id: id))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
|
||||
await cubit.getComments(id, CommentSourceType.recipe, true);
|
||||
|
||||
expect(cubit.state, isA<CommentsStateLoaded>());
|
||||
final loadedState = cubit.state as CommentsStateLoaded;
|
||||
expect(loadedState.comment, mockComment);
|
||||
});
|
||||
|
||||
test('getComments - успешная загрузка комментариев для plan', () async {
|
||||
const id = 'plan-slug';
|
||||
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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.getPlansComments(slug: id))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
|
||||
await cubit.getComments(id, CommentSourceType.plan, true);
|
||||
|
||||
expect(cubit.state, isA<CommentsStateLoaded>());
|
||||
final loadedState = cubit.state as CommentsStateLoaded;
|
||||
expect(loadedState.comment, mockComment);
|
||||
});
|
||||
|
||||
test('getComments - без isFirstFetch не показывает loading', () async {
|
||||
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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.getBlogPostComments(id: id))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
|
||||
await cubit.getComments(id, CommentSourceType.blogPost, false);
|
||||
|
||||
expect(cubit.state, isA<CommentsStateLoaded>());
|
||||
expect(cubit.state, isNot(isA<CommentsStateLoading>()));
|
||||
});
|
||||
|
||||
test('sendComment - отправка комментария для diet', () 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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.sendDietsComment(id: id, commentText: commentText, parentCommentId: null))
|
||||
.thenAnswer((_) async => {});
|
||||
when(() => mockFeedRepository.getDietsComments(id: id))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
|
||||
await cubit.sendComment(
|
||||
id: id,
|
||||
commentText: commentText,
|
||||
commentSourceType: CommentSourceType.diet,
|
||||
);
|
||||
|
||||
verify(() => mockFeedRepository.sendDietsComment(id: id, commentText: commentText, parentCommentId: null)).called(1);
|
||||
verify(() => mockFeedRepository.getDietsComments(id: id)).called(1);
|
||||
});
|
||||
|
||||
test('sendComment - отправка комментария для recipe', () 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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.sendRecipesComment(id: id, commentText: commentText, parentCommentId: null))
|
||||
.thenAnswer((_) async => {});
|
||||
when(() => mockFeedRepository.getRecipesComments(id: id))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
|
||||
await cubit.sendComment(
|
||||
id: id,
|
||||
commentText: commentText,
|
||||
commentSourceType: CommentSourceType.recipe,
|
||||
);
|
||||
|
||||
verify(() => mockFeedRepository.sendRecipesComment(id: id, commentText: commentText, parentCommentId: null)).called(1);
|
||||
verify(() => mockFeedRepository.getRecipesComments(id: id)).called(1);
|
||||
});
|
||||
|
||||
test('sendComment - отправка комментария для plan', () async {
|
||||
const id = 'plan-slug';
|
||||
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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.sendPlansComment(slug: id, commentText: commentText, parentCommentId: null))
|
||||
.thenAnswer((_) async => {});
|
||||
when(() => mockFeedRepository.getPlansComments(slug: id))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
|
||||
await cubit.sendComment(
|
||||
id: id,
|
||||
commentText: commentText,
|
||||
commentSourceType: CommentSourceType.plan,
|
||||
);
|
||||
|
||||
verify(() => mockFeedRepository.sendPlansComment(slug: id, commentText: commentText, parentCommentId: null)).called(1);
|
||||
verify(() => mockFeedRepository.getPlansComments(slug: id)).called(1);
|
||||
});
|
||||
|
||||
test('sendComment - отправка комментария с parentCommentId', () async {
|
||||
const id = '1';
|
||||
const commentText = 'Reply comment';
|
||||
const parentCommentId = 5;
|
||||
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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.sendBlogPostComment(id: id, commentText: commentText, parentCommentId: parentCommentId))
|
||||
.thenAnswer((_) async => {});
|
||||
when(() => mockFeedRepository.getBlogPostComments(id: id))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
|
||||
await cubit.sendComment(
|
||||
id: id,
|
||||
commentText: commentText,
|
||||
commentSourceType: CommentSourceType.blogPost,
|
||||
parentCommentId: parentCommentId,
|
||||
);
|
||||
|
||||
verify(() => mockFeedRepository.sendBlogPostComment(id: id, commentText: commentText, parentCommentId: parentCommentId)).called(1);
|
||||
});
|
||||
|
||||
test('sendComment - обработка ошибки', () async {
|
||||
const id = '1';
|
||||
const commentText = 'Test comment';
|
||||
|
||||
when(() => mockFeedRepository.sendBlogPostComment(id: id, commentText: commentText, parentCommentId: null))
|
||||
.thenThrow(Exception('Error'));
|
||||
|
||||
await cubit.sendComment(
|
||||
id: id,
|
||||
commentText: commentText,
|
||||
commentSourceType: CommentSourceType.blogPost,
|
||||
);
|
||||
|
||||
expect(cubit.state, isA<CommentsStateError>());
|
||||
final errorState = cubit.state as CommentsStateError;
|
||||
expect(errorState.errorMessage, contains('Error'));
|
||||
});
|
||||
|
||||
test('deleteComment - возвращает false если state не loaded', () async {
|
||||
// Начальное состояние не loaded
|
||||
final result = await cubit.deleteComment(id: 1);
|
||||
expect(result, false);
|
||||
});
|
||||
|
||||
test('deleteComment - обработка ошибки при удалении', () async {
|
||||
final comment = Comment(
|
||||
id: 1,
|
||||
authorName: 'Test',
|
||||
authorSlug: 'test',
|
||||
authorProfileIcon: '',
|
||||
body: 'Test',
|
||||
childrenComments: [],
|
||||
createdAt: DateTime.now(),
|
||||
);
|
||||
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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.getBlogPostComments(id: any(named: 'id')))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
await cubit.getComments('1', CommentSourceType.blogPost);
|
||||
|
||||
when(() => mockCommentsRepository.deleteComment(id: 1))
|
||||
.thenThrow(Exception('Delete error'));
|
||||
|
||||
final result = await cubit.deleteComment(id: 1);
|
||||
|
||||
expect(result, false);
|
||||
// Состояние должно вернуться к исходному
|
||||
final state = cubit.state as CommentsStateLoaded;
|
||||
expect(state.comment.data.length, 1);
|
||||
});
|
||||
|
||||
test('updateComment - обработка ошибки при обновлении', () async {
|
||||
final comment = Comment(
|
||||
id: 1,
|
||||
authorName: 'Test',
|
||||
authorSlug: 'test',
|
||||
authorProfileIcon: '',
|
||||
body: 'Old text',
|
||||
childrenComments: [],
|
||||
createdAt: DateTime.now(),
|
||||
);
|
||||
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),
|
||||
);
|
||||
|
||||
when(() => mockFeedRepository.getBlogPostComments(id: any(named: 'id')))
|
||||
.thenAnswer((_) async => mockComment);
|
||||
await cubit.getComments('1', CommentSourceType.blogPost);
|
||||
|
||||
when(() => mockCommentsRepository.updateComment(id: 1, body: 'New text'))
|
||||
.thenThrow(Exception('Update error'));
|
||||
|
||||
final result = await cubit.updateComment(id: 1, body: 'New text');
|
||||
|
||||
expect(result, false);
|
||||
// Состояние должно вернуться к исходному
|
||||
final state = cubit.state as CommentsStateLoaded;
|
||||
expect(state.comment.data.first.body, 'Old text');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user