+
Skip to content

Unable to test .isExecuting state for Async commands #7

@Gaby-Torres

Description

@Gaby-Torres

When writing widget tests, how do you mock the "isExecuting" state successfully to test for ui in that state. I've been trying some approaches and this is the closest I've landed. It says the .isExecuting state is true but the widget tests fail. When I step through the debugger, I don't see a rebuild triggered by widgets that listen to the executing state of sendBack. Additionally, it doesn't seem to turn the .isExecuting.value back once its meant to be "done" which adds more confusion.

Test:

testWidgets('loading', (tester) async {
  await tester.runAsync(() async {
    addTearDown(tester.view.reset);
    tester.view.physicalSize = const Size(800, 800.5);
    tester.view.devicePixelRatio = 1;
    Future<ResultDart<void, String>?> sendBack() async {
     await Future.delayed(const Duration(seconds: 10));
     return Success(());
    }
    when(vm.sendBack).thenReturn(Command.createAsyncNoParam(sendBack, initialValue: null));
    await tester.pumpWidget(MaterialApp(home: Page(vm: vm),),);
    Finder fab = find.byType(FloatingActionButton);
    //these pass
    expect(find.byType(FloatingActionButton), findsOne);
    expect(find.byType(Card), findsOne);
    expect(find.byType(CircularProgressIndicator), findsNothing);
    expect(vm.sendScheduleBack.isExecuting.value, false);
   
    //triggers sendBack command here
    await tester.tap(fab);
    await tester.pump();

    //this expect passes
    expect(vm.sendBack.isExecuting.value, true);

    //these widget expects fail
    expect(find.byType(CircularProgressIndicator), findsOne);
    expect(find.byType(FloatingActionButton), findsNothing);
    expect(find.byType(Card), findsNothing);
    expect(vm.sendBack.isExecuting.value, true);
    await tester.pumpAndSettle(const Duration(seconds: 15));

    //if the widget expect statements are commented out, this expect fails as well?
    expect(vm.sendBack.isExecuting.value, false);
  });
});

UI:

@override
Widget build(BuildContext context) {
 return ListenableBuilder(
 listenable: Listenable.merge([widget.vm.sendBack.isExecuting,]),
 builder: (context, _) {
  return Scaffold(

  appBar: AppBar(leading:
   (!widget.vm.sendBack.isExecuting.value)
      ? IconButton(onPressed: () {},icon: Icon(Icons.arrow_back,size: iconSize),)
      : null,),

  body: Stack(
   children: [
    if (widget.vm.sendBack.isExecuting.value)
     Center(child: Column(children:[ CircularProgressIndicator(),])),
    if (!widget.vm.sendBack.isExecuting.value)
     ListView(children: [Card(vm: widget.vm),]),
   ]),
  
  floatingActionButton: Container(
    child: ValueListenableBuilder(
     valueListenable: widget.vm.sendBack.isExecuting,
     builder: (context, isExecuting, child) {
      if (isExecuting) {returnSizedBox();}
       return  FloatingActionButton(onPressed:(){widget.vm.sendBack.execute();},
                child: Icon(Icons.save_as));
 })));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载