refactor(mobile): use user service methods (#16783)

* refactor: user entity

* chore: rebase fixes

* refactor(mobile): refactor to use user service methods

* fix: late init error

* fix: lint

---------

Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
shenlong
2025-03-18 19:02:33 +05:30
committed by GitHub
parent 6c2985df26
commit dd263b010c
17 changed files with 137 additions and 143 deletions

View File

@@ -27,12 +27,16 @@ void main() {
userApiRepository: mockUserApiRepo,
storeService: mockStoreService,
);
registerFallbackValue(UserStub.admin);
when(() => mockStoreService.get(StoreKey.currentUser))
.thenReturn(UserStub.admin);
when(() => mockStoreService.tryGet(StoreKey.currentUser))
.thenReturn(UserStub.admin);
});
group('getMyUser', () {
test('should return user from store', () {
when(() => mockStoreService.get(StoreKey.currentUser))
.thenReturn(UserStub.admin);
final result = sut.getMyUser();
expect(result, UserStub.admin);
});
@@ -47,8 +51,6 @@ void main() {
group('tryGetMyUser', () {
test('should return user from store', () {
when(() => mockStoreService.tryGet(StoreKey.currentUser))
.thenReturn(UserStub.admin);
final result = sut.tryGetMyUser();
expect(result, UserStub.admin);
});
@@ -107,26 +109,48 @@ void main() {
group('createProfileImage', () {
test('should return profile image path', () async {
const profileImagePath = 'profile.jpg';
final updatedUser =
UserStub.admin.copyWith(profileImagePath: profileImagePath);
when(
() => mockUserApiRepo.createProfileImage(
name: 'profile.jpg',
name: profileImagePath,
data: Uint8List(0),
),
).thenAnswer((_) async => 'profile.jpg');
).thenAnswer((_) async => profileImagePath);
when(() => mockStoreService.put(StoreKey.currentUser, updatedUser))
.thenAnswer((_) async => true);
when(() => mockUserRepo.update(updatedUser))
.thenAnswer((_) async => UserStub.admin);
final result = await sut.createProfileImage('profile.jpg', Uint8List(0));
expect(result, 'profile.jpg');
final result =
await sut.createProfileImage(profileImagePath, Uint8List(0));
verify(() => mockStoreService.put(StoreKey.currentUser, updatedUser))
.called(1);
verify(() => mockUserRepo.update(updatedUser)).called(1);
expect(result, profileImagePath);
});
test('should return null if profile image creation fails', () async {
const profileImagePath = 'profile.jpg';
final updatedUser =
UserStub.admin.copyWith(profileImagePath: profileImagePath);
when(
() => mockUserApiRepo.createProfileImage(
name: 'profile.jpg',
name: profileImagePath,
data: Uint8List(0),
),
).thenThrow(Exception('Failed to create profile image'));
final result = await sut.createProfileImage('profile.jpg', Uint8List(0));
final result =
await sut.createProfileImage(profileImagePath, Uint8List(0));
verifyNever(
() => mockStoreService.put(StoreKey.currentUser, updatedUser),
);
verifyNever(() => mockUserRepo.update(updatedUser));
expect(result, isNull);
});
});

View File

@@ -15,6 +15,7 @@ import 'package:immich_mobile/interfaces/partner_api.interface.dart';
import 'package:immich_mobile/services/sync.service.dart';
import 'package:mocktail/mocktail.dart';
import '../../domain/service.mock.dart';
import '../../infrastructure/repository.mock.dart';
import '../../repository.mocks.dart';
import '../../service.mocks.dart';
@@ -62,6 +63,7 @@ void main() {
MockPartnerApiRepository();
final MockUserApiRepository userApiRepository = MockUserApiRepository();
final MockPartnerRepository partnerRepository = MockPartnerRepository();
final MockUserService userService = MockUserService();
final owner = UserDto(
uid: "1",
@@ -101,11 +103,12 @@ void main() {
exifInfoRepository,
partnerRepository,
userRepository,
StoreService.I,
userService,
eTagRepository,
partnerApiRepository,
userApiRepository,
);
when(() => userService.getMyUser()).thenReturn(owner);
when(() => eTagRepository.get(owner.id))
.thenAnswer((_) async => ETag(id: owner.uid, time: DateTime.now()));
when(() => eTagRepository.deleteByIds(["1"])).thenAnswer((_) async {});

View File

@@ -31,6 +31,8 @@ void main() {
albumMediaRepository = MockAlbumMediaRepository();
albumApiRepository = MockAlbumApiRepository();
when(() => userService.getMyUser()).thenReturn(UserStub.user1);
when(() => albumRepository.transaction<void>(any())).thenAnswer(
(call) => (call.positionalArguments.first as Function).call(),
);
@@ -40,6 +42,7 @@ void main() {
sut = AlbumService(
syncService,
userService,
entityService,
albumRepository,
assetRepository,