vendredi 8 janvier 2021

How do I test an async handler in @testing-library/react-native?

I can not wrap my head around this but it's probably a simple one.

Let's say I have this:

   <MyComponent amazingAction={action}/>
   <MagicButton/>

So it's essentially a component with a handle and another component that when clicked will do an async action. Because of that async action, the handler in MyComponent will be triggered.

I essentially want to test that if I render both and trigger a click on the button, the handler will be called.

I tried this:

const maybeThisWorks = jest.fn();

const { getByText } = render(
      <MyComponent amazingAction={maybeThisWorks}/>
      <MagicButton title="My Button"/>
);

let button = getByText("My Button");
fireEvent.press(button);

expect(maybeThisWorks).toHaveBeenCalled();

Aucun commentaire:

Enregistrer un commentaire