Files
eatvio_app/test/features/notifications/service/notifications_service_test.dart
2026-01-14 18:47:11 +03:00

281 lines
8.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:dio/dio.dart';
import 'package:eatvio_app/features/notifications/service/notifications_service.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import '../../../fixtures/fixture_reader.dart';
import 'notifications_service_test.mocks.dart';
@GenerateNiceMocks([MockSpec<Dio>()])
void main() {
group('NotificationsService', () {
late NotificationsService notificationsService;
late MockDio mockDio;
setUp(() {
mockDio = MockDio();
notificationsService = NotificationsService(mockDio);
});
test('getNotifications - возвращает уведомления', () async {
const page = 1;
final json = FixtureReader.readAsMap('notifications/notifications_response.json');
final response = Response(
data: {
'data': json['notifications'],
'current_page': json['pagination']['current_page'],
'last_page': json['pagination']['last_page'],
'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, isA<Map<String, dynamic>?>());
expect(result, isNotNull);
expect(result!['notifications'], isA<List>());
verify(mockDio.get(any)).called(1);
});
test('markNotificationsAsRead - помечает уведомления как прочитанные', () async {
final response = Response(
data: {},
statusCode: 200,
requestOptions: RequestOptions(path: '/api/profile/mark_notifications_as_readed'),
);
when(mockDio.post(any))
.thenAnswer((_) async => response);
await notificationsService.markNotificationsAsRead();
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>()));
});
});
}