-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathprerender-api.test.js
More file actions
145 lines (126 loc) · 4.7 KB
/
prerender-api.test.js
File metadata and controls
145 lines (126 loc) · 4.7 KB
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
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { setupTest, teardownTest, loadFixture, viteBuild } from './lib/lifecycle.js';
import { getOutputFile, writeFixtureFile } from './lib/utils.js';
const writeEntry = async (dir, content) => writeFixtureFile(dir, 'src/index.js', content);
let env;
test.before.each(async () => {
env = await setupTest();
});
test.after.each(async () => {
await teardownTest(env);
});
test('Should accept the `prerender` function returning a string', async () => {
await loadFixture('simple', env);
await writeEntry(env.tmp.path, `
export async function prerender() {
return '<h1>Hello, World!</h1>';
}
`);
await viteBuild(env.tmp.path);
const prerenderedHtml = await getOutputFile(env.tmp.path, 'index.html');
assert.match(prerenderedHtml, '<h1>Hello, World!</h1>');
});
test('Should accept the `prerender` function returning an object', async () => {
await loadFixture('simple', env);
await writeEntry(env.tmp.path, `
export async function prerender() {
return {
html: '<h1>Hello, World!</h1>',
};
}
`);
await viteBuild(env.tmp.path);
const prerenderedHtml = await getOutputFile(env.tmp.path, 'index.html');
assert.match(prerenderedHtml, '<h1>Hello, World!</h1>');
});
test('Should stringify returned `data` object', async () => {
await loadFixture('simple', env);
await writeEntry(env.tmp.path, `
export async function prerender() {
return {
html: '<h1>Hello, World!</h1>',
data: { foo: 'bar' },
};
}
`);
await viteBuild(env.tmp.path);
const prerenderedHtml = await getOutputFile(env.tmp.path, 'index.html');
assert.match(
prerenderedHtml,
'<script type="application/json" id="prerender-data">{"foo":"bar"}</script>',
);
});
test('Should support `head.lang` property', async () => {
await loadFixture('simple', env);
await writeEntry(env.tmp.path, `
export async function prerender() {
return {
html: '<h1>Hello, World!</h1>',
head: { lang: 'de' },
};
}
`);
await viteBuild(env.tmp.path);
const prerenderedHtml = await getOutputFile(env.tmp.path, 'index.html');
assert.match(prerenderedHtml, '<html lang="de">');
});
test('Should support `head.title` property', async () => {
await loadFixture('simple', env);
await writeEntry(env.tmp.path, `
export async function prerender() {
return {
html: '<h1>Hello, World!</h1>',
head: { title: 'My Prerendered Site' },
};
}
`);
await viteBuild(env.tmp.path);
const prerenderedHtml = await getOutputFile(env.tmp.path, 'index.html');
assert.match(prerenderedHtml, '<title>My Prerendered Site</title>');
});
test('Should support `head.elements` property', async () => {
await loadFixture('simple', env);
await writeEntry(env.tmp.path, `
export async function prerender() {
return {
html: '<h1>Hello, World!</h1>',
head: {
elements: new Set([
{ type: 'link', props: { rel: 'stylesheet', href: 'foo.css' } },
{ type: 'meta', props: { property: 'og:title', content: 'Social media title' } },
]),
},
};
}
`);
await viteBuild(env.tmp.path);
const prerenderedHtml = await getOutputFile(env.tmp.path, 'index.html');
assert.match(prerenderedHtml, '<link rel="stylesheet" href="foo.css">');
assert.match(prerenderedHtml, '<meta property="og:title" content="Social media title">');
});
test('Should support `head.elements` property replacing', async () => {
await loadFixture('simple', env);
await writeEntry(env.tmp.path, `
export async function prerender() {
return {
html: '<h1>Hello, World!</h1>',
head: {
elements: new Set([
{ type: 'meta', props: { name: 'description', content: 'Foo' } },
]),
},
};
}
`);
await viteBuild(env.tmp.path);
const prerenderedHtml = await getOutputFile(env.tmp.path, 'index.html');
const matches = Array.from(
prerenderedHtml.matchAll(/<meta name="description"/g),
(m) => m[0]
);
assert.is(matches.length, 1);
assert.match(prerenderedHtml, '<meta name="description" content="Foo">');
assert.not.match(prerenderedHtml, '<meta name="description" content="A simple web page with a prerendered script.">');
});
test.run();