Today I Learned

1 post by wojciechbożek

Jest.js haveBeenCalledWith anything()

Sometimes we want to check if the callback has been called with some arguments, but providing all of them can be hard (some nested structure with other callbacks, etc.)

We can check however some of the calling arguments and replace rest with expect.anything()

  it('should trigger confirmation when clicking on report lost button', () => {
    const { container, getByTestId } = render(<ReportScreen />);
    Alert.alert = jest.fn();
    const reportLostButton = getByTestId('reportLostButton');
    act(() => {
      fireEvent.press(reportLostButton);
    })
    expect(Alert.alert).toHaveBeenCalledWith(
      'Report as lost', 
      expect.anything(), 
      expect.anything(), 
      expect.anything()
    );
  });