ModernMemoires/test/mood_text_area_widget_test....

89 lines
2.8 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moody/utils/widgets/mood_text_area_widget.dart';
void main() {
group('MoodTextAreaWidget Tests', () {
testWidgets('displays initial text correctly', (WidgetTester tester) async {
const initialText = 'Test Mood';
await tester.pumpWidget(const MaterialApp(
home: Material( // Wrap the MoodTextAreaWidget in a Material widget
child: MoodTextAreaWidget(
initialText: initialText,
),
),
));
// Verify the initial text is displayed
expect(find.text(initialText), findsOneWidget);
});
testWidgets('is disabled', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(
home: Material( // Wrap the MoodTextAreaWidget in a Material widget
child: MoodTextAreaWidget(
isDisabled: true,
),
),
));
final textField = tester.widget<TextField>(find.byType(TextField));
expect(textField.enabled, isFalse);
});
testWidgets('autofocuses if enabled', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(
home: Material( // Wrap the MoodTextAreaWidget in a Material widget
child: MoodTextAreaWidget(
autoFocus: true,
),
),
));
// Trigger a frame.
await tester.pump();
final TextField textField = tester.widget(find.byType(TextField));
final FocusNode focusNode = textField.focusNode!;
expect(focusNode.hasFocus, isTrue);
});
testWidgets('adds prefix "why? " if hasPrefix is true', (WidgetTester tester) async {
const initialText = 'Test Mood';
await tester.pumpWidget(const MaterialApp(
home: Material( // Wrap the MoodTextAreaWidget in a Material widget
child: MoodTextAreaWidget(
hasPrefix: true,
initialText: initialText,
),
),
));
// Verify the initial text is displayed with the prefix
expect(find.text('why? $initialText'), findsOneWidget);
});
testWidgets('maintains prefix "why? " during editing', (WidgetTester tester) async {
const initialText = 'Test Mood';
final TextEditingController controller = TextEditingController(text: initialText);
await tester.pumpWidget(MaterialApp(
home: Material( // Wrap the MoodTextAreaWidget in a Material widget
child: MoodTextAreaWidget(
hasPrefix: true,
controller: controller,
),
),
));
// Focus the TextField and enter some text.
await tester.enterText(find.byType(TextField), 'New Mood');
await tester.pump();
// Verify the text contains the prefix
expect(controller.text, startsWith('warum? '));
});
});
}