forked from eatvio/eatvio_app
feat(tests): added services tests and fixtures
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:eatvio_app/features/chat/models/chat_message_model.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import '../../../fixtures/fixture_reader.dart';
|
||||
|
||||
void main() {
|
||||
group('ChatMessageModel', () {
|
||||
@@ -23,22 +24,24 @@ void main() {
|
||||
});
|
||||
|
||||
test('fromJson создает ChatMessageModel из JSON', () {
|
||||
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 json = FixtureReader.readAsMap('chat/chat_message_response.json');
|
||||
|
||||
final model = ChatMessageModel.fromJson(json);
|
||||
|
||||
expect(model.id, 1);
|
||||
expect(model.data, 'Test message');
|
||||
expect(model.authorId, 123);
|
||||
expect(model.authorId, 1);
|
||||
});
|
||||
|
||||
test('fromJson создает ChatMessageModel с медиа из JSON', () {
|
||||
final json = FixtureReader.readAsMap('chat/chat_message_with_media_response.json');
|
||||
|
||||
final model = ChatMessageModel.fromJson(json);
|
||||
|
||||
expect(model.id, 2);
|
||||
expect(model.data, 'Message with attachment');
|
||||
expect(model.mediaData, isNotNull);
|
||||
expect(model.mediaData!.length, 1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:eatvio_app/features/chat/models/chat_message_model.dart';
|
||||
import 'package:eatvio_app/features/chat/models/chat_meta_model.dart';
|
||||
import 'package:eatvio_app/features/chat/models/chat_messages_model.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import '../../../fixtures/fixture_reader.dart';
|
||||
|
||||
void main() {
|
||||
group('ChatMessagesModel', () {
|
||||
@@ -19,10 +20,7 @@ void main() {
|
||||
|
||||
const meta = ChatMetaModel(lastPage: 1);
|
||||
|
||||
final model = ChatMessagesModel(
|
||||
messagesList: [message],
|
||||
meta: meta,
|
||||
);
|
||||
final model = ChatMessagesModel(messagesList: [message], meta: meta);
|
||||
|
||||
expect(model.messagesList.length, 1);
|
||||
expect(model.messagesList.first.id, 1);
|
||||
@@ -30,30 +28,14 @@ void main() {
|
||||
});
|
||||
|
||||
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 json = FixtureReader.readAsMap('chat/chat_messages_response.json');
|
||||
|
||||
final model = ChatMessagesModel.fromJson(json);
|
||||
|
||||
expect(model.messagesList.length, 1);
|
||||
expect(model.messagesList.length, 2);
|
||||
expect(model.messagesList.first.id, 1);
|
||||
expect(model.messagesList.first.data, 'First message');
|
||||
expect(model.meta.lastPage, 1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:eatvio_app/features/chat/models/chat_room_model.dart';
|
||||
import 'package:eatvio_app/features/chat/models/chat_room_settings_model.dart';
|
||||
import 'package:eatvio_app/features/chat/models/chat_user_model.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import '../../../fixtures/fixture_reader.dart';
|
||||
|
||||
void main() {
|
||||
group('ChatRoomModel', () {
|
||||
@@ -41,34 +42,12 @@ void main() {
|
||||
});
|
||||
|
||||
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 json = FixtureReader.readAsMap('chat/chat_room_response.json');
|
||||
|
||||
final model = ChatRoomModel.fromJson(json);
|
||||
|
||||
expect(model.id, 'room-1');
|
||||
expect(model.chosenPlanId, 1);
|
||||
expect(model.id, 'chat-1');
|
||||
expect(model.chosenPlanId, null);
|
||||
expect(model.users.sender.id, 1);
|
||||
expect(model.users.receiver.id, 2);
|
||||
});
|
||||
@@ -76,22 +55,10 @@ void main() {
|
||||
|
||||
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 roomJson = FixtureReader.readAsMap('chat/chat_room_response.json');
|
||||
final usersJson = roomJson['users'] as Map<String, dynamic>;
|
||||
|
||||
final users = Users.fromJson(json);
|
||||
final users = Users.fromJson(usersJson);
|
||||
|
||||
expect(users.sender.id, 1);
|
||||
expect(users.receiver.id, 2);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:eatvio_app/core/models/pagination_model.dart';
|
||||
import 'package:eatvio_app/features/chat/models/chat_room_pagination_model.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import '../../../fixtures/fixture_reader.dart';
|
||||
|
||||
void main() {
|
||||
group('ChatRoomPaginationModel', () {
|
||||
@@ -46,54 +47,12 @@ void main() {
|
||||
});
|
||||
|
||||
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 json = FixtureReader.readAsMap('chat/chat_room_pagination_response.json');
|
||||
|
||||
final pagination = ChatRoomPaginationModel.fromJson(json);
|
||||
|
||||
expect(pagination.data.length, 1);
|
||||
expect(pagination.data.first.id, 'room-1');
|
||||
expect(pagination.data.length, 2);
|
||||
expect(pagination.data.first.id, 'chat-1');
|
||||
expect(pagination.unreadChatsCount, 1);
|
||||
});
|
||||
});
|
||||
@@ -106,16 +65,7 @@ void main() {
|
||||
});
|
||||
|
||||
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 json = FixtureReader.readAsMap('chat/chat_message_response.json');
|
||||
|
||||
final result = parseLastMessage(json);
|
||||
|
||||
@@ -127,39 +77,12 @@ void main() {
|
||||
|
||||
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 paginationJson = FixtureReader.readAsMap('chat/chat_room_pagination_response.json');
|
||||
final chatRoomJson = (paginationJson['data'] as List).first as Map<String, dynamic>;
|
||||
|
||||
final chatRoom = ChatRoom.fromJson(json);
|
||||
final chatRoom = ChatRoom.fromJson(chatRoomJson);
|
||||
|
||||
expect(chatRoom.id, 'room-1');
|
||||
expect(chatRoom.id, 'chat-1');
|
||||
expect(chatRoom.unreadMessagesCount, 5);
|
||||
expect(chatRoom.lastMessage, isNotNull);
|
||||
});
|
||||
@@ -194,22 +117,11 @@ void main() {
|
||||
|
||||
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 paginationJson = FixtureReader.readAsMap('chat/chat_room_pagination_response.json');
|
||||
final chatRoomJson = (paginationJson['data'] as List).first as Map<String, dynamic>;
|
||||
final usersJson = chatRoomJson['users'] as Map<String, dynamic>;
|
||||
|
||||
final users = Users.fromJson(json);
|
||||
final users = Users.fromJson(usersJson);
|
||||
|
||||
expect(users.fromChatUser.id, 1);
|
||||
expect(users.toChatUser.id, 2);
|
||||
@@ -218,14 +130,12 @@ void main() {
|
||||
|
||||
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 paginationJson = FixtureReader.readAsMap('chat/chat_room_pagination_response.json');
|
||||
final chatRoomJson = (paginationJson['data'] as List).first as Map<String, dynamic>;
|
||||
final usersJson = chatRoomJson['users'] as Map<String, dynamic>;
|
||||
final chatUserJson = usersJson['from_chat_user'] as Map<String, dynamic>;
|
||||
|
||||
final chatUser = ChatUser.fromJson(json);
|
||||
final chatUser = ChatUser.fromJson(chatUserJson);
|
||||
|
||||
expect(chatUser.id, 1);
|
||||
expect(chatUser.name, 'User 1');
|
||||
|
||||
Reference in New Issue
Block a user