| 1 | import 'package:cake_wallet/core/auth_service.dart'; |
| 2 | import 'package:cake_wallet/core/secure_storage.dart'; |
| 3 | import 'package:cake_wallet/di.dart'; |
| 4 | import 'package:cake_wallet/store/app_store.dart'; |
| 5 | import 'package:cake_wallet/store/authentication_store.dart'; |
| 6 | import 'package:cake_wallet/store/settings_store.dart'; |
| 7 | import 'package:cake_wallet/store/wallet_list_store.dart'; |
| 8 | import 'package:cake_wallet/view_model/link_view_model.dart'; |
| 9 | import 'package:hive/hive.dart'; |
| 10 | import 'package:mocktail/mocktail.dart'; |
| 11 | |
| 12 | import 'mocks.dart'; |
| 13 | |
| 14 | class TestHelpers { |
| 15 | static void setup() { |
| 16 | // Fallback values can also be declared here |
| 17 | registerDependencies(); |
| 18 | } |
| 19 | |
| 20 | static void registerDependencies() { |
| 21 | getAndRegisterAppStore(); |
| 22 | getAndRegisterAuthService(); |
| 23 | getAndRegisterSettingsStore(); |
| 24 | getAndRegisterAuthenticationStore(); |
| 25 | getAndRegisterWalletListStore(); |
| 26 | |
| 27 | getAndRegisterLinkViewModel(); |
| 28 | getAndRegisterSecureStorage(); |
| 29 | getAndRegisterHiveInterface(); |
| 30 | } |
| 31 | |
| 32 | static MockSettingsStore getAndRegisterSettingsStore() { |
| 33 | _removeRegistrationIfExists<SettingsStore>(); |
| 34 | final service = MockSettingsStore(); |
| 35 | getIt.registerSingleton<SettingsStore>(service); |
| 36 | return service; |
| 37 | } |
| 38 | |
| 39 | static MockAppStore getAndRegisterAppStore() { |
| 40 | _removeRegistrationIfExists<AppStore>(); |
| 41 | final service = MockAppStore(); |
| 42 | final settingsStore = getAndRegisterSettingsStore(); |
| 43 | |
| 44 | when(() => service.settingsStore).thenAnswer((invocation) => settingsStore); |
| 45 | getIt.registerSingleton<AppStore>(service); |
| 46 | return service; |
| 47 | } |
| 48 | |
| 49 | static MockAuthService getAndRegisterAuthService() { |
| 50 | _removeRegistrationIfExists<AuthService>(); |
| 51 | final service = MockAuthService(); |
| 52 | getIt.registerSingleton<AuthService>(service); |
| 53 | return service; |
| 54 | } |
| 55 | |
| 56 | static MockAuthenticationStore getAndRegisterAuthenticationStore() { |
| 57 | _removeRegistrationIfExists<AuthenticationStore>(); |
| 58 | final service = MockAuthenticationStore(); |
| 59 | when(() => service.state).thenReturn(AuthenticationState.uninitialized); |
| 60 | getIt.registerSingleton<AuthenticationStore>(service); |
| 61 | return service; |
| 62 | } |
| 63 | |
| 64 | static MockWalletListStore getAndRegisterWalletListStore() { |
| 65 | _removeRegistrationIfExists<WalletListStore>(); |
| 66 | final service = MockWalletListStore(); |
| 67 | getIt.registerSingleton<WalletListStore>(service); |
| 68 | return service; |
| 69 | } |
| 70 | |
| 71 | static MockLinkViewModel getAndRegisterLinkViewModel() { |
| 72 | _removeRegistrationIfExists<LinkViewModel>(); |
| 73 | final service = MockLinkViewModel(); |
| 74 | getIt.registerSingleton<LinkViewModel>(service); |
| 75 | return service; |
| 76 | } |
| 77 | |
| 78 | static MockHiveInterface getAndRegisterHiveInterface() { |
| 79 | _removeRegistrationIfExists<HiveInterface>(); |
| 80 | final service = MockHiveInterface(); |
| 81 | getIt.registerSingleton<HiveInterface>(service); |
| 82 | return service; |
| 83 | } |
| 84 | |
| 85 | static MockSecureStorage getAndRegisterSecureStorage() { |
| 86 | _removeRegistrationIfExists<SecureStorage>(); |
| 87 | final service = MockSecureStorage(); |
| 88 | getIt.registerSingleton<SecureStorage>(service); |
| 89 | return service; |
| 90 | } |
| 91 | |
| 92 | static void _removeRegistrationIfExists<T extends Object>() { |
| 93 | if (getIt.isRegistered<T>()) { |
| 94 | getIt.unregister<T>(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static void tearDown() => getIt.reset(); |
| 99 | } |