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

eventWrapper for dom-testing-library shouldn't be async #30

Closed
mischnic opened this issue Sep 10, 2020 · 0 comments · Fixed by #31
Closed

eventWrapper for dom-testing-library shouldn't be async #30

mischnic opened this issue Sep 10, 2020 · 0 comments · Fixed by #31

Comments

@mischnic
Copy link
Contributor

  • preact-testing-library version: 2.0.0
  • preact version: 10.4.8
  • node version: 14.10.0
  • npm (or yarn) version: Yarn 1.22.5

Relevant code or config

// fails:
import React from "preact/compat";
import { act, render, fireEvent } from "@testing-library/preact";

// works:
// import React from "react";
// import { act, render, fireEvent } from "@testing-library/react";

import userEvent from "@testing-library/user-event";

function ComboBox() {
  let inputRef = React.useRef();

  let buttonProps = {
    onMouseDown: (e) => {
      e.preventDefault();
    },
  };

  return (
    <div>
      <input ref={inputRef} />
      <button {...buttonProps} />
    </div>
  );
}

test("test", () => {
  const { getAllByRole, queryByRole } = render(<ComboBox />);

  let button = queryByRole("button");
  let combobox = queryByRole("combobox");
  expect(document.activeElement).toBe(document.body);
  act(() => {
    userEvent.click(button);
  });
  expect(document.activeElement).toBe(document.body);
});

What you did:

Call preventDefault on an onMouseDown event to (among other things) prevent the button being focused.

What happened:

preventDefault() doesn't prevent focussing.

Problem description:

Behaves differently compared to a browser.

Suggested solution:

The eventWrapper is async,

eventWrapper: async cb => {
let result
await act(() => {
result = cb()
})
return result
}

unlike react-testing-library: https://github.com/testing-library/react-testing-library/blob/693228ce10f23e2b695730ea88dbdaa35506e00e/src/pure.js#L11-L26

this causes fireEvent.mouseDown to return a promise, breaking @testing-library/user-event:

https://github.com/testing-library/user-event/blob/986e06aec056a99f26c9b119be78268d9605573a/src/click.js#L65

https://github.com/testing-library/dom-testing-library/blob/65024555ee18bb765ae62fb1ba9cf4e6dc7b9f0c/src/events.js#L6

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

Successfully merging a pull request may close this issue.

1 participant