mercredi 27 mai 2020

React jest enzyme function called test

Hi I have a simple component I need to test:

MyComponent.js-----

import React from 'react';
const MyComponent = (props) => {

  onClickHandler = () => {
     console.log('clicked');
     props.outsideClickHandler();
  }

  return (
     <div>
        <span className='some-button' onClick={onClickHandler}></span>
     </div>
  );
}

MyComponent.test.js----

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

describe('MyComponent', () => {
    const onClickHandler = jest.fn();

    it('calls click event', () => {
      const wrapper = shallow(<MyComponent />);
      wrapper.find('.some-button').simulate('click');
      expect(onClickHandler.mock.calls.length).toEqual(1);  // tried this first
      expect(onClickHandler).toBeCalled(); // tried this next
    });
});

Tried above two types of expect, my console log value is coming

console.log('clicked'); comes 

but my test fails and I get this:

expect(received).toEqual(expected) // deep equality

Expected: 1
Received: 0

Aucun commentaire:

Enregistrer un commentaire