flushbar icon indicating copy to clipboard operation
flushbar copied to clipboard

[Question] Detecting flushbar in widget test.

Open evanblasband opened this issue 4 years ago • 1 comments

Situation I currently have a flushbar with a duration: const Duration(seconds: 2) that gets shown when a native callback happens. This is how I am calling it

// native callback happens
myFlushbar = Flushbar(...)..onStatusChanged = (...)
if(conditionsMet) { myFlushbar..show(context) }

so it shows for the 2 seconds then disappears.

Question Is there a way I can detect the flushbar in a widget test?? My current tests can not find it and I am guessing it is because the flushbar is only visible for a short time.

What I have tried I have tried both expect(...) and expectLater(...) in various orders before and after my mock native call in my tests. I have been looking for the Flushbar widget itself as well as the text it displays without success. Also the onStatusChanged prints are showing for appearing and dismissed but NOT for show nor hiding in my test but work fine in my app. Any advice is much appreciated.

evanblasband avatar Nov 11 '20 03:11 evanblasband

This is my workaround. I also struggled a lot with this. Hope it helps someone.

First, wrap your test in tester.runAsync(). Then, use await tester.pump() to trigger a frame before looking for the Widget. Finally, use the text in the Flushbar to detect it.

testWidgets(
      'show a Flushbar with error message',
      (tester) async {
        await tester.runAsync(() async {
              
          await tester.pumpWidget(...);

          await tester.pump();

          final errorMessage = find.text('An error occurred.');

          expect(errorMessage, findsOneWidget);
        });
      },
    );

manuelvargastapia avatar Feb 22 '21 23:02 manuelvargastapia