Yimaru-Mobile/test/helpers/test_helpers.dart

177 lines
6.5 KiB
Dart

import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:yimaru_app/app/app.locator.dart';
import 'package:stacked_services/stacked_services.dart';
import 'package:yimaru_app/services/authentication_service.dart';
import 'package:yimaru_app/services/api_service.dart';
import 'package:yimaru_app/services/secure_storage_service.dart';
import 'package:yimaru_app/services/dio_service.dart';
import 'package:yimaru_app/services/status_checker_service.dart';
import 'package:yimaru_app/services/permission_handler_service.dart';
import 'package:yimaru_app/services/image_picker_service.dart';
import 'package:yimaru_app/services/google_auth_service.dart';
import 'package:yimaru_app/services/image_downloader_service.dart';
// @stacked-import
import 'test_helpers.mocks.dart';
@GenerateMocks(
[],
customMocks: [
MockSpec<NavigationService>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<BottomSheetService>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<DialogService>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<AuthenticationService>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<ApiService>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<SecureStorageService>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<DioService>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<StatusCheckerService>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<PermissionHandlerService>(
onMissingStub: OnMissingStub.returnDefault),
MockSpec<ImagePickerService>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<GoogleAuthService>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<ImageDownloaderService>(
onMissingStub: OnMissingStub.returnDefault),
// @stacked-mock-spec
],
)
void registerServices() {
getAndRegisterNavigationService();
getAndRegisterBottomSheetService();
getAndRegisterDialogService();
getAndRegisterAuthenticationService();
getAndRegisterApiService();
getAndRegisterSecureStorageService();
getAndRegisterDioService();
getAndRegisterStatusCheckerService();
getAndRegisterPermissionHandlerService();
getAndRegisterImagePickerService();
getAndRegisterGoogleAuthService();
getAndRegisterImageDownloaderService();
// @stacked-mock-register
}
MockNavigationService getAndRegisterNavigationService() {
_removeRegistrationIfExists<NavigationService>();
final service = MockNavigationService();
locator.registerSingleton<NavigationService>(service);
return service;
}
MockBottomSheetService getAndRegisterBottomSheetService<T>({
SheetResponse<T>? showCustomSheetResponse,
}) {
_removeRegistrationIfExists<BottomSheetService>();
final service = MockBottomSheetService();
when(
service.showCustomSheet<T, T>(
enableDrag: anyNamed('enableDrag'),
enterBottomSheetDuration: anyNamed('enterBottomSheetDuration'),
exitBottomSheetDuration: anyNamed('exitBottomSheetDuration'),
ignoreSafeArea: anyNamed('ignoreSafeArea'),
isScrollControlled: anyNamed('isScrollControlled'),
barrierDismissible: anyNamed('barrierDismissible'),
additionalButtonTitle: anyNamed('additionalButtonTitle'),
variant: anyNamed('variant'),
title: anyNamed('title'),
hasImage: anyNamed('hasImage'),
imageUrl: anyNamed('imageUrl'),
showIconInMainButton: anyNamed('showIconInMainButton'),
mainButtonTitle: anyNamed('mainButtonTitle'),
showIconInSecondaryButton: anyNamed('showIconInSecondaryButton'),
secondaryButtonTitle: anyNamed('secondaryButtonTitle'),
showIconInAdditionalButton: anyNamed('showIconInAdditionalButton'),
takesInput: anyNamed('takesInput'),
barrierColor: anyNamed('barrierColor'),
barrierLabel: anyNamed('barrierLabel'),
customData: anyNamed('customData'),
data: anyNamed('data'),
description: anyNamed('description'),
),
).thenAnswer(
(realInvocation) =>
Future.value(showCustomSheetResponse ?? SheetResponse<T>()),
);
locator.registerSingleton<BottomSheetService>(service);
return service;
}
MockDialogService getAndRegisterDialogService() {
_removeRegistrationIfExists<DialogService>();
final service = MockDialogService();
locator.registerSingleton<DialogService>(service);
return service;
}
MockAuthenticationService getAndRegisterAuthenticationService() {
_removeRegistrationIfExists<AuthenticationService>();
final service = MockAuthenticationService();
locator.registerSingleton<AuthenticationService>(service);
return service;
}
MockApiService getAndRegisterApiService() {
_removeRegistrationIfExists<ApiService>();
final service = MockApiService();
locator.registerSingleton<ApiService>(service);
return service;
}
MockSecureStorageService getAndRegisterSecureStorageService() {
_removeRegistrationIfExists<SecureStorageService>();
final service = MockSecureStorageService();
locator.registerSingleton<SecureStorageService>(service);
return service;
}
MockDioService getAndRegisterDioService() {
_removeRegistrationIfExists<DioService>();
final service = MockDioService();
locator.registerSingleton<DioService>(service);
return service;
}
MockStatusCheckerService getAndRegisterStatusCheckerService() {
_removeRegistrationIfExists<StatusCheckerService>();
final service = MockStatusCheckerService();
locator.registerSingleton<StatusCheckerService>(service);
return service;
}
MockPermissionHandlerService getAndRegisterPermissionHandlerService() {
_removeRegistrationIfExists<PermissionHandlerService>();
final service = MockPermissionHandlerService();
locator.registerSingleton<PermissionHandlerService>(service);
return service;
}
MockImagePickerService getAndRegisterImagePickerService() {
_removeRegistrationIfExists<ImagePickerService>();
final service = MockImagePickerService();
locator.registerSingleton<ImagePickerService>(service);
return service;
}
MockGoogleAuthService getAndRegisterGoogleAuthService() {
_removeRegistrationIfExists<GoogleAuthService>();
final service = MockGoogleAuthService();
locator.registerSingleton<GoogleAuthService>(service);
return service;
}
MockImageDownloaderService getAndRegisterImageDownloaderService() {
_removeRegistrationIfExists<ImageDownloaderService>();
final service = MockImageDownloaderService();
locator.registerSingleton<ImageDownloaderService>(service);
return service;
}
// @stacked-mock-create
void _removeRegistrationIfExists<T extends Object>() {
if (locator.isRegistered<T>()) {
locator.unregister<T>();
}
}