Skip to content

Commit

Permalink
Add dialog tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed May 21, 2024
1 parent 2162f7a commit cb8c793
Show file tree
Hide file tree
Showing 6 changed files with 597 additions and 0 deletions.
126 changes: 126 additions & 0 deletions test/app/shared/widget/base/text_field_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
});
}
140 changes: 140 additions & 0 deletions test/app/shared/widget/dialog/becareful_dialog_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
});
}
Loading

0 comments on commit cb8c793

Please sign in to comment.