enzymejs / enzyme

JavaScript Testing utilities for React
https://enzymejs.github.io/enzyme/
MIT License
19.95k stars 2.01k forks source link

simulate('mouseleave') triggers onMouseLeave event when button is disabled #2502

Closed tulsidaskhatri closed 3 years ago

tulsidaskhatri commented 3 years ago

Events like onClick, onMouseEnter and onMouseLeave are not triggered if button is disabled, simulate function works fine for click and mouseenter but it doesn't not work for mouseleave event.

Current behavior

Unlike click and mouseenter, simulate triggers mouseleave for disable button.

import {mount} from 'enzyme';

describe('button', () => {
    it('should not trigger onClick when button is disabled', () => {
        const handleClick = jest.fn();
        const wrapper = mount(
            <button disabled onClick={handleClick}>
                Text
            </button>
        );
        wrapper.simulate('click');
        expect(handleClick).not.toHaveBeenCalled();
    });
    it('should not trigger onMouseEnter when button is disabled', () => {
        const handleMouseEnter = jest.fn();
        const wrapper = mount(
            <button disabled onMouseEnter={handleMouseEnter}>
                Text
            </button>
        );
        wrapper.simulate('mouseenter');
        expect(handleMouseEnter).not.toHaveBeenCalled();
    });
    it('should not trigger onMouseLeave when button is disabled', () => {
        const handleMouseLeave = jest.fn();
        const wrapper = mount(
            <button disabled onMouseLeave={handleMouseLeave}>
                Text
            </button>
        );
        wrapper.simulate('mouseleave');
        expect(handleMouseLeave).not.toHaveBeenCalled();
    });
});

Third test case (should not trigger onMouseLeave when button is disabled) fails.

Screenshot 2021-02-17 at 21 17 07

Expected behavior

Third test case (should not trigger onMouseLeave when button is disabled) passes.

Your environment

MacOS Yarn Visual Studio Code

API

Version

library version
enzyme 3.11.0
react 16.14.0
react-dom 16.14.0
react-test-renderer n/a
adapter (below)

Adapter

ljharb commented 3 years ago

For mount, this behavior is controlled by react-dom/test-utils, so i think you'll need to file a bug on react itself for this.

tulsidaskhatri commented 3 years ago

reported to react https://github.com/facebook/react/issues/20966