Skip to content

Commit c50b5ac

Browse files
authored
test(react-router-dom): streamline jsdom submitter bug workaround (#9824)
Work around the submitter bug in just one place, and link to my jsdom PR which will fix it, so that the workaround can be removed sooner rather than later 🤞 This workaround refactor also establishes a pattern for other jsdom bug polyfills which will be landing in forthcoming RR PRs (the bugs aren't relevant in the current test suite, but will be in the PRs 😅)
1 parent ee2fc0c commit c50b5ac

File tree

5 files changed

+41
-70
lines changed

5 files changed

+41
-70
lines changed

.changeset/slow-trees-notice.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router-dom": patch
3+
---
4+
5+
Streamline jsdom bug workaround in tests

contributors.yml

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
- JakubDrozd
7575
- janpaepke
7676
- jasonpaulos
77+
- jenseng
7778
- JesusTheHun
7879
- jimniels
7980
- jmargeta

packages/react-router-dom/__tests__/data-browser-router-test.tsx

+7-70
Original file line numberDiff line numberDiff line change
@@ -1506,14 +1506,7 @@ function testDomRouter(
15061506
function Comp() {
15071507
let location = useLocation();
15081508
return (
1509-
<Form
1510-
onSubmit={(e) => {
1511-
// jsdom doesn't handle submitter so we add it here
1512-
// See https://github.com/jsdom/jsdom/issues/3117
1513-
// @ts-expect-error
1514-
e.nativeEvent.submitter = e.currentTarget.querySelector("button");
1515-
}}
1516-
>
1509+
<Form>
15171510
<p>{location.pathname + location.search}</p>
15181511
<input name="a" defaultValue="1" />
15191512
<button type="submit" name="b" value="2">
@@ -1587,15 +1580,7 @@ function testDomRouter(
15871580
let location = useLocation();
15881581
let data = useActionData() as string | undefined;
15891582
return (
1590-
<Form
1591-
method="post"
1592-
onSubmit={(e) => {
1593-
// jsdom doesn't handle submitter so we add it here
1594-
// See https://github.com/jsdom/jsdom/issues/3117
1595-
// @ts-expect-error
1596-
e.nativeEvent.submitter = e.currentTarget.querySelector("button");
1597-
}}
1598-
>
1583+
<Form method="post">
15991584
<p>{location.pathname + location.search}</p>
16001585
{data && <p>{data}</p>}
16011586
<input name="a" defaultValue="1" />
@@ -1683,16 +1668,7 @@ function testDomRouter(
16831668
let navigation = useNavigation();
16841669
return (
16851670
<div>
1686-
<Form
1687-
method="post"
1688-
onSubmit={(e) => {
1689-
// jsdom doesn't handle submitter so we add it here
1690-
// See https://github.com/jsdom/jsdom/issues/3117
1691-
// @ts-expect-error
1692-
e.nativeEvent.submitter =
1693-
e.currentTarget.querySelector("button");
1694-
}}
1695-
>
1671+
<Form method="post">
16961672
<input name="test" value="value" />
16971673
<button type="submit" formMethod="get">
16981674
Submit Form
@@ -2501,16 +2477,7 @@ function testDomRouter(
25012477

25022478
function FormPage() {
25032479
return (
2504-
<Form
2505-
method="post"
2506-
onSubmit={(e) => {
2507-
// jsdom doesn't handle submitter so we add it here
2508-
// See https://github.com/jsdom/jsdom/issues/3117
2509-
// @ts-expect-error
2510-
e.nativeEvent.submitter =
2511-
e.currentTarget.querySelector("button");
2512-
}}
2513-
>
2480+
<Form method="post">
25142481
<input name="a" defaultValue="1" />
25152482
<input name="b" defaultValue="2" />
25162483
<button name="c" value="3" type="submit">
@@ -2538,16 +2505,7 @@ function testDomRouter(
25382505
function FormPage() {
25392506
let submit = useSubmit();
25402507
return (
2541-
<Form
2542-
method="post"
2543-
onSubmit={(e) => {
2544-
// jsdom doesn't handle submitter so we add it here
2545-
// See https://github.com/jsdom/jsdom/issues/3117
2546-
// @ts-expect-error
2547-
e.nativeEvent.submitter =
2548-
e.currentTarget.querySelector("button");
2549-
}}
2550-
>
2508+
<Form method="post">
25512509
<input name="a" defaultValue="1" />
25522510
<input name="b" defaultValue="2" />
25532511
<button
@@ -2581,16 +2539,7 @@ function testDomRouter(
25812539

25822540
function FormPage() {
25832541
return (
2584-
<Form
2585-
method="post"
2586-
onSubmit={(e) => {
2587-
// jsdom doesn't handle submitter so we add it here
2588-
// See https://github.com/jsdom/jsdom/issues/3117
2589-
// @ts-expect-error
2590-
e.nativeEvent.submitter =
2591-
e.currentTarget.querySelector("button");
2592-
}}
2593-
>
2542+
<Form method="post">
25942543
<input name="a" defaultValue="1" />
25952544
<input name="b" defaultValue="2" />
25962545
<button name="b" value="3" type="submit">
@@ -2617,16 +2566,7 @@ function testDomRouter(
26172566
function FormPage() {
26182567
let submit = useSubmit();
26192568
return (
2620-
<Form
2621-
method="post"
2622-
onSubmit={(e) => {
2623-
// jsdom doesn't handle submitter so we add it here
2624-
// See https://github.com/jsdom/jsdom/issues/3117
2625-
// @ts-expect-error
2626-
e.nativeEvent.submitter =
2627-
e.currentTarget.querySelector("button");
2628-
}}
2629-
>
2569+
<Form method="post">
26302570
<input name="a" defaultValue="1" />
26312571
<input name="b" defaultValue="2" />
26322572
<button
@@ -3104,9 +3044,6 @@ function testDomRouter(
31043044
</TestDataRouter>
31053045
);
31063046

3107-
// Note: jsdom doesn't properly attach event.submitter for
3108-
// <button type="submit"> clicks, so we have to use an input to drive
3109-
// this. See https://github.com/jsdom/jsdom/issues/3117
31103047
function Comp() {
31113048
let fetcher = useFetcher();
31123049
return (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Polyfill jsdom SubmitEvent.submitter, until https://github.com/jsdom/jsdom/pull/3481 is merged
2+
if (
3+
typeof SubmitEvent === "undefined" ||
4+
!SubmitEvent.prototype.hasOwnProperty("submitter")
5+
) {
6+
let maybeSubmitter;
7+
window.addEventListener(
8+
"click",
9+
(event) => {
10+
if ((event.target as any)?.form) maybeSubmitter = event.target;
11+
setImmediate(() => {
12+
// if this click doesn't imminently trigger a submit event, then forget it
13+
maybeSubmitter = undefined;
14+
});
15+
},
16+
{ capture: true }
17+
);
18+
window.addEventListener(
19+
"submit",
20+
(event: any) => {
21+
if (maybeSubmitter?.form === event.target)
22+
event.submitter = maybeSubmitter;
23+
},
24+
{ capture: true }
25+
);
26+
}

packages/react-router-dom/__tests__/setup.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { fetch, Request, Response } from "@remix-run/web-fetch";
22

3+
import "./polyfills/SubmitEvent.submitter";
4+
35
// https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#configuring-your-testing-environment
46
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
57

0 commit comments

Comments
 (0)