import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:jiu_client/core/theme/themes.dart'; import 'package:jiu_client/widgets/searchable_option_field.dart'; void main() { Widget host({ required ValueChanged onChanged, Future Function(String)? onCreate, }) { return MaterialApp( theme: buildTheme('a'), home: Scaffold( body: SearchableOptionField( options: [OptionItem(id: 1, name: '53度')], selectedId: null, hint: '选择系列', dialogTitle: '选择系列', onChanged: onChanged, onCreate: onCreate, ), ), ); } testWidgets('搜不到时显示「新增到基础数据」入口', (tester) async { await tester.pumpWidget(host(onChanged: (_) {}, onCreate: (_) async => 99)); // 打开搜索对话框 await tester.tap(find.byType(SearchableOptionField)); await tester.pumpAndSettle(); // 输入不存在的关键字 await tester.enterText(find.byType(TextField).first, '52度'); await tester.pumpAndSettle(); expect(find.textContaining('新增「52度」到基础数据'), findsOneWidget); }); testWidgets('确认新增后回填选中新 id', (tester) async { int? selected; await tester.pumpWidget( host(onChanged: (v) => selected = v, onCreate: (_) async => 99)); await tester.tap(find.byType(SearchableOptionField)); await tester.pumpAndSettle(); await tester.enterText(find.byType(TextField).first, '52度'); await tester.pumpAndSettle(); await tester.tap(find.textContaining('新增「52度」到基础数据')); await tester.pumpAndSettle(); // 确认框 await tester.tap(find.text('确认新增')); await tester.pumpAndSettle(); expect(selected, 99); }); testWidgets('未提供 onCreate 时不显示新增入口', (tester) async { await tester.pumpWidget(host(onChanged: (_) {})); await tester.tap(find.byType(SearchableOptionField)); await tester.pumpAndSettle(); await tester.enterText(find.byType(TextField).first, '52度'); await tester.pumpAndSettle(); expect(find.textContaining('新增'), findsNothing); }); }