Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we simulate click events and check its functionality's result simultaneously? #1455

Closed
PriyalSayonetech opened this issue Dec 29, 2017 · 1 comment

Comments

@PriyalSayonetech
Copy link

PriyalSayonetech commented Dec 29, 2017

Consider the below sample react code- (The code is to perform toggling when clicking a tab. when we click a tab, it becomes active and remaining all becomes inactive.)

propTypes: {
    selectedWidget: React.PropTypes.number,
    .........}

  render: function() {
   return (
      <div className="tabs">
        <div
          className={this.props.selectedWidget === 0? "active": void 0}
          onClick={function() { this.setActiveTab(0); }.bind(this)}>
            {Texts.get('HOME_METRICS_HEADER')}
        </div>
        <div
          className={this.props.selectedWidget === 1? "active": void 0}
          onClick={function() { this.setActiveTab(1); }.bind(this)}>
            {Texts.get('HOME_METRICS_HEADER_LENGTH')}
        </div>
    </div>
   )}

Now Below is the test i have written in enzyme:-

it('testing first tab click',()=>{

        const handleClick = sinon.spy();
        const wrapper = shallow(<Tabs selectedWidget={0} updateSelectedTab={handleClick}/>);
        wrapper.find('div.active').simulate('click');
        expect(handleClick).to.have.property('callCount', 1);

});

The above code, test is working perfectly. But i want to know whether the actual functionlity is happening? That is, when i check whether toggle functionality is working, it actually doesnt. When i click tab1, the tab2 must go inactive. but when i check for the inactive tab using "find()" its not found. So any idea about these guys?

Same way i have another doubt- On simulating a submit button, the submit functionality should actually work right? but when i check whether submitted data is saved in database, its not yet saved

@ljharb
Copy link
Member

ljharb commented Dec 30, 2017

What version of enzyme and React are you using?

Separately, I'd suggest avoiding simulate entirely; do .prop('onClick')() instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants