feat(tests): added services tests and fixtures

This commit is contained in:
misha
2025-12-11 15:53:35 +03:00
parent d1753e99f8
commit 070a3e39e7
65 changed files with 2092 additions and 1130 deletions

View File

@@ -1,173 +1,62 @@
import 'package:dio/dio.dart';
import 'package:eatvio_app/features/notifications/models/notification_model.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';
@GenerateMocks([Dio])
@GenerateNiceMocks([MockSpec<Dio>()])
void main() {
group('NotificationsService', () {
late NotificationsService service;
late NotificationsService notificationsService;
late MockDio mockDio;
setUp(() {
mockDio = MockDio();
service = NotificationsService(mockDio);
notificationsService = NotificationsService(mockDio);
});
test('getNotifications returns null when status code is not 200', () async {
when(mockDio.get(any)).thenAnswer((_) async => Response(
requestOptions: RequestOptions(path: '/api/profile/notifications'),
statusCode: 404,
data: {},
));
final result = await service.getNotifications(page: 1);
expect(result, isNull);
});
test('getNotifications parses notifications correctly', () async {
when(mockDio.get(any)).thenAnswer((_) async => Response(
requestOptions: RequestOptions(path: '/api/profile/notifications'),
statusCode: 200,
data: {
'data': [
{
'type': 'diary-reminder-notification',
'read_at': null,
'created_at': '2024-01-15T10:30:00Z',
'data': {},
}
],
'current_page': 1,
'last_page': 1,
'per_page': 10,
'total': 1,
},
));
final result = await service.getNotifications(page: 1);
expect(result, isNotNull);
expect(result!['notifications'], isA<List>());
expect(result['pagination'], isA<Map>());
expect(result['pagination']['current_page'], 1);
});
test('getNotifications handles unknown notification type', () async {
when(mockDio.get(any)).thenAnswer((_) async => Response(
requestOptions: RequestOptions(path: '/api/profile/notifications'),
statusCode: 200,
data: {
'data': [
{
'type': 'unknown-type',
'read_at': null,
'created_at': '2024-01-15T10:30:00Z',
'data': {},
}
],
'current_page': 1,
'last_page': 1,
'per_page': 10,
'total': 1,
},
));
final result = await service.getNotifications(page: 1);
expect(result, isNotNull);
expect(result!['notifications'], isA<List>());
final notifications = result['notifications'] as List;
expect(notifications.first, isA<UnknownNotification>());
});
test('getNotifications handles notification_class_name field', () async {
when(mockDio.get(any)).thenAnswer((_) async => Response(
requestOptions: RequestOptions(path: '/api/profile/notifications'),
statusCode: 200,
data: {
'data': [
{
'notification_class_name': 'App\\Notifications\\DiaryReminderNotification',
'read_at': null,
'created_at': '2024-01-15T10:30:00Z',
'data': {},
}
],
'current_page': 1,
'last_page': 1,
'per_page': 10,
'total': 1,
},
));
final result = await service.getNotifications(page: 1);
expect(result, isNotNull);
expect(result!['notifications'], isA<List>());
});
test('getNotifications handles read_at correctly', () async {
when(mockDio.get(any)).thenAnswer((_) async => Response(
requestOptions: RequestOptions(path: '/api/profile/notifications'),
statusCode: 200,
data: {
'data': [
{
'type': 'diary-reminder-notification',
'read_at': '2024-01-15T10:30:00Z',
'created_at': '2024-01-15T10:30:00Z',
'data': {},
}
],
'current_page': 1,
'last_page': 1,
'per_page': 10,
'total': 1,
},
));
final result = await service.getNotifications(page: 1);
expect(result, isNotNull);
expect(result!['notifications'], isA<List>());
});
test('getNotifications rethrows exceptions', () async {
when(mockDio.get(any)).thenThrow(DioException(
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'),
));
);
expect(() => service.getNotifications(page: 1), throwsA(isA<DioException>()));
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 calls correct endpoint', () async {
when(mockDio.post(any)).thenAnswer((_) async => Response(
requestOptions: RequestOptions(path: '/api/profile/mark_notifications_as_readed'),
statusCode: 200,
));
await service.markNotificationsAsRead();
verify(mockDio.post('/api/profile/mark_notifications_as_readed')).called(1);
});
test('markNotificationsAsRead rethrows exceptions', () async {
when(mockDio.post(any)).thenThrow(DioException(
test('markNotificationsAsRead - помечает уведомления как прочитанные', () async {
final response = Response(
data: {},
statusCode: 200,
requestOptions: RequestOptions(path: '/api/profile/mark_notifications_as_readed'),
));
);
expect(() => service.markNotificationsAsRead(), throwsA(isA<DioException>()));
when(mockDio.post(any))
.thenAnswer((_) async => response);
await notificationsService.markNotificationsAsRead();
verify(mockDio.post(any)).called(1);
});
});
}