Files
recipe-app/flutter/test/features/profile/data/profile_repository_test.dart
T
Nils-Johan Gynther aefc8804ad Add unit tests for ProfileRepository and implement new shaders
- Created `NativeAssetsManifest.json` and added font and shader assets for unit tests.
- Implemented `ink_sparkle.frag` and `stretch_effect.frag` shaders for visual effects.
- Developed unit tests for `ProfileRepository` to validate API interactions for fetching and updating user profiles.
- Utilized Mockito for mocking API client responses in tests.
2026-04-23 17:50:48 +02:00

59 lines
2.5 KiB
Dart

import 'package:flutter_test/flutter_test.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';
class MockApiClient extends Mock implements ApiClient {}
void main() {
group('ProfileRepository', () {
late ProfileRepository profileRepository;
late MockApiClient mockApiClient;
setUp(() {
mockApiClient = MockApiClient();
profileRepository = ProfileRepository(mockApiClient);
});
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('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('/api/profile', profileData)).thenAnswer((_) async => expectedProfile);
final result = await profileRepository.updateProfile(profileData);
expect(result, expectedProfile);
verify(mockApiClient.patchJson('/api/profile', 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('Failed to update profile'));
expect(() => profileRepository.updateProfile(profileData), throwsA(isA<ApiException>()));
verify(mockApiClient.patchJson('/api/profile', profileData)).called(1);
});
});
});
}