Refactor code structure for improved readability and maintainability

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Nils-Johan Gynther
2026-04-23 21:14:46 +02:00
parent cd4274575e
commit db1128ceaf
49 changed files with 285993 additions and 175 deletions
@@ -1,70 +1,83 @@
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';
import 'package:recipe_flutter/features/profile/domain/user_profile.dart';
class _FakeApiClient extends ApiClient {
dynamic _nextResponse;
Exception? _nextError;
void setResponse(dynamic response) {
_nextResponse = response;
_nextError = null;
}
void setError(Exception error) {
_nextError = error;
_nextResponse = null;
}
@override
Future<dynamic> getJson(String path, {String? token}) async {
if (_nextError != null) throw _nextError!;
return _nextResponse;
}
@override
Future<dynamic> patchJson(String path, {Object? body, String? token}) async {
if (_nextError != null) throw _nextError!;
return _nextResponse;
}
}
// Detta genererar MockRef i en separat fil
@GenerateMocks([Ref])
import 'profile_repository_test.mocks.dart';
void main() {
late _FakeApiClient fakeClient;
late ProviderContainer container;
late ProfileRepository repo;
class MockApiClient extends Mock implements ApiClient {}
late ProfileRepository profileRepository;
void main() {
group('ProfileRepository', () {
late ProfileRepository profileRepository;
late MockApiClient mockApiClient;
final profileJson = {
'id': 1,
'username': 'testuser',
'email': 'test@example.com',
'role': 'user',
'isPremium': false,
};
setUp(() {
mockApiClient = MockApiClient();
final mockRef = MockRef();
profileRepository = ProfileRepository(mockApiClient, mockRef);
setUp(() {
fakeClient = _FakeApiClient();
container = ProviderContainer(
overrides: [apiClientProvider.overrideWithValue(fakeClient)],
);
repo = container.read(profileRepositoryProvider);
});
tearDown(() => container.dispose());
group('getMe', () {
test('returns UserProfile on success', () async {
fakeClient.setResponse(profileJson);
final result = await repo.getMe();
expect(result, isA<UserProfile>());
expect(result.username, 'testuser');
});
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<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);
});
test('rethrows ApiException on failure', () async {
fakeClient.setError(const ApiException(
message: 'Unauthorized',
type: ApiErrorType.unauthorized,
));
expect(() => repo.getMe(), throwsA(isA<ApiException>()));
});
});
}
group('updateMe', () {
test('returns updated UserProfile on success', () async {
final updated = {...profileJson, 'email': 'new@example.com'};
fakeClient.setResponse(updated);
final result = await repo.updateMe(email: 'new@example.com');
expect(result.email, 'new@example.com');
});
});
}