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

test(react-router-dom): streamline jsdom submitter bug workaround #9824

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-trees-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router-dom": patch
---

Streamline jsdom bug workaround in tests
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
- JakubDrozd
- janpaepke
- jasonpaulos
- jenseng
- JesusTheHun
- jimniels
- jmargeta
Expand Down
77 changes: 7 additions & 70 deletions packages/react-router-dom/__tests__/data-browser-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1506,14 +1506,7 @@ function testDomRouter(
function Comp() {
let location = useLocation();
return (
<Form
onSubmit={(e) => {
// jsdom doesn't handle submitter so we add it here
// See https://github.com/jsdom/jsdom/issues/3117
// @ts-expect-error
e.nativeEvent.submitter = e.currentTarget.querySelector("button");
}}
>
<Form>
<p>{location.pathname + location.search}</p>
<input name="a" defaultValue="1" />
<button type="submit" name="b" value="2">
Expand Down Expand Up @@ -1587,15 +1580,7 @@ function testDomRouter(
let location = useLocation();
let data = useActionData() as string | undefined;
return (
<Form
method="post"
onSubmit={(e) => {
// jsdom doesn't handle submitter so we add it here
// See https://github.com/jsdom/jsdom/issues/3117
// @ts-expect-error
e.nativeEvent.submitter = e.currentTarget.querySelector("button");
}}
>
<Form method="post">
<p>{location.pathname + location.search}</p>
{data && <p>{data}</p>}
<input name="a" defaultValue="1" />
Expand Down Expand Up @@ -1683,16 +1668,7 @@ function testDomRouter(
let navigation = useNavigation();
return (
<div>
<Form
method="post"
onSubmit={(e) => {
// jsdom doesn't handle submitter so we add it here
// See https://github.com/jsdom/jsdom/issues/3117
// @ts-expect-error
e.nativeEvent.submitter =
e.currentTarget.querySelector("button");
}}
>
<Form method="post">
<input name="test" value="value" />
<button type="submit" formMethod="get">
Submit Form
Expand Down Expand Up @@ -2501,16 +2477,7 @@ function testDomRouter(

function FormPage() {
return (
<Form
method="post"
onSubmit={(e) => {
// jsdom doesn't handle submitter so we add it here
// See https://github.com/jsdom/jsdom/issues/3117
// @ts-expect-error
e.nativeEvent.submitter =
e.currentTarget.querySelector("button");
}}
>
<Form method="post">
<input name="a" defaultValue="1" />
<input name="b" defaultValue="2" />
<button name="c" value="3" type="submit">
Expand Down Expand Up @@ -2538,16 +2505,7 @@ function testDomRouter(
function FormPage() {
let submit = useSubmit();
return (
<Form
method="post"
onSubmit={(e) => {
// jsdom doesn't handle submitter so we add it here
// See https://github.com/jsdom/jsdom/issues/3117
// @ts-expect-error
e.nativeEvent.submitter =
e.currentTarget.querySelector("button");
}}
>
<Form method="post">
<input name="a" defaultValue="1" />
<input name="b" defaultValue="2" />
<button
Expand Down Expand Up @@ -2581,16 +2539,7 @@ function testDomRouter(

function FormPage() {
return (
<Form
method="post"
onSubmit={(e) => {
// jsdom doesn't handle submitter so we add it here
// See https://github.com/jsdom/jsdom/issues/3117
// @ts-expect-error
e.nativeEvent.submitter =
e.currentTarget.querySelector("button");
}}
>
<Form method="post">
<input name="a" defaultValue="1" />
<input name="b" defaultValue="2" />
<button name="b" value="3" type="submit">
Expand All @@ -2617,16 +2566,7 @@ function testDomRouter(
function FormPage() {
let submit = useSubmit();
return (
<Form
method="post"
onSubmit={(e) => {
// jsdom doesn't handle submitter so we add it here
// See https://github.com/jsdom/jsdom/issues/3117
// @ts-expect-error
e.nativeEvent.submitter =
e.currentTarget.querySelector("button");
}}
>
<Form method="post">
<input name="a" defaultValue="1" />
<input name="b" defaultValue="2" />
<button
Expand Down Expand Up @@ -3104,9 +3044,6 @@ function testDomRouter(
</TestDataRouter>
);

// Note: jsdom doesn't properly attach event.submitter for
// <button type="submit"> clicks, so we have to use an input to drive
// this. See https://github.com/jsdom/jsdom/issues/3117
function Comp() {
let fetcher = useFetcher();
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Polyfill jsdom SubmitEvent.submitter, until https://github.com/jsdom/jsdom/pull/3481 is merged
if (
typeof SubmitEvent === "undefined" ||
!SubmitEvent.prototype.hasOwnProperty("submitter")
) {
let maybeSubmitter;
window.addEventListener(
"click",
(event) => {
if ((event.target as any)?.form) maybeSubmitter = event.target;
setImmediate(() => {
// if this click doesn't imminently trigger a submit event, then forget it
maybeSubmitter = undefined;
});
},
{ capture: true }
);
window.addEventListener(
"submit",
(event: any) => {
if (maybeSubmitter?.form === event.target)
event.submitter = maybeSubmitter;
},
{ capture: true }
);
}
2 changes: 2 additions & 0 deletions packages/react-router-dom/__tests__/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { fetch, Request, Response } from "@remix-run/web-fetch";

import "./polyfills/SubmitEvent.submitter";

// https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#configuring-your-testing-environment
globalThis.IS_REACT_ACT_ENVIRONMENT = true;

Expand Down