|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:vsc_datetime_field/vsc_datetime_field.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + group('VscDatetimeField error handling', () { |
| 7 | + testWidgets('handles error widget in decoration without assertion error', |
| 8 | + (WidgetTester tester) async { |
| 9 | + // Build a VscDatetimeField with an error widget in the decoration |
| 10 | + await tester.pumpWidget( |
| 11 | + MaterialApp( |
| 12 | + home: Scaffold( |
| 13 | + body: VscDatetimeField( |
| 14 | + textFieldConfiguration: const TextFieldConfiguration( |
| 15 | + decoration: InputDecoration( |
| 16 | + labelText: 'Test Field', |
| 17 | + // This simulates what UiEditorDatetimeField does |
| 18 | + error: Text('External validation error'), |
| 19 | + errorMaxLines: 3, |
| 20 | + ), |
| 21 | + ), |
| 22 | + ), |
| 23 | + ), |
| 24 | + ), |
| 25 | + ); |
| 26 | + |
| 27 | + // The widget should build without throwing an assertion error |
| 28 | + expect(find.byType(VscDatetimeField), findsOneWidget); |
| 29 | + expect(find.text('External validation error'), findsOneWidget); |
| 30 | + |
| 31 | + // Now trigger an internal error by typing invalid date |
| 32 | + await tester.enterText(find.byType(TextField), '5/3/'); |
| 33 | + await tester.pump(); |
| 34 | + |
| 35 | + // Should still show only the external error widget, not internal errorText |
| 36 | + expect(find.text('External validation error'), findsOneWidget); |
| 37 | + expect(find.text('Invalid value'), findsNothing); |
| 38 | + }); |
| 39 | + |
| 40 | + testWidgets('shows internal error when no external error widget exists', |
| 41 | + (WidgetTester tester) async { |
| 42 | + await tester.pumpWidget( |
| 43 | + MaterialApp( |
| 44 | + home: Scaffold( |
| 45 | + body: VscDatetimeField( |
| 46 | + textFieldConfiguration: const TextFieldConfiguration( |
| 47 | + decoration: InputDecoration( |
| 48 | + labelText: 'Test Field', |
| 49 | + // No error widget provided |
| 50 | + ), |
| 51 | + ), |
| 52 | + ), |
| 53 | + ), |
| 54 | + ), |
| 55 | + ); |
| 56 | + |
| 57 | + // Enter invalid date |
| 58 | + await tester.enterText(find.byType(TextField), '5/3/'); |
| 59 | + await tester.pump(); |
| 60 | + |
| 61 | + // Should show the internal error text |
| 62 | + expect(find.text('Invalid value'), findsOneWidget); |
| 63 | + }); |
| 64 | + |
| 65 | + testWidgets('respects existing errorText when no internal error', |
| 66 | + (WidgetTester tester) async { |
| 67 | + await tester.pumpWidget( |
| 68 | + MaterialApp( |
| 69 | + home: Scaffold( |
| 70 | + body: VscDatetimeField( |
| 71 | + textFieldConfiguration: const TextFieldConfiguration( |
| 72 | + decoration: InputDecoration( |
| 73 | + labelText: 'Test Field', |
| 74 | + errorText: 'Pre-existing error text', |
| 75 | + ), |
| 76 | + ), |
| 77 | + ), |
| 78 | + ), |
| 79 | + ), |
| 80 | + ); |
| 81 | + |
| 82 | + // Should show the pre-existing error text |
| 83 | + expect(find.text('Pre-existing error text'), findsOneWidget); |
| 84 | + }); |
| 85 | + }); |
| 86 | +} |
0 commit comments