2023-05-17 13:23:20 +00:00
|
|
|
import { loadFixture } from './test-utils.ts';
|
|
|
|
import { assertEquals, assertExists, cheerio, fs } from './deps.ts';
|
2023-02-21 14:14:47 +00:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: 'Prerender',
|
2023-07-17 14:53:10 +00:00
|
|
|
permissions: 'inherit',
|
2023-05-17 13:23:20 +00:00
|
|
|
async fn(t) {
|
|
|
|
const environmentVariables = {
|
|
|
|
PRERENDER: 'true',
|
|
|
|
};
|
2023-07-17 14:53:10 +00:00
|
|
|
const { runBuild, cleanup } = loadFixture('./fixtures/prerender/', environmentVariables);
|
|
|
|
|
|
|
|
await t.step('Run the build', async () => {
|
|
|
|
await runBuild();
|
|
|
|
});
|
2023-05-17 13:23:20 +00:00
|
|
|
|
|
|
|
await t.step('Handler can process requests to non-existing routes', async () => {
|
|
|
|
const { default: handler } = await import(
|
|
|
|
'./fixtures/prerender/.netlify/edge-functions/entry.js'
|
|
|
|
);
|
|
|
|
assertExists(handler);
|
|
|
|
const response = await handler(new Request('http://example.com/index.html'));
|
2023-07-17 14:53:10 +00:00
|
|
|
assertEquals(response.status, 404, "No response because this route doesn't exist");
|
2023-05-17 13:23:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await t.step('Prerendered route exists', async () => {
|
|
|
|
let content: string | null = null;
|
|
|
|
try {
|
|
|
|
const path = new URL('./fixtures/prerender/dist/index.html', import.meta.url);
|
|
|
|
content = Deno.readTextFileSync(path);
|
|
|
|
} catch (e) {}
|
|
|
|
assertExists(content);
|
|
|
|
const $ = cheerio.load(content);
|
|
|
|
assertEquals($('h1').text(), 'testing');
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.env.delete('PRERENDER');
|
2023-07-17 14:53:10 +00:00
|
|
|
await cleanup();
|
2023-05-17 13:23:20 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: 'Hybrid rendering',
|
2023-07-17 14:53:10 +00:00
|
|
|
permissions: 'inherit',
|
2023-05-17 13:23:20 +00:00
|
|
|
async fn(t) {
|
|
|
|
const environmentVariables = {
|
|
|
|
PRERENDER: 'false',
|
|
|
|
};
|
|
|
|
const fixture = loadFixture('./fixtures/prerender/', environmentVariables);
|
2023-07-17 14:53:10 +00:00
|
|
|
await t.step('Run the build', async () => {
|
|
|
|
await fixture.runBuild();
|
|
|
|
});
|
2023-05-17 13:23:20 +00:00
|
|
|
|
|
|
|
const stop = await fixture.runApp('./fixtures/prerender/prod.js');
|
|
|
|
await t.step('Can fetch server route', async () => {
|
2023-07-17 14:53:10 +00:00
|
|
|
const { default: handler } = await import(
|
|
|
|
'./fixtures/prerender/.netlify/edge-functions/entry.js'
|
|
|
|
);
|
|
|
|
const response = await handler(new Request('http://example.com/'));
|
2023-05-17 13:23:20 +00:00
|
|
|
assertEquals(response.status, 200);
|
|
|
|
|
|
|
|
const html = await response.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
assertEquals($('h1').text(), 'testing');
|
|
|
|
});
|
|
|
|
stop();
|
|
|
|
|
|
|
|
await t.step('Handler can process requests to non-existing routes', async () => {
|
|
|
|
const { default: handler } = await import(
|
|
|
|
'./fixtures/prerender/.netlify/edge-functions/entry.js'
|
|
|
|
);
|
|
|
|
const response = await handler(new Request('http://example.com/index.html'));
|
2023-07-17 14:53:10 +00:00
|
|
|
assertEquals(response.status, 404, "No response because this route doesn't exist");
|
2023-05-17 13:23:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await t.step('Has no prerendered route', async () => {
|
|
|
|
let prerenderedRouteExists = false;
|
|
|
|
try {
|
|
|
|
const path = new URL('./fixtures/prerender/dist/index.html', import.meta.url);
|
|
|
|
prerenderedRouteExists = fs.existsSync(path);
|
|
|
|
} catch (e) {}
|
|
|
|
assertEquals(prerenderedRouteExists, false);
|
|
|
|
});
|
|
|
|
await fixture.cleanup();
|
2023-02-21 14:14:47 +00:00
|
|
|
},
|
2023-02-21 14:17:00 +00:00
|
|
|
});
|