|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_html/flutter_html.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | + |
| 5 | +import '../test_data.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + testWidgets('<a> test', (WidgetTester tester) async { |
| 9 | + await tester.pumpWidget( |
| 10 | + TestApp( |
| 11 | + child: Html( |
| 12 | + data: """<a>Hello, world!</a>""", |
| 13 | + ), |
| 14 | + ), |
| 15 | + ); |
| 16 | + expect(find.text("Hello, world!", findRichText: true), findsOneWidget); |
| 17 | + }); |
| 18 | + |
| 19 | + testWidgets('<a> test with href', (WidgetTester tester) async { |
| 20 | + await tester.pumpWidget( |
| 21 | + TestApp( |
| 22 | + child: Html( |
| 23 | + data: """<a href="https://example.com">Hello, world!</a>""", |
| 24 | + ), |
| 25 | + ), |
| 26 | + ); |
| 27 | + expect(find.text("Hello, world!", findRichText: true), findsOneWidget); |
| 28 | + }); |
| 29 | + |
| 30 | + testWidgets('<a> with widget child renders', (WidgetTester tester) async { |
| 31 | + await tester.pumpWidget( |
| 32 | + TestApp( |
| 33 | + child: Html( |
| 34 | + data: """<a href="https://example.com"><icon></icon></a>""", |
| 35 | + extensions: [ |
| 36 | + TagExtension( |
| 37 | + tagsToExtend: {"icon"}, |
| 38 | + child: const Icon(Icons.check), |
| 39 | + ), |
| 40 | + ], |
| 41 | + ), |
| 42 | + ), |
| 43 | + ); |
| 44 | + expect(find.byIcon(Icons.check), findsOneWidget); |
| 45 | + }); |
| 46 | + |
| 47 | + testWidgets('Tapping <a> test', (WidgetTester tester) async { |
| 48 | + String tappedUrl = ""; |
| 49 | + |
| 50 | + await tester.pumpWidget( |
| 51 | + TestApp( |
| 52 | + child: Html( |
| 53 | + data: """<a href="https://example.com">Hello, world!</a>""", |
| 54 | + onLinkTap: (url, _, __) { |
| 55 | + tappedUrl = url ?? ""; |
| 56 | + }, |
| 57 | + ), |
| 58 | + ), |
| 59 | + ); |
| 60 | + expect(find.text("Hello, world!", findRichText: true), findsOneWidget); |
| 61 | + expect(tappedUrl, equals("")); |
| 62 | + await tester.tap(find.text("Hello, world!", findRichText: true)); |
| 63 | + expect(tappedUrl, equals("https://example.com")); |
| 64 | + }); |
| 65 | + |
| 66 | + testWidgets('Tapping <a> with widget works', (WidgetTester tester) async { |
| 67 | + String tappedUrl = ""; |
| 68 | + |
| 69 | + await tester.pumpWidget( |
| 70 | + TestApp( |
| 71 | + child: Html( |
| 72 | + data: """<a href="https://example.com"><icon></icon></a>""", |
| 73 | + onLinkTap: (url, _, __) { |
| 74 | + tappedUrl = url ?? ""; |
| 75 | + }, |
| 76 | + extensions: [ |
| 77 | + TagExtension( |
| 78 | + tagsToExtend: {"icon"}, |
| 79 | + child: const Icon(Icons.check), |
| 80 | + ), |
| 81 | + ], |
| 82 | + ), |
| 83 | + ), |
| 84 | + ); |
| 85 | + expect(find.byIcon(Icons.check), findsOneWidget); |
| 86 | + expect(tappedUrl, equals("")); |
| 87 | + await tester.tap(find.byIcon(Icons.check)); |
| 88 | + expect(tappedUrl, equals("https://example.com")); |
| 89 | + }); |
| 90 | +} |
0 commit comments