test(installed-apps): add InstalledAppsController unit tests (3 cases)
This commit is contained in:
@@ -0,0 +1,110 @@
|
|||||||
|
import 'package:device_management/src/core/domain/repositories/installed_apps_repository.dart';
|
||||||
|
import 'package:device_management/src/core/providers/installed_apps_repository_provider.dart';
|
||||||
|
import 'package:device_management/src/features/installed_apps/presentation/providers/installed_apps_controller.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:sf_infrastructure/sf_infrastructure.dart';
|
||||||
|
import 'package:sf_shared/testing.dart';
|
||||||
|
import 'package:sf_tracking/sf_tracking.dart';
|
||||||
|
|
||||||
|
class MockInstalledAppsRepository extends Mock
|
||||||
|
implements InstalledAppsRepository {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
ProviderContainer buildContainer(InstalledAppsRepository repository) {
|
||||||
|
return makeContainer(
|
||||||
|
overrides: [
|
||||||
|
installedAppsRepositoryProvider.overrideWithValue(repository),
|
||||||
|
sfTrackingProvider.overrideWithValue(
|
||||||
|
SfTrackingRepository(clients: const []),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
group('InstalledAppsController.toggleApp', () {
|
||||||
|
test('transitions to AsyncData on success', () async {
|
||||||
|
final repository = MockInstalledAppsRepository();
|
||||||
|
when(
|
||||||
|
() => repository.updateInstalledApps(
|
||||||
|
identificator: any(named: 'identificator'),
|
||||||
|
apps: any(named: 'apps'),
|
||||||
|
),
|
||||||
|
).thenAnswer((_) async => []);
|
||||||
|
|
||||||
|
final container = buildContainer(repository);
|
||||||
|
addTearDown(container.dispose);
|
||||||
|
|
||||||
|
await container
|
||||||
|
.read(installedAppsControllerProvider.notifier)
|
||||||
|
.toggleApp(
|
||||||
|
identificator: 'imei-1',
|
||||||
|
appUid: 'app-1',
|
||||||
|
isEnabled: false,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
container.read(installedAppsControllerProvider),
|
||||||
|
isA<AsyncData<void>>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('calls repository with correct parameters', () async {
|
||||||
|
final repository = MockInstalledAppsRepository();
|
||||||
|
when(
|
||||||
|
() => repository.updateInstalledApps(
|
||||||
|
identificator: any(named: 'identificator'),
|
||||||
|
apps: any(named: 'apps'),
|
||||||
|
),
|
||||||
|
).thenAnswer((_) async => []);
|
||||||
|
|
||||||
|
final container = buildContainer(repository);
|
||||||
|
addTearDown(container.dispose);
|
||||||
|
|
||||||
|
await container
|
||||||
|
.read(installedAppsControllerProvider.notifier)
|
||||||
|
.toggleApp(
|
||||||
|
identificator: 'imei-1',
|
||||||
|
appUid: 'app-1',
|
||||||
|
isEnabled: false,
|
||||||
|
);
|
||||||
|
|
||||||
|
verify(
|
||||||
|
() => repository.updateInstalledApps(
|
||||||
|
identificator: 'imei-1',
|
||||||
|
apps: [
|
||||||
|
{'appUid': 'app-1', 'isEnabled': false},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
).called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('exposes AsyncError when repository fails', () async {
|
||||||
|
final repository = MockInstalledAppsRepository();
|
||||||
|
when(
|
||||||
|
() => repository.updateInstalledApps(
|
||||||
|
identificator: any(named: 'identificator'),
|
||||||
|
apps: any(named: 'apps'),
|
||||||
|
),
|
||||||
|
).thenThrow(
|
||||||
|
const ApiException(message: 'boom', isNetworkError: true));
|
||||||
|
|
||||||
|
final container = buildContainer(repository);
|
||||||
|
addTearDown(container.dispose);
|
||||||
|
|
||||||
|
await container
|
||||||
|
.read(installedAppsControllerProvider.notifier)
|
||||||
|
.toggleApp(
|
||||||
|
identificator: 'imei-1',
|
||||||
|
appUid: 'app-1',
|
||||||
|
isEnabled: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
container.read(installedAppsControllerProvider),
|
||||||
|
isA<AsyncError<void>>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user