jeudi 23 avril 2020

What is the purpose of testing if a React button successfully calls a function given to onClick?

Below is a very common pattern I have seen both online and at a company I interned at:

import React from 'react';
import { shallow } from 'enzyme';
import Button from './Button';

describe('Test Button component', () => {
  it('Test click event', () => {
    const mockCallBack = jest.fn();

    const button = shallow((<Button onClick={mockCallBack}>Ok!</Button>));
    button.find('button').simulate('click');
    expect(mockCallBack.mock.calls.length).toEqual(1);
  });
});

What I don't understand is why bother writing this test? If a component has an onClick property, and onClick is supplied a callback, of course clicking that component will result in the callback being called. When would this ever fail? What am I not considering here?

Aucun commentaire:

Enregistrer un commentaire