From ac32c2deb679040359d551c6857c7ff1441d6deb Mon Sep 17 00:00:00 2001 From: Octo8080X <64694349+Octo8080X@users.noreply.github.com> Date: Tue, 13 Jun 2023 06:37:12 +0900 Subject: [PATCH] docs: add "Writing Tests" example (#1223) Fixes [#1222](https://github.com/denoland/fresh/issues/1222) Add example implementation and documentation of test code using `createHandler()` newly added by [#622](https://github.com/denoland/fresh/pull/622) --------- Co-authored-by: Asher Gomez --- docs/examples/index.md | 1 + docs/examples/writing-tests.md | 108 +++++++++++++++++++++++++++++++++ docs/toc.json | 3 +- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 docs/examples/writing-tests.md diff --git a/docs/examples/index.md b/docs/examples/index.md index 8d5e49d1..1f23174d 100644 --- a/docs/examples/index.md +++ b/docs/examples/index.md @@ -10,3 +10,4 @@ like to see here, please open - [Modifying the ``](./examples/modifying-the-head) - [Setting the language](./examples/setting-the-language) +- [Writing Tests](./examples/writing-tests) diff --git a/docs/examples/writing-tests.md b/docs/examples/writing-tests.md new file mode 100644 index 00000000..7d5a251a --- /dev/null +++ b/docs/examples/writing-tests.md @@ -0,0 +1,108 @@ +--- +description: | + You can write HTTP tests for your Fresh project by creating an application handler. +--- + +You can write tests for your Fresh project by creating an application handler +through +[`createHandler()`](https://deno.land/x/fresh/server.ts?doc=&s=createHandler). + +## 1. Create your routes + +```tsx +// routes/index.tsx + +import { Handlers } from "$fresh/server.ts"; + +export const handler: Handlers = { + async POST(req) { + const form = await req.formData(); + + // Processing something + + return new Response(null, { + status: 303, + headers: { location: "/" }, + }); + }, +}; + +export default function HomePage() { + return ( +
+ Hello Deno! +
+ ); +} +``` + +```tsx +// routes/foo.tsx + +export default function FooPage() { + return ( +
+ Hello Foo! +
+ ); +} +``` + +## 2. Write your tests + +```ts +// tests/main_test.ts + +import { createHandler } from "$fresh/server.ts"; +import manifest from "../fresh.gen.ts"; +import { assert, assertEquals } from "$std/testing/asserts.ts"; +import type { ConnInfo } from "$std/http/server.ts"; + +const CONN_INFO: ConnInfo = { + localAddr: { hostname: "127.0.0.1", port: 8000, transport: "tcp" }, + remoteAddr: { hostname: "127.0.0.1", port: 53496, transport: "tcp" }, +}; + +Deno.test("HTTP assert test.", async (t) => { + const handler = await createHandler(manifest); + + await t.step("#1 GET /", async () => { + const resp = await handler(new Request("http://127.0.0.1/"), CONN_INFO); + assertEquals(resp.status, 200); + }); + + await t.step("#2 POST /", async () => { + const formData = new FormData(); + formData.append("text", "Deno!"); + const req = new Request("http://127.0.0.1/", { + method: "POST", + body: formData, + }); + const resp = await handler(req, CONN_INFO); + assertEquals(resp.status, 303); + }); + + await t.step("#3 GET /foo", async () => { + const resp = await handler( + new Request("http://127.0.0.1/foo"), + CONN_INFO, + ); + const text = await resp.text(); + assert(text.includes("
Hello Foo!
")); + }); +}); +``` + +## 3. Run the tests + +```sh +$ deno test --allow-read --allow-env --allow-net +running 1 test from ./tests/main_test.ts +HTTP assert test. ... + #1 GET / ... ok (31ms) + #2 POST / ... ok (35ms) + #3 GET /foo ... ok (12ms) +HTTP assert test. ... ok (118ms) + +ok | 1 passed (3 steps) | 0 failed (236ms) +``` diff --git a/docs/toc.json b/docs/toc.json index 1db57f93..17a88567 100644 --- a/docs/toc.json +++ b/docs/toc.json @@ -41,7 +41,8 @@ "title": "Examples", "pages": [ ["modifying-the-head", "Modifying the "], - ["setting-the-language", "Setting the language"] + ["setting-the-language", "Setting the language"], + ["writing-tests", "Writing Tests"] ] } }