feat(tests): updated tests coverage to 66.5%

This commit is contained in:
misha
2026-01-14 18:47:11 +03:00
parent 497ff19cfa
commit 13ccf56df0
102 changed files with 10146 additions and 379 deletions

View File

@@ -58,6 +58,202 @@ void main() {
verify(mockDio.post(any)).called(1);
});
test('getNotifications - возвращает null при statusCode != 200', () async {
const page = 1;
final response = Response(
data: {},
statusCode: 404,
requestOptions: RequestOptions(path: '/api/profile/notifications'),
);
when(mockDio.get(any)).thenAnswer((_) async => response);
final result = await notificationsService.getNotifications(page: page);
expect(result, isNull);
});
test('getNotifications - обрабатывает ошибку', () async {
const page = 1;
when(mockDio.get(any)).thenThrow(DioException(
requestOptions: RequestOptions(path: '/api/profile/notifications'),
));
expect(() => notificationsService.getNotifications(page: page), throwsA(isA<DioException>()));
});
test('getNotifications - определяет тип из поля type в корне', () async {
const page = 1;
final response = Response(
data: {
'data': [
{
'type': 'chosen-plan-started-to-client',
'data': {
'plan_name': 'Test Plan',
'plan_id': 1,
},
'read_at': null,
'created_at': '2024-01-01T12:00:00Z',
},
],
'current_page': 1,
'last_page': 1,
'per_page': 10,
'total': 1,
},
statusCode: 200,
requestOptions: RequestOptions(path: '/api/profile/notifications'),
);
when(mockDio.get(any)).thenAnswer((_) async => response);
final result = await notificationsService.getNotifications(page: page);
expect(result, isNotNull);
expect(result!['notifications'], isA<List>());
expect(result['notifications'].length, 1);
});
test('getNotifications - определяет тип из поля notification_class_name в корне', () async {
const page = 1;
final response = Response(
data: {
'data': [
{
'notification_class_name': 'App\\Notifications\\Plan\\PlanSubscriptionStartedToAuthor',
'data': {
'plan_name': 'Test Plan',
'user_name': 'Test User',
'chat_id': 'chat-1',
},
'read_at': null,
'created_at': '2024-01-01T12:00:00Z',
},
],
'current_page': 1,
'last_page': 1,
'per_page': 10,
'total': 1,
},
statusCode: 200,
requestOptions: RequestOptions(path: '/api/profile/notifications'),
);
when(mockDio.get(any)).thenAnswer((_) async => response);
final result = await notificationsService.getNotifications(page: page);
expect(result, isNotNull);
expect(result!['notifications'], isA<List>());
expect(result['notifications'].length, 1);
});
test('getNotifications - определяет тип из поля type в data', () async {
const page = 1;
final response = Response(
data: {
'data': [
{
'data': {
'type': 'diet-started',
'diet_name': 'Test Diet',
},
'read_at': null,
'created_at': '2024-01-01T12:00:00Z',
},
],
'current_page': 1,
'last_page': 1,
'per_page': 10,
'total': 1,
},
statusCode: 200,
requestOptions: RequestOptions(path: '/api/profile/notifications'),
);
when(mockDio.get(any)).thenAnswer((_) async => response);
final result = await notificationsService.getNotifications(page: page);
expect(result, isNotNull);
expect(result!['notifications'], isA<List>());
expect(result['notifications'].length, 1);
});
test('getNotifications - обрабатывает ошибку парсинга отдельного уведомления', () async {
const page = 1;
final response = Response(
data: {
'data': [
{
'type': 'invalid-type',
'data': null, // Invalid data
'read_at': null,
'created_at': '2024-01-01T12:00:00Z',
},
],
'current_page': 1,
'last_page': 1,
'per_page': 10,
'total': 1,
},
statusCode: 200,
requestOptions: RequestOptions(path: '/api/profile/notifications'),
);
when(mockDio.get(any)).thenAnswer((_) async => response);
final result = await notificationsService.getNotifications(page: page);
expect(result, isNotNull);
expect(result!['notifications'], isA<List>());
// При ошибке парсинга возвращается UnknownNotification
expect(result['notifications'].length, 1);
});
test('getNotifications - обрабатывает уведомление с read_at', () async {
const page = 1;
final response = Response(
data: {
'data': [
{
'type': 'chosen-plan-started-to-client',
'data': {
'plan_name': 'Test Plan',
'plan_id': 1,
},
'read_at': '2024-01-01T13:00:00Z',
'created_at': '2024-01-01T12:00:00Z',
},
],
'current_page': 1,
'last_page': 1,
'per_page': 10,
'total': 1,
},
statusCode: 200,
requestOptions: RequestOptions(path: '/api/profile/notifications'),
);
when(mockDio.get(any)).thenAnswer((_) async => response);
final result = await notificationsService.getNotifications(page: page);
expect(result, isNotNull);
expect(result!['notifications'], isA<List>());
expect(result['notifications'].length, 1);
});
test('markNotificationsAsRead - обрабатывает ошибку', () async {
when(mockDio.post(any)).thenThrow(DioException(
requestOptions: RequestOptions(path: '/api/profile/mark_notifications_as_readed'),
));
expect(() => notificationsService.markNotificationsAsRead(), throwsA(isA<DioException>()));
});
});
}