1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2024-12-29 15:11:58 +00:00

fix(mobile): mobile album sort not persisting (#5584)

* chore(deps): use mocktail instead of mockito

* refactor: move stubs to fixtures/

* fix: fetch assetsortmode based on storeindex

* test: validate AlbumSortByOptions provider

---------

Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
This commit is contained in:
shenlong 2023-12-10 02:31:23 +00:00 committed by GitHub
parent 188cdf9367
commit 8847ebeef2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 372 additions and 152 deletions

1
.gitignore vendored
View file

@ -11,3 +11,4 @@ coverage
mobile/gradle.properties mobile/gradle.properties
mobile/openapi/pubspec.lock mobile/openapi/pubspec.lock
mobile/*.jks mobile/*.jks
mobile/libisar.dylib

View file

@ -99,7 +99,7 @@ class AlbumSortByOptions extends _$AlbumSortByOptions {
.watch(appSettingsServiceProvider) .watch(appSettingsServiceProvider)
.getSetting(AppSettingsEnum.selectedAlbumSortOrder); .getSetting(AppSettingsEnum.selectedAlbumSortOrder);
return AlbumSortMode.values.firstWhere( return AlbumSortMode.values.firstWhere(
(e) => e.index == sortOpt, (e) => e.storeIndex == sortOpt,
orElse: () => AlbumSortMode.title, orElse: () => AlbumSortMode.title,
); );
} }

View file

@ -964,14 +964,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.4" version: "1.0.4"
mockito: mocktail:
dependency: "direct dev" dependency: "direct main"
description: description:
name: mockito name: mocktail
sha256: "7d5b53bcd556c1bc7ffbe4e4d5a19c3e112b7e925e9e172dd7c6ad0630812616" sha256: bac151b31e4ed78bd59ab89aa4c0928f297b1180186d5daf03734519e5f596c1
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "5.4.2" version: "1.0.1"
nested: nested:
dependency: transitive dependency: transitive
description: description:

View file

@ -58,6 +58,7 @@ dependencies:
wakelock_plus: ^1.1.1 wakelock_plus: ^1.1.1
flutter_local_notifications: ^15.1.0+1 flutter_local_notifications: ^15.1.0+1
timezone: ^0.9.2 timezone: ^0.9.2
mocktail: ^1.0.1
openapi: openapi:
path: openapi path: openapi
@ -84,7 +85,6 @@ dev_dependencies:
flutter_launcher_icons: "^0.9.2" flutter_launcher_icons: "^0.9.2"
flutter_native_splash: ^2.2.16 flutter_native_splash: ^2.2.16
isar_generator: *isar_version isar_generator: *isar_version
mockito: ^5.3.2
integration_test: integration_test:
sdk: flutter sdk: flutter
custom_lint: ^0.5.6 custom_lint: ^0.5.6

View file

@ -1,28 +1,25 @@
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/modules/album/providers/album_sort_by_options.provider.dart'; import 'package:immich_mobile/modules/album/providers/album_sort_by_options.provider.dart';
import 'package:immich_mobile/modules/settings/services/app_settings.service.dart';
import 'package:immich_mobile/shared/models/album.dart'; import 'package:immich_mobile/shared/models/album.dart';
import 'package:immich_mobile/shared/models/asset.dart'; import 'package:immich_mobile/shared/models/asset.dart';
import 'package:immich_mobile/shared/models/user.dart';
import 'package:isar/isar.dart'; import 'package:isar/isar.dart';
import 'package:mocktail/mocktail.dart';
import 'album.stub.dart'; import 'fixtures/album.stub.dart';
import 'asset.stub.dart'; import 'fixtures/asset.stub.dart';
import 'mocks/app_settings_provider.mock.dart';
import 'test_utils.dart';
void main() { void main() {
/// Verify the sort modes
group("AlbumSortMode", () {
late final Isar db; late final Isar db;
setUpAll(() async { setUpAll(() async {
await Isar.initializeIsarCore(download: true); db = await TestUtils.initIsar();
db = await Isar.open(
[
AssetSchema,
AlbumSchema,
UserSchema,
],
maxSizeMiB: 256,
directory: ".",
);
}); });
final albums = [ final albums = [
@ -179,4 +176,146 @@ void main() {
); );
}); });
}); });
});
/// Verify the sort mode provider
group('AlbumSortByOptions', () {
late AppSettingsService settingsMock;
late ProviderContainer container;
setUp(() async {
settingsMock = AppSettingsServiceMock();
container = TestUtils.createContainer(
overrides: [getAppSettingsServiceMock(settingsMock)],
);
});
test('Returns the default sort mode when none set', () {
// Returns the default value when nothing is set
when(
() => settingsMock.getSetting(AppSettingsEnum.selectedAlbumSortOrder),
).thenReturn(0);
expect(AlbumSortMode.created, container.read(albumSortByOptionsProvider));
});
test('Returns the correct sort mode with index from Store', () {
// Returns the default value when nothing is set
when(
() => settingsMock.getSetting(AppSettingsEnum.selectedAlbumSortOrder),
).thenReturn(3);
expect(
AlbumSortMode.lastModified,
container.read(albumSortByOptionsProvider),
);
});
test('Properly saves the correct store index of sort mode', () {
container
.read(albumSortByOptionsProvider.notifier)
.changeSortMode(AlbumSortMode.mostOldest);
verify(
() => settingsMock.setSetting(
AppSettingsEnum.selectedAlbumSortOrder,
AlbumSortMode.mostOldest.storeIndex,
),
);
});
test('Notifies listeners on state change', () {
when(
() => settingsMock.getSetting(AppSettingsEnum.selectedAlbumSortOrder),
).thenReturn(0);
final listener = ListenerMock<AlbumSortMode>();
container.listen(
albumSortByOptionsProvider,
listener,
fireImmediately: true,
);
// Created -> Most Oldest
container
.read(albumSortByOptionsProvider.notifier)
.changeSortMode(AlbumSortMode.mostOldest);
// Most Oldest -> Title
container
.read(albumSortByOptionsProvider.notifier)
.changeSortMode(AlbumSortMode.title);
verifyInOrder([
() => listener.call(null, AlbumSortMode.created),
() => listener.call(AlbumSortMode.created, AlbumSortMode.mostOldest),
() => listener.call(AlbumSortMode.mostOldest, AlbumSortMode.title),
]);
verifyNoMoreInteractions(listener);
});
});
/// Verify the sort order provider
group('AlbumSortOrder', () {
late AppSettingsService settingsMock;
late ProviderContainer container;
setUp(() async {
settingsMock = AppSettingsServiceMock();
container = TestUtils.createContainer(
overrides: [getAppSettingsServiceMock(settingsMock)],
);
});
test('Returns the default sort order when none set - false', () {
when(
() => settingsMock.getSetting(AppSettingsEnum.selectedAlbumSortReverse),
).thenReturn(false);
expect(false, container.read(albumSortOrderProvider));
});
test('Properly saves the correct order', () {
container.read(albumSortOrderProvider.notifier).changeSortDirection(true);
verify(
() => settingsMock.setSetting(
AppSettingsEnum.selectedAlbumSortReverse,
true,
),
);
});
test('Notifies listeners on state change', () {
when(
() => settingsMock.getSetting(AppSettingsEnum.selectedAlbumSortReverse),
).thenReturn(false);
final listener = ListenerMock<bool>();
container.listen(
albumSortOrderProvider,
listener,
fireImmediately: true,
);
// false -> true
container.read(albumSortOrderProvider.notifier).changeSortDirection(true);
// true -> false
container
.read(albumSortOrderProvider.notifier)
.changeSortDirection(false);
verifyInOrder([
() => listener.call(null, false),
() => listener.call(false, true),
() => listener.call(true, false),
]);
verifyNoMoreInteractions(listener);
});
});
} }

View file

@ -0,0 +1,9 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/modules/settings/providers/app_settings.provider.dart';
import 'package:immich_mobile/modules/settings/services/app_settings.service.dart';
import 'package:mocktail/mocktail.dart';
class AppSettingsServiceMock with Mock implements AppSettingsService {}
Override getAppSettingsServiceMock(AppSettingsService service) =>
appSettingsServiceProvider.overrideWith((ref) => service);

View file

@ -11,7 +11,7 @@ import 'package:immich_mobile/shared/services/hash.service.dart';
import 'package:immich_mobile/shared/services/immich_logger.service.dart'; import 'package:immich_mobile/shared/services/immich_logger.service.dart';
import 'package:immich_mobile/shared/services/sync.service.dart'; import 'package:immich_mobile/shared/services/sync.service.dart';
import 'package:isar/isar.dart'; import 'package:isar/isar.dart';
import 'package:mockito/mockito.dart'; import 'package:mocktail/mocktail.dart';
void main() { void main() {
Asset makeAsset({ Asset makeAsset({

View file

@ -0,0 +1,71 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/modules/backup/models/backup_album.model.dart';
import 'package:immich_mobile/modules/backup/models/duplicated_asset.model.dart';
import 'package:immich_mobile/shared/models/album.dart';
import 'package:immich_mobile/shared/models/android_device_asset.dart';
import 'package:immich_mobile/shared/models/asset.dart';
import 'package:immich_mobile/shared/models/etag.dart';
import 'package:immich_mobile/shared/models/exif_info.dart';
import 'package:immich_mobile/shared/models/ios_device_asset.dart';
import 'package:immich_mobile/shared/models/logger_message.model.dart';
import 'package:immich_mobile/shared/models/store.dart';
import 'package:immich_mobile/shared/models/user.dart';
import 'package:isar/isar.dart';
import 'package:mocktail/mocktail.dart';
// Listener Mock to test when a provider notifies its listeners
class ListenerMock<T> extends Mock {
// ignore: avoid-declaring-call-method
void call(T? previous, T next);
}
final class TestUtils {
const TestUtils._();
/// Downloads Isar binaries (if required) and initializes a new Isar db
static Future<Isar> initIsar() async {
await Isar.initializeIsarCore(download: true);
final db = await Isar.open(
[
StoreValueSchema,
ExifInfoSchema,
AssetSchema,
AlbumSchema,
UserSchema,
BackupAlbumSchema,
DuplicatedAssetSchema,
LoggerMessageSchema,
ETagSchema,
AndroidDeviceAssetSchema,
IOSDeviceAssetSchema,
],
maxSizeMiB: 256,
directory: ".",
);
// Clear and close db on test end
addTearDown(() async {
await db.writeTxn(() => db.clear());
await db.close();
});
return db;
}
/// Creates a new ProviderContainer to test Riverpod providers
static ProviderContainer createContainer({
ProviderContainer? parent,
List<Override> overrides = const [],
List<ProviderObserver>? observers,
}) {
final container = ProviderContainer(
parent: parent,
overrides: overrides,
observers: observers,
);
// Dispose on test end
addTearDown(container.dispose);
return container;
}
}