Skip to content

Commit

Permalink
test: add tests for TransactionDetailsPage (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitrajsinha authored Oct 18, 2024
1 parent fdf2352 commit 568d379
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 17 deletions.
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';

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,
);
});

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,
);
});

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);
});
});
}

17 changes: 0 additions & 17 deletions test/features/transaction/transaction_tile_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,3 @@ void main() {
});
}

sealed class MockTransactionNotifierType {
const MockTransactionNotifierType();
}

class MockTransactionNotifierWithData extends MockTransactionNotifierType {
const MockTransactionNotifierWithData({required this.transactionType});

final TransactionType transactionType;
}

class MockTransactionNotifierWithNullData extends MockTransactionNotifierType {
const MockTransactionNotifierWithNullData();
}

class MockTransactionNotifierWithError extends MockTransactionNotifierType {
const MockTransactionNotifierWithError();
}
18 changes: 18 additions & 0 deletions test/helpers/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ class MockAccountBalanceNotifier
FutureOr<AccountBalance?> build() async => accountBalance;
}

sealed class MockTransactionNotifierType {
const MockTransactionNotifierType();
}

class MockTransactionNotifierWithData extends MockTransactionNotifierType {
const MockTransactionNotifierWithData({required this.transactionType});

final TransactionType transactionType;
}

class MockTransactionNotifierWithNullData extends MockTransactionNotifierType {
const MockTransactionNotifierWithNullData();
}

class MockTransactionNotifierWithError extends MockTransactionNotifierType {
const MockTransactionNotifierWithError();
}

class Listener<T> extends Mock {
void call(T? previous, T next);
}

0 comments on commit 568d379

Please sign in to comment.