Files
ReelShort/test/discover/discover_search_field_test.dart
T

58 lines
1.8 KiB
Dart

// Author: fruit.com.cn
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/l10n/app_localizations.dart';
import 'package:fruitshorts/pages/discover/widgets/discover_search_field.dart';
void main() {
testWidgets('should_clear_text_and_keep_focus_when_clear_is_tapped', (
tester,
) async {
final controller = TextEditingController();
final focusNode = FocusNode();
final changes = <String>[];
addTearDown(controller.dispose);
addTearDown(focusNode.dispose);
await tester.pumpWidget(
MaterialApp(
locale: const Locale('en'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(
body: DiscoverSearchField(
controller: controller,
focusNode: focusNode,
onChanged: changes.add,
loading: true,
),
),
),
);
final search = find.byKey(const Key('discover-search'));
expect(find.byKey(const Key('discover-search-clear')), findsNothing);
await tester.tap(search);
await tester.enterText(search, 'velvet');
await tester.pump();
changes.clear();
final clear = find.byKey(const Key('discover-search-clear'));
final loading = find.byKey(const Key('discover-search-loading'));
expect(clear, findsOneWidget);
expect(loading, findsOneWidget);
expect(tester.getSize(clear), const Size(18, 18));
expect(tester.getRect(loading).right, lessThan(tester.getRect(clear).left));
await tester.tap(clear);
await tester.pump();
expect(controller.text, isEmpty);
expect(changes, ['']);
expect(find.byKey(const Key('discover-search-clear')), findsNothing);
expect(focusNode.hasFocus, isTrue);
});
}