how to mock a callback argument in jest?

Viewed 77

i know i have to configure my mockedShow function and this is the code:

export const mockedShow = jest.fn();
export const mockedHide = jest.fn();

jest.mock('react-native-toast-message', () => {
  const RNToast = jest.requireActual('react-native-toast-message');
  const React = require('react');
  class Toast extends React.Component {
    static show = mockedShow;
    static hide = mockedHide;

    render() {
      return React.createElement('Toast', this.props, this.props.children);
    }
  }
  Toast.propTypes = RNToast.propTypes;
  return Toast;
});

This is how i used it:

 useEffect(() => {
    if (state.pinError) {
      console.log('Invalid PIN');
      Toast.show({
        type: 'error',
        text1: 'Invalid PIN',
        visibilityTime: 1500,
        topOffset: 25,
        onHide: () => {
          console.log('Invalid PIN onHide');
          dispatch(actions.setPinError(false));
          setHasSetPin(false);
          setPin(0);
        },
      });
    }
  }, [state.pinError]);

when i am testing i see the log of 'Invalid PIN' but no 'Invalid PIN onHide' so i thought my onHide is not being called. I actually also did this on my test but i still don't see the log 'Invalid PIN onHide':

jest.advanceTimersByTime(1699);

How do i modify the mockedShow jest fn to fire the onHide callback from its argument?

0 Answers
Related