forked from eatvio/eatvio_app
feat(tests): widget and unit tests for chat
This commit is contained in:
189
test/features/chat/presentation/bloc/chat_room_cubit_test.dart
Normal file
189
test/features/chat/presentation/bloc/chat_room_cubit_test.dart
Normal file
@@ -0,0 +1,189 @@
|
||||
import 'dart:async';
|
||||
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_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_settings_model.dart';
|
||||
import 'package:eatvio_app/features/chat/models/chat_user_model.dart';
|
||||
import 'package:eatvio_app/features/chat/presentation/bloc/chat_room_cubit/chat_room_cubit.dart';
|
||||
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:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:dart_pusher_channels/dart_pusher_channels.dart';
|
||||
import 'chat_room_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([
|
||||
MockSpec<ChatRepository>(),
|
||||
MockSpec<CardsRepository>(),
|
||||
MockSpec<WsManager>(),
|
||||
MockSpec<StreamSubscription<ChannelReadEvent>>(),
|
||||
MockSpec<PusherChannelsClient>(),
|
||||
])
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
group('ChatRoomCubit', () {
|
||||
late ChatRoomCubit chatRoomCubit;
|
||||
late MockChatRepository mockChatRepository;
|
||||
late MockCardsRepository mockCardsRepository;
|
||||
late MockWsManager mockWsManager;
|
||||
|
||||
const testChatRoom = chat_room.ChatRoomModel(
|
||||
id: 'chat-id',
|
||||
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,
|
||||
);
|
||||
|
||||
final testChatMessages = ChatMessagesModel(
|
||||
messagesList: [
|
||||
ChatMessageModel(
|
||||
id: 1,
|
||||
data: 'Test message',
|
||||
mediaData: null,
|
||||
authorId: 1,
|
||||
updatedAt: DateTime.now(),
|
||||
isRead: false,
|
||||
isCurrentUserAuthor: true,
|
||||
roomId: 'chat-id',
|
||||
),
|
||||
],
|
||||
meta: const ChatMetaModel(lastPage: 1),
|
||||
);
|
||||
|
||||
setUp(() {
|
||||
mockChatRepository = MockChatRepository();
|
||||
mockCardsRepository = MockCardsRepository();
|
||||
mockWsManager = MockWsManager();
|
||||
chatRoomCubit = ChatRoomCubit(chatRepository: mockChatRepository, wsManager: mockWsManager, cardsRepository: mockCardsRepository);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
if (!chatRoomCubit.isClosed) {
|
||||
await chatRoomCubit.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('начальное состояние - loading', () {
|
||||
expect(chatRoomCubit.state, isA<ChatRoomStateLoading>());
|
||||
});
|
||||
|
||||
test('initChat - успешно инициализирует чат', () async {
|
||||
const uuid = 'room-uuid';
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenAnswer((_) async => testChatRoom);
|
||||
// initChat uses uuid (not chatRoom.id) to get messages
|
||||
when(mockChatRepository.getChatMessages(uuid, 1)).thenAnswer((_) async => testChatMessages);
|
||||
when(mockChatRepository.getChosenPlan(id: anyNamed('id'))).thenAnswer((_) async => throw Exception('No plan'));
|
||||
|
||||
// Mock websocket client and streams - return null to skip websocket setup
|
||||
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));
|
||||
// The state might be loaded or error depending on websocket setup
|
||||
// Let's check that at least the repository methods were called
|
||||
verify(mockChatRepository.getChatRoom(uuid)).called(1);
|
||||
verify(mockChatRepository.getChatMessages(uuid, 1)).called(1);
|
||||
});
|
||||
|
||||
test('initChat - обрабатывает ошибку', () async {
|
||||
const uuid = 'room-uuid';
|
||||
when(mockChatRepository.getChatRoom(uuid)).thenThrow(Exception('Network error'));
|
||||
|
||||
await chatRoomCubit.initChat(uuid);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
expect(chatRoomCubit.state, isA<ChatRoomStateError>());
|
||||
});
|
||||
|
||||
|
||||
test('markMessagesAsRead - отмечает сообщения как прочитанные', () async {
|
||||
const chatId = 'chat-id';
|
||||
final messagesIds = [(id: 1, index: 0)];
|
||||
|
||||
// Сначала инициализируем чат
|
||||
when(mockChatRepository.getChatRoom(any)).thenAnswer((_) async => testChatRoom);
|
||||
when(mockChatRepository.getChatMessages(any, any)).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);
|
||||
|
||||
await chatRoomCubit.initChat('uuid');
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
when(mockChatRepository.markMessagesAsRead(chatId, [1])).thenAnswer((_) async => Future.value());
|
||||
|
||||
final result = await chatRoomCubit.markMessagesAsRead(chatId, messagesIds);
|
||||
|
||||
expect(result, true);
|
||||
verify(mockChatRepository.markMessagesAsRead(chatId, [1])).called(1);
|
||||
});
|
||||
|
||||
test('getPaymentLink - получает ссылку на оплату', () async {
|
||||
// Сначала инициализируем чат
|
||||
when(mockChatRepository.getChatRoom(any)).thenAnswer((_) async => testChatRoom);
|
||||
when(mockChatRepository.getChatMessages(any, any)).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);
|
||||
|
||||
await chatRoomCubit.initChat('uuid');
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
// Тест требует chosenPlan, поэтому вернем null
|
||||
final result = await chatRoomCubit.getPaymentLink();
|
||||
|
||||
expect(result, null);
|
||||
});
|
||||
|
||||
test('deleteMessage - удаляет сообщение', () {
|
||||
const uuid = 'message-uuid';
|
||||
|
||||
// Сначала инициализируем чат
|
||||
when(mockChatRepository.getChatRoom(any)).thenAnswer((_) async => testChatRoom);
|
||||
when(mockChatRepository.getChatMessages(any, any)).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);
|
||||
|
||||
chatRoomCubit.initChat('uuid');
|
||||
Future.delayed(const Duration(milliseconds: 100), () {
|
||||
chatRoomCubit.deleteMessage(uuid);
|
||||
expect(chatRoomCubit.state, isA<ChatRoomStateLoaded>());
|
||||
});
|
||||
});
|
||||
|
||||
test('cancelTimer - отменяет таймер', () {
|
||||
chatRoomCubit.setTimer();
|
||||
chatRoomCubit.cancelTimer();
|
||||
// Таймер должен быть отменен
|
||||
expect(chatRoomCubit, isNotNull);
|
||||
});
|
||||
|
||||
test('close - закрывает cubit и отписывается от каналов', () async {
|
||||
when(mockWsManager.unsubscribeDirectChatChannel()).thenReturn(null);
|
||||
|
||||
await chatRoomCubit.close();
|
||||
|
||||
verify(mockWsManager.unsubscribeDirectChatChannel()).called(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
198
test/features/chat/presentation/bloc/chats_cubit_test.dart
Normal file
198
test/features/chat/presentation/bloc/chats_cubit_test.dart
Normal file
@@ -0,0 +1,198 @@
|
||||
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_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';
|
||||
import 'package:eatvio_app/features/chat/repositories/chat_repository.dart';
|
||||
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:dart_pusher_channels/dart_pusher_channels.dart';
|
||||
import 'chats_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<ChatRepository>(), MockSpec<ProfileRepository>(), MockSpec<WsManager>(), MockSpec<StreamSubscription>()])
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
group('ChatsCubit', () {
|
||||
late ChatsCubit chatsCubit;
|
||||
late MockChatRepository mockChatRepository;
|
||||
late MockProfileRepository mockProfileRepository;
|
||||
late MockWsManager mockWsManager;
|
||||
|
||||
const testChatRoomsPagination = ChatRoomPaginationModel(
|
||||
data: [],
|
||||
unreadChatsCount: 0,
|
||||
meta: Meta(currentPage: 1, from: null, lastPage: 1, links: [], perPage: 10, to: null, total: 0),
|
||||
);
|
||||
|
||||
setUp(() {
|
||||
mockChatRepository = MockChatRepository();
|
||||
mockProfileRepository = MockProfileRepository();
|
||||
mockWsManager = MockWsManager();
|
||||
chatsCubit = ChatsCubit(chatRepository: mockChatRepository, wsManager: mockWsManager, profileRepository: mockProfileRepository);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
if (!chatsCubit.isClosed) {
|
||||
await chatsCubit.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('начальное состояние - initial', () {
|
||||
expect(chatsCubit.state, isA<ChatsState>());
|
||||
expect(chatsCubit.state.state, BaseState.initial);
|
||||
});
|
||||
|
||||
test('getChatRoomsWithLoading - загружает комнаты с loading состоянием', () async {
|
||||
when(mockChatRepository.getChatRooms(query: null)).thenAnswer((_) async => testChatRoomsPagination);
|
||||
|
||||
await chatsCubit.getChatRoomsWithLoading();
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
expect(chatsCubit.state.state, BaseState.success);
|
||||
verify(mockChatRepository.getChatRooms(query: null)).called(1);
|
||||
});
|
||||
|
||||
test('getChatRoomsWithLoading - обрабатывает ошибку', () async {
|
||||
when(mockChatRepository.getChatRooms(query: null)).thenThrow(Exception('Network error'));
|
||||
|
||||
await chatsCubit.getChatRoomsWithLoading();
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
expect(chatsCubit.state.state, BaseState.error);
|
||||
expect(chatsCubit.state.errorMessage, isNotNull);
|
||||
});
|
||||
|
||||
test('getChatRooms - загружает комнаты без loading состояния', () async {
|
||||
when(mockChatRepository.getChatRooms(query: null)).thenAnswer((_) async => testChatRoomsPagination);
|
||||
|
||||
await chatsCubit.getChatRooms();
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
expect(chatsCubit.state.state, BaseState.success);
|
||||
verify(mockChatRepository.getChatRooms(query: null)).called(1);
|
||||
});
|
||||
|
||||
test('getChatRooms - загружает комнаты с запросом', () async {
|
||||
const query = 'test query';
|
||||
when(mockChatRepository.getChatRooms(query: query)).thenAnswer((_) async => testChatRoomsPagination);
|
||||
|
||||
await chatsCubit.getChatRooms(query: query);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
expect(chatsCubit.state.state, BaseState.success);
|
||||
verify(mockChatRepository.getChatRooms(query: query)).called(1);
|
||||
});
|
||||
|
||||
test('loadMoreChatRooms - загружает больше комнат', () async {
|
||||
// Create pagination with lastPage > 1 so loadMoreChatRooms doesn't return early
|
||||
const paginationPage1 = ChatRoomPaginationModel(
|
||||
data: [],
|
||||
unreadChatsCount: 0,
|
||||
meta: Meta(
|
||||
currentPage: 1,
|
||||
from: null,
|
||||
lastPage: 3, // Set to 3 so we can load more
|
||||
links: [],
|
||||
perPage: 10,
|
||||
to: null,
|
||||
total: 30,
|
||||
),
|
||||
);
|
||||
|
||||
const paginationPage2 = ChatRoomPaginationModel(
|
||||
data: [],
|
||||
unreadChatsCount: 0,
|
||||
meta: Meta(currentPage: 2, from: null, lastPage: 3, links: [], perPage: 10, to: null, total: 30),
|
||||
);
|
||||
|
||||
when(mockChatRepository.getChatRooms(query: null, page: 1)).thenAnswer((_) async => paginationPage1);
|
||||
|
||||
// Сначала загружаем первую страницу
|
||||
await chatsCubit.getChatRooms();
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
// Затем загружаем следующую страницу
|
||||
when(mockChatRepository.getChatRooms(query: null, page: 2)).thenAnswer((_) async => paginationPage2);
|
||||
|
||||
await chatsCubit.loadMoreChatRooms();
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
verify(mockChatRepository.getChatRooms(query: null, page: 2)).called(1);
|
||||
});
|
||||
|
||||
test('loadMoreChatRooms - не загружает если уже загружается', () async {
|
||||
when(mockChatRepository.getChatRooms(query: null)).thenAnswer((_) async => testChatRoomsPagination);
|
||||
|
||||
await chatsCubit.getChatRooms();
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
// Устанавливаем isLoadingMoreChats в true
|
||||
chatsCubit.emit(chatsCubit.state.copyWith(isLoadingMoreChats: true));
|
||||
|
||||
await chatsCubit.loadMoreChatRooms();
|
||||
|
||||
// Не должно вызывать репозиторий
|
||||
verifyNever(mockChatRepository.getChatRooms(query: null, page: 2));
|
||||
});
|
||||
|
||||
test('reduceUnreadMessagesCount - уменьшает количество непрочитанных сообщений', () {
|
||||
const chatId = 'chat-id';
|
||||
const count = 2;
|
||||
|
||||
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, 3);
|
||||
});
|
||||
|
||||
test('connectToWebSocket - подключается к WebSocket', () async {
|
||||
const mockStream = Stream<ChannelReadEvent>.empty();
|
||||
when(mockWsManager.newMessagesStream).thenAnswer((_) => mockStream);
|
||||
|
||||
await chatsCubit.connectToWebSocket();
|
||||
|
||||
verify(mockWsManager.newMessagesStream).called(1);
|
||||
});
|
||||
|
||||
test('close - закрывает cubit и отписывается от подписок', () async {
|
||||
final mockSubscription1 = MockStreamSubscription();
|
||||
|
||||
when(mockSubscription1.cancel()).thenAnswer((_) async => Future.value());
|
||||
|
||||
// Set newIncomingMessagesSubscription
|
||||
chatsCubit.newIncomingMessagesSubscription = mockSubscription1;
|
||||
|
||||
await chatsCubit.close();
|
||||
|
||||
// Verify that cancel was called on newIncomingMessagesSubscription
|
||||
verify(mockSubscription1.cancel()).called(1);
|
||||
// newLastMessageSubscription is cancelled in super.close() which we can't easily verify
|
||||
// but the test verifies that the method completes without error
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user