Created
February 3, 2020 14:31
-
-
Save vishna/d27c6a5fee14e059d9c7759727630568 to your computer and use it in GitHub Desktop.
WidgetTester RichEdit Extension - find a piece of text, e.g. tap a link in the text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// found nothing better than this | |
/// https://github.com/flutter/flutter/blob/master/packages/flutter/test/widgets/hyperlink_test.dart#L47 | |
/// sooo... | |
extension WidgetTesterRichEditExtension on WidgetTester { | |
Iterable<InlineSpan> findTextSpans( | |
Finder richTextFinder, bool Function(InlineSpan) predicate) { | |
final richText = widget<RichText>(richTextFinder); | |
expect(richText.text, isInstanceOf<TextSpan>()); | |
final TextSpan innerText = richText.text; | |
return innerText.children.where(predicate); | |
} | |
} | |
/// then | |
void findALinkAnTapIt(WidgetTester tester) async { | |
final richTextFinder = find.byKey(ValueKey("myKey")); | |
final spans = tester.findTextSpans( | |
richTextFinder, | |
(inlineSpan) => | |
inlineSpan is TextSpan && | |
inlineSpan.recognizer is TapGestureRecognizer); | |
final TextSpan = spans.first; | |
expect(spans, hasLength(1)); | |
final TextSpan linkSpan = spans.first; | |
final TapGestureRecognizer recognizer = linkSpan.recognizer; | |
recognizer.onTap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment