import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:mockito/annotations.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.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'; // Detta genererar MockRef i en separat fil @GenerateMocks([Ref]) import 'profile_repository_test.mocks.dart'; void main() { class MockApiClient extends Mock implements ApiClient {} late ProfileRepository profileRepository; void main() { group('ProfileRepository', () { late ProfileRepository profileRepository; late MockApiClient mockApiClient; setUp(() { mockApiClient = MockApiClient(); final mockRef = MockRef(); 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 { when(mockApiClient.getJson('/api/profile')).thenThrow(ApiException(message: 'Failed to fetch profile', type: ApiErrorType.server)); expect(() => profileRepository.getProfile(), throwsA(isA())); 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())); verify(mockApiClient.patchJson(any, profileData)).called(1); }); }); }); }