dimanche 7 février 2021

Testing widget parent's function execute from a child

I'm stucked with some testing on flutter. I provied a function from parent to a child like this :

// WidgetParent 
    class WidgetParent extends State<WidgetParent> {
    
    myFunction(Location location) {
        BlocProvider.of<Bloc>(context)
            .add(blocFunction(location: location));
    
     @override
      Widget build(BuildContext context) {
       return Container(... //Stuff
       myChild(myFunction: myFunction)
         )
        }
      }
    }

The function is execute when a GestureDetector is tap on the child. I try to test this with creating a local function in my test file, to provided to the child like this :

// myChildTest
    void main() {
  group('SearchBarLocationResult', () {

onPressed(Location location) {
      didTap = true;
      mapBloc
          .add(blocFunction(location:location)); // Mocked bloc
    }
 
testWidgets('check gesture detector on tap function execute',
        (tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
              body: myChild(
            myFunction: onPressed,
          )),
        ),
      );
      expect(didTap, isFalse);
      await tester.tap(find.byType(GestureDetector));
      expect(didTap, isTrue);
      verify(mapBloc.add(
              blocFunction(location:location)))
          .called(1);
    });
 });
}

So, this test is not really good because i'm testing that the onTap is tap, and that my bloc is called, but from a local function in my file. I would like to directly passed the function "myFunction" of my WidgetParent to my child, to really test that she is call and that she execute my bloc function. I would like to do something like this :

MaterialApp(
   home: Scaffold(
      body: myChild(
          myFunction: ParentWidget.myFunction,
              )),
            ),

Is there a way to provided directly another widget's function to the widget concerned by the test ?

Aucun commentaire:

Enregistrer un commentaire