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

Add UI Tests for CurrencyDropdown Widget #312

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions lib/features/currency/currency_dropdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CurrencyDropdown extends HookConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) => Directionality(
textDirection: TextDirection.rtl,
child: ElevatedButton.icon(
key: const Key('currencyDropdownButton'),
Copy link
Contributor

Choose a reason for hiding this comment

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

please remove this line as keys aren't being used in tests

icon: const Icon(Icons.keyboard_arrow_down),
label: _buildCurrencyLabel(context),
style: ElevatedButton.styleFrom(
Expand Down
79 changes: 79 additions & 0 deletions test/features/currency/currency_dropdown_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'package:didpay/features/currency/currency_dropdown.dart';
import 'package:didpay/features/payment/payment_amount_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../helpers/test_data.dart';
import '../../helpers/widget_helpers.dart';

void main() async {
await TestData.initializeDids();

late ValueNotifier<PaymentAmountState?> mockState;
const testCurrency = 'USD';

setUp(() {
// Initialize mockState with a non-null offeringsMap
Copy link
Contributor

Choose a reason for hiding this comment

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

comments in test file are not required!

mockState = ValueNotifier<PaymentAmountState?>(
PaymentAmountState(
offeringsMap: TestData.getOfferingsMap(),
), // Use TestData as suggested
);
});

Widget createWidgetUnderTest() {
return WidgetHelpers.testableWidget(
// Use WidgetHelpers for consistent test setup
child: CurrencyDropdown(
paymentCurrency: testCurrency,
state: mockState,
),
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

try to follow the existing test suite in terms of naming/initializing this widget (e.g. see payout_test.dart)


testWidgets('CurrencyDropdown shows the correct initial currency',
Copy link
Contributor

Choose a reason for hiding this comment

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

try updating the test descriptions to be consistent with the existing test suite

(tester) async {
await tester.pumpWidget(createWidgetUnderTest());

// Check if the currency label displays the initial currency
expect(find.text(testCurrency), findsOneWidget);
});

testWidgets('CurrencyDropdown opens the Select Currency modal when tapped',
(tester) async {
await tester.pumpWidget(createWidgetUnderTest());

// Tap on the currency dropdown button
await tester.tap(find.text(testCurrency));
await tester.pumpAndSettle(); // Wait for the modal to open

// Verify if the modal is displayed
expect(
find.text('Select currency'),
findsOneWidget,
); // Look for modal text or widget
});

testWidgets('CurrencyDropdown updates state when a currency is selected',
(tester) async {
await tester.pumpWidget(createWidgetUnderTest());

// Open the modal by tapping on the currency dropdown button
await tester.tap(
find.text(testCurrency),
); // Using the 'USD' label text to find the button
await tester.pumpAndSettle();

// Simulate selecting a currency by updating the mockState directly
const selectedCurrency = 'EUR';
mockState.value = PaymentAmountState(
filterCurrency:
selectedCurrency, // Assuming 'filterCurrency' is the correct field for currency selection
);
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 updating mockState directly, we should use tester.tap() to select a new currency. pull in the latest changes on main to use an updated version of TestData to make this easier for you to do.


await tester.pumpAndSettle();

// Verify the state reflects the selected currency
expect(mockState.value?.filterCurrency, selectedCurrency);
});
}
Loading