Skip to content

Commit 6f82ad8

Browse files
committed
Fix unit tests
1 parent 1766db9 commit 6f82ad8

File tree

8 files changed

+250
-335
lines changed

8 files changed

+250
-335
lines changed

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import {
1111
ScrollRestoration,
1212
createBrowserRouter,
1313
} from "../../index";
14-
import type { FrameworkContextObject } from "../../lib/dom/ssr/entry";
1514
import { createMemoryRouter, redirect } from "react-router";
1615
import { FrameworkContext, Scripts } from "../../lib/dom/ssr/components";
1716
import "@testing-library/jest-dom/extend-expect";
17+
import { mockFrameworkContext } from "../utils/framework";
1818

1919
describe(`ScrollRestoration`, () => {
2020
it("restores the scroll position for a page when re-visited", () => {
@@ -207,23 +207,7 @@ describe(`ScrollRestoration`, () => {
207207
window.scrollTo = scrollTo;
208208
});
209209

210-
let context: FrameworkContextObject = {
211-
routeModules: { root: { default: () => null } },
212-
manifest: {
213-
routes: {
214-
root: {
215-
hasLoader: false,
216-
hasAction: false,
217-
hasErrorBoundary: false,
218-
id: "root",
219-
module: "root.js",
220-
},
221-
},
222-
entry: { imports: [], module: "" },
223-
url: "",
224-
version: "",
225-
},
226-
};
210+
let context = mockFrameworkContext();
227211

228212
it("should render a <script> tag", () => {
229213
let router = createMemoryRouter([

packages/react-router/__tests__/dom/ssr/components-test.tsx

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { FrameworkContext } from "../../../lib/dom/ssr/components";
1414
import invariant from "../../../lib/dom/ssr/invariant";
1515
import { ServerRouter } from "../../../lib/dom/ssr/server";
1616
import "@testing-library/jest-dom/extend-expect";
17+
import { mockEntryContext, mockFrameworkContext } from "../../utils/framework";
1718

1819
const setIntentEvents = ["focus", "mouseEnter", "touchStart"] as const;
1920
type PrefetchEventHandlerProps = {
@@ -24,7 +25,7 @@ function itPrefetchesPageLinks<
2425
Props extends { to: any; prefetch?: any } & PrefetchEventHandlerProps
2526
>(Component: React.ComponentType<Props>) {
2627
describe('prefetch="intent"', () => {
27-
let context = {
28+
let context = mockFrameworkContext({
2829
routeModules: { idk: { default: () => null } },
2930
manifest: {
3031
routes: {
@@ -40,8 +41,7 @@ function itPrefetchesPageLinks<
4041
url: "",
4142
version: "",
4243
},
43-
future: {},
44-
};
44+
});
4545

4646
beforeEach(() => {
4747
jest.useFakeTimers();
@@ -159,7 +159,7 @@ describe("<ServerRouter>", () => {
159159
"Expected a context"
160160
);
161161

162-
let context = {
162+
let context = mockEntryContext({
163163
manifest: {
164164
routes: {
165165
root: {
@@ -197,9 +197,7 @@ describe("<ServerRouter>", () => {
197197
},
198198
empty: { default: {} },
199199
},
200-
staticHandlerContext,
201-
future: {},
202-
};
200+
});
203201

204202
jest.spyOn(console, "warn").mockImplementation(() => {});
205203
jest.spyOn(console, "error");
@@ -218,54 +216,55 @@ describe("<ServerRouter>", () => {
218216

219217
describe("<HydratedRouter>", () => {
220218
it("handles empty default export objects from the compiler", async () => {
221-
window.__reactRouterContext = {
222-
url: "/",
223-
future: {},
224-
};
225-
window.__reactRouterRouteModules = {
226-
root: {
227-
default: () => {
228-
return (
229-
<>
230-
<h1>Root</h1>
231-
<Outlet />
232-
</>
233-
);
219+
let context = mockFrameworkContext({
220+
manifest: {
221+
routes: {
222+
root: {
223+
hasLoader: false,
224+
hasAction: false,
225+
hasErrorBoundary: false,
226+
id: "root",
227+
module: "root.js",
228+
path: "/",
229+
},
230+
empty: {
231+
hasLoader: false,
232+
hasAction: false,
233+
hasErrorBoundary: false,
234+
id: "empty",
235+
module: "empty.js",
236+
index: true,
237+
parentId: "root",
238+
},
234239
},
240+
entry: { imports: [], module: "" },
241+
url: "",
242+
version: "",
235243
},
236-
empty: { default: {} },
237-
};
238-
window.__reactRouterManifest = {
239-
routes: {
244+
routeModules: {
240245
root: {
241-
hasLoader: false,
242-
hasAction: false,
243-
hasErrorBoundary: false,
244-
id: "root",
245-
module: "root.js",
246-
path: "/",
247-
},
248-
empty: {
249-
hasLoader: false,
250-
hasAction: false,
251-
hasErrorBoundary: false,
252-
id: "empty",
253-
module: "empty.js",
254-
index: true,
255-
parentId: "root",
246+
default: () => {
247+
return (
248+
<>
249+
<h1>Root</h1>
250+
<Outlet />
251+
</>
252+
);
253+
},
256254
},
255+
empty: { default: {} },
257256
},
258-
entry: { imports: [], module: "" },
259-
url: "",
260-
version: "",
261-
};
257+
});
258+
window.__reactRouterContext = context;
259+
window.__reactRouterRouteModules = context.routeModules;
260+
window.__reactRouterManifest = context.manifest;
262261
window.__reactRouterContext!.stream = new ReadableStream({
263262
start(controller) {
264263
window.__reactRouterContext!.streamController = controller;
265264
},
266265
}).pipeThrough(new TextEncoderStream());
267266
window.__reactRouterContext!.streamController.enqueue(
268-
// ts-expect-error
267+
// @ts-expect-error
269268
'[{"1":2,"6":4,"7":4},"loaderData",{"3":4,"5":4},"root",null,"empty","actionData","errors"]\n'
270269
);
271270
window.__reactRouterContext!.streamController.close();

packages/react-router/__tests__/server-runtime/data-test.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { decodeViaTurboStream } from "../../lib/dom/ssr/single-fetch";
2-
import type { ServerBuild } from "../../lib/server-runtime/build";
32
import { createRequestHandler } from "../../lib/server-runtime/server";
3+
import { mockServerBuild } from "./utils";
44

55
describe("loaders", () => {
66
// so that HTML/Fetch requests are the same, and so redirects don't hang on to
@@ -11,20 +11,13 @@ describe("loaders", () => {
1111
};
1212

1313
let routeId = "routes/random";
14-
let build = {
15-
routes: {
16-
[routeId]: {
17-
id: routeId,
18-
path: "/random",
19-
module: {
20-
loader,
21-
},
22-
},
14+
let build = mockServerBuild({
15+
[routeId]: {
16+
path: "/random",
17+
default: {},
18+
loader,
2319
},
24-
entry: { module: {} },
25-
prerender: [],
26-
future: {},
27-
} as unknown as ServerBuild;
20+
});
2821

2922
let handler = createRequestHandler(build);
3023

packages/react-router/__tests__/server-runtime/handle-error-test.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1-
import type { ServerBuild } from "../../lib/server-runtime/build";
21
import { createRequestHandler } from "../../lib/server-runtime/server";
32
import { ErrorResponseImpl } from "../../lib/router/utils";
3+
import { mockServerBuild } from "./utils";
4+
import type { HandleDocumentRequestFunction } from "../../lib/server-runtime/build";
45

5-
function getHandler(routeModule = {}, entryServerModule = {}) {
6-
let routeId = "root";
6+
function getHandler(
7+
routeModule = {},
8+
opts: {
9+
handleDocumentRequest?: HandleDocumentRequestFunction;
10+
} = {}
11+
) {
712
let handleErrorSpy = jest.fn();
8-
let build = {
9-
routes: {
10-
[routeId]: {
11-
id: routeId,
13+
let build = mockServerBuild(
14+
{
15+
root: {
1216
path: "/",
13-
module: {
14-
default() {},
15-
...routeModule,
16-
},
17-
},
18-
},
19-
entry: {
20-
module: {
21-
handleError: handleErrorSpy,
22-
default() {
23-
return new Response("<html><body>Dummy document</body></html>");
24-
},
25-
...entryServerModule,
17+
default() {},
18+
...routeModule,
2619
},
2720
},
28-
future: {},
29-
prerender: [],
30-
} as unknown as ServerBuild;
21+
{
22+
handleError: handleErrorSpy,
23+
handleDocumentRequest: opts.handleDocumentRequest,
24+
}
25+
);
3126

3227
return {
3328
handler: createRequestHandler(build),
@@ -76,7 +71,7 @@ describe("handleError", () => {
7671

7772
it("provides render-thrown Error", async () => {
7873
let { handler, handleErrorSpy } = getHandler(undefined, {
79-
default() {
74+
handleDocumentRequest() {
8075
throw new Error("Render error");
8176
},
8277
});

packages/react-router/__tests__/server-runtime/handler-test.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
import { createRequestHandler } from "../../lib/server-runtime/server";
2+
import { mockServerBuild } from "./utils";
23

34
describe("createRequestHandler", () => {
45
it("retains request headers when stripping body off for loaders", async () => {
5-
let handler = createRequestHandler({
6-
ssr: true,
7-
routes: {
8-
root: {
9-
id: "routes/test",
10-
path: "/test",
11-
module: {
12-
loader: ({ request }) =>
13-
Response.json(request.headers.get("X-Foo")),
14-
} as any,
15-
},
6+
let build = mockServerBuild({
7+
root: {
8+
path: "/test",
9+
loader: ({ request }) => Response.json(request.headers.get("X-Foo")),
1610
},
17-
assets: {} as any,
18-
entry: { module: {} as any },
19-
// @ts-expect-error
20-
future: {},
21-
prerender: [],
2211
});
12+
let handler = createRequestHandler(build);
2313

2414
let response = await handler(
2515
new Request("http://.../test", {

0 commit comments

Comments
 (0)