-
Notifications
You must be signed in to change notification settings - Fork 594
/
server_components_test.ts
191 lines (170 loc) · 4.76 KB
/
server_components_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { assertEquals } from "./deps.ts";
import {
assertSelector,
assertTextMany,
withFakeServe,
withFresh,
withPageName,
} from "./test_utils.ts";
import { STATUS_CODE } from "../server.ts";
Deno.test({
name: "render async server component",
async fn() {
await withFakeServe(
"./tests/fixture_server_components/main.ts",
async (server) => {
const doc = await server.getHtml(`/basic`);
assertTextMany(doc, "h1", ["it works"]);
},
);
},
});
Deno.test({
name: "uses returned response",
async fn() {
await withFakeServe(
"./tests/fixture_server_components/main.ts",
async (server) => {
const res = await server.get(`/response`);
const text = await res.text();
assertEquals(text, "it works");
},
);
},
});
Deno.test({
name: "revives islands in async server component",
async fn() {
await withPageName(
"./tests/fixture_server_components/main.ts",
async (page, address) => {
await page.goto(`${address}/island`);
await page.waitForSelector("button");
let text = await page.$eval("button", (el) => el.textContent);
assertEquals(text, "update 0");
await page.click("button");
text = await page.$eval("button", (el) => el.textContent);
assertEquals(text, "update 1");
},
);
},
});
Deno.test("passes context to server component", async () => {
await withFresh(
"./tests/fixture_server_components/main.ts",
async (address) => {
const res = await fetch(`${address}/context/foo`);
const json = await res.json();
assertEquals(typeof json.localAddr, "object");
assertEquals(typeof json.remoteAddr, "object");
json.localAddr.port = 8000;
json.remoteAddr.port = 8000;
assertEquals(
json,
{
localAddr: {
hostname: "localhost",
port: 8000,
transport: "tcp",
},
remoteAddr: {
hostname: "127.0.0.1",
port: 8000,
transport: "tcp",
},
config: {
basePath: "",
build: {
outDir: "tests/fixture_server_components/_fresh",
target: [
"chrome99",
"firefox99",
"safari15",
],
},
dev: false,
plugins: [
{ entrypoints: {}, name: "twind", renderAsync: "AsyncFunction" },
],
render: "Function",
router: "<undefined>",
server: {},
staticDir: "tests/fixture_server_components/static",
},
data: "<undefined>",
error: "<undefined>",
codeFrame: "<undefined>",
pattern: "/context/:id",
render: "AsyncFunction",
Component: "Function",
destination: "route",
next: "Function",
basePath: "",
renderNotFound: "AsyncFunction",
url: `${address}/context/foo`,
route: "/context/:id",
params: {
id: "foo",
},
state: {},
isPartial: false,
},
);
},
);
});
Deno.test({
name: "can call context.renderNotFound()",
async fn() {
await withFakeServe(
"./tests/fixture_server_components/main.ts",
async (server) => {
const res = await server.get(`/fail`);
assertEquals(res.status, STATUS_CODE.NotFound);
const html = await res.text();
assertEquals(html, "Not found.");
},
);
},
});
Deno.test({
name: "works with async plugins",
async fn() {
await withPageName(
"./tests/fixture_server_components/main.ts",
async (page, address) => {
await page.goto(`${address}/twind`);
await page.waitForSelector("h1");
const text = await page.$eval("h1", (el) => el.textContent);
assertEquals(text, "it works");
// Check that CSS was applied accordingly
const color = await page.$eval("h1", (el) => {
return globalThis.getComputedStyle(el).color;
});
assertEquals(color, "rgb(220, 38, 38)");
},
);
},
});
Deno.test({
name: "renders async app template",
async fn() {
await withFakeServe(
"./tests/fixture_async_app/main.ts",
async (server) => {
const doc = await server.getHtml(``);
assertSelector(doc, "html > body > .app > .layout > .page");
},
);
},
});
Deno.test("define helpers", async () => {
await withFakeServe(
"./tests/fixture_define_helpers/main.ts",
async (server) => {
const doc = await server.getHtml(``);
assertSelector(doc, "html > body > .app > .layout > .page");
assertTextMany(doc, "p", ["Layout: it works", "Page: it works"]);
},
);
});