From b5816cd001308533319fcd61b0bfef396e4228dd Mon Sep 17 00:00:00 2001
From: Craig Doremus <942174+cdoremus@users.noreply.github.com>
Date: Wed, 15 Nov 2023 12:25:13 -0500
Subject: [PATCH] feat(tests): add `fresh-testing-library` tests (#1990)
---
.vscode/import_map.json | 4 +-
deno.json | 3 +-
.../components/gallery/Background.test.tsx | 16 +++++
tests/www/components/gallery/Button.test.tsx | 18 ++++++
.../www/components/gallery/Carousel.test.tsx | 17 +++++
tests/www/components/gallery/CodeBox.test.tsx | 43 +++++++++++++
.../components/gallery/ColoredButton.test.tsx | 16 +++++
.../www/components/gallery/Features.test.tsx | 31 +++++++++
tests/www/components/gallery/Footer.test.tsx | 29 +++++++++
tests/www/components/gallery/Header.test.tsx | 47 ++++++++++++++
tests/www/components/gallery/Hero.test.tsx | 16 +++++
tests/www/components/gallery/Input.test.tsx | 15 +++++
.../components/gallery/LinkButton.test.tsx | 63 +++++++++++++++++++
tests/www/islands/Counter.test.tsx | 25 ++++++++
tests/www/islands/SearchButton.test.tsx | 32 ++++++++++
tests/www/routes/index.test.tsx | 37 +++++++++++
tests/www/routes/routes.middleware.test.tsx | 14 +++++
www/components/DocsSidebar.tsx | 3 +-
www/deno.json | 4 +-
www/islands/SearchButton.tsx | 22 +++++--
20 files changed, 447 insertions(+), 8 deletions(-)
create mode 100644 tests/www/components/gallery/Background.test.tsx
create mode 100644 tests/www/components/gallery/Button.test.tsx
create mode 100644 tests/www/components/gallery/Carousel.test.tsx
create mode 100644 tests/www/components/gallery/CodeBox.test.tsx
create mode 100644 tests/www/components/gallery/ColoredButton.test.tsx
create mode 100644 tests/www/components/gallery/Features.test.tsx
create mode 100644 tests/www/components/gallery/Footer.test.tsx
create mode 100644 tests/www/components/gallery/Header.test.tsx
create mode 100644 tests/www/components/gallery/Hero.test.tsx
create mode 100644 tests/www/components/gallery/Input.test.tsx
create mode 100644 tests/www/components/gallery/LinkButton.test.tsx
create mode 100644 tests/www/islands/Counter.test.tsx
create mode 100644 tests/www/islands/SearchButton.test.tsx
create mode 100644 tests/www/routes/index.test.tsx
create mode 100644 tests/www/routes/routes.middleware.test.tsx
diff --git a/.vscode/import_map.json b/.vscode/import_map.json
index a9b65fe7..1e99475a 100644
--- a/.vscode/import_map.json
+++ b/.vscode/import_map.json
@@ -14,6 +14,8 @@
"@preact/signals-core@1.3.0": "https://esm.sh/@preact/signals-core@1.3.0",
"$std/": "https://deno.land/std@0.205.0/",
"$ga4": "https://raw.githubusercontent.com/denoland/ga4/main/mod.ts",
- "$marked-mangle": "https://esm.sh/marked-mangle@1.0.1"
+ "$marked-mangle": "https://esm.sh/marked-mangle@1.0.1",
+ "$fresh-testing-library": "https://deno.land/x/fresh_testing_library@0.11.1/mod.ts",
+ "$fresh-testing-library/": "https://deno.land/x/fresh_testing_library@0.11.1/"
}
}
diff --git a/deno.json b/deno.json
index 44b8c0a2..f51c6eb2 100644
--- a/deno.json
+++ b/deno.json
@@ -7,7 +7,8 @@
"screenshot": "deno run -A www/utils/screenshot.ts",
"check:types": "deno check **/*.ts && deno check **/*.tsx",
"ok": "deno fmt --check && deno lint && deno task check:types && deno task test",
- "install-puppeteer": "PUPPETEER_PRODUCT=chrome deno run -A --unstable https://deno.land/x/puppeteer@16.2.0/install.ts && PUPPETEER_PRODUCT=firefox deno run -A --unstable https://deno.land/x/puppeteer@16.2.0/install.ts"
+ "install-puppeteer": "PUPPETEER_PRODUCT=chrome deno run -A --unstable https://deno.land/x/puppeteer@16.2.0/install.ts && PUPPETEER_PRODUCT=firefox deno run -A --unstable https://deno.land/x/puppeteer@16.2.0/install.ts",
+ "test:www": "deno test -A tests/www/"
},
"exclude": [
"**/_fresh/*",
diff --git a/tests/www/components/gallery/Background.test.tsx b/tests/www/components/gallery/Background.test.tsx
new file mode 100644
index 00000000..a0c9e884
--- /dev/null
+++ b/tests/www/components/gallery/Background.test.tsx
@@ -0,0 +1,16 @@
+import { assertEquals } from "$std/testing/asserts.ts";
+import { cleanup, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import Background from "$fresh/www/components/gallery/Background.tsx";
+
+describe("components/gallery/Background.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should display background image", () => {
+ const { getByTitle } = render();
+ const bg = getByTitle("background");
+ const style = bg.getAttribute("style");
+ assertEquals(style, "background-image: url(/gallery/grid.svg);");
+ });
+});
diff --git a/tests/www/components/gallery/Button.test.tsx b/tests/www/components/gallery/Button.test.tsx
new file mode 100644
index 00000000..537db63f
--- /dev/null
+++ b/tests/www/components/gallery/Button.test.tsx
@@ -0,0 +1,18 @@
+import { assertEquals, assertExists } from "$std/testing/asserts.ts";
+import { cleanup, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import Button from "../../../../www/components/gallery/Button.tsx";
+
+describe("components/gallery/LinkButton.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should show button", () => {
+ const { getByRole } = render(
+ ,
+ );
+ const button = getByRole("button");
+ assertExists(button);
+ assertEquals((button as HTMLButtonElement).value, "Click Me!");
+ });
+});
diff --git a/tests/www/components/gallery/Carousel.test.tsx b/tests/www/components/gallery/Carousel.test.tsx
new file mode 100644
index 00000000..3637fcf0
--- /dev/null
+++ b/tests/www/components/gallery/Carousel.test.tsx
@@ -0,0 +1,17 @@
+import { assertEquals } from "$std/testing/asserts.ts";
+import { cleanup, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import Carousel from "../../../../www/components/gallery/Carousel.tsx";
+
+describe("Carousel.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should display navigation buttons", () => {
+ const { getAllByRole } = render(
+ ,
+ );
+ const buttons = getAllByRole("button");
+ assertEquals(buttons.length, 6);
+ });
+});
diff --git a/tests/www/components/gallery/CodeBox.test.tsx b/tests/www/components/gallery/CodeBox.test.tsx
new file mode 100644
index 00000000..057bd8e4
--- /dev/null
+++ b/tests/www/components/gallery/CodeBox.test.tsx
@@ -0,0 +1,43 @@
+import { assert } from "$std/testing/asserts.ts";
+import { cleanup, fireEvent, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import CodeBox from "$fresh/www/components/gallery/CodeBox.tsx";
+
+describe("components/gallery/LinkButton.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should show Copy button", async () => {
+ const code = "console.log('Hello World')";
+ const { getByText, getByRole, findByText } = render(
+ ,
+ );
+ // summary
+ assert(getByText("Code"));
+ // button text
+ assert(getByText("Copy"));
+ const button = getByRole("button");
+ // click button, button text should change to "Copied!"
+ fireEvent.click(button);
+ const copied = await findByText("Copied!");
+ assert(copied);
+ });
+
+ it("should show source code", () => {
+ const code = "console.log('Hello World')";
+ const { getByText, getByRole } = render();
+ // find code
+ const codeElement = getByRole("code");
+ const content = codeElement.textContent;
+ // Prism library breaks up code for styling purposes
+ assert(
+ getByText((content) => content.includes("console")),
+ );
+ assert(
+ getByText((content) => content.includes("log")),
+ );
+ assert(
+ getByText((content) => content.includes("Hello World")),
+ );
+ });
+});
diff --git a/tests/www/components/gallery/ColoredButton.test.tsx b/tests/www/components/gallery/ColoredButton.test.tsx
new file mode 100644
index 00000000..49cef825
--- /dev/null
+++ b/tests/www/components/gallery/ColoredButton.test.tsx
@@ -0,0 +1,16 @@
+import { assert } from "$std/testing/asserts.ts";
+import { cleanup, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import ColoredButton from "$fresh/www/components/gallery/ColoredButton.tsx";
+
+describe("components/gallery/ColoredButton.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should have disabled set to false", () => {
+ const { getByTitle } = render();
+ const bg = getByTitle("colored button");
+ const disabled = bg.getAttribute("disabled");
+ assert(!disabled);
+ });
+});
diff --git a/tests/www/components/gallery/Features.test.tsx b/tests/www/components/gallery/Features.test.tsx
new file mode 100644
index 00000000..7167d47b
--- /dev/null
+++ b/tests/www/components/gallery/Features.test.tsx
@@ -0,0 +1,31 @@
+import { assert, assertEquals } from "$std/testing/asserts.ts";
+import { cleanup, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import Features from "$fresh/www/components/gallery/Features.tsx";
+
+describe("components/gallery/Features.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should only display 2 links", () => {
+ const screen = render();
+ const links = screen.getAllByRole("link");
+ assertEquals(links.length, 2);
+ });
+
+ it("should display all feature descriptions", () => {
+ const screen = render();
+ const desc1 = screen.getByText(
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, nunc ut aliquam aliquam",
+ );
+ assert(desc1);
+ const desc2 = screen.getByText(
+ "Nunc nisl aliquet nisl, eget aliquam nisl nisl sit amet lorem. Sed euismod, nunc ut aliquam aliquam, nunc nisl aliquet nisl,",
+ );
+ assert(desc2);
+ const desc3 = screen.getByText(
+ "Eget aliquam nisl nisl sit amet lorem.",
+ );
+ assert(desc3);
+ });
+});
diff --git a/tests/www/components/gallery/Footer.test.tsx b/tests/www/components/gallery/Footer.test.tsx
new file mode 100644
index 00000000..db28d8c2
--- /dev/null
+++ b/tests/www/components/gallery/Footer.test.tsx
@@ -0,0 +1,29 @@
+import { assertEquals, assertExists } from "$std/testing/asserts.ts";
+import { cleanup, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import Footer from "../../../../www/components/gallery/Footer.tsx";
+
+describe("Footer.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should display text", () => {
+ //@ts-ignore Ignore missing 'children' prop
+ const { getByText, getAllByText } = render();
+ const fresh = getByText("Fresh");
+ assertEquals(fresh.textContent, "Fresh");
+ const frameworkText = getAllByText("Full Stack Framework");
+ assertExists(frameworkText);
+ });
+
+ it("should display menu", () => {
+ //@ts-ignore Ignore missing 'children' prop
+ const { getByText } = render();
+ const menu1 = getByText("Getting Started");
+ assertExists(menu1);
+ const menu2 = getByText("Community");
+ assertExists(menu2);
+ const menu3 = getByText("Discord");
+ assertExists(menu3);
+ });
+});
diff --git a/tests/www/components/gallery/Header.test.tsx b/tests/www/components/gallery/Header.test.tsx
new file mode 100644
index 00000000..21be7fbc
--- /dev/null
+++ b/tests/www/components/gallery/Header.test.tsx
@@ -0,0 +1,47 @@
+import { assertEquals, fail } from "$std/testing/asserts.ts";
+import { cleanup, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import Header from "$fresh/www/components/gallery/Header.tsx";
+
+describe("components/gallery/Header.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should show 3 links", () => {
+ const screen = render();
+ const links = screen.getAllByRole("link");
+ assertEquals(links.length, 3);
+ });
+
+ it("should show '/docs' as active", () => {
+ const screen = render();
+ const links = screen.getAllByRole("link");
+ let docsActive = false;
+ for (const link of links) {
+ if (link.getAttribute("href") === "/docs") {
+ if (link.classList.contains("border-b-2")) {
+ docsActive = true;
+ }
+ }
+ }
+ if (!docsActive) {
+ fail("The '/docs' link should be active!!");
+ }
+ });
+
+ it("should show '/docs' as not active", () => {
+ const screen = render();
+ const links = screen.getAllByRole("link");
+ let docsActive = false;
+ for (const link of links) {
+ if (link.getAttribute("href") === "/docs") {
+ if (link.classList.contains("border-b-2")) {
+ docsActive = true;
+ }
+ }
+ }
+ if (docsActive) {
+ fail("The '/docs' link should NOT be active!!");
+ }
+ });
+});
diff --git a/tests/www/components/gallery/Hero.test.tsx b/tests/www/components/gallery/Hero.test.tsx
new file mode 100644
index 00000000..b4c52b22
--- /dev/null
+++ b/tests/www/components/gallery/Hero.test.tsx
@@ -0,0 +1,16 @@
+import { assertEquals } from "$std/testing/asserts.ts";
+import { cleanup, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import Hero from "$fresh/www/components/gallery/Hero.tsx";
+
+describe("components/gallery/Hero.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should show 2 links", () => {
+ const { getAllByRole } = render();
+ // Two anchor links in component
+ const links = getAllByRole("link");
+ assertEquals(links.length, 2);
+ });
+});
diff --git a/tests/www/components/gallery/Input.test.tsx b/tests/www/components/gallery/Input.test.tsx
new file mode 100644
index 00000000..57436e21
--- /dev/null
+++ b/tests/www/components/gallery/Input.test.tsx
@@ -0,0 +1,15 @@
+import { assert } from "$std/testing/asserts.ts";
+import { cleanup, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import Input from "../../../../www/components/gallery/Input.tsx";
+
+describe("Input.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should be disabled", () => {
+ const screen = render();
+ const input = screen.getByPlaceholderText("text input");
+ assert((input as HTMLInputElement).disabled);
+ });
+});
diff --git a/tests/www/components/gallery/LinkButton.test.tsx b/tests/www/components/gallery/LinkButton.test.tsx
new file mode 100644
index 00000000..446a2ec2
--- /dev/null
+++ b/tests/www/components/gallery/LinkButton.test.tsx
@@ -0,0 +1,63 @@
+import {
+ assert,
+ assertEquals,
+ assertExists,
+ assertFalse,
+ assertStringIncludes,
+} from "$std/testing/asserts.ts";
+import { cleanup, fireEvent, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import LinkButton from "../../../../www/components/gallery/LinkButton.tsx";
+import IconHeart from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/heart.tsx";
+
+describe("components/gallery/LinkButton.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should show link button title", () => {
+ const screen = render(
+ ,
+ );
+ const anchor = screen.getByRole("link");
+ const attr = anchor.getAttribute("title");
+ assertEquals(attr, "Link Button");
+ });
+
+ it("should show link button classes", () => {
+ const screen = render(
+ ,
+ );
+ const anchor = screen.getByRole("link");
+ const classes = anchor.classList;
+ // can only test for twind classes
+ assert(classes.contains("rounded"));
+ assert(classes.contains("bg-white"));
+ });
+
+ it("should show link button text", () => {
+ const screen = render(
+
+ Click Me!
+ ,
+ );
+ const anchor = screen.getByRole("link");
+ const text = screen.getByText("Click Me!");
+ assertExists(text);
+ });
+
+ it("should render IconHeart component", () => {
+ const screen = render(
+
+
+ Like me
+ ,
+ );
+ const icon = screen.getByTitle("icon-heart");
+ const hidden = icon.getAttribute("aria-hidden");
+ assert(hidden);
+ });
+});
diff --git a/tests/www/islands/Counter.test.tsx b/tests/www/islands/Counter.test.tsx
new file mode 100644
index 00000000..832ed7be
--- /dev/null
+++ b/tests/www/islands/Counter.test.tsx
@@ -0,0 +1,25 @@
+import { assertExists, assertFalse } from "$std/testing/asserts.ts";
+import { cleanup, fireEvent, render, setup } from "$fresh-testing-library";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import Counter from "../../../www/islands/Counter.tsx";
+
+describe("islands/Counter.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should display count and increment/decrement it correctly", async () => {
+ const count = 9;
+ const screen = render();
+ const plusOne = screen.getByTitle("Add 1");
+ const minusOne = screen.getByTitle("Subtract 1");
+ assertExists(screen.queryByText("9"));
+
+ await fireEvent.click(plusOne);
+ assertFalse(screen.queryByText("9"));
+ assertExists(screen.queryByText("10"));
+
+ await fireEvent.click(minusOne);
+ assertExists(screen.queryByText("9"));
+ assertFalse(screen.queryByText("10"));
+ });
+});
diff --git a/tests/www/islands/SearchButton.test.tsx b/tests/www/islands/SearchButton.test.tsx
new file mode 100644
index 00000000..afcbdd07
--- /dev/null
+++ b/tests/www/islands/SearchButton.test.tsx
@@ -0,0 +1,32 @@
+import { assert, assertExists, assertFalse } from "$std/testing/asserts.ts";
+import {
+ cleanup,
+ fireEvent,
+ render,
+ setup,
+} from "$fresh-testing-library/components.ts";
+import { fn } from "$fresh-testing-library/expect.ts";
+import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
+import SearchButton from "../../../www/islands/SearchButton.tsx";
+import docsearch from "https://esm.sh/@docsearch/js@3?target=es2020";
+
+describe("islands/SearchButton.tsx", () => {
+ beforeAll(setup);
+ afterEach(cleanup);
+
+ it("should contain class applied in props", () => {
+ // create mock implementation of docsearch
+ // @ts-ignore mock impl
+ const dsearch = fn(docsearch).mockImplementation((
+ applId: string,
+ apiKey: string,
+ indexName: string,
+ container: HTMLElement | string,
+ ) => {});
+ const { getByTitle } = render(
+ ,
+ );
+ const search = getByTitle("Search Button");
+ assert(search.classList.contains("font-bold"));
+ });
+});
diff --git a/tests/www/routes/index.test.tsx b/tests/www/routes/index.test.tsx
new file mode 100644
index 00000000..3981af60
--- /dev/null
+++ b/tests/www/routes/index.test.tsx
@@ -0,0 +1,37 @@
+import { createHandlerContext } from "$fresh-testing-library/server.ts";
+import { expect } from "$fresh-testing-library/expect.ts";
+import { handler } from "../../../www/routes/index.tsx";
+import manifest from "../../../demo/fresh.gen.ts";
+
+Deno.test("index.tsx route handler test...", async (t) => {
+ await t.step("should redirect to init.ts", () => {
+ expect(handler.GET).toBeDefined();
+ const req = new Request("http://localhost:8000/");
+ req.headers.set("user-agent", "Chrome");
+ // @ts-ignore manifest typing doesn't matter
+ const ctx = createHandlerContext(req, { manifest });
+ // @ts-ignore handler is not null
+ const resp = handler.GET(req, ctx) as Response;
+ console.log("resp", resp);
+ expect(resp.status).toBe(307);
+ expect(resp.headers.get("Location")?.endsWith("init.ts"));
+ });
+
+ await t.step("should not redirect and return status=200", () => {
+ expect(handler.GET).toBeDefined();
+ const req = new Request("http://localhost:8000/");
+ req.headers.set("user-agent", "Chrome");
+ req.headers.set("accept", "text/html");
+ // @ts-ignore manifest typing doesn't matter
+ const ctx = createHandlerContext(req, { manifest });
+ // mock render to return Response-like object
+ // @ts-ignore mock object used
+ ctx.render = () => {
+ return ({ body: "Hello World!", status: 200 });
+ };
+ // @ts-ignore handler is not null
+ const resp = handler.GET(req, ctx) as Response;
+ // console.log("resp", resp);
+ expect(resp.status).toBe(200);
+ });
+});
diff --git a/tests/www/routes/routes.middleware.test.tsx b/tests/www/routes/routes.middleware.test.tsx
new file mode 100644
index 00000000..e4f1c710
--- /dev/null
+++ b/tests/www/routes/routes.middleware.test.tsx
@@ -0,0 +1,14 @@
+import { createMiddlewareHandlerContext } from "$fresh-testing-library/server.ts";
+import { expect } from "$fresh-testing-library/expect.ts";
+import { handler } from "../../../www/routes/_middleware.ts";
+import manifest from "../../../demo/fresh.gen.ts";
+
+Deno.test("should return middleware status=200 & content-type", async () => {
+ const req = new Request(`http://localhost:3000/`);
+ // @ts-ignore ignores "type ... is not assignable to type Manifest" error
+ const ctx = createMiddlewareHandlerContext(req, { manifest });
+ const resp = await handler(req, ctx);
+ // console.log("Response", resp);
+ expect(resp.status).toBe(200);
+ expect(resp.headers.get("content-type")).toBe("text/plain;charset=UTF-8");
+});
diff --git a/www/components/DocsSidebar.tsx b/www/components/DocsSidebar.tsx
index 1b401a80..69c05d4c 100644
--- a/www/components/DocsSidebar.tsx
+++ b/www/components/DocsSidebar.tsx
@@ -8,6 +8,7 @@ import VersionSelect from "../islands/VersionSelect.tsx";
import { type VersionLink } from "../routes/docs/[...slug].tsx";
import { Logo } from "$fresh/www/components/Header.tsx";
import DocsTitle from "$fresh/www/components/DocsTitle.tsx";
+import docsearch from "https://esm.sh/@docsearch/js@3?target=es2020";
export default function DocsSidebar(
props: {
@@ -27,7 +28,7 @@ export default function DocsSidebar(
-
+
void; class?: string },
+) {
const ref = useRef(null);
useEffect(() => {
if (ref.current) {
- docsearch({
+ props.docsearch({
appId: "CWUS37S0PK",
apiKey: "caa591b6dcb2c9308551361d954a728b",
indexName: "fresh",
@@ -22,7 +31,12 @@ export default function SearchButton(props: { class?: string }) {
href="/docsearch.css"
/>
-
+
+
>
);
}