From aa610a314d919d223004e6d9623eb7c618c43642 Mon Sep 17 00:00:00 2001 From: Victor Eronmosele Date: Tue, 22 Oct 2024 14:14:23 +0100 Subject: [PATCH] test: add ui tests for `DapForm` --- test/features/dap/dap_form_test.dart | 86 ++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 test/features/dap/dap_form_test.dart diff --git a/test/features/dap/dap_form_test.dart b/test/features/dap/dap_form_test.dart new file mode 100644 index 00000000..cdeb86b2 --- /dev/null +++ b/test/features/dap/dap_form_test.dart @@ -0,0 +1,86 @@ +import 'package:dap/dap.dart'; +import 'package:didpay/features/dap/dap_form.dart'; +import 'package:didpay/features/dap/dap_qr_tile.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; + +import '../../helpers/widget_helpers.dart'; + +void main() async { + group('DapForm', () { + Widget dapFormTestWidget({AsyncValue? dap, String? dapText}) => + WidgetHelpers.testableWidget( + child: DapForm( + buttonTitle: 'Next', + dapText: ValueNotifier(dapText), + dap: ValueNotifier?>(dap), + onSubmit: (_, __) async {}, + ), + ); + + testWidgets('should show button title', (tester) async { + await tester.pumpWidget(dapFormTestWidget()); + await tester.pumpAndSettle(); + + expect(find.widgetWithText(FilledButton, 'Next'), findsOneWidget); + }); + + testWidgets('should show QR Code CTA', (tester) async { + await tester.pumpWidget(dapFormTestWidget()); + await tester.pumpAndSettle(); + + expect( + find.widgetWithText( + DapQrTile, + "Don't know their DAP? Scan their QR code instead", + ), + findsOneWidget, + ); + }); + + testWidgets('should show CircularProgressIndicator when DAP is loading', + (tester) async { + await tester.pumpWidget(dapFormTestWidget()); + await tester.pump(Duration.zero); + + expect(find.byType(CircularProgressIndicator), findsNothing); + + await tester.pumpWidget(dapFormTestWidget(dap: const AsyncLoading())); + await tester.pump(Duration.zero); + + expect(find.byType(CircularProgressIndicator), findsOneWidget); + }); + + testWidgets( + 'should show error message when the Next button is tapped and the dap text is invalid', + (tester) async { + const invalidDapText = 'invalid_dap_text'; + const expectedErrorMessage = 'Invalid DAP'; + + await tester.pumpWidget(dapFormTestWidget(dapText: invalidDapText)); + await tester.pumpAndSettle(); + + expect(find.text(expectedErrorMessage), findsNothing); + + await tester.tap(find.widgetWithText(FilledButton, 'Next')); + await tester.pumpAndSettle(); + + expect(find.text(expectedErrorMessage), findsOneWidget); + }); + + testWidgets( + 'should resolve DAP without errors when the Next button is tapped and the dap text is valid', + (tester) async { + const validDapText = '@moegrammer/didpay.me'; + + await tester.pumpWidget(dapFormTestWidget(dapText: validDapText)); + await tester.pumpAndSettle(); + + await tester.tap(find.widgetWithText(FilledButton, 'Next')); + await tester.pumpAndSettle(); + + expect(find.text('Invalid DAP'), findsNothing); + }); + }); +}