Files
recipe-app/flutter/test/features/profile/data/profile_repository_test.dart
T

65 lines
2.7 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mockito/mockito.dart';
import 'package:mockito/annotations.dart';
import 'package:recipe_flutter/core/api/api_client.dart';
import 'package:recipe_flutter/core/api/api_exception.dart';
import 'package:recipe_flutter/features/profile/data/profile_repository.dart';
@GenerateMocks([Ref])
void main() {}
class MockApiClient extends Mock implements ApiClient {}
void main() {
group('ProfileRepository', () {
late ProfileRepository profileRepository;
late MockApiClient mockApiClient;
setUp(() {
mockApiClient = MockApiClient();
final mockRef = MockRef(); // Ensure MockRef is generated by Mockito
profileRepository = ProfileRepository(mockApiClient, mockRef);
});
group('getProfile', () {
test('should return profile data when API call is successful', () async {
final expectedProfile = {'username': 'testuser', 'email': 'test@example.com'};
when(mockApiClient.getJson('/api/profile')).thenAnswer((_) async => expectedProfile);
final result = await profileRepository.getProfile();
expect(result, expectedProfile);
verify(mockApiClient.getJson('/api/profile')).called(1);
});
test('should throw ApiException when API call fails', () async {, type: ApiErrorType.server
when(mockApiClient.getJson('/api/profile')).thenThrow(ApiException(message: 'Failed to fetch profile'));
expect(() => profileRepository.getProfile(), throwsA(isA<ApiException>()));
verify(mockApiClient.getJson('/api/profile')).called(1);
});
});
group('updateProfile', () {
test('should return updated profile data when API call is successful', () async {
final profileData = {'username': 'newuser', 'email': 'new@example.com'};
final expectedProfile = {'username': 'newuser', 'email': 'new@example.com'};
when(mockApiClient.patchJson(any, profileData)).thenAnswer((_) async => expectedProfile);
final result = await profileRepository.updateProfile(profileData);
expect(result, expectedProfile);
verify(mockApiClient.patchJson(any, profileData)).called(1);
});
test('should throw ApiException when API call fails', () async {
final profileData = {'username': 'newuser', 'email': 'new@example.com'};
when(mockApiClient.patchJson(any, profileData)).thenThrow(ApiException(message: 'Failed to update profile', type: ApiErrorType.server));
expect(() => profileRepository.updateProfile(profileData), throwsA(isA<ApiException>()));
verify(mockApiClient.patchJson(any, profileData)).called(1);
});
});
});
}