Files
eatvio_app/test/features/notifications/service/notifications_service_test.dart

174 lines
5.5 KiB
Dart

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 'notifications_service_test.mocks.dart';
@GenerateMocks([Dio])
void main() {
group('NotificationsService', () {
late NotificationsService service;
late MockDio mockDio;
setUp(() {
mockDio = MockDio();
service = 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(
requestOptions: RequestOptions(path: '/api/profile/notifications'),
));
expect(() => service.getNotifications(page: 1), throwsA(isA<DioException>()));
});
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(
requestOptions: RequestOptions(path: '/api/profile/mark_notifications_as_readed'),
));
expect(() => service.markNotificationsAsRead(), throwsA(isA<DioException>()));
});
});
}