|
| 1 | +import 'package:didpay/features/account/account_balance.dart'; |
| 2 | +import 'package:didpay/features/account/account_balance_notifier.dart'; |
| 3 | +import 'package:didpay/features/tbdex/tbdex_service.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:hooks_riverpod/hooks_riverpod.dart'; |
| 6 | +import 'package:mocktail/mocktail.dart'; |
| 7 | + |
| 8 | +import '../../helpers/mocks.dart'; |
| 9 | +import '../../helpers/riverpod_helpers.dart'; |
| 10 | +import '../../helpers/test_data.dart'; |
| 11 | + |
| 12 | +Future<void> main() async { |
| 13 | + await TestData.initializeDids(); |
| 14 | + |
| 15 | + final pfis = TestData.getPfis(); |
| 16 | + final did = TestData.aliceDid; |
| 17 | + |
| 18 | + setUpAll(() { |
| 19 | + registerFallbackValue( |
| 20 | + const AsyncData<AccountBalance?>(null), |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + group('AccountBalanceNotifier', () { |
| 25 | + group('fetchAccountBalance', () { |
| 26 | + test('should return null if pfis is empty', () async { |
| 27 | + final notifier = AccountBalanceNotifier(); |
| 28 | + final result = await notifier.fetchAccountBalance(did, []); |
| 29 | + expect(result, null); |
| 30 | + }); |
| 31 | + |
| 32 | + test('should return null if pfis is null', () async { |
| 33 | + final notifier = AccountBalanceNotifier(); |
| 34 | + final result = await notifier.fetchAccountBalance(did, null); |
| 35 | + expect(result, null); |
| 36 | + }); |
| 37 | + |
| 38 | + test('should set state to AsyncValue.data(accountBalance) if successful', |
| 39 | + () async { |
| 40 | + final mockTbdexService = MockTbdexService(); |
| 41 | + final accountBalance = TestData.getAccountBalance(); |
| 42 | + |
| 43 | + when( |
| 44 | + () => mockTbdexService.getAccountBalance(did, pfis), |
| 45 | + ).thenAnswer((_) async => accountBalance); |
| 46 | + |
| 47 | + final container = createContainer( |
| 48 | + overrides: [ |
| 49 | + tbdexServiceProvider.overrideWith( |
| 50 | + (ref) => mockTbdexService, |
| 51 | + ), |
| 52 | + ], |
| 53 | + ); |
| 54 | + |
| 55 | + final listener = Listener<AsyncValue<void>>(); |
| 56 | + |
| 57 | + final accountBalanceNotifier = |
| 58 | + container.read(accountBalanceProvider.notifier); |
| 59 | + |
| 60 | + container.listen(accountBalanceProvider, listener.call); |
| 61 | + |
| 62 | + await accountBalanceNotifier.fetchAccountBalance(did, pfis); |
| 63 | + |
| 64 | + verify( |
| 65 | + () => listener( |
| 66 | + const AsyncLoading<AccountBalance?>(), |
| 67 | + any(that: isA<AsyncData<AccountBalance?>>()), |
| 68 | + ), |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + test('should return account balance if successful', () async { |
| 73 | + final mockTbdexService = MockTbdexService(); |
| 74 | + final accountBalance = TestData.getAccountBalance(); |
| 75 | + |
| 76 | + when( |
| 77 | + () => mockTbdexService.getAccountBalance(did, pfis), |
| 78 | + ).thenAnswer((_) async => accountBalance); |
| 79 | + |
| 80 | + final container = createContainer( |
| 81 | + overrides: [ |
| 82 | + tbdexServiceProvider.overrideWith( |
| 83 | + (ref) => mockTbdexService, |
| 84 | + ), |
| 85 | + ], |
| 86 | + ); |
| 87 | + |
| 88 | + final accountBalanceNotifier = |
| 89 | + container.read(accountBalanceProvider.notifier); |
| 90 | + |
| 91 | + final result = |
| 92 | + await accountBalanceNotifier.fetchAccountBalance(did, pfis); |
| 93 | + |
| 94 | + expect(result, accountBalance); |
| 95 | + }); |
| 96 | + |
| 97 | + test('should set state to AsyncValue.error when error occurs', () async { |
| 98 | + final mockTbdexService = MockTbdexService(); |
| 99 | + |
| 100 | + when( |
| 101 | + () => mockTbdexService.getAccountBalance(did, pfis), |
| 102 | + ).thenThrow(Exception('Error fetching account balance')); |
| 103 | + |
| 104 | + final container = createContainer( |
| 105 | + overrides: [ |
| 106 | + tbdexServiceProvider.overrideWith( |
| 107 | + (ref) => mockTbdexService, |
| 108 | + ), |
| 109 | + ], |
| 110 | + ); |
| 111 | + |
| 112 | + final listener = Listener<AsyncValue<void>>(); |
| 113 | + |
| 114 | + final accountBalanceNotifier = |
| 115 | + container.read(accountBalanceProvider.notifier); |
| 116 | + |
| 117 | + container.listen(accountBalanceProvider, listener.call); |
| 118 | + |
| 119 | + await accountBalanceNotifier.fetchAccountBalance(did, pfis); |
| 120 | + |
| 121 | + verify( |
| 122 | + () => listener( |
| 123 | + const AsyncLoading<AccountBalance?>(), |
| 124 | + any(that: isA<AsyncError>()), |
| 125 | + ), |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + test('should return null when error occurs', () async { |
| 130 | + final mockTbdexService = MockTbdexService(); |
| 131 | + |
| 132 | + when( |
| 133 | + () => mockTbdexService.getAccountBalance(did, pfis), |
| 134 | + ).thenThrow(Exception('Error fetching account balance')); |
| 135 | + |
| 136 | + final container = createContainer( |
| 137 | + overrides: [ |
| 138 | + tbdexServiceProvider.overrideWith( |
| 139 | + (ref) => mockTbdexService, |
| 140 | + ), |
| 141 | + ], |
| 142 | + ); |
| 143 | + |
| 144 | + final accountBalanceNotifier = |
| 145 | + container.read(accountBalanceProvider.notifier); |
| 146 | + |
| 147 | + final result = |
| 148 | + await accountBalanceNotifier.fetchAccountBalance(did, pfis); |
| 149 | + |
| 150 | + expect(result, null); |
| 151 | + }); |
| 152 | + }); |
| 153 | + }); |
| 154 | +} |
0 commit comments