jeudi 20 décembre 2018

react testing component event

i have the following react component (.jsx)

@observable isVisible = true;
handleVisibility() {
    this.isVisible = !this.isVisible;
}

render() {
    return <div
        ...
        onMouseEnter={event => this.handleVisibility(event)}
        onMouseLeave={event => this.handleVisibility(event)}
    </div>;
}
ComponentListElement.propTypes = {
    componentName: PropTypes.string.isRequired,
    id: PropTypes.string.isRequired,
};
ComponentListElement.wrappedComponent.propTypes = {
    schema: PropTypes.object.isRequired,
};

i'm trying to create a test to see if the onMouseEnter and onMouseLeave event handlers are being called. i've tried a dozen ways but nothing works. the most common method i found online to do this was this:

    // Init
    const handleVisibility = sinon.spy(event);

    // Action
    const wrapper = mount(<Provider schema={mockSchema}>
        <ComponentListElement
            componentName="ComponentName1"
            id="1"
            onMouseLeave={handleVisibility}/>
    </Provider>);
    wrapper.find("div.my-element").at(0).simulate("mouseLeave");

    // Test
    console.log(wrapper);
    expect(handleVisibility.callCount).to.be.equal(1);

but it also doesn't work ... even though the logic of it seems completely sound. Any thoughts on how to make this work?

Aucun commentaire:

Enregistrer un commentaire