|
| 1 | +// ignore_for_file: discarded_futures |
| 2 | + |
| 3 | +import 'package:account_repository/account_repository.dart'; |
| 4 | +import 'package:built_collection/built_collection.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:mocktail/mocktail.dart'; |
| 7 | +import 'package:neon_framework/src/blocs/accounts.dart'; |
| 8 | +import 'package:rxdart/rxdart.dart'; |
| 9 | + |
| 10 | +class _MockAccountRepository extends Mock implements AccountRepository {} |
| 11 | + |
| 12 | +class _MockAccount extends Mock implements Account {} |
| 13 | + |
| 14 | +class _FakeAccount extends Fake implements Account {} |
| 15 | + |
| 16 | +void main() { |
| 17 | + late BehaviorSubject<({BuiltList<Account> accounts, Account? active})> accounts; |
| 18 | + late AccountRepository accountRepository; |
| 19 | + late AccountsBloc bloc; |
| 20 | + |
| 21 | + setUpAll(() { |
| 22 | + registerFallbackValue(_FakeAccount()); |
| 23 | + }); |
| 24 | + |
| 25 | + setUp(() { |
| 26 | + accounts = BehaviorSubject(); |
| 27 | + |
| 28 | + accountRepository = _MockAccountRepository(); |
| 29 | + when(() => accountRepository.accounts).thenAnswer((_) => accounts); |
| 30 | + |
| 31 | + bloc = AccountsBloc( |
| 32 | + allAppImplementations: BuiltSet(), |
| 33 | + accountRepository: accountRepository, |
| 34 | + ); |
| 35 | + |
| 36 | + verify(() => accountRepository.accounts).called(1); |
| 37 | + }); |
| 38 | + |
| 39 | + tearDown(() { |
| 40 | + bloc.dispose(); |
| 41 | + |
| 42 | + accounts.close(); |
| 43 | + |
| 44 | + verifyNoMoreInteractions(accountRepository); |
| 45 | + }); |
| 46 | + |
| 47 | + group('removeAccount', () { |
| 48 | + test('Success', () { |
| 49 | + when(() => accountRepository.logOut(any())).thenAnswer((_) async {}); |
| 50 | + |
| 51 | + final account = _MockAccount(); |
| 52 | + |
| 53 | + bloc.removeAccount(account); |
| 54 | + verify(() => accountRepository.logOut(account)).called(1); |
| 55 | + }); |
| 56 | + test('DeleteCredentialsFailure', () { |
| 57 | + when(() => accountRepository.logOut(any())).thenThrow(const DeleteCredentialsFailure('')); |
| 58 | + |
| 59 | + final account = _MockAccount(); |
| 60 | + |
| 61 | + bloc.removeAccount(account); |
| 62 | + verify(() => accountRepository.logOut(account)).called(1); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + test('setActiveAccount', () { |
| 67 | + when(() => accountRepository.switchAccount(any())).thenAnswer((_) async {}); |
| 68 | + |
| 69 | + final account = _MockAccount(); |
| 70 | + when(() => account.id).thenReturn('id'); |
| 71 | + |
| 72 | + bloc.setActiveAccount(account); |
| 73 | + verify(() => accountRepository.switchAccount('id')).called(1); |
| 74 | + }); |
| 75 | + |
| 76 | + test('accounts', () async { |
| 77 | + when(() => accountRepository.getRemoteWipeStatus(any())).thenAnswer((_) async => false); |
| 78 | + |
| 79 | + final account = _MockAccount(); |
| 80 | + when(() => account.id).thenReturn('id'); |
| 81 | + |
| 82 | + accounts.add((accounts: BuiltList([account]), active: null)); |
| 83 | + |
| 84 | + await Future<void>.delayed(const Duration(milliseconds: 1)); |
| 85 | + |
| 86 | + expect(bloc.accounts, emits(BuiltList<Account>([account]))); |
| 87 | + verify(() => accountRepository.getRemoteWipeStatus(account)).called(1); |
| 88 | + }); |
| 89 | + |
| 90 | + test('activeAccount', () async { |
| 91 | + when(() => accountRepository.getRemoteWipeStatus(any())).thenAnswer((_) async => false); |
| 92 | + |
| 93 | + final account = _MockAccount(); |
| 94 | + when(() => account.id).thenReturn('id'); |
| 95 | + |
| 96 | + accounts.add((accounts: BuiltList([account]), active: account)); |
| 97 | + |
| 98 | + await Future<void>.delayed(const Duration(milliseconds: 1)); |
| 99 | + |
| 100 | + expect(bloc.activeAccount, emits(account)); |
| 101 | + verify(() => accountRepository.getRemoteWipeStatus(account)).called(1); |
| 102 | + }); |
| 103 | + |
| 104 | + test('hasAccounts', () async { |
| 105 | + when(() => accountRepository.getRemoteWipeStatus(any())).thenAnswer((_) async => false); |
| 106 | + |
| 107 | + final account = _MockAccount(); |
| 108 | + when(() => account.id).thenReturn('id'); |
| 109 | + |
| 110 | + accounts.add((accounts: BuiltList([account]), active: account)); |
| 111 | + |
| 112 | + await Future<void>.delayed(const Duration(milliseconds: 1)); |
| 113 | + |
| 114 | + expect(bloc.hasAccounts, true); |
| 115 | + verify(() => accountRepository.getRemoteWipeStatus(account)).called(1); |
| 116 | + }); |
| 117 | + |
| 118 | + test('accountByID', () { |
| 119 | + final account = _MockAccount(); |
| 120 | + when(() => account.id).thenReturn('id'); |
| 121 | + |
| 122 | + when(() => accountRepository.accountByID(any())).thenReturn(account); |
| 123 | + |
| 124 | + expect(bloc.accountByID('id'), account); |
| 125 | + verify(() => accountRepository.accountByID('id')).called(1); |
| 126 | + }); |
| 127 | + |
| 128 | + group('Remote wipe', () { |
| 129 | + group('Should not wipe', () { |
| 130 | + test('Status false', () async { |
| 131 | + when(() => accountRepository.getRemoteWipeStatus(any())).thenAnswer((_) async => false); |
| 132 | + |
| 133 | + final account = _MockAccount(); |
| 134 | + when(() => account.id).thenReturn('id'); |
| 135 | + |
| 136 | + accounts.add((accounts: BuiltList([account]), active: account)); |
| 137 | + |
| 138 | + await Future<void>.delayed(const Duration(milliseconds: 1)); |
| 139 | + |
| 140 | + verify(() => accountRepository.getRemoteWipeStatus(account)).called(1); |
| 141 | + }); |
| 142 | + |
| 143 | + test('GetRemoteWipeStatusFailure', () async { |
| 144 | + when(() => accountRepository.getRemoteWipeStatus(any())).thenThrow(const GetRemoteWipeStatusFailure('')); |
| 145 | + |
| 146 | + final account = _MockAccount(); |
| 147 | + when(() => account.id).thenReturn('id'); |
| 148 | + |
| 149 | + accounts.add((accounts: BuiltList([account]), active: account)); |
| 150 | + |
| 151 | + await Future<void>.delayed(const Duration(milliseconds: 1)); |
| 152 | + |
| 153 | + verify(() => accountRepository.getRemoteWipeStatus(account)).called(1); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + group('Should wipe', () { |
| 158 | + setUp(() { |
| 159 | + when(() => accountRepository.getRemoteWipeStatus(any())).thenAnswer((_) async => true); |
| 160 | + when(() => accountRepository.logOut(any())).thenAnswer((_) async {}); |
| 161 | + }); |
| 162 | + |
| 163 | + test('Post success', () async { |
| 164 | + when(() => accountRepository.postRemoteWipeSuccess(any())).thenAnswer((_) async {}); |
| 165 | + |
| 166 | + final account = _MockAccount(); |
| 167 | + when(() => account.id).thenReturn('id'); |
| 168 | + |
| 169 | + accounts.add((accounts: BuiltList([account]), active: account)); |
| 170 | + |
| 171 | + await Future<void>.delayed(const Duration(milliseconds: 1)); |
| 172 | + |
| 173 | + verify(() => accountRepository.getRemoteWipeStatus(account)).called(1); |
| 174 | + verify(() => accountRepository.logOut(account)).called(1); |
| 175 | + verify(() => accountRepository.postRemoteWipeSuccess(account)).called(1); |
| 176 | + }); |
| 177 | + |
| 178 | + test('PostRemoteWipeSuccessFailure', () async { |
| 179 | + when(() => accountRepository.postRemoteWipeSuccess(any())).thenThrow(const PostRemoteWipeSuccessFailure('')); |
| 180 | + |
| 181 | + final account = _MockAccount(); |
| 182 | + when(() => account.id).thenReturn('id'); |
| 183 | + |
| 184 | + accounts.add((accounts: BuiltList([account]), active: account)); |
| 185 | + |
| 186 | + await Future<void>.delayed(const Duration(milliseconds: 1)); |
| 187 | + |
| 188 | + verify(() => accountRepository.getRemoteWipeStatus(account)).called(1); |
| 189 | + verify(() => accountRepository.logOut(account)).called(1); |
| 190 | + verify(() => accountRepository.postRemoteWipeSuccess(account)).called(1); |
| 191 | + }); |
| 192 | + }); |
| 193 | + }); |
| 194 | +} |
0 commit comments