feat(tests): add fresh-testing-library tests (#1990)

This commit is contained in:
Craig Doremus
2023-11-15 18:25:13 +01:00
committed by GitHub
parent 5d690bede1
commit b5816cd001
20 changed files with 447 additions and 8 deletions
+3 -1
View File
@@ -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/"
}
}
+2 -1
View File
@@ -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/*",
@@ -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(<Background title="background" />);
const bg = getByTitle("background");
const style = bg.getAttribute("style");
assertEquals(style, "background-image: url(/gallery/grid.svg);");
});
});
@@ -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(
<Button value="Click Me!" />,
);
const button = getByRole("button");
assertExists(button);
assertEquals((button as HTMLButtonElement).value, "Click Me!");
});
});
@@ -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(
<Carousel showNavigation={true} automatic={true} interval={1000} />,
);
const buttons = getAllByRole("button");
assertEquals(buttons.length, 6);
});
});
@@ -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(
<CodeBox code={code} />,
);
// 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(<CodeBox code={code} />);
// 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")),
);
});
});
@@ -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(<ColoredButton title="colored button" />);
const bg = getByTitle("colored button");
const disabled = bg.getAttribute("disabled");
assert(!disabled);
});
});
@@ -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(<Features />);
const links = screen.getAllByRole("link");
assertEquals(links.length, 2);
});
it("should display all feature descriptions", () => {
const screen = render(<Features />);
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);
});
});
@@ -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(<Footer></Footer>);
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(<Footer></Footer>);
const menu1 = getByText("Getting Started");
assertExists(menu1);
const menu2 = getByText("Community");
assertExists(menu2);
const menu3 = getByText("Discord");
assertExists(menu3);
});
});
@@ -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(<Header active="" />);
const links = screen.getAllByRole("link");
assertEquals(links.length, 3);
});
it("should show '/docs' as active", () => {
const screen = render(<Header active="/docs" />);
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(<Header active="/" />);
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!!");
}
});
});
@@ -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(<Hero />);
// Two anchor links in component
const links = getAllByRole("link");
assertEquals(links.length, 2);
});
});
@@ -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(<Input placeholder="text input" disabled />);
const input = screen.getByPlaceholderText("text input");
assert((input as HTMLInputElement).disabled);
});
});
@@ -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(
<LinkButton title="Link Button" href="https://google.com" />,
);
const anchor = screen.getByRole("link");
const attr = anchor.getAttribute("title");
assertEquals(attr, "Link Button");
});
it("should show link button classes", () => {
const screen = render(
<LinkButton title="Link Button" href="https://google.com" />,
);
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(
<LinkButton title="Link Button" href="https://google.com">
Click Me!
</LinkButton>,
);
const anchor = screen.getByRole("link");
const text = screen.getByText("Click Me!");
assertExists(text);
});
it("should render IconHeart component", () => {
const screen = render(
<LinkButton href="https://google.com">
<IconHeart
class="w-5 h-5 mr-1 inline-block text-gray-400"
aria-hidden="true"
title="icon-heart"
/>
Like me
</LinkButton>,
);
const icon = screen.getByTitle("icon-heart");
const hidden = icon.getAttribute("aria-hidden");
assert(hidden);
});
});
+25
View File
@@ -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(<Counter start={count} />);
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"));
});
});
+32
View File
@@ -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(
<SearchButton class="font-bold" docsearch={dsearch} />,
);
const search = getByTitle("Search Button");
assert(search.classList.contains("font-bold"));
});
});
+37
View File
@@ -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);
});
});
@@ -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");
});
+2 -1
View File
@@ -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(
</div>
<hr />
</div>
<SearchButton class="mr-4 sm:mr-0" />
<SearchButton class="mr-4 sm:mr-0" docsearch={docsearch} />
<div class="mb-4">
<VersionSelect
selectedVersion={props.selectedVersion}
+3 -1
View File
@@ -15,7 +15,9 @@
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3",
"$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.8.0/mod.ts",
"$fresh-testing-library/": "https://deno.land/x/fresh_testing_library@0.8.0/"
},
"compilerOptions": {
"jsx": "react-jsx",
+18 -4
View File
@@ -1,12 +1,21 @@
import { Head } from "$fresh/runtime.ts";
import { useEffect, useRef } from "preact/hooks";
import docsearch from "https://esm.sh/@docsearch/js@3?target=es2020";
export default function SearchButton(props: { class?: string }) {
// Copied from algolia source code
type DocSearchProps = {
appId: string;
apiKey: string;
indexName: string;
container: HTMLElement | string;
};
export default function SearchButton(
props: { docsearch: (args: DocSearchProps) => void; class?: string },
) {
const ref = useRef<HTMLDivElement>(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"
/>
</Head>
<div class={"h-9 mb-6 " + (props.class ?? "")} ref={ref}></div>
<div
title="Search Button"
class={"h-9 mb-6 " + (props.class ?? "")}
ref={ref}
>
</div>
</>
);
}