|
| 1 | +import 'package:dcc_toolkit/ui/annotated_text/annotated_text.dart'; |
| 2 | +import 'package:flutter/gestures.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + testWidgets('renders annotated text without action', (WidgetTester tester) async { |
| 8 | + await tester.pumpWidget( |
| 9 | + const MaterialApp( |
| 10 | + home: AnnotatedText( |
| 11 | + text: 'Hello [world]', |
| 12 | + actions: {}, |
| 13 | + defaultStyle: TextStyle(color: Colors.black), |
| 14 | + annotationStyle: TextStyle(color: Colors.blue), |
| 15 | + ), |
| 16 | + ), |
| 17 | + ); |
| 18 | + |
| 19 | + // Get the only RichText widget in the tree |
| 20 | + final richTextWidget = tester.widget<RichText>(find.byType(RichText)); |
| 21 | + final rootSpan = richTextWidget.text as TextSpan; |
| 22 | + |
| 23 | + // Combine all spans into a single string |
| 24 | + final fullText = rootSpan.children!.map((span) => (span as TextSpan).text).join(); |
| 25 | + |
| 26 | + expect(fullText, equals('Hello world')); |
| 27 | + final annotatedSpan = rootSpan.children![1]; // "world" |
| 28 | + expect((annotatedSpan as TextSpan).style!.color, equals(Colors.blue)); |
| 29 | + }); |
| 30 | + |
| 31 | + testWidgets('annotated text applies annotationStyle', (WidgetTester tester) async { |
| 32 | + const annotationStyle = TextStyle(color: Colors.blue); |
| 33 | + |
| 34 | + await tester.pumpWidget( |
| 35 | + const MaterialApp( |
| 36 | + home: AnnotatedText( |
| 37 | + text: 'Hello [world]', |
| 38 | + actions: {}, |
| 39 | + defaultStyle: TextStyle(color: Colors.black), |
| 40 | + annotationStyle: annotationStyle, |
| 41 | + ), |
| 42 | + ), |
| 43 | + ); |
| 44 | + |
| 45 | + final richText = tester.widget<RichText>(find.byType(RichText)); |
| 46 | + final rootSpan = richText.text as TextSpan; |
| 47 | + |
| 48 | + final annotatedSpan = rootSpan.children![1]; // "world" |
| 49 | + expect((annotatedSpan as TextSpan).style!.color, equals(annotationStyle.color)); |
| 50 | + }); |
| 51 | + |
| 52 | + testWidgets('annotated text with action has a GestureRecognizer', (WidgetTester tester) async { |
| 53 | + await tester.pumpWidget( |
| 54 | + MaterialApp( |
| 55 | + home: AnnotatedText( |
| 56 | + text: 'Click [here](onTap)', |
| 57 | + actions: {'onTap': () {}}, |
| 58 | + defaultStyle: const TextStyle(color: Colors.black), |
| 59 | + annotationStyle: const TextStyle(color: Colors.blue), |
| 60 | + ), |
| 61 | + ), |
| 62 | + ); |
| 63 | + |
| 64 | + // Get the RichText widget |
| 65 | + final richTextWidget = tester.widget<RichText>(find.byType(RichText)); |
| 66 | + final rootSpan = richTextWidget.text as TextSpan; |
| 67 | + |
| 68 | + // Locate the annotated span (second span in the children list) |
| 69 | + final annotatedSpan = rootSpan.children![1] as TextSpan; |
| 70 | + |
| 71 | + // Verify a recognizer exists and is a TapGestureRecognizer |
| 72 | + expect(annotatedSpan.recognizer, isNotNull); |
| 73 | + expect(annotatedSpan.recognizer, isA<TapGestureRecognizer>()); |
| 74 | + }); |
| 75 | +} |
0 commit comments