|
| 1 | +import 'package:didpay/features/transaction/transaction.dart'; |
| 2 | +import 'package:didpay/features/transaction/transaction_details_page.dart'; |
| 3 | +import 'package:didpay/features/transaction/transaction_notifier.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:intl/intl.dart'; |
| 7 | + |
| 8 | +import '../../helpers/mocks.dart'; |
| 9 | +import '../../helpers/test_data.dart'; |
| 10 | +import '../../helpers/widget_helpers.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + group('TransactionDetailsPage', () { |
| 14 | + final sendTransaction = TestData.getTransaction(); |
| 15 | + final depositTransaction = |
| 16 | + TestData.getTransaction(type: TransactionType.deposit); |
| 17 | + final withdrawTransaction = |
| 18 | + TestData.getTransaction(type: TransactionType.withdraw); |
| 19 | + |
| 20 | + const mockTransactionNotifierWithSendTransaction = |
| 21 | + MockTransactionNotifierWithData( |
| 22 | + transactionType: TransactionType.send, |
| 23 | + ); |
| 24 | + const mockTransactionNotifierWithError = MockTransactionNotifierWithError(); |
| 25 | + |
| 26 | + late MockTransactionNotifier mockSendTransactionNotifier; |
| 27 | + late MockTransactionNotifier mockDepositTransactionNotifier; |
| 28 | + late MockTransactionNotifier mockWithdrawTransactionNotifier; |
| 29 | + late MockTransactionNotifier nullMockTransactionNotifier; |
| 30 | + late MockTransactionNotifier erroringMockTransactionNotifier; |
| 31 | + |
| 32 | + setUp(() { |
| 33 | + mockSendTransactionNotifier = |
| 34 | + MockTransactionNotifier(() => sendTransaction); |
| 35 | + mockDepositTransactionNotifier = |
| 36 | + MockTransactionNotifier(() => depositTransaction); |
| 37 | + mockWithdrawTransactionNotifier = |
| 38 | + MockTransactionNotifier(() => withdrawTransaction); |
| 39 | + nullMockTransactionNotifier = MockTransactionNotifier(); |
| 40 | + erroringMockTransactionNotifier = MockTransactionNotifier( |
| 41 | + () => throw StateError('Error loading transaction'), |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + Widget transactionDetailsTestWidget({ |
| 46 | + required MockTransactionNotifierType mockTransactionNotifierType, |
| 47 | + }) => |
| 48 | + WidgetHelpers.testableWidget( |
| 49 | + child: TransactionDetailsPage( |
| 50 | + pfi: TestData.getPfi('did:dht:pfiDid'), |
| 51 | + exchangeId: 'rfq_01ha835rhefwmagsknrrhvaa0k', |
| 52 | + ), |
| 53 | + overrides: [ |
| 54 | + transactionProvider.overrideWith( |
| 55 | + () => switch (mockTransactionNotifierType) { |
| 56 | + MockTransactionNotifierWithData() => switch ( |
| 57 | + mockTransactionNotifierType.transactionType) { |
| 58 | + TransactionType.send => mockSendTransactionNotifier, |
| 59 | + TransactionType.deposit => mockDepositTransactionNotifier, |
| 60 | + TransactionType.withdraw => mockWithdrawTransactionNotifier, |
| 61 | + }, |
| 62 | + MockTransactionNotifierWithNullData() => |
| 63 | + nullMockTransactionNotifier, |
| 64 | + MockTransactionNotifierWithError() => |
| 65 | + erroringMockTransactionNotifier, |
| 66 | + }, |
| 67 | + ), |
| 68 | + ], |
| 69 | + ); |
| 70 | + |
| 71 | + testWidgets('should show correct payout and payin amounts', (tester) async { |
| 72 | + await tester.pumpWidget( |
| 73 | + transactionDetailsTestWidget( |
| 74 | + mockTransactionNotifierType: |
| 75 | + mockTransactionNotifierWithSendTransaction, |
| 76 | + ), |
| 77 | + ); |
| 78 | + await tester.pumpAndSettle(); |
| 79 | + |
| 80 | + expect( |
| 81 | + find.text('100.01'), |
| 82 | + findsOneWidget, |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + testWidgets('should show transaction date', (tester) async { |
| 87 | + await tester.pumpWidget( |
| 88 | + transactionDetailsTestWidget( |
| 89 | + mockTransactionNotifierType: |
| 90 | + mockTransactionNotifierWithSendTransaction, |
| 91 | + ), |
| 92 | + ); |
| 93 | + await tester.pumpAndSettle(); |
| 94 | + |
| 95 | + expect( |
| 96 | + find.text( |
| 97 | + DateFormat("MMM dd 'at' hh:mm a") |
| 98 | + .format(sendTransaction.createdAt.toLocal()), |
| 99 | + ), |
| 100 | + findsOneWidget, |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + testWidgets('should show transaction status chip', (tester) async { |
| 105 | + await tester.pumpWidget( |
| 106 | + transactionDetailsTestWidget( |
| 107 | + mockTransactionNotifierType: |
| 108 | + mockTransactionNotifierWithSendTransaction, |
| 109 | + ), |
| 110 | + ); |
| 111 | + await tester.pumpAndSettle(); |
| 112 | + |
| 113 | + expect( |
| 114 | + find.text('Order submitted'), |
| 115 | + findsOneWidget, |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + testWidgets('should display error when transaction fetch fails', |
| 120 | + (tester) async { |
| 121 | + await tester.pumpWidget( |
| 122 | + transactionDetailsTestWidget( |
| 123 | + mockTransactionNotifierType: mockTransactionNotifierWithError, |
| 124 | + ), |
| 125 | + ); |
| 126 | + await tester.pumpAndSettle(); |
| 127 | + |
| 128 | + expect(find.text('Bad state: Error loading transaction'), findsOneWidget); |
| 129 | + }); |
| 130 | + }); |
| 131 | +} |
| 132 | + |
0 commit comments