forked from eatvio/eatvio_app
feat(tests): updated tests coverage to 66.5%
This commit is contained in:
@@ -194,5 +194,114 @@ void main() {
|
||||
// newLastMessageSubscription is cancelled in super.close() which we can't easily verify
|
||||
// but the test verifies that the method completes without error
|
||||
});
|
||||
|
||||
test('reduceUnreadMessagesCount - уменьшает до 0 и уменьшает chatsWithUnreadMessagesCount', () {
|
||||
const chatId = 'chat-id';
|
||||
const count = 5;
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
test('reduceUnreadMessagesCount - не уменьшает chatsWithUnreadMessagesCount если unreadMessagesCount > 0', () {
|
||||
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;
|
||||
expect(updatedState.chatsWithUnreadMessagesCount, 1);
|
||||
});
|
||||
|
||||
test('loadMoreChatRooms - не загружает если lastPage == page', () async {
|
||||
const paginationPage1 = ChatRoomPaginationModel(
|
||||
data: [],
|
||||
unreadChatsCount: 0,
|
||||
meta: Meta(currentPage: 1, from: null, lastPage: 1, links: [], perPage: 10, to: null, total: 10),
|
||||
);
|
||||
|
||||
when(mockChatRepository.getChatRooms(query: null, page: 1)).thenAnswer((_) async => paginationPage1);
|
||||
|
||||
await chatsCubit.getChatRooms();
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
// lastPage = meta.lastPage + 1 = 2, page = 2, поэтому не должно загружать
|
||||
await chatsCubit.loadMoreChatRooms();
|
||||
|
||||
verifyNever(mockChatRepository.getChatRooms(query: null, page: 2));
|
||||
});
|
||||
|
||||
test('loadMoreChatRooms - обрабатывает ошибку', () async {
|
||||
const paginationPage1 = ChatRoomPaginationModel(
|
||||
data: [],
|
||||
unreadChatsCount: 0,
|
||||
meta: Meta(currentPage: 1, from: null, lastPage: 3, links: [], perPage: 10, to: null, total: 30),
|
||||
);
|
||||
|
||||
when(mockChatRepository.getChatRooms(query: null, page: 1)).thenAnswer((_) async => paginationPage1);
|
||||
when(mockChatRepository.getChatRooms(query: null, page: 2)).thenThrow(Exception('Ошибка загрузки'));
|
||||
|
||||
await chatsCubit.getChatRooms();
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
await chatsCubit.loadMoreChatRooms();
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
expect(chatsCubit.state.state, BaseState.error);
|
||||
expect(chatsCubit.state.errorMessage, isNotNull);
|
||||
expect(chatsCubit.state.isLoadingMoreChats, false);
|
||||
});
|
||||
|
||||
test('getChatRooms - обрабатывает ошибку', () async {
|
||||
when(mockChatRepository.getChatRooms(query: null)).thenThrow(Exception('Network error'));
|
||||
|
||||
await chatsCubit.getChatRooms();
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
expect(chatsCubit.state.state, BaseState.error);
|
||||
expect(chatsCubit.state.errorMessage, isNotNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user