vendredi 24 février 2017

Why is my button undefined in my test?

My test:

  describe('button component', () => {
    it('should toggle off when clicked', () => {
      let component;
      component = ReactTestUtils.renderIntoDocument(<Search />);
      let searchbtn = ReactTestUtils.findRenderedDOMComponentWithTag(component, 'button');
      ReactTestUtils.Simulate.click(searchbtn);
      console.log(searchbtn, 'search button***'); //UNDEFINED
      expect(searchbtn.calledOnce).to.equal(false);
    })
  });

This is my search component:

  render() {
    return (
      <div className="search">
        <button className="searchButton" onClick={this.handleSearch}>{this.state.on ? 'ON' : 'OFF'}</button>
      </div>
    );
  }

Do I need to spy on it or mock it? or is there a better way to test buttons in react?

Aucun commentaire:

Enregistrer un commentaire