forked from eatvio/eatvio_app
feat(tests): tests coverage 67.2
This commit is contained in:
@@ -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