ModernMemoires/test/why_widget_test.dart

84 lines
2.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moody/utils/widgets/why_widget.dart';
void main() {
testWidgets('WhyWidget adds "warum? " when typing', (WidgetTester tester) async {
TextEditingController controller = TextEditingController();
// Build our app and trigger a frame.
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: WhyWidget(
controller: controller,
prependWhy: true,
),
),
));
// Tap the TextField to focus it.
await tester.tap(find.byType(TextField));
await tester.pump();
// Enter text and check if "warum? " is prepended.
await tester.enterText(find.byType(TextField), 'test');
await tester.pump();
// Verify that "warum? " is prepended to the entered text.
expect(controller.text, 'warum? test');
});
testWidgets('WhyWidget does not add "warum? " when prependWhy is false', (WidgetTester tester) async {
TextEditingController controller = TextEditingController();
// Build our app and trigger a frame.
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: WhyWidget(
controller: controller,
prependWhy: false,
),
),
));
// Tap the TextField to focus it.
await tester.tap(find.byType(TextField));
await tester.pump();
// Enter text and check if "warum? " is not prepended.
await tester.enterText(find.byType(TextField), 'test');
await tester.pump();
// Verify that "warum? " is not prepended to the entered text.
expect(controller.text, 'test');
});
testWidgets('WhyWidget hides cursor when focus is lost', (WidgetTester tester) async {
TextEditingController controller = TextEditingController();
// Build our app and trigger a frame.
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: WhyWidget(
controller: controller,
prependWhy: true,
),
),
));
// Tap the TextField to focus it.
await tester.tap(find.byType(TextField));
await tester.pump();
// Emulate text field losing focus.
FocusScope.of(tester.element(find.byType(TextField))).unfocus();
await tester.pump();
// Verify that the cursor is hidden.
expect(controller.text.contains('|'), false);
});
}