Files
sweet/tests/main_test.ts
T
Luca Casonato 95289380f3 feat: add auto reload on restart (#70)
This commit adds support for auto reloading open pages when fresh
restarts. This is particularly useful when `deno run` is used with
`--watch`.
2021-10-27 01:19:01 +02:00

141 lines
4.4 KiB
TypeScript

import { ServerContext } from "../server.ts";
import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
import routes from "./fixture/routes.gen.ts";
const ctx = await ServerContext.fromRoutes(routes);
const router = ctx.handler();
Deno.test("/ page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, `<html lang="en">`);
assertStringIncludes(body, "index.js");
assertStringIncludes(body, "<p>Hello!</p>");
assertStringIncludes(body, "<p>Viewing JIT render.</p>");
assertStringIncludes(
body,
`<script id="__FRSH_PROPS" type="application/json">`,
);
assertStringIncludes(
body,
`"data":[["home","Hello!"]]`,
);
});
Deno.test("/props/123 page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/props/123"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(
body,
`{"props":{"params":{"id":"123"},"url":"https://fresh.deno.dev/props/123","route":"/props/:id"}}`,
);
});
Deno.test("/[name] page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/foo"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, "[name].js");
assertStringIncludes(body, "<div>Hello foo</div>");
assertStringIncludes(
body,
`<script id="__FRSH_PROPS" type="application/json">`,
);
assertStringIncludes(
body,
`"params":{"name":"foo"}`,
);
});
Deno.test("/intercept - GET html", async () => {
const req = new Request("https://fresh.deno.dev/intercept", {
headers: { "accept": "text/html" },
});
const resp = await router(req);
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assert(body.includes("<div>This is HTML</div>"));
});
Deno.test("/intercept - GET text", async () => {
const req = new Request("https://fresh.deno.dev/intercept", {
headers: { "accept": "text/plain" },
});
const resp = await router(req);
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertEquals(body, "This is plain text");
});
Deno.test("/intercept - POST", async () => {
const req = new Request("https://fresh.deno.dev/intercept", {
method: "POST",
});
const resp = await router(req);
assert(resp);
assertEquals(resp.status, 200);
const body = await resp.text();
assertEquals(body, "POST response");
});
Deno.test("/intercept - DELETE", async () => {
const req = new Request("https://fresh.deno.dev/intercept", {
method: "DELETE",
});
const resp = await router(req);
assert(resp);
assertEquals(resp.status, 405);
});
Deno.test("/api/get_only - NOTAMETHOD", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/api/get_only", {
method: "NOTAMETHOD",
}),
);
assert(resp);
assertEquals(resp.status, 405);
});
Deno.test("/api/xyz not found", async () => {
const resp = await router(new Request("https://fresh.deno.dev/api/xyz"));
assert(resp);
assertEquals(resp.status, 404);
});
Deno.test("/static page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/static"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assert(!body.includes(`static.js`));
assertStringIncludes(body, "<p>This is a static page.</p>");
assert(!body.includes("__FRSH_PROPS"));
});
Deno.test("/books/:id page - /books/123", async () => {
const resp = await router(new Request("https://fresh.deno.dev/books/123"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, "<div>Book 123</div>");
});
Deno.test("/books/:id page - /books/abc", async () => {
const resp = await router(new Request("https://fresh.deno.dev/books/abc"));
assert(resp);
assertEquals(resp.status, 404);
});