Yimaru-Mobile/StudioProjects/yimaru_app/test/helpers/test_helpers.dart

135 lines
4.8 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';
// @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),
// @stacked-mock-spec
],
)
void registerServices() {
getAndRegisterNavigationService();
getAndRegisterBottomSheetService();
getAndRegisterDialogService();
getAndRegisterAuthenticationService();
getAndRegisterApiService();
getAndRegisterSecureStorageService();
getAndRegisterDioService();
getAndRegisterStatusCheckerService();
// @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;
}
// @stacked-mock-create
void _removeRegistrationIfExists<T extends Object>() {
if (locator.isRegistered<T>()) {
locator.unregister<T>();
}
}