forked from eatvio/eatvio_app
refactor(tests): update model instantiation to use const for improved performance
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
import 'package:eatvio_app/core/websockets/ws_manager.dart';
|
||||
import 'package:eatvio_app/features/notifications/models/notification_model.dart';
|
||||
import 'package:eatvio_app/features/notifications/presentation/notifications_cubit/notifications_cubit.dart';
|
||||
import 'package:eatvio_app/features/notifications/presentation/notifications_cubit/notifications_state.dart';
|
||||
import 'package:eatvio_app/features/notifications/repository/notifications_repository.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import 'notifications_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateMocks([WsManager, NotificationsRepository])
|
||||
void main() {
|
||||
group('NotificationsCubit', () {
|
||||
late NotificationsCubit cubit;
|
||||
late MockWsManager mockWsManager;
|
||||
late MockNotificationsRepository mockNotificationsRepository;
|
||||
|
||||
setUp(() {
|
||||
mockWsManager = MockWsManager();
|
||||
mockNotificationsRepository = MockNotificationsRepository();
|
||||
|
||||
// Мокируем stream как null, чтобы избежать проблем с подписками в тестах
|
||||
when(mockWsManager.newNotificationsStream).thenAnswer((_) => null);
|
||||
|
||||
cubit = NotificationsCubit(
|
||||
wsManager: mockWsManager,
|
||||
notificationsRepository: mockNotificationsRepository,
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await cubit.close();
|
||||
});
|
||||
|
||||
test('начальное состояние - empty', () {
|
||||
expect(cubit.state, isA<NotificationsStateEmpty>());
|
||||
});
|
||||
|
||||
test('getNotifications - успешная загрузка уведомлений', () async {
|
||||
final notifications = [
|
||||
ChosenPlanStartedToClientNotification(
|
||||
type: NotificationType.chosenPlanStartedToClientNotification,
|
||||
planName: 'Test Plan',
|
||||
planId: 1,
|
||||
isRead: false,
|
||||
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.notifications.length, 1);
|
||||
expect(state.currentPage, 1);
|
||||
expect(state.hasNextPage, false);
|
||||
}
|
||||
verify(mockNotificationsRepository.getNotifications(page: 1)).called(1);
|
||||
});
|
||||
|
||||
test('getNotifications - обработка ошибки', () async {
|
||||
when(mockNotificationsRepository.getNotifications(page: 1))
|
||||
.thenThrow(Exception('Ошибка загрузки'));
|
||||
|
||||
await cubit.getNotifications();
|
||||
|
||||
// Состояние должно остаться empty при ошибке
|
||||
expect(cubit.state, isA<NotificationsStateEmpty>());
|
||||
});
|
||||
|
||||
test('loadMoreNotifications - успешная загрузка следующей страницы', () async {
|
||||
final firstPageNotifications = [
|
||||
ChosenPlanStartedToClientNotification(
|
||||
type: NotificationType.chosenPlanStartedToClientNotification,
|
||||
planName: 'Plan 1',
|
||||
planId: 1,
|
||||
isRead: false,
|
||||
createdAt: DateTime.now(),
|
||||
),
|
||||
];
|
||||
|
||||
final secondPageNotifications = [
|
||||
ChosenPlanStartedToClientNotification(
|
||||
type: NotificationType.chosenPlanStartedToClientNotification,
|
||||
planName: 'Plan 2',
|
||||
planId: 2,
|
||||
isRead: false,
|
||||
createdAt: DateTime.now(),
|
||||
),
|
||||
];
|
||||
|
||||
when(mockNotificationsRepository.getNotifications(page: 1))
|
||||
.thenAnswer((_) async => {
|
||||
'notifications': firstPageNotifications,
|
||||
'pagination': {'current_page': 1, 'last_page': 2},
|
||||
});
|
||||
|
||||
when(mockNotificationsRepository.getNotifications(page: 2))
|
||||
.thenAnswer((_) async => {
|
||||
'notifications': secondPageNotifications,
|
||||
'pagination': {'current_page': 2, 'last_page': 2},
|
||||
});
|
||||
|
||||
await cubit.getNotifications();
|
||||
|
||||
final firstState = cubit.state;
|
||||
expect(firstState, isA<NotificationsStateLoaded>());
|
||||
if (firstState is NotificationsStateLoaded) {
|
||||
expect(firstState.hasNextPage, true);
|
||||
|
||||
await cubit.loadMoreNotifications();
|
||||
|
||||
final secondState = cubit.state;
|
||||
expect(secondState, isA<NotificationsStateLoaded>());
|
||||
if (secondState is NotificationsStateLoaded) {
|
||||
expect(secondState.notifications.length, 2);
|
||||
expect(secondState.currentPage, 2);
|
||||
expect(secondState.hasNextPage, 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': 1},
|
||||
});
|
||||
|
||||
await cubit.getNotifications();
|
||||
await cubit.loadMoreNotifications();
|
||||
|
||||
verify(mockNotificationsRepository.getNotifications(page: 1)).called(1);
|
||||
verifyNever(mockNotificationsRepository.getNotifications(page: 2));
|
||||
});
|
||||
|
||||
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())
|
||||
.thenAnswer((_) async => {});
|
||||
|
||||
await cubit.getNotifications();
|
||||
await cubit.markNotificationsAsRead();
|
||||
|
||||
verify(mockNotificationsRepository.markNotificationsAsRead()).called(1);
|
||||
verify(mockNotificationsRepository.getNotifications(page: 1)).called(2); // Один раз при getNotifications, второй раз после markNotificationsAsRead
|
||||
});
|
||||
|
||||
test('connectToWebSocket - подключается к WebSocket', () async {
|
||||
await cubit.connectToWebSocket();
|
||||
|
||||
verify(mockWsManager.newNotificationsStream).called(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user