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

Bug: React test triggers mouseout event for disabled button #20966

Open
tulsidaskhatri opened this issue Mar 9, 2021 · 7 comments
Open

Bug: React test triggers mouseout event for disabled button #20966

tulsidaskhatri opened this issue Mar 9, 2021 · 7 comments

Comments

@tulsidaskhatri
Copy link

React version: 16.14.0

Steps To Reproduce

  1. Create a test file in a react project, and paste the following code.
  2. Run the test
  3. The test for onMouseLeave event fails.
import { act } from "react-dom/test-utils";
import React, { useState } from "react";
import ReactDOM from "react-dom";

const Counter = () => {
  const increaseCount = () => {
    setCount((prev) => prev + 1);
  };
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>You clicked {count} times</p>
      <button
        disabled
        onClick={increaseCount}
        onMouseEnter={increaseCount}
        onMouseLeave={increaseCount}
      >
        Click me
      </button>
    </div>
  );
};

describe("button with react test-utils", () => {
  let container;

  beforeEach(() => {
    container = document.createElement("div");
    document.body.appendChild(container);
  });

  afterEach(() => {
    document.body.removeChild(container);
    container = null;
  });

  it("should not trigger onClick when button is disabled", () => {
    act(() => {
      ReactDOM.render(<Counter />, container);
    });
    const button = container.querySelector("button");
    const label = container.querySelector("p");

    act(() => {
      button.dispatchEvent(new MouseEvent("click", { bubbles: true }));
    });

    expect(label.textContent).toBe("You clicked 0 times");
  });
  it("should not trigger onMouseEnter when button is disabled", () => {
    act(() => {
      ReactDOM.render(<Counter />, container);
    });
    const button = container.querySelector("button");
    const label = container.querySelector("p");

    act(() => {
      button.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
    });

    expect(label.textContent).toBe("You clicked 0 times");
  });
  it("should not trigger onMouseLeave when button is disabled", () => {
    act(() => {
      ReactDOM.render(<Counter />, container);
    });
    const button = container.querySelector("button");
    const label = container.querySelector("p");

    act(() => {
      button.dispatchEvent(new MouseEvent("mouseout", { bubbles: true }));
    });

    expect(label.textContent).toBe("You clicked 0 times");
  });
});

The current behavior

Screenshot 2021-03-09 at 21 45 13

The expected behavior

All tests should pass

@tulsidaskhatri tulsidaskhatri added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Mar 9, 2021
@bvaughn bvaughn added Component: DOM Type: Bug and removed Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug labels Mar 9, 2021
@jordyvandomselaar
Copy link

I'd like to take this issue as an opportunity for my first PR on React.

@jordyvandomselaar
Copy link

image
Gotcha ;)

jordyvandomselaar added a commit to jordyvandomselaar/react that referenced this issue Mar 11, 2021
jordyvandomselaar added a commit to jordyvandomselaar/react that referenced this issue Mar 11, 2021
jordyvandomselaar added a commit to jordyvandomselaar/react that referenced this issue Mar 11, 2021
@jordyvandomselaar
Copy link

@tulsidaskhatri thanks for the clear issue with tests for the cases you were talking about! Made opening a PR a lot easier =)

@Nazeeh21
Copy link

Nazeeh21 commented Jul 24, 2021

I have tried running the tests as described. And still onMouseLeave test case if failing. So can I work on this issue?

@jordyvandomselaar
Copy link

@Nazeeh21 have you tried running the tests in #20985?

@Nazeeh21
Copy link

No, I haven't tried that. I have just tried the tests mentioned in this issue.

@jordyvandomselaar
Copy link

Ah, fair enough. In the PR I linked, I've tried to solve this issue, I'd love to hear your feedback if you have any =)

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

No branches or pull requests

4 participants