|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:didpay/features/did/did_provider.dart'; |
| 4 | +import 'package:didpay/features/tbdex/tbdex_service.dart'; |
| 5 | +import 'package:didpay/features/transaction/transaction.dart'; |
| 6 | +import 'package:didpay/features/transaction/transaction_notifier.dart'; |
| 7 | +import 'package:flutter_test/flutter_test.dart'; |
| 8 | +import 'package:hooks_riverpod/hooks_riverpod.dart'; |
| 9 | +import 'package:mocktail/mocktail.dart'; |
| 10 | + |
| 11 | +import '../../helpers/mocks.dart'; |
| 12 | +import '../../helpers/riverpod_helpers.dart'; |
| 13 | +import '../../helpers/test_data.dart'; |
| 14 | + |
| 15 | +void main() async { |
| 16 | + await TestData.initializeDids(); |
| 17 | + |
| 18 | + final pfi = TestData.getPfi('did:dht:pfiDid'); |
| 19 | + const exchangeId = 'rfq_01ha835rhefwmagsknrrhvaa0k'; |
| 20 | + final parameters = TransactionProviderParameters(pfi, exchangeId); |
| 21 | + final did = TestData.aliceDid; |
| 22 | + |
| 23 | + setUpAll(() { |
| 24 | + registerFallbackValue( |
| 25 | + const AsyncData<Transaction?>(null), |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + group('TransactionNotifier', () { |
| 30 | + test('should set the state to AsyncValue.data(transaction) on read', |
| 31 | + () async { |
| 32 | + final mockTbdexService = MockTbdexService(); |
| 33 | + final exchange = TestData.getExchange(); |
| 34 | + |
| 35 | + when( |
| 36 | + () => mockTbdexService.getExchange( |
| 37 | + did, |
| 38 | + pfi.did, |
| 39 | + exchangeId, |
| 40 | + ), |
| 41 | + ).thenAnswer((_) async => exchange); |
| 42 | + |
| 43 | + final container = createContainer( |
| 44 | + overrides: [ |
| 45 | + didProvider.overrideWith( |
| 46 | + (ref) => did, |
| 47 | + ), |
| 48 | + tbdexServiceProvider.overrideWith( |
| 49 | + (ref) => mockTbdexService, |
| 50 | + ), |
| 51 | + ], |
| 52 | + ); |
| 53 | + |
| 54 | + final listener = Listener<AsyncValue<void>>(); |
| 55 | + |
| 56 | + final transactionProviderListenable = transactionProvider(parameters); |
| 57 | + |
| 58 | + container.listen(transactionProviderListenable, listener.call); |
| 59 | + |
| 60 | + await container.read(transactionProviderListenable.future); |
| 61 | + |
| 62 | + verify( |
| 63 | + () => listener( |
| 64 | + const AsyncLoading<Transaction?>(), |
| 65 | + any(that: isA<AsyncData<Transaction?>>()), |
| 66 | + ), |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + test('should set the state to AsyncValue.error when error occurs', |
| 71 | + () async { |
| 72 | + final mockTbdexService = MockTbdexService(); |
| 73 | + |
| 74 | + when( |
| 75 | + () => mockTbdexService.getExchange( |
| 76 | + did, |
| 77 | + pfi.did, |
| 78 | + exchangeId, |
| 79 | + ), |
| 80 | + ).thenThrow(Exception('Error fetching exchange')); |
| 81 | + |
| 82 | + final container = createContainer( |
| 83 | + overrides: [ |
| 84 | + didProvider.overrideWith( |
| 85 | + (ref) => did, |
| 86 | + ), |
| 87 | + tbdexServiceProvider.overrideWith( |
| 88 | + (ref) => mockTbdexService, |
| 89 | + ), |
| 90 | + ], |
| 91 | + ); |
| 92 | + |
| 93 | + final listener = Listener<AsyncValue<void>>(); |
| 94 | + |
| 95 | + final transactionProviderListenable = transactionProvider(parameters); |
| 96 | + |
| 97 | + container.listen(transactionProviderListenable, listener.call); |
| 98 | + |
| 99 | + unawaited(await container.read(transactionProviderListenable.future)); |
| 100 | + |
| 101 | + verify( |
| 102 | + () => listener( |
| 103 | + any(that: isA<AsyncError>()), |
| 104 | + const AsyncData<Transaction?>(null), |
| 105 | + ), |
| 106 | + ); |
| 107 | + }); |
| 108 | + }); |
| 109 | +} |
0 commit comments