From 0160a41d08ef8dc2f597f1efb00f56cac1c50a45 Mon Sep 17 00:00:00 2001 From: Anggoran Date: Sat, 27 Apr 2024 15:10:45 +0700 Subject: [PATCH] init test --- deno.json | 12 ++---- tests/main_test.ts | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 tests/main_test.ts diff --git a/deno.json b/deno.json index fa99818..4473063 100644 --- a/deno.json +++ b/deno.json @@ -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/fresh@1.6.8/", "preact": "https://esm.sh/preact@10.19.6", diff --git a/tests/main_test.ts b/tests/main_test.ts new file mode 100644 index 0000000..971020e --- /dev/null +++ b/tests/main_test.ts @@ -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('')); + }); + + 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('')); + }); + + 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('
'), + ); + }); + + 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('
'), + ); + }); +});