Skip to content

Commit ca27e99

Browse files
authored
Consolidate getWindow test utility (#14161)
1 parent 3c75d2b commit ca27e99

15 files changed

+111
-205
lines changed

packages/react-router/__tests__/dom/concurrent-mode-navigations-test.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import {
1717
screen,
1818
waitFor,
1919
} from "@testing-library/react";
20-
import { JSDOM } from "jsdom";
2120
import { createDeferred } from "../router/utils/utils";
2221
import getHtml from "../utils/getHtml";
22+
import getWindow from "../utils/getWindow";
2323

2424
describe("Handles concurrent mode features during navigations", () => {
2525
function getComponents() {
@@ -149,7 +149,7 @@ describe("Handles concurrent mode features during navigations", () => {
149149
getComponents();
150150

151151
let { container } = render(
152-
<BrowserRouter window={getWindowImpl("/", false)}>
152+
<BrowserRouter window={getWindow("/", false)}>
153153
<Routes>
154154
<Route path="/" element={<Home />} />
155155
<Route
@@ -181,7 +181,7 @@ describe("Handles concurrent mode features during navigations", () => {
181181
getComponents();
182182

183183
let { container } = render(
184-
<HashRouter window={getWindowImpl("/", true)}>
184+
<HashRouter window={getWindow("/", true)}>
185185
<Routes>
186186
<Route path="/" element={<Home />} />
187187
<Route
@@ -306,7 +306,7 @@ describe("Handles concurrent mode features during navigations", () => {
306306
getComponents();
307307

308308
let { container } = render(
309-
<BrowserRouter window-={getWindowImpl("/", true)}>
309+
<BrowserRouter window-={getWindow("/", true)}>
310310
<Routes>
311311
<Route path="/" element={<Home />} />
312312
<Route path="/about" element={<About />} />
@@ -324,7 +324,7 @@ describe("Handles concurrent mode features during navigations", () => {
324324
getComponents();
325325

326326
let { container } = render(
327-
<HashRouter window-={getWindowImpl("/", true)}>
327+
<HashRouter window-={getWindow("/", true)}>
328328
<Routes>
329329
<Route path="/" element={<Home />} />
330330
<Route path="/about" element={<About />} />
@@ -356,10 +356,3 @@ describe("Handles concurrent mode features during navigations", () => {
356356
});
357357
});
358358
});
359-
360-
function getWindowImpl(initialUrl: string, isHash = false): Window {
361-
// Need to use our own custom DOM in order to get a working history
362-
const dom = new JSDOM(`<!DOCTYPE html>`, { url: "http://localhost/" });
363-
dom.window.history.replaceState(null, "", (isHash ? "#" : "") + initialUrl);
364-
return dom.window as unknown as Window;
365-
}

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { JSDOM } from "jsdom";
21
// Drop support for the submitter parameter, as in a legacy browser. This
32
// needs to be done before react-router-dom is required, since it does some
43
// FormData detection.
@@ -14,13 +13,14 @@ import {
1413
createHashRouter,
1514
createRoutesFromElements,
1615
} from "../../index";
16+
import getWindow from "../utils/getWindow";
1717

1818
testDomRouter("<DataBrowserRouter>", createBrowserRouter, (url) =>
19-
getWindowImpl(url, false),
19+
getWindow(url, false),
2020
);
2121

2222
testDomRouter("<DataHashRouter>", createHashRouter, (url) =>
23-
getWindowImpl(url, true),
23+
getWindow(url, true),
2424
);
2525

2626
function testDomRouter(
@@ -120,10 +120,3 @@ function testDomRouter(
120120
});
121121
});
122122
}
123-
124-
function getWindowImpl(initialUrl: string, isHash = false): Window {
125-
// Need to use our own custom DOM in order to get a working history
126-
const dom = new JSDOM(`<!DOCTYPE html>`, { url: "http://localhost/" });
127-
dom.window.history.replaceState(null, "", (isHash ? "#" : "") + initialUrl);
128-
return dom.window as unknown as Window;
129-
}

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
screen,
66
waitFor,
77
} from "@testing-library/react";
8-
import { JSDOM } from "jsdom";
98
import * as React from "react";
109
import type {
1110
RouteObject,
@@ -41,14 +40,15 @@ import {
4140
} from "../../index";
4241

4342
import getHtml from "../utils/getHtml";
43+
import getWindow from "../utils/getWindow";
4444
import { createDeferred, tick } from "../router/utils/utils";
4545

4646
testDomRouter("<DataBrowserRouter>", createBrowserRouter, (url) =>
47-
getWindowImpl(url, false),
47+
getWindow(url, false),
4848
);
4949

5050
testDomRouter("<DataHashRouter>", createHashRouter, (url) =>
51-
getWindowImpl(url, true),
51+
getWindow(url, true),
5252
);
5353

5454
function testDomRouter(
@@ -8002,10 +8002,3 @@ function testDomRouter(
80028002
}
80038003
});
80048004
}
8005-
8006-
function getWindowImpl(initialUrl: string, isHash = false): Window {
8007-
// Need to use our own custom DOM in order to get a working history
8008-
const dom = new JSDOM(`<!DOCTYPE html>`, { url: "http://localhost/" });
8009-
dom.window.history.replaceState(null, "", (isHash ? "#" : "") + initialUrl);
8010-
return dom.window as unknown as Window;
8011-
}

packages/react-router/__tests__/dom/flush-sync-navigations-test.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as React from "react";
22
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
3-
import { JSDOM } from "jsdom";
43

54
import {
65
createBrowserRouter,
@@ -9,6 +8,7 @@ import {
98
useFetcher,
109
} from "../../index";
1110
import { RouterProvider } from "../../lib/dom-export/dom-router-provider";
11+
import getWindow from "../utils/getWindow";
1212

1313
describe("flushSync", () => {
1414
it("wraps useNavigate updates in flushSync when specified", async () => {
@@ -42,7 +42,7 @@ describe("flushSync", () => {
4242
},
4343
],
4444
{
45-
window: getWindowImpl("/"),
45+
window: getWindow("/"),
4646
},
4747
);
4848
render(<RouterProvider router={router} />);
@@ -116,7 +116,7 @@ describe("flushSync", () => {
116116
},
117117
],
118118
{
119-
window: getWindowImpl("/"),
119+
window: getWindow("/"),
120120
},
121121
);
122122
render(<RouterProvider router={router} />);
@@ -176,7 +176,7 @@ describe("flushSync", () => {
176176
},
177177
],
178178
{
179-
window: getWindowImpl("/"),
179+
window: getWindow("/"),
180180
},
181181
);
182182
render(<RouterProvider router={router} />);
@@ -239,7 +239,7 @@ describe("flushSync", () => {
239239
},
240240
],
241241
{
242-
window: getWindowImpl("/"),
242+
window: getWindow("/"),
243243
},
244244
);
245245
render(<RouterProvider router={router} />);
@@ -267,10 +267,3 @@ describe("flushSync", () => {
267267
router.dispose();
268268
});
269269
});
270-
271-
function getWindowImpl(initialUrl: string, isHash = false): Window {
272-
// Need to use our own custom DOM in order to get a working history
273-
const dom = new JSDOM(`<!DOCTYPE html>`, { url: "http://localhost/" });
274-
dom.window.history.replaceState(null, "", (isHash ? "#" : "") + initialUrl);
275-
return dom.window as unknown as Window;
276-
}

packages/react-router/__tests__/dom/nav-link-active-test.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { render, fireEvent, waitFor, screen } from "@testing-library/react";
22
import "@testing-library/jest-dom";
3-
import { JSDOM } from "jsdom";
43
import * as React from "react";
54
import * as TestRenderer from "react-test-renderer";
65
import {
@@ -14,6 +13,7 @@ import {
1413
createBrowserRouter,
1514
createRoutesFromElements,
1615
} from "../../index";
16+
import getWindow from "../utils/getWindow";
1717

1818
describe("NavLink", () => {
1919
describe("when it does not match", () => {
@@ -1062,10 +1062,3 @@ function createDeferred() {
10621062
reject,
10631063
};
10641064
}
1065-
1066-
function getWindow(initialUrl: string): Window {
1067-
// Need to use our own custom DOM in order to get a working history
1068-
const dom = new JSDOM(`<!DOCTYPE html>`, { url: "https://remix.run/" });
1069-
dom.window.history.replaceState(null, "", initialUrl);
1070-
return dom.window as unknown as Window;
1071-
}

packages/react-router/__tests__/dom/scroll-restoration-test.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { JSDOM } from "jsdom";
21
import * as React from "react";
32
import { render, fireEvent, screen } from "@testing-library/react";
43
import "@testing-library/jest-dom";
54

65
import getHtml from "../utils/getHtml";
6+
import getWindow from "../utils/getWindow";
77
import {
88
Link,
99
Outlet,
@@ -22,7 +22,7 @@ describe(`ScrollRestoration`, () => {
2222
.spyOn(console, "warn")
2323
.mockImplementation(() => {});
2424

25-
let testWindow = getWindowImpl("/base");
25+
let testWindow = getWindow("/base");
2626
const mockScroll = jest.fn();
2727
window.scrollTo = mockScroll;
2828

@@ -76,7 +76,7 @@ describe(`ScrollRestoration`, () => {
7676

7777
it("removes the basename from the location provided to getKey", () => {
7878
let getKey = jest.fn(() => "mykey");
79-
let testWindow = getWindowImpl("/base");
79+
let testWindow = getWindow("/base");
8080
window.scrollTo = () => {};
8181

8282
let router = createBrowserRouter(
@@ -131,7 +131,7 @@ describe(`ScrollRestoration`, () => {
131131
.spyOn(console, "warn")
132132
.mockImplementation(() => {});
133133

134-
let testWindow = getWindowImpl("/base");
134+
let testWindow = getWindow("/base");
135135
const mockScroll = jest.fn();
136136
window.scrollTo = mockScroll;
137137

@@ -346,10 +346,3 @@ const testPages = [
346346
},
347347
},
348348
];
349-
350-
function getWindowImpl(initialUrl: string): Window {
351-
// Need to use our own custom DOM in order to get a working history
352-
const dom = new JSDOM(`<!DOCTYPE html>`, { url: "http://localhost/" });
353-
dom.window.history.replaceState(null, "", initialUrl);
354-
return dom.window as unknown as Window;
355-
}

packages/react-router/__tests__/dom/special-characters-test.tsx

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-disable jest/expect-expect */
22

3-
import { JSDOM } from "jsdom";
43
import * as React from "react";
54
import {
65
cleanup,
@@ -28,6 +27,7 @@ import {
2827
useNavigate,
2928
useParams,
3029
} from "../../index";
30+
import getWindow from "../utils/getWindow";
3131
import getHtml from "../utils/getHtml";
3232

3333
/**
@@ -160,12 +160,7 @@ describe("special character tests", () => {
160160
expectedLocation: Omit<Location, "state" | "key">,
161161
expectedParams = {},
162162
) {
163-
// Need to use our own custom DOM in order to get a working history
164-
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
165-
url: "https://remix.run/",
166-
});
167-
let testWindow = dom.window as unknown as Window;
168-
testWindow.history.replaceState(null, "", navigatePath);
163+
let testWindow = getWindow(navigatePath);
169164

170165
function Comp({ heading }) {
171166
return (
@@ -618,13 +613,7 @@ describe("special character tests", () => {
618613
expectedLocation: Omit<Location, "state" | "key">,
619614
expectedParams = {},
620615
) {
621-
// Need to use our own custom DOM in order to get a working history
622-
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
623-
url: "https://remix.run/",
624-
});
625-
let testWindow = dom.window as unknown as Window;
626-
testWindow.history.replaceState(null, "", path);
627-
616+
let testWindow = getWindow(path);
628617
let renderedUseLocation: Omit<Location, "state" | "key"> | null = null;
629618
let renderedParams: Params<string> | null = null;
630619

@@ -1112,13 +1101,3 @@ describe("special character tests", () => {
11121101
});
11131102
});
11141103
});
1115-
1116-
function getWindow(initialPath) {
1117-
// Need to use our own custom DOM in order to get a working history
1118-
const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
1119-
url: "https://remix.run/",
1120-
});
1121-
let testWindow = dom.window as unknown as Window;
1122-
testWindow.history.pushState({}, "", initialPath);
1123-
return testWindow;
1124-
}

0 commit comments

Comments
 (0)