From cb8c7939b6b9865e34cae0c75358d1159d2c262c Mon Sep 17 00:00:00 2001 From: Bibash Shrestha Date: Tue, 21 May 2024 14:13:47 +0545 Subject: [PATCH] Add dialog tests --- .../shared/widget/base/text_field_test.dart | 126 +++++++++++++++ .../widget/dialog/becareful_dialog_test.dart | 140 ++++++++++++++++ .../widget/dialog/confirm_dialog_test.dart | 150 ++++++++++++++++++ .../dialog/error_details_dialog_test.dart | 71 +++++++++ .../widget/dialog/error_dialog_test.dart | 56 +++++++ .../widget/dialog/text_field_dialog_test.dart | 54 +++++++ 6 files changed, 597 insertions(+) create mode 100644 test/app/shared/widget/base/text_field_test.dart create mode 100644 test/app/shared/widget/dialog/becareful_dialog_test.dart create mode 100644 test/app/shared/widget/dialog/confirm_dialog_test.dart create mode 100644 test/app/shared/widget/dialog/error_details_dialog_test.dart create mode 100644 test/app/shared/widget/dialog/error_dialog_test.dart create mode 100644 test/app/shared/widget/dialog/text_field_dialog_test.dart diff --git a/test/app/shared/widget/base/text_field_test.dart b/test/app/shared/widget/base/text_field_test.dart new file mode 100644 index 000000000..7cb47c1c9 --- /dev/null +++ b/test/app/shared/widget/base/text_field_test.dart @@ -0,0 +1,126 @@ +import 'package:altme/app/app.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../../../../helpers/helpers.dart'; + +void main() { + group('BaseTextField Widget Tests', () { + late TextEditingController controller; + + setUp(() { + controller = TextEditingController(); + }); + + testWidgets('renders BaseTextField with default properties', + (WidgetTester tester) async { + await tester.pumpApp( + Scaffold( + body: BaseTextField( + controller: controller, + ), + ), + ); + + expect(find.byType(TextFormField), findsOneWidget); + expect(find.byType(SizedBox), findsNWidgets(2)); + }); + + testWidgets('renders BaseTextField with label', + (WidgetTester tester) async { + const label = 'Test Label'; + + await tester.pumpApp( + Scaffold( + body: BaseTextField( + controller: controller, + label: label, + ), + ), + ); + + expect(find.text(label), findsOneWidget); + }); + + testWidgets('renders BaseTextField with hint', (WidgetTester tester) async { + const hint = 'Test Hint'; + + await tester.pumpApp( + Scaffold( + body: BaseTextField( + controller: controller, + hint: hint, + ), + ), + ); + + expect(find.text(hint), findsOneWidget); + }); + + testWidgets('renders BaseTextField with error text', + (WidgetTester tester) async { + const error = 'Test Error'; + + await tester.pumpApp( + Scaffold( + body: BaseTextField( + controller: controller, + error: error, + ), + ), + ); + + expect(find.text(error), findsOneWidget); + }); + + testWidgets('renders BaseTextField with prefix and suffix icons', + (WidgetTester tester) async { + const prefixIcon = Icon(Icons.email); + const suffixIcon = Icon(Icons.visibility); + + await tester.pumpApp( + Scaffold( + body: BaseTextField( + controller: controller, + prefixIcon: prefixIcon, + suffixIcon: suffixIcon, + ), + ), + ); + + expect(find.byIcon(Icons.email), findsOneWidget); + expect(find.byIcon(Icons.visibility), findsOneWidget); + }); + + testWidgets('input text updates the controller', + (WidgetTester tester) async { + const inputText = 'Hello, Flutter!'; + + await tester.pumpApp( + Scaffold( + body: BaseTextField( + controller: controller, + ), + ), + ); + + await tester.enterText(find.byType(TextFormField), inputText); + expect(controller.text, inputText); + }); + + testWidgets('obscureText property works correctly', + (WidgetTester tester) async { + await tester.pumpApp( + Scaffold( + body: BaseTextField( + controller: controller, + obscureText: true, + ), + ), + ); + + final TextField textField = tester.widget(find.byType(TextField)); + expect(textField.obscureText, isTrue); + }); + }); +} diff --git a/test/app/shared/widget/dialog/becareful_dialog_test.dart b/test/app/shared/widget/dialog/becareful_dialog_test.dart new file mode 100644 index 000000000..1afd7dc66 --- /dev/null +++ b/test/app/shared/widget/dialog/becareful_dialog_test.dart @@ -0,0 +1,140 @@ +import 'package:altme/app/app.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../../../../helpers/helpers.dart'; + +void main() { + group('BeCarefulDialog Widget Tests', () { + testWidgets('renders BeCarefulDialog with title', + (WidgetTester tester) async { + const title = 'Test Title'; + + await tester.pumpApp( + const Scaffold( + body: BeCarefulDialog( + title: title, + ), + ), + ); + + expect(find.text(title), findsOneWidget); + }); + + testWidgets('renders BeCarefulDialog with subtitle', + (WidgetTester tester) async { + const title = 'Test Title'; + const subtitle = 'Test Subtitle'; + + await tester.pumpApp( + const Scaffold( + body: BeCarefulDialog( + title: title, + subtitle: subtitle, + ), + ), + ); + + expect(find.text(title), findsOneWidget); + expect(find.text(subtitle), findsOneWidget); + }); + + testWidgets('renders BeCarefulDialog with yes and no buttons', + (WidgetTester tester) async { + const title = 'Test Title'; + const yesText = 'Yes'; + const noText = 'No'; + + await tester.pumpApp( + const Scaffold( + body: BeCarefulDialog( + title: title, + yes: yesText, + no: noText, + ), + ), + ); + + expect(find.text(yesText.toUpperCase()), findsOneWidget); + expect(find.text(noText.toUpperCase()), findsOneWidget); + }); + + testWidgets('clicking no button closes the dialog', + (WidgetTester tester) async { + const title = 'Test Title'; + const noText = 'No'; + + await tester.pumpApp( + Scaffold( + body: Builder( + builder: (context) { + return ElevatedButton( + onPressed: () { + BeCarefulDialog.show( + context: context, + title: title, + no: noText, + ); + }, + child: const Text('Show Dialog'), + ); + }, + ), + ), + ); + + await tester.tap(find.text('Show Dialog')); + await tester.pumpAndSettle(); + + expect(find.text(title), findsOneWidget); + expect(find.text(noText.toUpperCase()), findsOneWidget); + + await tester.tap(find.text(noText.toUpperCase())); + await tester.pumpAndSettle(); + + expect(find.text(title), findsNothing); + }); + + testWidgets( + 'clicking yes button triggers onContinueClick and closes the dialog', + (WidgetTester tester) async { + const title = 'Test Title'; + const yesText = 'Yes'; + bool onContinueClicked = false; + + await tester.pumpApp( + Scaffold( + body: Builder( + builder: (context) { + return ElevatedButton( + onPressed: () { + BeCarefulDialog.show( + context: context, + title: title, + yes: yesText, + onContinueClick: () { + onContinueClicked = true; + }, + ); + }, + child: const Text('Show Dialog'), + ); + }, + ), + ), + ); + + await tester.tap(find.text('Show Dialog')); + await tester.pumpAndSettle(); + + expect(find.text(title), findsOneWidget); + expect(find.text(yesText.toUpperCase()), findsOneWidget); + + await tester.tap(find.text(yesText.toUpperCase())); + await tester.pumpAndSettle(); + + expect(find.text(title), findsNothing); + expect(onContinueClicked, isTrue); + }); + }); +} diff --git a/test/app/shared/widget/dialog/confirm_dialog_test.dart b/test/app/shared/widget/dialog/confirm_dialog_test.dart new file mode 100644 index 000000000..fbe07bd1f --- /dev/null +++ b/test/app/shared/widget/dialog/confirm_dialog_test.dart @@ -0,0 +1,150 @@ +import 'package:altme/app/app.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../../../../helpers/helpers.dart'; + +void main() { + group('ConfirmDialog Widget Tests', () { + testWidgets('renders ConfirmDialog with title', + (WidgetTester tester) async { + const title = 'Test Title'; + + await tester.pumpApp( + const ConfirmDialog( + title: title, + ), + ); + + expect(find.text(title), findsOneWidget); + }); + + testWidgets('renders ConfirmDialog with subtitle', + (WidgetTester tester) async { + const title = 'Test Title'; + const subtitle = 'Test Subtitle'; + + await tester.pumpApp( + const ConfirmDialog( + title: title, + subtitle: subtitle, + ), + ); + + expect(find.text(title), findsOneWidget); + expect(find.text(subtitle), findsOneWidget); + }); + + testWidgets('renders ConfirmDialog with yes and no buttons', + (WidgetTester tester) async { + const title = 'Test Title'; + const yesText = 'Yes'; + const noText = 'No'; + + await tester.pumpApp( + const ConfirmDialog( + title: title, + yes: yesText, + no: noText, + ), + ); + + expect(find.text(yesText.toUpperCase()), findsOneWidget); + expect(find.text(noText.toUpperCase()), findsOneWidget); + }); + + testWidgets('clicking no button closes the dialog with false', + (WidgetTester tester) async { + const title = 'Test Title'; + const noText = 'No'; + + await tester.pumpApp( + Builder( + builder: (context) { + return ElevatedButton( + onPressed: () { + showDialog( + context: context, + builder: (_) => const ConfirmDialog( + title: title, + no: noText, + ), + ); + }, + child: const Text('Show Dialog'), + ); + }, + ), + ); + + await tester.tap(find.text('Show Dialog')); + await tester.pumpAndSettle(); + + expect(find.text(title), findsOneWidget); + expect(find.text(noText.toUpperCase()), findsOneWidget); + + await tester + .tap(find.widgetWithText(MyOutlinedButton, noText.toUpperCase())); + await tester.pumpAndSettle(); + + expect(find.text(title), findsNothing); + }); + + testWidgets('clicking yes button closes the dialog with true', + (WidgetTester tester) async { + const title = 'Test Title'; + const yesText = 'Yes'; + bool? result; + + await tester.pumpApp( + Builder( + builder: (context) { + return ElevatedButton( + onPressed: () async { + result = await showDialog( + context: context, + builder: (_) => const ConfirmDialog( + title: title, + yes: yesText, + ), + ); + }, + child: const Text('Show Dialog'), + ); + }, + ), + ); + + await tester.tap(find.text('Show Dialog')); + await tester.pumpAndSettle(); + + expect(find.text(title), findsOneWidget); + expect(find.text(yesText.toUpperCase()), findsOneWidget); + + await tester + .tap(find.widgetWithText(MyElevatedButton, yesText.toUpperCase())); + await tester.pumpAndSettle(); + + expect(find.text(title), findsNothing); + expect(result, isTrue); + }); + + testWidgets( + 'ConfirmDialog shows only yes button when showNoButton is false', + (WidgetTester tester) async { + const title = 'Test Title'; + const yesText = 'Yes'; + const noText = 'No'; + + await tester.pumpApp(const ConfirmDialog( + title: title, + yes: yesText, + no: noText, + showNoButton: false, + )); + + expect(find.text(yesText.toUpperCase()), findsOneWidget); + expect(find.text(noText.toUpperCase()), findsNothing); + }); + }); +} diff --git a/test/app/shared/widget/dialog/error_details_dialog_test.dart b/test/app/shared/widget/dialog/error_details_dialog_test.dart new file mode 100644 index 000000000..9518cc48f --- /dev/null +++ b/test/app/shared/widget/dialog/error_details_dialog_test.dart @@ -0,0 +1,71 @@ +import 'package:altme/app/app.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../../../../helpers/helpers.dart'; + +void main() { + group('ErrorDetailsDialog Widget Tests', () { + testWidgets('renders ErrorDetailsDialog with error description', + (WidgetTester tester) async { + const errorDescription = 'An error occurred'; + + await tester.pumpApp( + const ErrorDetailsDialog( + erroDescription: errorDescription, + ), + ); + + expect(find.text(errorDescription), findsOneWidget); + }); + + testWidgets('renders ErrorDetailsDialog with more details link', + (WidgetTester tester) async { + const errorDescription = 'An error occurred'; + const errorUrl = 'https://example.com'; + + await tester.pumpApp( + const ErrorDetailsDialog( + erroDescription: errorDescription, + erroUrl: errorUrl, + ), + ); + + expect(find.text(errorDescription), findsOneWidget); + expect(find.text('More Details'), findsOneWidget); + }); + + testWidgets('clicking OK button closes the dialog', + (WidgetTester tester) async { + const errorDescription = 'An error occurred'; + + await tester.pumpApp( + Builder( + builder: (context) { + return ElevatedButton( + onPressed: () { + showDialog( + context: context, + builder: (_) => const ErrorDetailsDialog( + erroDescription: errorDescription, + ), + ); + }, + child: const Text('Show Dialog'), + ); + }, + ), + ); + + await tester.tap(find.text('Show Dialog')); + await tester.pumpAndSettle(); + + expect(find.text(errorDescription), findsOneWidget); + + await tester.tap(find.text('OK')); + await tester.pumpAndSettle(); + + expect(find.text(errorDescription), findsNothing); + }); + }); +} diff --git a/test/app/shared/widget/dialog/error_dialog_test.dart b/test/app/shared/widget/dialog/error_dialog_test.dart new file mode 100644 index 000000000..1e383c976 --- /dev/null +++ b/test/app/shared/widget/dialog/error_dialog_test.dart @@ -0,0 +1,56 @@ +import 'package:altme/app/app.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../../../../helpers/helpers.dart'; + +void main() { + group('ErrorDialog Widget Tests', () { + testWidgets( + 'displays the ErrorDialog with the correct title and description', + (WidgetTester tester) async { + const title = 'Error Title'; + const description = 'An error occurred'; + + await tester.pumpApp( + const Scaffold( + body: ErrorDialog( + title: title, + erroDescription: description, + ), + ), + ); + + expect(find.text(title), findsOneWidget); + expect(find.text('More Details'.toUpperCase()), findsOneWidget); + + await tester.tap(find.text('More Details'.toUpperCase())); + await tester.pumpAndSettle(); + + expect(find.byType(ErrorDetailsDialog), findsOneWidget); + expect(find.text(description), findsOneWidget); + }); + + testWidgets('Popup works correctly', (WidgetTester tester) async { + const title = 'Error Title'; + const description = 'An error occurred'; + + await tester.pumpApp( + const Scaffold( + body: ErrorDialog( + title: title, + erroDescription: description, + ), + ), + ); + + expect(find.byType(AlertDialog), findsOneWidget); + expect(find.text('OK'), findsOneWidget); + + await tester.tap(find.text('OK')); + await tester.pumpAndSettle(); + + expect(find.byType(AlertDialog), findsNothing); + }); + }); +} diff --git a/test/app/shared/widget/dialog/text_field_dialog_test.dart b/test/app/shared/widget/dialog/text_field_dialog_test.dart new file mode 100644 index 000000000..267382bea --- /dev/null +++ b/test/app/shared/widget/dialog/text_field_dialog_test.dart @@ -0,0 +1,54 @@ +import 'package:altme/app/app.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../../../../helpers/helpers.dart'; + +void main() { + group('Text Field Dialog', () { + testWidgets('TextFieldDialog widget test', (WidgetTester tester) async { + await tester.pumpApp( + const TextFieldDialog( + title: 'Title', + label: 'Label', + subtitle: 'Subtitle', + initialValue: 'Initial Value', + yes: 'Yes', + no: 'No', + ), + ); + // Verify that the title, label, subtitle, and initial value are displayed. + expect(find.text('Title'), findsOneWidget); + expect(find.text('Label'), findsOneWidget); + expect(find.text('Subtitle'), findsOneWidget); + expect(find.text('Initial Value'), findsOneWidget); + + // Verify that the Yes and No buttons are displayed. + expect(find.text('Yes'.toUpperCase()), findsOneWidget); + expect(find.text('No'.toUpperCase()), findsOneWidget); + + // Tap on the Yes button and verify that the text is popped. + await tester.tap(find.text('Yes'.toUpperCase())); + await tester.pumpAndSettle(); + expect(find.text('Initial Value'), findsNothing); + }); + + testWidgets('Popup works correctly', (WidgetTester tester) async { + await tester.pumpApp( + const TextFieldDialog( + title: 'Title', + label: 'Label', + subtitle: 'Subtitle', + initialValue: 'Initial Value', + yes: 'Yes', + no: 'No', + ), + ); + + expect(find.text('No'.toUpperCase()), findsOneWidget); + + await tester.tap(find.text('No'.toUpperCase())); + await tester.pumpAndSettle(); + expect(find.text('No'.toUpperCase()), findsNothing); + }); + }); +}