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:
@@ -28,6 +28,32 @@ void main() {
|
||||
expect(model.messagesList.first.id, 1);
|
||||
expect(model.meta.lastPage, 1);
|
||||
});
|
||||
|
||||
test('fromJson создает ChatMessagesModel из JSON', () {
|
||||
final json = {
|
||||
'data': [
|
||||
{
|
||||
'id': 1,
|
||||
'data': 'Test message',
|
||||
'media_data': null,
|
||||
'author_id': 123,
|
||||
'updated_at': '2024-01-01T00:00:00Z',
|
||||
'read_by_to_user': false,
|
||||
'is_author_message_auth_user': true,
|
||||
'room_id': 'room-1',
|
||||
}
|
||||
],
|
||||
'meta': {
|
||||
'last_page': 1,
|
||||
},
|
||||
};
|
||||
|
||||
final model = ChatMessagesModel.fromJson(json);
|
||||
|
||||
expect(model.messagesList.length, 1);
|
||||
expect(model.messagesList.first.id, 1);
|
||||
expect(model.meta.lastPage, 1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,63 @@ void main() {
|
||||
expect(model.chosenPlanId, 1);
|
||||
expect(model.users.sender.id, 1);
|
||||
});
|
||||
|
||||
test('fromJson создает ChatRoomModel из JSON', () {
|
||||
final json = {
|
||||
'id': 'room-1',
|
||||
'chat_room_setting': {
|
||||
'room_name': 'Room Name',
|
||||
'state': 'active',
|
||||
},
|
||||
'users': {
|
||||
'from_chat_user': {
|
||||
'id': 1,
|
||||
'name': 'Sender',
|
||||
'slug': 'sender',
|
||||
'profile_photo_url': 'https://example.com/sender.jpg',
|
||||
},
|
||||
'to_chat_user': {
|
||||
'id': 2,
|
||||
'name': 'Receiver',
|
||||
'slug': 'receiver',
|
||||
'profile_photo_url': 'https://example.com/receiver.jpg',
|
||||
},
|
||||
},
|
||||
'chosen_plan_id': 1,
|
||||
'chosen_plan': null,
|
||||
};
|
||||
|
||||
final model = ChatRoomModel.fromJson(json);
|
||||
|
||||
expect(model.id, 'room-1');
|
||||
expect(model.chosenPlanId, 1);
|
||||
expect(model.users.sender.id, 1);
|
||||
expect(model.users.receiver.id, 2);
|
||||
});
|
||||
});
|
||||
|
||||
group('Users', () {
|
||||
test('fromJson создает Users из JSON', () {
|
||||
final json = {
|
||||
'from_chat_user': {
|
||||
'id': 1,
|
||||
'name': 'Sender',
|
||||
'slug': 'sender',
|
||||
'profile_photo_url': 'https://example.com/sender.jpg',
|
||||
},
|
||||
'to_chat_user': {
|
||||
'id': 2,
|
||||
'name': 'Receiver',
|
||||
'slug': 'receiver',
|
||||
'profile_photo_url': 'https://example.com/receiver.jpg',
|
||||
},
|
||||
};
|
||||
|
||||
final users = Users.fromJson(json);
|
||||
|
||||
expect(users.sender.id, 1);
|
||||
expect(users.receiver.id, 2);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,193 @@ void main() {
|
||||
expect(pagination.data.first.id, 'room-1');
|
||||
expect(pagination.unreadChatsCount, 1);
|
||||
});
|
||||
|
||||
test('fromJson создает ChatRoomPaginationModel из JSON', () {
|
||||
final json = {
|
||||
'data': [
|
||||
{
|
||||
'id': 'room-1',
|
||||
'last_message': {
|
||||
'id': 1,
|
||||
'data': 'Test message',
|
||||
'media_data': null,
|
||||
'author_id': 123,
|
||||
'updated_at': '2024-01-01T00:00:00Z',
|
||||
'read_by_to_user': false,
|
||||
'is_author_message_auth_user': true,
|
||||
'room_id': 'room-1',
|
||||
},
|
||||
'users': {
|
||||
'from_chat_user': {
|
||||
'id': 1,
|
||||
'name': 'User 1',
|
||||
'profile_photo_path': null,
|
||||
'profile_photo_url': 'https://example.com/user1.jpg',
|
||||
},
|
||||
'to_chat_user': {
|
||||
'id': 2,
|
||||
'name': 'User 2',
|
||||
'profile_photo_path': null,
|
||||
'profile_photo_url': 'https://example.com/user2.jpg',
|
||||
},
|
||||
},
|
||||
'unread_messages_count': 5,
|
||||
'chosen_plan': null,
|
||||
},
|
||||
],
|
||||
'number_of_unread_chat_rooms': 1,
|
||||
'meta': {
|
||||
'current_page': 1,
|
||||
'from': 1,
|
||||
'last_page': 1,
|
||||
'links': [],
|
||||
'per_page': 10,
|
||||
'to': 1,
|
||||
'total': 1,
|
||||
},
|
||||
};
|
||||
|
||||
final pagination = ChatRoomPaginationModel.fromJson(json);
|
||||
|
||||
expect(pagination.data.length, 1);
|
||||
expect(pagination.data.first.id, 'room-1');
|
||||
expect(pagination.unreadChatsCount, 1);
|
||||
});
|
||||
});
|
||||
|
||||
group('parseLastMessage', () {
|
||||
test('parseLastMessage возвращает null для List', () {
|
||||
final result = parseLastMessage([]);
|
||||
|
||||
expect(result, isNull);
|
||||
});
|
||||
|
||||
test('parseLastMessage создает ChatMessageModel из Map', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'data': 'Test message',
|
||||
'media_data': null,
|
||||
'author_id': 123,
|
||||
'updated_at': '2024-01-01T00:00:00Z',
|
||||
'read_by_to_user': false,
|
||||
'is_author_message_auth_user': true,
|
||||
'room_id': 'room-1',
|
||||
};
|
||||
|
||||
final result = parseLastMessage(json);
|
||||
|
||||
expect(result, isNotNull);
|
||||
expect(result!.id, 1);
|
||||
expect(result.data, 'Test message');
|
||||
});
|
||||
});
|
||||
|
||||
group('ChatRoom', () {
|
||||
test('fromJson создает ChatRoom из JSON', () {
|
||||
final json = {
|
||||
'id': 'room-1',
|
||||
'last_message': {
|
||||
'id': 1,
|
||||
'data': 'Test message',
|
||||
'media_data': null,
|
||||
'author_id': 123,
|
||||
'updated_at': '2024-01-01T00:00:00Z',
|
||||
'read_by_to_user': false,
|
||||
'is_author_message_auth_user': true,
|
||||
'room_id': 'room-1',
|
||||
},
|
||||
'users': {
|
||||
'from_chat_user': {
|
||||
'id': 1,
|
||||
'name': 'User 1',
|
||||
'profile_photo_path': null,
|
||||
'profile_photo_url': 'https://example.com/user1.jpg',
|
||||
},
|
||||
'to_chat_user': {
|
||||
'id': 2,
|
||||
'name': 'User 2',
|
||||
'profile_photo_path': null,
|
||||
'profile_photo_url': 'https://example.com/user2.jpg',
|
||||
},
|
||||
},
|
||||
'unread_messages_count': 5,
|
||||
'chosen_plan': null,
|
||||
};
|
||||
|
||||
final chatRoom = ChatRoom.fromJson(json);
|
||||
|
||||
expect(chatRoom.id, 'room-1');
|
||||
expect(chatRoom.unreadMessagesCount, 5);
|
||||
expect(chatRoom.lastMessage, isNotNull);
|
||||
});
|
||||
|
||||
test('fromJson обрабатывает last_message как List (возвращает null)', () {
|
||||
final json = {
|
||||
'id': 'room-1',
|
||||
'last_message': [],
|
||||
'users': {
|
||||
'from_chat_user': {
|
||||
'id': 1,
|
||||
'name': 'User 1',
|
||||
'profile_photo_path': null,
|
||||
'profile_photo_url': 'https://example.com/user1.jpg',
|
||||
},
|
||||
'to_chat_user': {
|
||||
'id': 2,
|
||||
'name': 'User 2',
|
||||
'profile_photo_path': null,
|
||||
'profile_photo_url': 'https://example.com/user2.jpg',
|
||||
},
|
||||
},
|
||||
'unread_messages_count': 5,
|
||||
'chosen_plan': null,
|
||||
};
|
||||
|
||||
final chatRoom = ChatRoom.fromJson(json);
|
||||
|
||||
expect(chatRoom.lastMessage, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('Users', () {
|
||||
test('fromJson создает Users из JSON', () {
|
||||
final json = {
|
||||
'from_chat_user': {
|
||||
'id': 1,
|
||||
'name': 'User 1',
|
||||
'profile_photo_path': null,
|
||||
'profile_photo_url': 'https://example.com/user1.jpg',
|
||||
},
|
||||
'to_chat_user': {
|
||||
'id': 2,
|
||||
'name': 'User 2',
|
||||
'profile_photo_path': null,
|
||||
'profile_photo_url': 'https://example.com/user2.jpg',
|
||||
},
|
||||
};
|
||||
|
||||
final users = Users.fromJson(json);
|
||||
|
||||
expect(users.fromChatUser.id, 1);
|
||||
expect(users.toChatUser.id, 2);
|
||||
});
|
||||
});
|
||||
|
||||
group('ChatUser', () {
|
||||
test('fromJson создает ChatUser из JSON', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'name': 'User 1',
|
||||
'profile_photo_path': null,
|
||||
'profile_photo_url': 'https://example.com/user1.jpg',
|
||||
};
|
||||
|
||||
final chatUser = ChatUser.fromJson(json);
|
||||
|
||||
expect(chatUser.id, 1);
|
||||
expect(chatUser.name, 'User 1');
|
||||
expect(chatUser.profilePhotoUrl, 'https://example.com/user1.jpg');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,36 @@ void main() {
|
||||
|
||||
expect(model.profilePhotoUrl, isNull);
|
||||
});
|
||||
|
||||
test('fromJson создает ChatUserModel из JSON', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'name': 'User Name',
|
||||
'slug': 'user-slug',
|
||||
'profile_photo_url': 'https://example.com/photo.jpg',
|
||||
};
|
||||
|
||||
final model = ChatUserModel.fromJson(json);
|
||||
|
||||
expect(model.id, 1);
|
||||
expect(model.name, 'User Name');
|
||||
expect(model.slug, 'user-slug');
|
||||
expect(model.profilePhotoUrl, 'https://example.com/photo.jpg');
|
||||
});
|
||||
|
||||
test('fromJson создает ChatUserModel из JSON без фото', () {
|
||||
final json = {
|
||||
'id': 1,
|
||||
'name': 'User Name',
|
||||
'slug': 'user-slug',
|
||||
};
|
||||
|
||||
final model = ChatUserModel.fromJson(json);
|
||||
|
||||
expect(model.id, 1);
|
||||
expect(model.name, 'User Name');
|
||||
expect(model.profilePhotoUrl, isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user