Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Detecting flushbar in widget test. #162

Open
evanblasband opened this issue Nov 11, 2020 · 1 comment
Open

[Question] Detecting flushbar in widget test. #162

evanblasband opened this issue Nov 11, 2020 · 1 comment

Comments

@evanblasband
Copy link

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.

@manuelvargastapia
Copy link

manuelvargastapia commented Feb 22, 2021

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);
        });
      },
    );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants