3

I have a class that extends EditableText, which provides styled editing - colour spans and so on.

The parent widget has a bool flag to use default TextInput or the custom one that extends EditableText.

Testing works fine for the default one:

final String dummyDesc = 'dummy desc';
final Finder mention = find.byType(Mention);
await tester.enterText(mention, dummyDesc);

however, if I do the same on a page where Mention uses the custom input, the test fails as it cannot find a TextInput - as there isn't one.

Error:

Bad state: No element

How Do you use enterText on a widget that extends EditableText? Or something similar?

bionara
  • 228
  • 1
  • 10

1 Answers1

2

This appears to be due to the showKeyboard method that enterText calls internally: https://github.com/flutter/flutter/blob/3b067049adc14ffb300e78d9b0d0adf3d581de7c/packages/flutter_test/lib/src/widget_tester.dart#L854

It is checking that the finder matches or is a descendent of EditableText, which does it's matching based on the runtimeType.

It's a bit hacky, but you could provide a runtimeType getter, which returns EditableText and will allow it to work.

class Mention extends EditableText {
  // ...

  Type get runtimeType => EditableText;

  // ...
}

Caveat: This will mean that you will not be able to find.byType(Mention) any more. You can work around this by writing a custom finder, or using the predicate finder find.byElementPredicate((Element element) => element.widget is Mention).

Lee
  • 2,993
  • 3
  • 21
  • 27