Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

test: add tests for TransactionDetailsPage #316

Merged
merged 6 commits into from
Oct 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions test/features/transaction/transaction_details_page_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import 'package:didpay/features/transaction/transaction.dart';
import 'package:didpay/features/transaction/transaction_details_page.dart';
import 'package:didpay/features/transaction/transaction_notifier.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:intl/intl.dart';

import '../../helpers/mocks.dart';
import '../../helpers/test_data.dart';
import '../../helpers/widget_helpers.dart';
import 'transaction_tile_test.dart';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of importing another test file, you could put the shared data in the test_data.dart file and import that in this test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1!


void main() {
group('TransactionDetailsPage', () {
final sendTransaction = TestData.getTransaction();
final depositTransaction =
TestData.getTransaction(type: TransactionType.deposit);
final withdrawTransaction =
TestData.getTransaction(type: TransactionType.withdraw);

const mockTransactionNotifierWithSendTransaction =
MockTransactionNotifierWithData(
transactionType: TransactionType.send,
);
const mockTransactionNotifierWithError = MockTransactionNotifierWithError();

late MockTransactionNotifier mockSendTransactionNotifier;
late MockTransactionNotifier mockDepositTransactionNotifier;
late MockTransactionNotifier mockWithdrawTransactionNotifier;
late MockTransactionNotifier nullMockTransactionNotifier;
late MockTransactionNotifier erroringMockTransactionNotifier;

setUp(() {
mockSendTransactionNotifier =
MockTransactionNotifier(() => sendTransaction);
mockDepositTransactionNotifier =
MockTransactionNotifier(() => depositTransaction);
mockWithdrawTransactionNotifier =
MockTransactionNotifier(() => withdrawTransaction);
nullMockTransactionNotifier = MockTransactionNotifier();
erroringMockTransactionNotifier = MockTransactionNotifier(
() => throw StateError('Error loading transaction'),
);
});

Widget transactionDetailsTestWidget({
required MockTransactionNotifierType mockTransactionNotifierType,
}) =>
WidgetHelpers.testableWidget(
child: TransactionDetailsPage(
pfi: TestData.getPfi('did:dht:pfiDid'),
exchangeId: 'rfq_01ha835rhefwmagsknrrhvaa0k',
),
overrides: [
transactionProvider.overrideWith(
() => switch (mockTransactionNotifierType) {
MockTransactionNotifierWithData() => switch (
mockTransactionNotifierType.transactionType) {
TransactionType.send => mockSendTransactionNotifier,
TransactionType.deposit => mockDepositTransactionNotifier,
TransactionType.withdraw => mockWithdrawTransactionNotifier,
},
MockTransactionNotifierWithNullData() =>
nullMockTransactionNotifier,
MockTransactionNotifierWithError() =>
erroringMockTransactionNotifier,
},
),
],
);

testWidgets('should show correct payout and payin amounts', (tester) async {
await tester.pumpWidget(
transactionDetailsTestWidget(
mockTransactionNotifierType:
mockTransactionNotifierWithSendTransaction,
),
);
await tester.pumpAndSettle();

expect(
find.text('100.01'),
findsOneWidget,
); // Adjust to your currency setup.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

testWidgets('should show transaction date', (tester) async {
await tester.pumpWidget(
transactionDetailsTestWidget(
mockTransactionNotifierType:
mockTransactionNotifierWithSendTransaction,
),
);
await tester.pumpAndSettle();

expect(
find.text(
DateFormat("MMM dd 'at' hh:mm a")
.format(sendTransaction.createdAt.toLocal()),
),
findsOneWidget,
);
});

testWidgets('should show transaction status chip', (tester) async {
await tester.pumpWidget(
transactionDetailsTestWidget(
mockTransactionNotifierType:
mockTransactionNotifierWithSendTransaction,
),
);
await tester.pumpAndSettle();

expect(
find.text('Order submitted'),
findsOneWidget,
); // Adjust based on status
});

testWidgets('should display error when transaction fetch fails',
(tester) async {
await tester.pumpWidget(
transactionDetailsTestWidget(
mockTransactionNotifierType: mockTransactionNotifierWithError,
),
);
await tester.pumpAndSettle();

expect(find.text('Bad state: Error loading transaction'), findsOneWidget);
});
});
}
Loading