Skip to content

Commit 7a5be6f

Browse files
committed
Update unit tests
1 parent 3f03f3f commit 7a5be6f

File tree

12 files changed

+71
-79
lines changed

12 files changed

+71
-79
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ describe("RouterProvider works when no DOM APIs are available", () => {
5353
});
5454

5555
it("is defensive against a view transition navigation", async () => {
56+
let warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
57+
5658
let router = createMemoryRouter([
5759
{
5860
path: "/",
@@ -121,6 +123,14 @@ describe("RouterProvider works when no DOM APIs are available", () => {
121123
},
122124
});
123125

126+
expect(warnSpy).toHaveBeenCalledTimes(1);
127+
expect(warnSpy).toHaveBeenCalledWith(
128+
"You provided the `viewTransition` option to a router update, but you do " +
129+
"not appear to be running in a DOM environment as `window.startViewTransition` " +
130+
"is not available."
131+
);
132+
warnSpy.mockRestore();
133+
124134
unsubscribe();
125135
});
126136

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import * as React from "react";
66
import * as ReactDOMServer from "react-dom/server";
7-
import { json } from "react-router";
87
import type { StaticHandlerContext } from "../../index";
98
import {
109
Form,
@@ -638,7 +637,7 @@ describe("A <StaticRouterProvider>", () => {
638637
{
639638
path: "/",
640639
loader: () => {
641-
throw json(
640+
throw Response.json(
642641
{ not: "found" },
643642
{ status: 404, statusText: "Not Found" }
644643
);
@@ -688,7 +687,7 @@ describe("A <StaticRouterProvider>", () => {
688687
path: "/",
689688
lazy: async () => ({
690689
loader: () => {
691-
throw json(
690+
throw Response.json(
692691
{ not: "found" },
693692
{ status: 404, statusText: "Not Found" }
694693
);

packages/react-router/__tests__/dom/stub-test.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
useFetcher,
99
useLoaderData,
1010
useMatches,
11-
json,
1211
createRoutesStub,
1312
} from "../../index";
1413

@@ -62,7 +61,7 @@ test("loaders work", async () => {
6261
return <pre data-testid="data">Message: {data.message}</pre>;
6362
},
6463
loader() {
65-
return json({ message: "hello" });
64+
return Response.json({ message: "hello" });
6665
},
6766
},
6867
]);
@@ -87,7 +86,7 @@ test("actions work", async () => {
8786
);
8887
},
8988
action() {
90-
return json({ message: "hello" });
89+
return Response.json({ message: "hello" });
9190
},
9291
},
9392
]);
@@ -116,7 +115,7 @@ test("fetchers work", async () => {
116115
{
117116
path: "/api",
118117
loader() {
119-
return json({ count: ++count });
118+
return Response.json({ count: ++count });
120119
},
121120
},
122121
]);
@@ -133,7 +132,7 @@ test("fetchers work", async () => {
133132
// eslint-disable-next-line jest/expect-expect
134133
test("can pass a predefined loader", () => {
135134
async function loader(_args: LoaderFunctionArgs) {
136-
return json({ hi: "there" });
135+
return Response.json({ hi: "there" });
137136
}
138137

139138
createRoutesStub([
@@ -160,7 +159,7 @@ test("can pass context values", async () => {
160159
);
161160
},
162161
loader({ context }) {
163-
return json(context);
162+
return Response.json(context);
164163
},
165164
children: [
166165
{
@@ -170,7 +169,7 @@ test("can pass context values", async () => {
170169
return <pre data-testid="hello">Context: {data.context}</pre>;
171170
},
172171
loader({ context }) {
173-
return json(context);
172+
return Response.json(context);
174173
},
175174
},
176175
],

packages/react-router/__tests__/dom/use-blocker-test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { act } from "react-dom/test-utils";
44
import type { Blocker, RouteObject } from "../../index";
55
import {
66
createMemoryRouter,
7-
json,
87
Link,
98
NavLink,
109
Outlet,
@@ -19,7 +18,7 @@ const LOADER_LATENCY_MS = 200;
1918

2019
async function slowLoader() {
2120
await sleep(LOADER_LATENCY_MS / 2);
22-
return json(null);
21+
return Response.json(null);
2322
}
2423

2524
describe("navigation blocking with useBlocker", () => {

packages/react-router/__tests__/router/data-strategy-test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
DataStrategyMatch,
44
DataStrategyResult,
55
} from "../../lib/router/utils";
6-
import { json } from "../../lib/router/utils";
76
import { createDeferred, setup } from "./utils/data-router-setup";
87
import { createFormData, tick } from "./utils/utils";
98

@@ -63,7 +62,7 @@ describe("router dataStrategy", () => {
6362
expect(A.loaders.json.stub).toHaveBeenCalledTimes(1);
6463
expect(A.loaders.text.stub).toHaveBeenCalledTimes(1);
6564

66-
await A.loaders.json.resolve(json({ message: "hello json" }));
65+
await A.loaders.json.resolve({ message: "hello json" });
6766
await A.loaders.text.resolve(new Response("hello text"));
6867

6968
expect(t.router.state.loaderData).toEqual({
@@ -597,7 +596,7 @@ describe("router dataStrategy", () => {
597596
formData: createFormData({}),
598597
});
599598

600-
await A.actions.json.resolve(json({ message: "hello json" }));
599+
await A.actions.json.resolve({ message: "hello json" });
601600

602601
expect(t.router.state.actionData).toEqual({
603602
json: { message: "hello json" },
@@ -686,7 +685,7 @@ describe("router dataStrategy", () => {
686685
let key = "key";
687686
let A = await t.fetch("/test", key);
688687

689-
await A.loaders.json.resolve(json({ message: "hello json" }));
688+
await A.loaders.json.resolve({ message: "hello json" });
690689

691690
expect(t.fetchers[key].data.message).toBe("hello json");
692691

@@ -772,7 +771,7 @@ describe("router dataStrategy", () => {
772771
formData: createFormData({}),
773772
});
774773

775-
await A.actions.json.resolve(json({ message: "hello json" }));
774+
await A.actions.json.resolve({ message: "hello json" });
776775

777776
expect(t.fetchers[key].data.message).toBe("hello json");
778777

@@ -881,7 +880,7 @@ describe("router dataStrategy", () => {
881880
});
882881

883882
let A = await t.navigate("/test");
884-
await A.loaders.json.resolve(json({ message: "hello json" }));
883+
await A.loaders.json.resolve({ message: "hello json" });
885884
await A.loaders.reverse.resolve(
886885
new Response("hello text", {
887886
headers: { "Content-Type": "application/reverse" },

packages/react-router/__tests__/router/lazy-test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createMemoryHistory } from "../../lib/router/history";
22
import { createRouter, createStaticHandler } from "../../lib/router/router";
3-
import { json } from "../../lib/router/utils";
43

54
import type { TestRouteObject } from "./utils/data-router-setup";
65
import { cleanup, createDeferred, setup } from "./utils/data-router-setup";
@@ -210,7 +209,7 @@ describe("lazily loaded route modules", () => {
210209
await tick();
211210
return {
212211
async loader() {
213-
return json({ value: "LAZY LOADER" });
212+
return Response.json({ value: "LAZY LOADER" });
214213
},
215214
};
216215
},
@@ -234,7 +233,7 @@ describe("lazily loaded route modules", () => {
234233
await tick();
235234
return {
236235
async loader() {
237-
return json({ value: "LAZY LOADER" });
236+
return Response.json({ value: "LAZY LOADER" });
238237
},
239238
};
240239
},
@@ -429,7 +428,7 @@ describe("lazily loaded route modules", () => {
429428
let consoleWarn = jest.spyOn(console, "warn");
430429
let lazyLoaderStub = jest.fn(async () => {
431430
await tick();
432-
return json({ value: "LAZY LOADER" });
431+
return Response.json({ value: "LAZY LOADER" });
433432
});
434433

435434
let { query } = createStaticHandler([
@@ -438,7 +437,7 @@ describe("lazily loaded route modules", () => {
438437
path: "/lazy",
439438
loader: async () => {
440439
await tick();
441-
return json({ value: "STATIC LOADER" });
440+
return Response.json({ value: "STATIC LOADER" });
442441
},
443442
lazy: async () => {
444443
await tick();
@@ -470,7 +469,7 @@ describe("lazily loaded route modules", () => {
470469
let consoleWarn = jest.spyOn(console, "warn");
471470
let lazyLoaderStub = jest.fn(async () => {
472471
await tick();
473-
return json({ value: "LAZY LOADER" });
472+
return Response.json({ value: "LAZY LOADER" });
474473
});
475474

476475
let { query } = createStaticHandler([
@@ -479,7 +478,7 @@ describe("lazily loaded route modules", () => {
479478
path: "/lazy",
480479
loader: async () => {
481480
await tick();
482-
return json({ value: "STATIC LOADER" });
481+
return Response.json({ value: "STATIC LOADER" });
483482
},
484483
lazy: async () => {
485484
await tick();

packages/react-router/__tests__/router/navigation-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { HydrationState } from "../../lib/router/router";
2-
import { json } from "../../lib/router/utils";
32
import { cleanup, setup } from "./utils/data-router-setup";
43
import { createFormData } from "./utils/utils";
54

@@ -104,10 +103,12 @@ describe("navigations", () => {
104103
});
105104
});
106105

107-
it("unwraps non-redirect json Responses (json helper)", async () => {
106+
it("unwraps non-redirect json Responses (Response.json() helper)", async () => {
108107
let t = initializeTest();
109108
let A = await t.navigate("/foo");
110-
await A.loaders.foo.resolve(json({ key: "value" }, 200));
109+
await A.loaders.foo.resolve(
110+
Response.json({ key: "value" }, { status: 200 })
111+
);
111112
expect(t.router.state.loaderData).toMatchObject({
112113
root: "ROOT",
113114
foo: { key: "value" },

packages/react-router/__tests__/router/ssr-test.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
import {
1515
ErrorResponseImpl,
1616
isRouteErrorResponse,
17-
json,
1817
redirect,
1918
} from "../../lib/router/utils";
2019
import { createDeferred } from "./utils/data-router-setup";
@@ -60,8 +59,8 @@ describe("ssr", () => {
6059
{
6160
id: "json",
6261
path: "json",
63-
loader: () => json({ type: "loader" }),
64-
action: () => json({ type: "action" }),
62+
loader: () => Response.json({ type: "loader" }),
63+
action: () => Response.json({ type: "action" }),
6564
},
6665
{
6766
id: "deferred",
@@ -1069,7 +1068,7 @@ describe("ssr", () => {
10691068
{
10701069
id: "root",
10711070
path: "/",
1072-
loader: () => json({ data: "ROOT" }, { status: 201 }),
1071+
loader: () => Response.json({ data: "ROOT" }, { status: 201 }),
10731072
children: [
10741073
{
10751074
id: "child",
@@ -1090,12 +1089,12 @@ describe("ssr", () => {
10901089
{
10911090
id: "root",
10921091
path: "/",
1093-
loader: () => json({ data: "ROOT" }, { status: 201 }),
1092+
loader: () => Response.json({ data: "ROOT" }, { status: 201 }),
10941093
children: [
10951094
{
10961095
id: "child",
10971096
index: true,
1098-
loader: () => json({ data: "CHILD" }, { status: 202 }),
1097+
loader: () => Response.json({ data: "CHILD" }, { status: 202 }),
10991098
action: () => {
11001099
throw new Error("💥");
11011100
},
@@ -1114,7 +1113,7 @@ describe("ssr", () => {
11141113
{
11151114
id: "root",
11161115
path: "/",
1117-
loader: () => json({ data: "ROOT" }, { status: 201 }),
1116+
loader: () => Response.json({ data: "ROOT" }, { status: 201 }),
11181117
children: [
11191118
{
11201119
id: "child",
@@ -1135,12 +1134,12 @@ describe("ssr", () => {
11351134
{
11361135
id: "root",
11371136
path: "/",
1138-
loader: () => json({ data: "ROOT" }, { status: 201 }),
1137+
loader: () => Response.json({ data: "ROOT" }, { status: 201 }),
11391138
children: [
11401139
{
11411140
id: "child",
11421141
index: true,
1143-
loader: () => json({ data: "CHILD" }, { status: 202 }),
1142+
loader: () => Response.json({ data: "CHILD" }, { status: 202 }),
11441143
action: () => {
11451144
throw new Response(null, { status: 400 });
11461145
},
@@ -1159,13 +1158,13 @@ describe("ssr", () => {
11591158
{
11601159
id: "root",
11611160
path: "/",
1162-
loader: () => json({ data: "ROOT" }, { status: 201 }),
1161+
loader: () => Response.json({ data: "ROOT" }, { status: 201 }),
11631162
children: [
11641163
{
11651164
id: "child",
11661165
index: true,
1167-
loader: () => json({ data: "ROOT" }, { status: 202 }),
1168-
action: () => json({ data: "ROOT" }, { status: 203 }),
1166+
loader: () => Response.json({ data: "ROOT" }, { status: 202 }),
1167+
action: () => Response.json({ data: "ROOT" }, { status: 203 }),
11691168
},
11701169
],
11711170
},
@@ -1181,12 +1180,12 @@ describe("ssr", () => {
11811180
{
11821181
id: "root",
11831182
path: "/",
1184-
loader: () => json({ data: "ROOT" }, { status: 201 }),
1183+
loader: () => Response.json({ data: "ROOT" }, { status: 201 }),
11851184
children: [
11861185
{
11871186
id: "child",
11881187
index: true,
1189-
loader: () => json({ data: "ROOT" }, { status: 202 }),
1188+
loader: () => Response.json({ data: "ROOT" }, { status: 202 }),
11901189
},
11911190
],
11921191
},
@@ -1944,7 +1943,7 @@ describe("ssr", () => {
19441943
});
19451944

19461945
it("should not unwrap responses returned from loaders", async () => {
1947-
let response = json({ key: "value" });
1946+
let response = Response.json({ key: "value" });
19481947
let { queryRoute } = createStaticHandler([
19491948
{
19501949
id: "root",
@@ -1959,7 +1958,7 @@ describe("ssr", () => {
19591958
});
19601959

19611960
it("should not unwrap responses returned from actions", async () => {
1962-
let response = json({ key: "value" });
1961+
let response = Response.json({ key: "value" });
19631962
let { queryRoute } = createStaticHandler([
19641963
{
19651964
id: "root",
@@ -1974,7 +1973,7 @@ describe("ssr", () => {
19741973
});
19751974

19761975
it("should not unwrap responses thrown from loaders", async () => {
1977-
let response = json({ key: "value" });
1976+
let response = Response.json({ key: "value" });
19781977
let { queryRoute } = createStaticHandler([
19791978
{
19801979
id: "root",
@@ -1994,7 +1993,7 @@ describe("ssr", () => {
19941993
});
19951994

19961995
it("should not unwrap responses thrown from actions", async () => {
1997-
let response = json({ key: "value" });
1996+
let response = Response.json({ key: "value" });
19981997
let { queryRoute } = createStaticHandler([
19991998
{
20001999
id: "root",

0 commit comments

Comments
 (0)