-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy patherror-data-request-test.ts
169 lines (147 loc) · 4.53 KB
/
error-data-request-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { test, expect } from "@playwright/test";
import { UNSAFE_ErrorResponseImpl as ErrorResponseImpl } from "react-router";
import {
createAppFixture,
createFixture,
js,
} from "./helpers/create-fixture.js";
import type { Fixture, AppFixture } from "./helpers/create-fixture.js";
test.describe("ErrorBoundary", () => {
let fixture: Fixture;
let appFixture: AppFixture;
let _consoleError: any;
let errorLogs: any[];
test.beforeAll(async () => {
_consoleError = console.error;
console.error = (v) => errorLogs.push(v);
fixture = await createFixture({
files: {
"app/root.tsx": js`
import { Links, Meta, Outlet, Scripts } from "react-router";
export default function Root() {
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<Outlet />
<Scripts />
</body>
</html>
);
}
`,
"app/routes/_index.tsx": js`
import { Link, Form } from "react-router";
export default function () {
return <h1>Index</h1>
}
`,
[`app/routes/loader-throw-error.jsx`]: js`
export async function loader() {
throw Error("BLARGH");
}
export default function () {
return <h1>Hello</h1>
}
`,
[`app/routes/loader-return-json.jsx`]: js`
export async function loader() {
return { ok: true };
}
export default function () {
return <h1>Hello</h1>
}
`,
[`app/routes/action-throw-error.jsx`]: js`
export async function action() {
throw Error("YOOOOOOOO WHAT ARE YOU DOING");
}
export default function () {
return <h1>Goodbye</h1>;
}
`,
[`app/routes/action-return-json.jsx`]: js`
export async function action() {
return { ok: true };
}
export default function () {
return <h1>Hi!</h1>
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test.beforeEach(async () => {
errorLogs = [];
});
test.afterAll(() => {
console.error = _consoleError;
appFixture.close();
});
function assertLoggedErrorInstance(message: string) {
let error = errorLogs[0] as Error;
expect(error).toBeInstanceOf(Error);
expect(error.message).toEqual(message);
}
test("returns a 200 empty response on a data fetch to a path with no loaders", async () => {
let { status, headers, data } = await fixture.requestSingleFetchData(
"/_root.data"
);
expect(status).toBe(200);
expect(headers.has("X-Remix-Error")).toBe(false);
expect(data).toEqual({});
});
test("returns a 405 on a data fetch POST to a path with no action", async () => {
let { status, headers, data } = await fixture.requestSingleFetchData(
"/_root.data?index",
{
method: "POST",
}
);
expect(status).toBe(405);
expect(headers.has("X-Remix-Error")).toBe(false);
expect(data).toEqual({
error: new ErrorResponseImpl(
405,
"Method Not Allowed",
'Error: You made a POST request to "/" but did not provide an `action` for route "routes/_index", so there is no way to handle the request.'
),
});
assertLoggedErrorInstance(
'You made a POST request to "/" but did not provide an `action` for route "routes/_index", so there is no way to handle the request.'
);
});
test("returns a 405 on a data fetch with a bad method", async () => {
try {
await fixture.requestSingleFetchData("/loader-return-json.data", {
method: "TRACE",
});
expect(false).toBe(true);
} catch (e) {
expect((e as Error).message).toMatch(
"'TRACE' HTTP method is unsupported."
);
}
});
test("returns a 404 on a data fetch to a path with no matches", async () => {
let { status, headers, data } = await fixture.requestSingleFetchData(
"/i/match/nothing.data"
);
expect(status).toBe(404);
expect(headers.has("X-Remix-Error")).toBe(false);
expect(data).toEqual({
root: {
error: new ErrorResponseImpl(
404,
"Not Found",
'Error: No route matches URL "/i/match/nothing"'
),
},
});
assertLoggedErrorInstance('No route matches URL "/i/match/nothing"');
});
});