Skip to content

Commit

Permalink
init test
Browse files Browse the repository at this point in the history
  • Loading branch information
anggoran committed Apr 27, 2024
1 parent a731ddb commit 0160a41
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 8 deletions.
12 changes: 4 additions & 8 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@
"start": "deno run -A --watch=static/,routes/ dev.ts",
"build": "deno run -A dev.ts build",
"preview": "deno run -A main.ts",
"update": "deno run -A -r https://fresh.deno.dev/update ."
"update": "deno run -A -r https://fresh.deno.dev/update .",
"test": "deno test --allow-read --allow-env --allow-net"
},
"lint": {
"rules": {
"tags": [
"fresh",
"recommended"
]
"tags": ["fresh", "recommended"]
}
},
"exclude": [
"**/_fresh/*"
],
"exclude": ["**/_fresh/*"],
"imports": {
"$fresh/": "https://deno.land/x/[email protected]/",
"preact": "https://esm.sh/[email protected]",
Expand Down
92 changes: 92 additions & 0 deletions tests/main_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { createHandler, ServeHandlerInfo } from "$fresh/server.ts";
import manifest from "../fresh.gen.ts";
import config from "../fresh.config.ts";
import { assertEquals } from "$std/assert/assert_equals.ts";
import { assert } from "$std/assert/assert.ts";
import { assertExists } from "$std/assert/mod.ts";

const CONNECTION: ServeHandlerInfo = {
remoteAddr: { hostname: "localhost", port: 8000, transport: "tcp" },
};

Deno.test("Feature: Listening Pinyin", async (t) => {
const handler = await createHandler(manifest, config);

await t.step("Get the question", async () => {
const req = new Request("http://localhost/blueprint");
const resp = await handler(req, CONNECTION);
const text = await resp.text();
assert(resp.ok);
assert(text.includes('<button type="button">🔈</button>'));
});

await t.step("Post the answer", async () => {
const formData = new FormData();
formData.append("question_id", "1057");
formData.append("initial", "sh");
formData.append("final", "eng");
formData.append("tone", "1st tone");
const req = new Request("http://localhost/blueprint", {
method: "POST",
body: formData,
});
const resp = await handler(req, CONNECTION).then((value) => {
const url = new URL(value.headers.get("location")!);
assertEquals(value.status, 303);
assertExists(url.searchParams.get("question"));
assertExists(url.searchParams.get("answer"));
assertExists(url.searchParams.get("truth"));
return handler(new Request(url!));
});

const text = await resp.text();
assert(resp.ok);
assert(text.includes('<button type="button">shēng</button>'));
});

await t.step("Get the correct state", async () => {
const formData = new FormData();
formData.append("question_id", "1057");
formData.append("initial", "sh");
formData.append("final", "eng");
formData.append("tone", "1st tone");
const req = new Request("http://localhost/blueprint", {
method: "POST",
body: formData,
});
const resp = await handler(req, CONNECTION).then((value) => {
const url = value.headers.get("location");
assertEquals(value.status, 303);
return handler(new Request(url!));
});

const text = await resp.text();
assert(resp.ok);
assert(
text.includes('<div class="h-screen content-center bg-green-300 ">'),
);
});

await t.step("Get the false state", async () => {
const formData = new FormData();
formData.append("question_id", "1");
formData.append("initial", "sh");
formData.append("final", "eng");
formData.append("tone", "1st tone");
const req = new Request("http://localhost/blueprint", {
method: "POST",
body: formData,
});
const resp = await handler(req, CONNECTION).then((value) => {
const url = value.headers.get("location");
assertEquals(value.status, 303);
return handler(new Request(url!));
});

const text = await resp.text();
assert(resp.ok);
assert(
text.includes('<div class="h-screen content-center bg-red-300 ">'),
);
});
});

0 comments on commit 0160a41

Please sign in to comment.