lundi 14 décembre 2020

Simulating onClick event on element but getting error: Method “simulate” is meant to be run on 1 node. 0 found instead

code of my return function where li element should be found:

      return (
          <li
            className={className}
            key={optionName.fruitName}
            id = 'clickItem'
            onClick={onClick}
            style=
          >
            <img className="img" src={optionName.img} alt="" />
            <h4 className = 'name'>{optionName.fruitName}</h4>
          </li>
        );

my test case: what im trying to do is to trigger onclick function by simulating click but not finding any node.

describe('onClick function testing', () => {
let options=[
  {
    fruitName: 'Papaya',
    img:'https://c.ndtvimg.com/2019-06/qlo3s8ko_papaya-seeds_625x300_06_June_19.jpg' 
  },
  {
    fruitName: 'Apple',
     img: 'https://www.dukachapchap.co.ke/wp-content/uploads/2019/01/APPLE.jpg'
  },
  {
    fruitName: 'Banana',
    img: 'https://images.immediate.co.uk/production/volatile/sites/30/2017/01/Bananas-218094b- 
     scaled.jpg?quality=90&resize=960%2C872'
  }
  ]

  it('should be called with click', () => {
  const logSpy = jest.spyOn(console, 'log');
  const wrapper = shallow(<AutoComplete options = {options}></AutoComplete>)
  expect(wrapper.find('.option-active')).toBeDefined();
  const event = { currentTarget: { innerText: 'Papaya' }};
  const li = wrapper.find('li').simulate('click',event);
  expect(li).toBeTruthy();
 
  });

Aucun commentaire:

Enregistrer un commentaire