forked from eatvio/eatvio_app
feat(tests): tests coverage 67.2
This commit is contained in:
@@ -10,6 +10,8 @@ import 'package:eatvio_app/features/chat/presentation/bloc/chat_room_cubit/chat_
|
||||
import 'package:eatvio_app/features/chat/presentation/bloc/chat_room_cubit/chat_room_state.dart';
|
||||
import 'package:eatvio_app/features/chat/repositories/chat_repository.dart';
|
||||
import 'package:eatvio_app/features/my_finance/repositories/cards_repository.dart';
|
||||
import 'package:eatvio_app/features/my_finance/models/card_model.dart';
|
||||
import 'package:eatvio_app/features/chat/models/payment_model.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/plans/enums/chosen_plan_state_enum.dart';
|
||||
@@ -588,5 +590,487 @@ void main() {
|
||||
|
||||
expect(chatRoomCubit.state, isA<ChatRoomStateError>());
|
||||
});
|
||||
|
||||
test('checkCards - устанавливает hasCard если есть карта с isDefault', () async {
|
||||
const uuid = 'room-uuid';
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => testChatRoom);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => testChatMessages);
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
when(mockChatRepository.getChosenPlan(id: anyNamed('id'))).thenAnswer((_) async => throw Exception('No plan'));
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
// Мокаем карту с isDefault = true
|
||||
final cardWithDefault = CardModel(
|
||||
id: 1,
|
||||
cardNumber: '1234',
|
||||
isDefault: true,
|
||||
);
|
||||
when(mockCardsRepository.getCards()).thenAnswer((_) async => [cardWithDefault]);
|
||||
|
||||
await chatRoomCubit.checkCards();
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
final state = chatRoomCubit.state as ChatRoomStateLoaded;
|
||||
expect(state.hasCard, true);
|
||||
});
|
||||
|
||||
test('checkCards - не устанавливает hasCard если нет карты с isDefault', () async {
|
||||
const uuid = 'room-uuid';
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => testChatRoom);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => testChatMessages);
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
when(mockChatRepository.getChosenPlan(id: anyNamed('id'))).thenAnswer((_) async => throw Exception('No plan'));
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
// Мокаем карту с isDefault = false
|
||||
final cardWithoutDefault = CardModel(
|
||||
id: 1,
|
||||
cardNumber: '1234',
|
||||
isDefault: false,
|
||||
);
|
||||
when(mockCardsRepository.getCards()).thenAnswer((_) async => [cardWithoutDefault]);
|
||||
|
||||
await chatRoomCubit.checkCards();
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
final state = chatRoomCubit.state as ChatRoomStateLoaded;
|
||||
expect(state.hasCard, false);
|
||||
});
|
||||
|
||||
test('getPaymentLink - возвращает ссылку на оплату когда есть chosenPlan', () async {
|
||||
const uuid = 'room-uuid';
|
||||
const planId = 123;
|
||||
final chatRoomWithPlan = testChatRoom.copyWith(chosenPlanId: planId);
|
||||
const testPlan = PlanModel(
|
||||
id: 1,
|
||||
slug: 'test-plan',
|
||||
name: 'Test Plan',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: 'active',
|
||||
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
|
||||
details: 'Test details',
|
||||
packetsCount: 3,
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
isFavorite: false,
|
||||
minPriceOfOptions: 1000,
|
||||
maxPriceOfOptions: 2000,
|
||||
wasLikedByMe: false,
|
||||
);
|
||||
final chosenPlan = ChosenPlanModel(
|
||||
id: planId,
|
||||
optionType: 'options1',
|
||||
startDate: DateTime.now(),
|
||||
chosenPlanState: ChosenPlanStateEnum.statePendingPayment,
|
||||
chatId: 'chat-1',
|
||||
consultantId: 1,
|
||||
plan: testPlan,
|
||||
);
|
||||
const paymentUrl = 'https://example.com/payment';
|
||||
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => chatRoomWithPlan);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => testChatMessages);
|
||||
when(mockChatRepository.getChosenPlan(id: planId)).thenAnswer((_) async => chosenPlan);
|
||||
when(mockChatRepository.getPaymentLink(id: planId)).thenAnswer((_) async => PaymentModel(paymentUrl: paymentUrl));
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
final result = await chatRoomCubit.getPaymentLink();
|
||||
|
||||
expect(result, paymentUrl);
|
||||
verify(mockChatRepository.getPaymentLink(id: planId)).called(1);
|
||||
});
|
||||
|
||||
test('getPaymentLink - обрабатывает ошибку', () async {
|
||||
const uuid = 'room-uuid';
|
||||
const planId = 123;
|
||||
final chatRoomWithPlan = testChatRoom.copyWith(chosenPlanId: planId);
|
||||
const testPlan = PlanModel(
|
||||
id: 1,
|
||||
slug: 'test-plan',
|
||||
name: 'Test Plan',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: 'active',
|
||||
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
|
||||
details: 'Test details',
|
||||
packetsCount: 3,
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
isFavorite: false,
|
||||
minPriceOfOptions: 1000,
|
||||
maxPriceOfOptions: 2000,
|
||||
wasLikedByMe: false,
|
||||
);
|
||||
final chosenPlan = ChosenPlanModel(
|
||||
id: planId,
|
||||
optionType: 'options1',
|
||||
startDate: DateTime.now(),
|
||||
chosenPlanState: ChosenPlanStateEnum.statePendingPayment,
|
||||
chatId: 'chat-1',
|
||||
consultantId: 1,
|
||||
plan: testPlan,
|
||||
);
|
||||
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => chatRoomWithPlan);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => testChatMessages);
|
||||
when(mockChatRepository.getChosenPlan(id: planId)).thenAnswer((_) async => chosenPlan);
|
||||
when(mockChatRepository.getPaymentLink(id: planId)).thenThrow(Exception('Payment error'));
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
final result = await chatRoomCubit.getPaymentLink();
|
||||
|
||||
expect(result, null);
|
||||
});
|
||||
|
||||
test('complainAboutConsultation - отправляет жалобу на консультацию', () async {
|
||||
const uuid = 'room-uuid';
|
||||
const planId = 123;
|
||||
final chatRoomWithPlan = testChatRoom.copyWith(chosenPlanId: planId);
|
||||
const testPlan = PlanModel(
|
||||
id: 1,
|
||||
slug: 'test-plan',
|
||||
name: 'Test Plan',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: 'active',
|
||||
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
|
||||
details: 'Test details',
|
||||
packetsCount: 3,
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
isFavorite: false,
|
||||
minPriceOfOptions: 1000,
|
||||
maxPriceOfOptions: 2000,
|
||||
wasLikedByMe: false,
|
||||
);
|
||||
final chosenPlan = ChosenPlanModel(
|
||||
id: planId,
|
||||
optionType: 'options1',
|
||||
startDate: DateTime.now(),
|
||||
chosenPlanState: ChosenPlanStateEnum.stateStarted,
|
||||
chatId: 'chat-1',
|
||||
consultantId: 1,
|
||||
plan: testPlan,
|
||||
);
|
||||
const description = 'Test complaint';
|
||||
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => chatRoomWithPlan);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => testChatMessages);
|
||||
when(mockChatRepository.getChosenPlan(id: planId)).thenAnswer((_) async => chosenPlan);
|
||||
when(mockChatRepository.complainAboutConsultation(id: planId, body: anyNamed('body'))).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
final result = await chatRoomCubit.complainAboutConsultation(description: description);
|
||||
|
||||
expect(result, true);
|
||||
verify(mockChatRepository.complainAboutConsultation(id: planId, body: anyNamed('body'))).called(1);
|
||||
});
|
||||
|
||||
test('complainAboutConsultation - возвращает false если нет planId', () async {
|
||||
const uuid = 'room-uuid';
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => testChatRoom);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => testChatMessages);
|
||||
when(mockChatRepository.getChosenPlan(id: anyNamed('id'))).thenAnswer((_) async => throw Exception('No plan'));
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
final result = await chatRoomCubit.complainAboutConsultation(description: 'Test');
|
||||
|
||||
expect(result, false);
|
||||
});
|
||||
|
||||
test('complainAboutConsultation - обрабатывает ошибку', () async {
|
||||
const uuid = 'room-uuid';
|
||||
const planId = 123;
|
||||
final chatRoomWithPlan = testChatRoom.copyWith(chosenPlanId: planId);
|
||||
const testPlan = PlanModel(
|
||||
id: 1,
|
||||
slug: 'test-plan',
|
||||
name: 'Test Plan',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: 'active',
|
||||
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
|
||||
details: 'Test details',
|
||||
packetsCount: 3,
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
isFavorite: false,
|
||||
minPriceOfOptions: 1000,
|
||||
maxPriceOfOptions: 2000,
|
||||
wasLikedByMe: false,
|
||||
);
|
||||
final chosenPlan = ChosenPlanModel(
|
||||
id: planId,
|
||||
optionType: 'options1',
|
||||
startDate: DateTime.now(),
|
||||
chosenPlanState: ChosenPlanStateEnum.stateStarted,
|
||||
chatId: 'chat-1',
|
||||
consultantId: 1,
|
||||
plan: testPlan,
|
||||
);
|
||||
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => chatRoomWithPlan);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => testChatMessages);
|
||||
when(mockChatRepository.getChosenPlan(id: planId)).thenAnswer((_) async => chosenPlan);
|
||||
when(mockChatRepository.complainAboutConsultation(id: planId, body: anyNamed('body'))).thenThrow(Exception('Error'));
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
final result = await chatRoomCubit.complainAboutConsultation(description: 'Test');
|
||||
|
||||
expect(result, false);
|
||||
});
|
||||
|
||||
test('finishChosenPlan - завершает выбранный план', () async {
|
||||
const uuid = 'room-uuid';
|
||||
const planId = 123;
|
||||
final chatRoomWithPlan = testChatRoom.copyWith(chosenPlanId: planId);
|
||||
const testPlan = PlanModel(
|
||||
id: 1,
|
||||
slug: 'test-plan',
|
||||
name: 'Test Plan',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: 'active',
|
||||
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
|
||||
details: 'Test details',
|
||||
packetsCount: 3,
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
isFavorite: false,
|
||||
minPriceOfOptions: 1000,
|
||||
maxPriceOfOptions: 2000,
|
||||
wasLikedByMe: false,
|
||||
);
|
||||
final chosenPlan = ChosenPlanModel(
|
||||
id: planId,
|
||||
optionType: 'options1',
|
||||
startDate: DateTime.now(),
|
||||
chosenPlanState: ChosenPlanStateEnum.stateStarted,
|
||||
chatId: 'chat-1',
|
||||
consultantId: 1,
|
||||
plan: testPlan,
|
||||
);
|
||||
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => chatRoomWithPlan);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => testChatMessages);
|
||||
when(mockChatRepository.getChosenPlan(id: planId)).thenAnswer((_) async => chosenPlan);
|
||||
when(mockChatRepository.finishChosenPlan(id: planId)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
final result = await chatRoomCubit.finishChosenPlan();
|
||||
|
||||
expect(result, true);
|
||||
verify(mockChatRepository.finishChosenPlan(id: planId)).called(1);
|
||||
});
|
||||
|
||||
test('finishChosenPlan - возвращает false если нет planId', () async {
|
||||
const uuid = 'room-uuid';
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => testChatRoom);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => testChatMessages);
|
||||
when(mockChatRepository.getChosenPlan(id: anyNamed('id'))).thenAnswer((_) async => throw Exception('No plan'));
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
final result = await chatRoomCubit.finishChosenPlan();
|
||||
|
||||
expect(result, false);
|
||||
});
|
||||
|
||||
test('finishChosenPlan - обрабатывает ошибку', () async {
|
||||
const uuid = 'room-uuid';
|
||||
const planId = 123;
|
||||
final chatRoomWithPlan = testChatRoom.copyWith(chosenPlanId: planId);
|
||||
const testPlan = PlanModel(
|
||||
id: 1,
|
||||
slug: 'test-plan',
|
||||
name: 'Test Plan',
|
||||
image: 'https://example.com/image.jpg',
|
||||
duration: 7,
|
||||
state: 'active',
|
||||
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
|
||||
details: 'Test details',
|
||||
packetsCount: 3,
|
||||
author: Author(authorSlug: 'author', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false),
|
||||
totalLikes: 10,
|
||||
totalViews: 100,
|
||||
totalComments: 5,
|
||||
isFavorite: false,
|
||||
minPriceOfOptions: 1000,
|
||||
maxPriceOfOptions: 2000,
|
||||
wasLikedByMe: false,
|
||||
);
|
||||
final chosenPlan = ChosenPlanModel(
|
||||
id: planId,
|
||||
optionType: 'options1',
|
||||
startDate: DateTime.now(),
|
||||
chosenPlanState: ChosenPlanStateEnum.stateStarted,
|
||||
chatId: 'chat-1',
|
||||
consultantId: 1,
|
||||
plan: testPlan,
|
||||
);
|
||||
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => chatRoomWithPlan);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => testChatMessages);
|
||||
when(mockChatRepository.getChosenPlan(id: planId)).thenAnswer((_) async => chosenPlan);
|
||||
when(mockChatRepository.finishChosenPlan(id: planId)).thenThrow(Exception('Error'));
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
final result = await chatRoomCubit.finishChosenPlan();
|
||||
|
||||
expect(result, false);
|
||||
});
|
||||
|
||||
test('loadNextPage - обрабатывает ошибку', () async {
|
||||
const uuid = 'room-uuid';
|
||||
final initialMessages = ChatMessagesModel(
|
||||
messagesList: [
|
||||
ChatMessageModel(
|
||||
id: 1,
|
||||
data: 'Initial message',
|
||||
mediaData: null,
|
||||
authorId: 1,
|
||||
updatedAt: DateTime.now(),
|
||||
isRead: false,
|
||||
isCurrentUserAuthor: true,
|
||||
roomId: 'chat-id',
|
||||
),
|
||||
],
|
||||
meta: const ChatMetaModel(lastPage: 2),
|
||||
);
|
||||
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => testChatRoom);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => initialMessages);
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
when(mockChatRepository.getChosenPlan(id: anyNamed('id'))).thenAnswer((_) async => throw Exception('No plan'));
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
when(mockChatRepository.getChatMessages(uuid, 2)).thenThrow(Exception('Error'));
|
||||
|
||||
await chatRoomCubit.loadNextPage(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(chatRoomCubit.state, isA<ChatRoomStateError>());
|
||||
});
|
||||
|
||||
test('loadNextPage - не загружает если currentPage >= lastPage', () async {
|
||||
const uuid = 'room-uuid';
|
||||
final initialMessages = ChatMessagesModel(
|
||||
messagesList: [
|
||||
ChatMessageModel(
|
||||
id: 1,
|
||||
data: 'Initial message',
|
||||
mediaData: null,
|
||||
authorId: 1,
|
||||
updatedAt: DateTime.now(),
|
||||
isRead: false,
|
||||
isCurrentUserAuthor: true,
|
||||
roomId: 'chat-id',
|
||||
),
|
||||
],
|
||||
meta: const ChatMetaModel(lastPage: 1),
|
||||
);
|
||||
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => testChatRoom);
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => initialMessages);
|
||||
when(mockWsManager.client).thenReturn(null);
|
||||
when(mockWsManager.initPrivateDirectChatChannel(any)).thenAnswer((_) async => Future.value());
|
||||
when(mockWsManager.subscribeDirectChatChannel()).thenReturn(null);
|
||||
when(mockWsManager.directChatStream).thenReturn(null);
|
||||
when(mockWsManager.clientTypingStream).thenReturn(null);
|
||||
when(mockChatRepository.getChosenPlan(id: anyNamed('id'))).thenAnswer((_) async => throw Exception('No plan'));
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
await chatRoomCubit.loadNextPage(uuid);
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
// Не должно вызывать getChatMessages для страницы 2
|
||||
verifyNever(mockChatRepository.getChatMessages(uuid, 2));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'dart:async';
|
||||
import 'package:eatvio_app/core/enums/base_state.dart';
|
||||
import 'package:eatvio_app/core/models/pagination_model.dart';
|
||||
import 'package:eatvio_app/core/websockets/ws_manager.dart';
|
||||
import 'package:eatvio_app/features/chat/models/chat_message_model.dart';
|
||||
import 'package:eatvio_app/features/chat/models/chat_room_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/chat/presentation/bloc/chats_cubit/chats_cubit.dart';
|
||||
import 'package:eatvio_app/features/chat/presentation/bloc/chats_cubit/chats_state.dart';
|
||||
@@ -303,5 +304,175 @@ void main() {
|
||||
expect(chatsCubit.state.state, BaseState.error);
|
||||
expect(chatsCubit.state.errorMessage, isNotNull);
|
||||
});
|
||||
|
||||
test('_setNewLastMessage - обновляет последнее сообщение в чате', () async {
|
||||
const chatId = 'chat-id';
|
||||
const chatRoom = ChatRoom(
|
||||
id: chatId,
|
||||
lastMessage: null,
|
||||
users: Users(
|
||||
fromChatUser: ChatUser(id: 1, name: 'User 1', profilePhotoPath: null, profilePhotoUrl: ''),
|
||||
toChatUser: ChatUser(id: 2, name: 'User 2', profilePhotoPath: null, profilePhotoUrl: ''),
|
||||
),
|
||||
unreadMessagesCount: 0,
|
||||
chosenPlan: null,
|
||||
);
|
||||
|
||||
const pagination = ChatRoomPaginationModel(
|
||||
data: [chatRoom],
|
||||
unreadChatsCount: 0,
|
||||
meta: Meta(currentPage: 1, from: null, lastPage: 1, links: [], perPage: 10, to: null, total: 1),
|
||||
);
|
||||
|
||||
chatsCubit.emit(ChatsState(state: BaseState.success, chatRooms: pagination.data));
|
||||
|
||||
final newMessage = ChatMessageModel(
|
||||
id: 1,
|
||||
data: 'Test message',
|
||||
mediaData: null,
|
||||
urlsMediaInfo: null,
|
||||
authorId: 1,
|
||||
updatedAt: DateTime.now(),
|
||||
isRead: false,
|
||||
isCurrentUserAuthor: true,
|
||||
roomId: chatId,
|
||||
type: 'text',
|
||||
);
|
||||
|
||||
// Используем рефлексию для вызова приватного метода через тестовый доступ
|
||||
// Или проверяем через публичный API, если есть
|
||||
// В данном случае, _setNewLastMessage вызывается через newUserMessagesStream
|
||||
final streamController = StreamController<ChatMessageModel>();
|
||||
when(mockChatRepository.newUserMessagesStream).thenAnswer((_) => streamController.stream);
|
||||
|
||||
// Пересоздаем cubit чтобы подписаться на stream
|
||||
await chatsCubit.close();
|
||||
chatsCubit = ChatsCubit(chatRepository: mockChatRepository, wsManager: mockWsManager, profileRepository: mockProfileRepository);
|
||||
chatsCubit.emit(ChatsState(state: BaseState.success, chatRooms: pagination.data));
|
||||
|
||||
streamController.add(newMessage);
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
final updatedState = chatsCubit.state;
|
||||
final updatedRoom = updatedState.chatRooms.firstWhere((r) => r.id == chatId);
|
||||
expect(updatedRoom.lastMessage, isNotNull);
|
||||
expect(updatedRoom.lastMessage?.id, 1);
|
||||
|
||||
await streamController.close();
|
||||
});
|
||||
|
||||
test('_setNewLastMessage - обновляет последнее сообщение через newUserMessagesStream', () async {
|
||||
const chatId = 'chat-id';
|
||||
const chatRoom = ChatRoom(
|
||||
id: chatId,
|
||||
lastMessage: null,
|
||||
users: Users(
|
||||
fromChatUser: ChatUser(id: 1, name: 'User 1', profilePhotoPath: null, profilePhotoUrl: ''),
|
||||
toChatUser: ChatUser(id: 2, name: 'User 2', profilePhotoPath: null, profilePhotoUrl: ''),
|
||||
),
|
||||
unreadMessagesCount: 0,
|
||||
chosenPlan: null,
|
||||
);
|
||||
|
||||
const pagination = ChatRoomPaginationModel(
|
||||
data: [chatRoom],
|
||||
unreadChatsCount: 0,
|
||||
meta: Meta(currentPage: 1, from: null, lastPage: 1, links: [], perPage: 10, to: null, total: 1),
|
||||
);
|
||||
|
||||
final newMessage = ChatMessageModel(
|
||||
id: 1,
|
||||
data: 'Test message',
|
||||
mediaData: null,
|
||||
urlsMediaInfo: null,
|
||||
authorId: 1,
|
||||
updatedAt: DateTime.now(),
|
||||
isRead: false,
|
||||
isCurrentUserAuthor: true,
|
||||
roomId: chatId,
|
||||
type: 'text',
|
||||
);
|
||||
|
||||
final streamController = StreamController<ChatMessageModel>();
|
||||
when(mockChatRepository.newUserMessagesStream).thenAnswer((_) => streamController.stream);
|
||||
|
||||
// Пересоздаем cubit чтобы подписаться на stream
|
||||
await chatsCubit.close();
|
||||
chatsCubit = ChatsCubit(chatRepository: mockChatRepository, wsManager: mockWsManager, profileRepository: mockProfileRepository);
|
||||
chatsCubit.emit(ChatsState(state: BaseState.success, chatRooms: pagination.data));
|
||||
|
||||
streamController.add(newMessage);
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
final updatedState = chatsCubit.state;
|
||||
final updatedRoom = updatedState.chatRooms.firstWhere((r) => r.id == chatId);
|
||||
expect(updatedRoom.lastMessage, isNotNull);
|
||||
expect(updatedRoom.lastMessage?.id, 1);
|
||||
|
||||
await streamController.close();
|
||||
});
|
||||
|
||||
test('reduceUnreadMessagesCount - обрабатывает чат который не существует', () {
|
||||
const chatId = 'non-existent-chat';
|
||||
const count = 2;
|
||||
|
||||
const chatRoom = ChatRoom(
|
||||
id: 'other-chat-id',
|
||||
lastMessage: null,
|
||||
users: Users(
|
||||
fromChatUser: ChatUser(id: 1, name: 'User 1', profilePhotoPath: null, profilePhotoUrl: ''),
|
||||
toChatUser: ChatUser(id: 2, name: 'User 2', profilePhotoPath: null, profilePhotoUrl: ''),
|
||||
),
|
||||
unreadMessagesCount: 5,
|
||||
chosenPlan: null,
|
||||
);
|
||||
|
||||
const pagination = ChatRoomPaginationModel(
|
||||
data: [chatRoom],
|
||||
unreadChatsCount: 1,
|
||||
meta: Meta(currentPage: 1, from: null, lastPage: 1, links: [], perPage: 10, to: null, total: 1),
|
||||
);
|
||||
|
||||
chatsCubit.emit(ChatsState(state: BaseState.success, chatRooms: pagination.data, chatsWithUnreadMessagesCount: 1));
|
||||
|
||||
chatsCubit.reduceUnreadMessagesCount(chatId: chatId, count: count);
|
||||
|
||||
final updatedState = chatsCubit.state;
|
||||
// Чат не должен измениться
|
||||
expect(updatedState.chatRooms.length, 1);
|
||||
expect(updatedState.chatRooms.first.unreadMessagesCount, 5);
|
||||
expect(updatedState.chatsWithUnreadMessagesCount, 1);
|
||||
});
|
||||
|
||||
test('reduceUnreadMessagesCount - обрабатывает уменьшение больше чем unreadMessagesCount', () {
|
||||
const chatId = 'chat-id';
|
||||
const count = 10; // Больше чем unreadMessagesCount
|
||||
|
||||
const chatRoom = ChatRoom(
|
||||
id: chatId,
|
||||
lastMessage: null,
|
||||
users: Users(
|
||||
fromChatUser: ChatUser(id: 1, name: 'User 1', profilePhotoPath: null, profilePhotoUrl: ''),
|
||||
toChatUser: ChatUser(id: 2, name: 'User 2', profilePhotoPath: null, profilePhotoUrl: ''),
|
||||
),
|
||||
unreadMessagesCount: 5,
|
||||
chosenPlan: null,
|
||||
);
|
||||
|
||||
const pagination = ChatRoomPaginationModel(
|
||||
data: [chatRoom],
|
||||
unreadChatsCount: 1,
|
||||
meta: Meta(currentPage: 1, from: null, lastPage: 1, links: [], perPage: 10, to: null, total: 1),
|
||||
);
|
||||
|
||||
chatsCubit.emit(ChatsState(state: BaseState.success, chatRooms: pagination.data, chatsWithUnreadMessagesCount: 1));
|
||||
|
||||
chatsCubit.reduceUnreadMessagesCount(chatId: chatId, count: count);
|
||||
|
||||
final updatedState = chatsCubit.state;
|
||||
final updatedRoom = updatedState.chatRooms.firstWhere((r) => r.id == chatId);
|
||||
expect(updatedRoom.unreadMessagesCount, 0);
|
||||
expect(updatedState.chatsWithUnreadMessagesCount, 0);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user