feat(tests): added services tests and fixtures

This commit is contained in:
misha
2025-12-11 15:53:35 +03:00
parent d1753e99f8
commit 070a3e39e7
65 changed files with 2092 additions and 1130 deletions

View File

@@ -1,23 +1,16 @@
import 'package:dio/dio.dart';
import 'package:eatvio_app/core/models/pagination_model.dart';
import 'package:eatvio_app/features/chat/models/chat_message_model.dart';
import 'package:eatvio_app/features/chat/models/chat_messages_model.dart';
import 'package:eatvio_app/features/chat/models/chat_meta_model.dart';
import 'package:eatvio_app/features/chat/models/chat_room_model.dart' as chat_room;
import 'package:eatvio_app/features/chat/models/chat_room_pagination_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:eatvio_app/features/chat/models/payment_model.dart';
import 'package:eatvio_app/features/chat/models/plan_complain_model.dart';
import 'package:eatvio_app/features/chat/services/chat_service.dart';
import 'package:eatvio_app/features/plans/enums/chosen_plan_state_enum.dart';
import 'package:eatvio_app/features/plans/models/chosen_plan_model.dart';
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:retrofit/retrofit.dart';
import '../../../fixtures/fixture_reader.dart';
import 'chat_service_test.mocks.dart';
@GenerateNiceMocks([MockSpec<ChatService>()])
@@ -30,48 +23,24 @@ void main() {
});
test('getChatRooms - возвращает список комнат чата', () async {
const expectedPagination = ChatRoomPaginationModel(
data: [],
unreadChatsCount: 0,
meta: Meta(
currentPage: 1,
from: null,
lastPage: 1,
links: [],
perPage: 10,
to: null,
total: 0,
),
);
final paginationFixture = FixtureReader.readAsMap('chat/chat_room_pagination_response.json');
final expectedPagination = ChatRoomPaginationModel.fromJson(paginationFixture);
when(mockChatService.getChatRooms(query: null, page: 1))
.thenAnswer((_) async => expectedPagination);
when(mockChatService.getChatRooms(query: null, page: 1)).thenAnswer((_) async => expectedPagination);
final result = await mockChatService.getChatRooms(query: null, page: 1);
expect(result, isA<ChatRoomPaginationModel>());
expect(result.data.length, 0);
expect(result.data.length, 2);
verify(mockChatService.getChatRooms(query: null, page: 1)).called(1);
});
test('getChatRooms - возвращает список комнат с запросом', () async {
const query = 'test query';
const expectedPagination = ChatRoomPaginationModel(
data: [],
unreadChatsCount: 0,
meta: Meta(
currentPage: 1,
from: null,
lastPage: 1,
links: [],
perPage: 10,
to: null,
total: 0,
),
);
final paginationFixture = FixtureReader.readAsMap('chat/chat_room_pagination_response.json');
final expectedPagination = ChatRoomPaginationModel.fromJson(paginationFixture);
when(mockChatService.getChatRooms(query: query, page: 1))
.thenAnswer((_) async => expectedPagination);
when(mockChatService.getChatRooms(query: query, page: 1)).thenAnswer((_) async => expectedPagination);
final result = await mockChatService.getChatRooms(query: query, page: 1);
@@ -80,32 +49,11 @@ void main() {
});
test('getChatRoom - возвращает комнату чата по UUID', () async {
const uuid = 'room-uuid-123';
const expectedRoom = chat_room.ChatRoomModel(
id: uuid,
chatRoomSettings: ChatRoomSettingsModel(
name: 'Test Room',
state: 'active',
),
users: chat_room.Users(
sender: ChatUserModel(
id: 1,
slug: 'user-1',
name: 'User 1',
profilePhotoUrl: null,
),
receiver: ChatUserModel(
id: 2,
slug: 'user-2',
name: 'User 2',
profilePhotoUrl: null,
),
),
chosenPlanId: null,
);
const uuid = 'chat-1';
final roomFixture = FixtureReader.readAsMap('chat/chat_room_response.json');
final expectedRoom = chat_room.ChatRoomModel.fromJson(roomFixture);
when(mockChatService.getChatRoom(uuid: uuid))
.thenAnswer((_) async => expectedRoom);
when(mockChatService.getChatRoom(uuid: uuid)).thenAnswer((_) async => expectedRoom);
final result = await mockChatService.getChatRoom(uuid: uuid);
@@ -119,84 +67,34 @@ void main() {
const data = 'Test message';
const socketId = 'socket-id-123';
when(mockChatService.sendMessage(
slug: slug,
data: data,
mediaData: null,
socketId: socketId,
)).thenAnswer((_) async => Future.value());
when(mockChatService.sendMessage(slug: slug, data: data, mediaData: null, socketId: socketId)).thenAnswer((_) async => Future.value());
await mockChatService.sendMessage(
slug: slug,
data: data,
mediaData: null,
socketId: socketId,
);
await mockChatService.sendMessage(slug: slug, data: data, mediaData: null, socketId: socketId);
verify(mockChatService.sendMessage(
slug: slug,
data: data,
mediaData: null,
socketId: socketId,
)).called(1);
verify(mockChatService.sendMessage(slug: slug, data: data, mediaData: null, socketId: socketId)).called(1);
});
test('getChatMessages - возвращает сообщения чата', () async {
const chatId = 'chat-id-123';
const chatId = 'chat-1';
const page = 1;
final expectedMessages = ChatMessagesModel(
messagesList: [
ChatMessageModel(
id: 1,
data: 'Message 1',
mediaData: null,
authorId: 1,
updatedAt: DateTime.now(),
isRead: false,
isCurrentUserAuthor: true,
roomId: chatId,
),
],
meta: const ChatMetaModel(lastPage: 1),
);
final messagesFixture = FixtureReader.readAsMap('chat/chat_messages_response.json');
final expectedMessages = ChatMessagesModel.fromJson(messagesFixture);
when(mockChatService.getChatMessages(id: chatId, page: page))
.thenAnswer((_) async => expectedMessages);
when(mockChatService.getChatMessages(id: chatId, page: page)).thenAnswer((_) async => expectedMessages);
final result = await mockChatService.getChatMessages(id: chatId, page: page);
expect(result, isA<ChatMessagesModel>());
expect(result.messagesList.length, 1);
expect(result.messagesList.length, 2);
verify(mockChatService.getChatMessages(id: chatId, page: page)).called(1);
});
test('getChatRoomIdByUserSlug - возвращает комнату по slug пользователя', () async {
const slug = 'user-slug';
const expectedRoom = chat_room.ChatRoomModel(
id: 'room-id',
chatRoomSettings: ChatRoomSettingsModel(
name: 'Test Room',
state: 'active',
),
users: chat_room.Users(
sender: ChatUserModel(
id: 1,
slug: slug,
name: 'User',
profilePhotoUrl: null,
),
receiver: ChatUserModel(
id: 2,
slug: 'other-user',
name: 'Other User',
profilePhotoUrl: null,
),
),
chosenPlanId: null,
);
const slug = 'user-1';
final roomFixture = FixtureReader.readAsMap('chat/chat_room_response.json');
final expectedRoom = chat_room.ChatRoomModel.fromJson(roomFixture);
when(mockChatService.getChatRoomIdByUserSlug(user: slug))
.thenAnswer((_) async => expectedRoom);
when(mockChatService.getChatRoomIdByUserSlug(user: slug)).thenAnswer((_) async => expectedRoom);
final result = await mockChatService.getChatRoomIdByUserSlug(user: slug);
@@ -208,27 +106,17 @@ void main() {
const chatId = 'chat-id-123';
final messageIds = [1, 2, 3];
when(mockChatService.markMessagesAsRead(
id: chatId,
body: {'messages_ids': messageIds},
)).thenAnswer((_) async => Future.value());
when(mockChatService.markMessagesAsRead(id: chatId, body: {'messages_ids': messageIds})).thenAnswer((_) async => Future.value());
await mockChatService.markMessagesAsRead(
id: chatId,
body: {'messages_ids': messageIds},
);
await mockChatService.markMessagesAsRead(id: chatId, body: {'messages_ids': messageIds});
verify(mockChatService.markMessagesAsRead(
id: chatId,
body: {'messages_ids': messageIds},
)).called(1);
verify(mockChatService.markMessagesAsRead(id: chatId, body: {'messages_ids': messageIds})).called(1);
});
test('cancelChosenPlan - отменяет выбранный план', () async {
const planId = 123;
when(mockChatService.cancelChosenPlan(id: planId))
.thenAnswer((_) async => Future.value());
when(mockChatService.cancelChosenPlan(id: planId)).thenAnswer((_) async => Future.value());
await mockChatService.cancelChosenPlan(id: planId);
@@ -239,8 +127,7 @@ void main() {
const planId = 123;
final date = {'start_date': '2024-01-15'};
when(mockChatService.choseStartPlanDate(id: planId, date: date))
.thenAnswer((_) async => Future.value());
when(mockChatService.choseStartPlanDate(id: planId, date: date)).thenAnswer((_) async => Future.value());
await mockChatService.choseStartPlanDate(id: planId, date: date);
@@ -249,60 +136,24 @@ void main() {
test('getPaymentLink - возвращает ссылку на оплату', () async {
const planId = 123;
final expectedPayment = PaymentModel(paymentUrl: 'https://payment.url');
final paymentFixture = FixtureReader.readAsMap('chat/payment_response.json');
final expectedPayment = PaymentModel.fromJson(paymentFixture);
when(mockChatService.getPaymentLink(id: planId))
.thenAnswer((_) async => expectedPayment);
when(mockChatService.getPaymentLink(id: planId)).thenAnswer((_) async => expectedPayment);
final result = await mockChatService.getPaymentLink(id: planId);
expect(result, isA<PaymentModel>());
expect(result.paymentUrl, 'https://payment.url');
expect(result.paymentUrl, 'https://example.com/checkout/payment-123');
verify(mockChatService.getPaymentLink(id: planId)).called(1);
});
test('getChosenPlan - возвращает выбранный план', () async {
const planId = 123;
final expectedPlan = ChosenPlanModel(
id: planId,
optionType: 'standard',
startDate: DateTime.now(),
chosenPlanState: ChosenPlanStateEnum.stateInCoordination,
chatId: 'chat-id',
consultantId: 1,
plan: const PlanModel(
id: 456,
slug: 'plan-slug',
name: 'Test Plan',
image: '',
duration: 30,
state: 'active',
type: PlanType(
fit: true,
slim: false,
behavior: false,
wellness: false,
),
details: '',
packetsCount: 1,
author: Author(
authorSlug: 'author',
authorName: 'Author',
authorProfileIcon: '',
authorIsMe: false,
),
totalLikes: 0,
totalViews: 0,
totalComments: 0,
isFavorite: false,
minPriceOfOptions: 0.0,
maxPriceOfOptions: 100.0,
wasLikedByMe: false,
),
);
const planId = 1;
final chosenPlanFixture = FixtureReader.readAsMap('chat/chosen_plan_response.json');
final expectedPlan = ChosenPlanModel.fromJson(chosenPlanFixture);
when(mockChatService.getChosenPlan(id: planId))
.thenAnswer((_) async => expectedPlan);
when(mockChatService.getChosenPlan(id: planId)).thenAnswer((_) async => expectedPlan);
final result = await mockChatService.getChosenPlan(id: planId);
@@ -314,8 +165,7 @@ void main() {
test('finishChosenPlan - завершает выбранный план', () async {
const planId = 123;
when(mockChatService.finishChosenPlan(id: planId))
.thenAnswer((_) async => Future.value());
when(mockChatService.finishChosenPlan(id: planId)).thenAnswer((_) async => Future.value());
await mockChatService.finishChosenPlan(id: planId);
@@ -324,30 +174,22 @@ void main() {
test('complainAboutConsultation - отправляет жалобу на консультацию', () async {
const planId = 123;
final complainModel = PlanComplainModel(description: 'Test complaint');
final complainFixture = FixtureReader.readAsMap('chat/plan_complain_response.json');
final complainModel = PlanComplainModel.fromJson(complainFixture);
when(mockChatService.complainAboutConsultation(id: planId, body: complainModel))
.thenAnswer((_) async => Future.value());
when(mockChatService.complainAboutConsultation(id: planId, body: complainModel)).thenAnswer((_) async => Future.value());
await mockChatService.complainAboutConsultation(id: planId, body: complainModel);
verify(mockChatService.complainAboutConsultation(id: planId, body: complainModel))
.called(1);
verify(mockChatService.complainAboutConsultation(id: planId, body: complainModel)).called(1);
});
test('getChatByUserSlug - возвращает HttpResponse', () async {
const slug = 'user-slug';
const chatId = 'chat-id-123';
final response = HttpResponse(
{'id': chatId},
Response(
requestOptions: RequestOptions(path: ''),
statusCode: 200,
),
);
final response = HttpResponse({'id': chatId}, Response(requestOptions: RequestOptions(path: ''), statusCode: 200));
when(mockChatService.getChatByUserSlug(slug: slug))
.thenAnswer((_) async => response);
when(mockChatService.getChatByUserSlug(slug: slug)).thenAnswer((_) async => response);
final result = await mockChatService.getChatByUserSlug(slug: slug);
@@ -357,4 +199,3 @@ void main() {
});
});
}