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

userEvent.keyboard('{Enter}') doesn't trigger form submission when the submit button is outside the form element, even if it’s associated with the form element by using the button's form attribute. #1220

Open
tomohiro3 opened this issue May 21, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@tomohiro3
Copy link

tomohiro3 commented May 21, 2024

Reproduction example

https://codesandbox.io/p/devbox/user-event-keyboard-enter-repro-6jvyx5?file=%2Fsrc%2FApp.test.tsx%3A26%2C1

Prerequisites

  1. Render form element with an id and attach an event handler to onSubmit. Render radio buttons in it. Render a button outside the form with a form attribute which has the form id.
  2. Give a mocked function to the form's onSubmit.
  3. Give a focus in any ways to the radio button.
  4. Call await user.keyboard("{enter}").
  5. Assert onSubmit.
const App = ({ cb }) => {
  return (
    <div>
      <form id="form-test" onSubmit={cb}>
        <label>
          TestInput
          <input type="radio" />
        </label>
      </form>
      <button form="form-test">TestButton</button>
    </div>
  );
};

describe("Test", () => {
  it("works", async () => {
    const click = jest.fn();
    const user = userEvent.setup();

    render(<App cb={click} />);

    await user.click(screen.getByRole("radio", { name: "TestInput" }));
    await user.keyboard("{enter}");

    expect(click).toHaveBeenCalledTimes(1);
  });
});

Expected behavior

I expect the mock function given to onSubmit gets called.

Actual behavior

The mock function given to onSubmit doesn't get called

User-event version

14.5.2

Environment

Testing Library framework:

"@testing-library/jest-dom": "~6.4.0",
"@testing-library/react": "~15.0.6",
"@testing-library/user-event": "~14.5.0",

JS framework:
"react": "~18.3.1",

Test environment:

"jest": "~29.7.0",

DOM implementation:

"jest-environment-jsdom": "~29.7.0",

Additional context

I feel there are inconsistencies on the implicit submission of forms

  • The test passes if I place the button inside the form element.
  • The test passes if I change the radio button to a normal text input.
  • The test fails with a checkbox too.
  • I checked the form submission with the Enter keydown and the same elements structure, as written earlier, works on the latest version of Google Chrome, Safari, Firefox and Microsoft Edge.
@tomohiro3 tomohiro3 added bug Something isn't working needs assessment This needs to be looked at by a team member labels May 21, 2024
@tomohiro3 tomohiro3 changed the title The "Enter" keydown event doesn't trigger form submission when the submit button is outside the form element, even if it’s associated with the form using the form attribute. userEvent.keyboard('{Enter}') doesn't trigger form submission when the submit button is outside the form element, even if it’s associated with the form using the form attribute. May 28, 2024
@ph-fritsche
Copy link
Member

Thanks for the report.

I think the inaccuracy is here:

} else if (isElementType(target, 'input')) {
const form = target.form
const submit = form?.querySelector(
'input[type="submit"], button:not([type]), button[type="submit"]',
)
if (submit) {
return () => instance.dispatchUIEvent(submit, 'click')

@ph-fritsche ph-fritsche removed the needs assessment This needs to be looked at by a team member label Jun 3, 2024
@tomohiro3 tomohiro3 changed the title userEvent.keyboard('{Enter}') doesn't trigger form submission when the submit button is outside the form element, even if it’s associated with the form using the form attribute. userEvent.keyboard('{Enter}') doesn't trigger form submission when the submit button is outside the form element, even if it’s associated with the form element by using the button's form attribute. Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants