test(tests): add extensive widget tests for MarkdownEditor and CommentsActionsBottomSheet, improving test coverage and functionality validation

This commit is contained in:
mso7nueh
2025-12-11 21:19:41 +03:00
parent d1753e99f8
commit c378c7fc16
44 changed files with 1783 additions and 2 deletions

View File

@@ -189,6 +189,225 @@ void main() {
verify(mockWsManager.newNotificationsStream).called(1);
});
test('loadMoreNotifications - обработка ошибки при загрузке', () async {
final notifications = [
ChosenPlanStartedToClientNotification(
type: NotificationType.chosenPlanStartedToClientNotification,
planName: 'Plan 1',
planId: 1,
isRead: false,
createdAt: DateTime.now(),
),
];
when(mockNotificationsRepository.getNotifications(page: 1))
.thenAnswer((_) async => {
'notifications': notifications,
'pagination': {'current_page': 1, 'last_page': 2},
});
when(mockNotificationsRepository.getNotifications(page: 2))
.thenThrow(Exception('Ошибка загрузки'));
await cubit.getNotifications();
await cubit.loadMoreNotifications();
final state = cubit.state;
expect(state, isA<NotificationsStateLoaded>());
if (state is NotificationsStateLoaded) {
expect(state.isLoadingMore, false);
}
});
test('loadMoreNotifications - не загружает если уже загружается', () async {
final notifications = [
ChosenPlanStartedToClientNotification(
type: NotificationType.chosenPlanStartedToClientNotification,
planName: 'Plan 1',
planId: 1,
isRead: false,
createdAt: DateTime.now(),
),
];
when(mockNotificationsRepository.getNotifications(page: 1))
.thenAnswer((_) async => {
'notifications': notifications,
'pagination': {'current_page': 1, 'last_page': 2},
});
when(mockNotificationsRepository.getNotifications(page: 2))
.thenAnswer((_) async {
await Future.delayed(const Duration(milliseconds: 50));
return {
'notifications': notifications,
'pagination': {'current_page': 2, 'last_page': 2},
};
});
await cubit.getNotifications();
// Вызываем loadMoreNotifications дважды быстро
final future1 = cubit.loadMoreNotifications();
// Небольшая задержка, чтобы первый вызов успел установить isLoadingMore
await Future.delayed(const Duration(milliseconds: 10));
final future2 = cubit.loadMoreNotifications();
await future1;
await future2;
// Должен быть вызван только один раз, так как второй вызов должен быть заблокирован isLoadingMore
verify(mockNotificationsRepository.getNotifications(page: 2)).called(1);
});
test('markNotificationsAsRead - обработка ошибки', () async {
final notifications = [
ChosenPlanStartedToClientNotification(
type: NotificationType.chosenPlanStartedToClientNotification,
planName: 'Plan 1',
planId: 1,
isRead: false,
createdAt: DateTime.now(),
),
];
when(mockNotificationsRepository.getNotifications(page: 1))
.thenAnswer((_) async => {
'notifications': notifications,
'pagination': {'current_page': 1, 'last_page': 1},
});
when(mockNotificationsRepository.markNotificationsAsRead())
.thenThrow(Exception('Ошибка пометки'));
await cubit.getNotifications();
await cubit.markNotificationsAsRead();
final state = cubit.state;
expect(state, isA<NotificationsStateLoaded>());
if (state is NotificationsStateLoaded) {
expect(state.isLoading, false);
}
});
test('getNotifications - возвращает null от репозитория', () async {
when(mockNotificationsRepository.getNotifications(page: 1))
.thenAnswer((_) async => null);
await cubit.getNotifications();
// Состояние должно остаться empty
expect(cubit.state, isA<NotificationsStateEmpty>());
});
test('loadMoreNotifications - возвращает null от репозитория', () async {
final notifications = [
ChosenPlanStartedToClientNotification(
type: NotificationType.chosenPlanStartedToClientNotification,
planName: 'Plan 1',
planId: 1,
isRead: false,
createdAt: DateTime.now(),
),
];
when(mockNotificationsRepository.getNotifications(page: 1))
.thenAnswer((_) async => {
'notifications': notifications,
'pagination': {'current_page': 1, 'last_page': 2},
});
when(mockNotificationsRepository.getNotifications(page: 2))
.thenAnswer((_) async => null);
await cubit.getNotifications();
await cubit.loadMoreNotifications();
final state = cubit.state;
expect(state, isA<NotificationsStateLoaded>());
if (state is NotificationsStateLoaded) {
expect(state.isLoadingMore, false);
expect(state.notifications.length, 1); // Только первая страница
}
});
test('getNotifications - проверяет hasUnreadNotifications', () async {
final notifications = [
ChosenPlanStartedToClientNotification(
type: NotificationType.chosenPlanStartedToClientNotification,
planName: 'Plan 1',
planId: 1,
isRead: false,
createdAt: DateTime.now(),
),
ChosenPlanStartedToClientNotification(
type: NotificationType.chosenPlanStartedToClientNotification,
planName: 'Plan 2',
planId: 2,
isRead: true,
createdAt: DateTime.now(),
),
];
final result = {
'notifications': notifications,
'pagination': {
'current_page': 1,
'last_page': 1,
},
};
when(mockNotificationsRepository.getNotifications(page: 1))
.thenAnswer((_) async => result);
await cubit.getNotifications();
final state = cubit.state;
expect(state, isA<NotificationsStateLoaded>());
if (state is NotificationsStateLoaded) {
expect(state.hasUnreadNotifications, true); // Есть непрочитанное
}
});
test('getNotifications - hasUnreadNotifications false когда все прочитаны', () async {
final notifications = [
ChosenPlanStartedToClientNotification(
type: NotificationType.chosenPlanStartedToClientNotification,
planName: 'Plan 1',
planId: 1,
isRead: true,
createdAt: DateTime.now(),
),
ChosenPlanStartedToClientNotification(
type: NotificationType.chosenPlanStartedToClientNotification,
planName: 'Plan 2',
planId: 2,
isRead: true,
createdAt: DateTime.now(),
),
];
final result = {
'notifications': notifications,
'pagination': {
'current_page': 1,
'last_page': 1,
},
};
when(mockNotificationsRepository.getNotifications(page: 1))
.thenAnswer((_) async => result);
await cubit.getNotifications();
final state = cubit.state;
expect(state, isA<NotificationsStateLoaded>());
if (state is NotificationsStateLoaded) {
expect(state.hasUnreadNotifications, false); // Все прочитаны
}
});
});
}