Files
eatvio_app/test/features/feed/models/comment_model_test.dart
2026-01-14 18:47:11 +03:00

289 lines
7.5 KiB
Dart

import 'package:eatvio_app/features/feed/models/comment_model.dart';
import 'package:eatvio_app/core/models/pagination_model.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('CommentModel', () {
test('creates CommentModel with empty list', () {
const links = Links(
first: 'http://example.com?page=1',
last: 'http://example.com?page=1',
prev: null,
next: null,
);
const meta = Meta(
currentPage: 1,
from: null,
lastPage: 1,
links: [],
path: '/api/comments',
perPage: 10,
to: null,
total: 0,
);
const model = CommentModel(
data: [],
links: links,
meta: meta,
);
expect(model.data, isEmpty);
expect(model.meta.total, 0);
});
test('creates CommentModel with comments', () {
final comments = [
Comment(
id: 1,
authorName: 'John Doe',
authorSlug: 'john-doe',
authorProfileIcon: 'icon1.jpg',
body: 'First comment',
childrenComments: [],
createdAt: DateTime(2023, 1, 1),
),
Comment(
id: 2,
authorName: 'Jane Smith',
authorSlug: 'jane-smith',
authorProfileIcon: 'icon2.jpg',
body: 'Second comment',
childrenComments: [],
createdAt: DateTime(2023, 1, 2),
),
];
const links = Links(
first: 'http://example.com?page=1',
last: 'http://example.com?page=1',
prev: null,
next: null,
);
const meta = Meta(
currentPage: 1,
from: 1,
lastPage: 1,
links: [],
path: '/api/comments',
perPage: 10,
to: 2,
total: 2,
);
final model = CommentModel(
data: comments,
links: links,
meta: meta,
);
expect(model.data.length, 2);
expect(model.data[0].id, 1);
expect(model.data[0].authorName, 'John Doe');
expect(model.data[1].id, 2);
expect(model.data[1].authorName, 'Jane Smith');
});
test('creates CommentModel from JSON', () {
final json = {
'data': [
{
'id': 1,
'author_name': 'John Doe',
'author_slug': 'john-doe',
'author_profile_icon': 'icon1.jpg',
'body': 'Test comment',
'children_comments': [],
'created_at': '2023-01-01T00:00:00Z',
},
],
'links': {
'first': 'http://example.com?page=1',
'last': 'http://example.com?page=1',
'prev': null,
'next': null,
},
'meta': {
'current_page': 1,
'from': 1,
'last_page': 1,
'links': [],
'path': '/api/comments',
'per_page': 10,
'to': 1,
'total': 1,
},
};
final model = CommentModel.fromJson(json);
expect(model.data.length, 1);
expect(model.data[0].id, 1);
expect(model.data[0].authorName, 'John Doe');
});
});
group('Comment', () {
test('creates Comment with all parameters', () {
final comment = Comment(
id: 1,
authorName: 'John Doe',
authorSlug: 'john-doe',
authorProfileIcon: 'icon.jpg',
body: 'Test comment body',
childrenComments: [],
createdAt: DateTime(2023, 1, 1),
);
expect(comment.id, 1);
expect(comment.authorName, 'John Doe');
expect(comment.authorSlug, 'john-doe');
expect(comment.authorProfileIcon, 'icon.jpg');
expect(comment.body, 'Test comment body');
expect(comment.childrenComments, isEmpty);
expect(comment.createdAt, DateTime(2023, 1, 1));
});
test('creates Comment with nested children', () {
final childComment = Comment(
id: 2,
authorName: 'Jane Smith',
authorSlug: 'jane-smith',
authorProfileIcon: 'icon2.jpg',
body: 'Reply comment',
childrenComments: [],
createdAt: DateTime(2023, 1, 2),
);
final parentComment = Comment(
id: 1,
authorName: 'John Doe',
authorSlug: 'john-doe',
authorProfileIcon: 'icon1.jpg',
body: 'Parent comment',
childrenComments: [childComment],
createdAt: DateTime(2023, 1, 1),
);
expect(parentComment.childrenComments.length, 1);
expect(parentComment.childrenComments[0].id, 2);
expect(parentComment.childrenComments[0].authorName, 'Jane Smith');
});
test('creates Comment from JSON', () {
final json = {
'id': 1,
'author_name': 'John Doe',
'author_slug': 'john-doe',
'author_profile_icon': 'icon.jpg',
'body': 'Test comment',
'children_comments': [],
'created_at': '2023-01-01T00:00:00Z',
};
final comment = Comment.fromJson(json);
expect(comment.id, 1);
expect(comment.authorName, 'John Doe');
expect(comment.body, 'Test comment');
});
test('toJson serializes Comment correctly', () {
final comment = Comment(
id: 1,
authorName: 'John Doe',
authorSlug: 'john-doe',
authorProfileIcon: 'icon.jpg',
body: 'Test comment body',
childrenComments: [],
createdAt: DateTime(2023, 1, 1),
);
final json = comment.toJson();
expect(json['id'], 1);
expect(json['author_name'], 'John Doe');
expect(json['author_slug'], 'john-doe');
expect(json['body'], 'Test comment body');
expect(json['children_comments'], isA<List>());
});
test('toJson serializes Comment with nested children correctly', () {
final childComment = Comment(
id: 2,
authorName: 'Jane Smith',
authorSlug: 'jane-smith',
authorProfileIcon: 'icon2.jpg',
body: 'Reply comment',
childrenComments: [],
createdAt: DateTime(2023, 1, 2),
);
final parentComment = Comment(
id: 1,
authorName: 'John Doe',
authorSlug: 'john-doe',
authorProfileIcon: 'icon1.jpg',
body: 'Parent comment',
childrenComments: [childComment],
createdAt: DateTime(2023, 1, 1),
);
final json = parentComment.toJson();
expect(json['id'], 1);
expect(json['children_comments'], isA<List>());
expect((json['children_comments'] as List).length, 1);
});
});
group('CommentModel', () {
test('toJson serializes CommentModel correctly', () {
final comments = [
Comment(
id: 1,
authorName: 'John Doe',
authorSlug: 'john-doe',
authorProfileIcon: 'icon1.jpg',
body: 'First comment',
childrenComments: [],
createdAt: DateTime(2023, 1, 1),
),
];
const links = Links(
first: 'http://example.com?page=1',
last: 'http://example.com?page=1',
prev: null,
next: null,
);
const meta = Meta(
currentPage: 1,
from: 1,
lastPage: 1,
links: [],
path: '/api/comments',
perPage: 10,
to: 1,
total: 1,
);
final model = CommentModel(
data: comments,
links: links,
meta: meta,
);
final json = model.toJson();
expect(json.containsKey('data'), isTrue);
expect(json.containsKey('links'), isTrue);
expect(json.containsKey('meta'), isTrue);
expect((json['data'] as List).length, 1);
});
});
}