From 5644101b68a90517b0d6d7c0ceb9b1effab2dbef Mon Sep 17 00:00:00 2001 From: Nils-Johan Gynther Date: Thu, 23 Apr 2026 18:01:13 +0200 Subject: [PATCH] refactor(profile): update ProfileRepository to include MockRef for improved testing and enhance error handling in API calls --- flutter/lib/core/api/api_client.dart | 2 +- .../profile/data/profile_repository_test.dart | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/flutter/lib/core/api/api_client.dart b/flutter/lib/core/api/api_client.dart index a295c109..0bfa05c0 100644 --- a/flutter/lib/core/api/api_client.dart +++ b/flutter/lib/core/api/api_client.dart @@ -24,7 +24,7 @@ class ApiClient { if (token != null) 'Authorization': 'Bearer $token', }; - Future getJson(String path, {String? token}) async { + Future> getJson(String path, {String? token}) async { final response = await _client.get( Uri.parse('$baseUrl$path'), headers: _headers(token: token), diff --git a/flutter/test/features/profile/data/profile_repository_test.dart b/flutter/test/features/profile/data/profile_repository_test.dart index 7d393809..17f09591 100644 --- a/flutter/test/features/profile/data/profile_repository_test.dart +++ b/flutter/test/features/profile/data/profile_repository_test.dart @@ -13,7 +13,8 @@ void main() { setUp(() { mockApiClient = MockApiClient(); - profileRepository = ProfileRepository(mockApiClient); + final mockRef = MockRef(); + profileRepository = ProfileRepository(mockApiClient, mockRef); }); group('getProfile', () { @@ -27,7 +28,7 @@ void main() { verify(mockApiClient.getJson('/api/profile')).called(1); }); - test('should throw ApiException when API call fails', () async { + 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())); @@ -39,20 +40,20 @@ void main() { 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('/api/profile', profileData)).thenAnswer((_) async => expectedProfile); + when(mockApiClient.patchJson(any, profileData)).thenAnswer((_) async => expectedProfile); final result = await profileRepository.updateProfile(profileData); expect(result, expectedProfile); - verify(mockApiClient.patchJson('/api/profile', profileData)).called(1); + 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('/api/profile', profileData)).thenThrow(ApiException(message: 'Failed to update profile')); + when(mockApiClient.patchJson(any, profileData)).thenThrow(ApiException(message: 'Failed to update profile', type: ApiErrorType.server)); expect(() => profileRepository.updateProfile(profileData), throwsA(isA())); - verify(mockApiClient.patchJson('/api/profile', profileData)).called(1); + verify(mockApiClient.patchJson(any, profileData)).called(1); }); }); });