Skip to content

Commit

Permalink
Merge pull request #27 from nyaomaru/add-test
Browse files Browse the repository at this point in the history
Add test
  • Loading branch information
nyaomaru authored Sep 28, 2024
2 parents 88ee3ad + d960cbf commit 110fe2d
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 43 deletions.
58 changes: 58 additions & 0 deletions src/components/Heading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { JSX } from "preact";

type HeadingProps = {
variant: "h1" | "h2" | "h3" | "h4" | "h5" | "h6";
text: string;
} & JSX.HTMLAttributes<HTMLHeadingElement>;

export function Heading({ variant, text, ...props }: HeadingProps) {
if (variant === "h1") {
return (
<h1 {...props} class="text-5xl font-bold text-gray-800">
{text}
</h1>
);
}

if (variant === "h2") {
return (
<h2 {...props} class="text-3xl font-semibold text-gray-800">
{text}
</h2>
);
}

if (variant === "h3") {
return (
<h3 {...props} class="text-2xl">
{text}
</h3>
);
}

if (variant === "h4") {
return (
<h4 {...props} class="text-xl">
{text}
</h4>
);
}

if (variant === "h5") {
return (
<h5 {...props}>
{text}
</h5>
);
}

if (variant === "h6") {
return (
<h6 {...props}>
{text}
</h6>
);
}

return <div></div>;
}
19 changes: 11 additions & 8 deletions src/routes/about.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Handlers, PageProps, RouteContext } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import NextContentButton from "#src/islands/NextContentButton.tsx";
import { Heading } from "#src/components/Heading.tsx";
import { PageLayout } from "#src/components/PageLayout.tsx";

const loadFooValue = () => {
Expand Down Expand Up @@ -34,14 +35,16 @@ export default async function AboutPage(props: PageProps, ctx: RouteContext) {
return (
<PageLayout>
<main f-client-nav>
<h1 class="text-4xl font-bold">About</h1>
<p>This is the about page.</p>
<Partial name="about-content">
<div>You are on the page '{props.url.href}'.</div>
<p>foo is: {value}</p>
</Partial>
<div class="mt-8">
<NextContentButton name="Next Content" />
<Heading variant="h1" text={"About"}></Heading>
<div class="mt-4">
<p>This is the about page.</p>
<Partial name="about-content">
<div>You are on the page '{props.url.href}'.</div>
<p>foo is: {value}</p>
</Partial>
<div class="mt-4 mb-8">
<NextContentButton name="Next Content" />
</div>
</div>
</main>
</PageLayout>
Expand Down
3 changes: 2 additions & 1 deletion src/routes/chart.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ChartIsland from "#src/islands/Chart.tsx";
import { ChartColors } from "$fresh_charts/utils.ts";
import { Heading } from "#src/components/Heading.tsx";
import { PageLayout } from "#src/components/PageLayout.tsx";

const createRandomNumber = () => {
Expand All @@ -13,7 +14,7 @@ const createChartNumberArray = () => {
export default function Home() {
return (
<PageLayout>
<h1 class="text-4xl font-bold">Chart</h1>
<Heading variant="h1" text={"Chart"}></Heading>
<div class="mt-4 mx-auto max-w-screen-md">
<ChartIsland
type="bar"
Expand Down
1 change: 1 addition & 0 deletions src/routes/countdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default defineRoute((_req, _ctx) => {
date.setHours(date.getHours() + 1);
return (
<PageLayout>
<h1 class="text-4xl font-bold">Count Down</h1>
<article class="prose lg:prose-xl">
<p>
The big event is happening <Countdown target={date.toISOString()} />.
Expand Down
3 changes: 2 additions & 1 deletion src/routes/greet/[name].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Head } from "$fresh/runtime.ts";
import { PageProps, RouteConfig } from "$fresh/server.ts";
import { Heading } from "#src/components/Heading.tsx";
import { PageLayout } from "#src/components/PageLayout.tsx";

export const config: RouteConfig = {
Expand All @@ -16,7 +17,7 @@ export default function GreetPage(props: PageProps) {
</Head>
<main>
<PageLayout>
<h1 class="text-4xl font-bold">Greetings to you, {name}!</h1>
<Heading variant="h1" text={`Greetings to you, ${name}!`}></Heading>
<p>
This page is <em>not</em>{" "}
applied layout because I want to check ignore layout config.
Expand Down
24 changes: 12 additions & 12 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Handlers, PageProps, RouteConfig } from "$fresh/server.ts";
import { type ContentSecurityPolicy, useCSP } from "$fresh/runtime.ts";
import { useSignal } from "@preact/signals";
import { Heading } from "#src/components/Heading.tsx";
import { Link } from "#src/components/Link.tsx";
import Counter from "#src/islands/Counter.tsx";

Expand Down Expand Up @@ -59,15 +60,16 @@ export default function Home({ data }: PageProps<string>) {
src="/logo.svg"
alt="the Fresh logo: a sliced lemon dripping with juice"
/>
<h1 class="text-5xl font-bold text-gray-800">Nyaomaru Deno Sample</h1>
<p class="mt-4 text-lg text-gray-600">I am a cat being!!</p>
</header>

<main class="my-10">
<section class="mb-10">
<h2 class="text-3xl font-semibold text-gray-800 text-center">
Counter
</h2>
<main class="my-4">
<Heading variant="h1" text={"Nyaomaru Deno Sample"}></Heading>
<div class="block items-center text-center">
<p class="mt-4 text-lg text-gray-600">I am a cat being!!</p>
</div>

<section class="mb-10 text-center">
<Heading variant="h2" text={"Counter"}></Heading>
<div class="mt-6 flex flex-col items-center">
<Counter count={count}>
<p class="mt-4 text-gray-700">
Expand All @@ -77,17 +79,15 @@ export default function Home({ data }: PageProps<string>) {
</div>
</section>

<section class="mb-10">
<h2 class="text-3xl font-semibold text-gray-800 text-center">
Page Buttons
</h2>
<section class="mb-10 text-center">
<Heading variant="h2" text={"Page Buttons"}></Heading>
<div class="mt-6 flex flex-wrap justify-center gap-4">
<Link text="About" href="about" />
<Link text="Chart" href="chart" />
<Link text="Search" href="search" />
<Link text="Countdown" href="countdown" />
</div>
<div class="my-4 flex gap-4">
<div class="my-4 flex flex-wrap justify-center gap-4">
<Link text="Map" href="map" />
<Link
text="Projects"
Expand Down
6 changes: 5 additions & 1 deletion src/routes/map.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { defineRoute } from "$fresh/server.ts";
import { Heading } from "#src/components/Heading.tsx";
import { PageLayout } from "#src/components/PageLayout.tsx";
import { MapIsland } from "#src/islands/MapIsland.tsx";

export default defineRoute((_req, _ctx) => {
return (
<PageLayout>
<MapIsland />
<Heading variant="h1" text={"Map"}></Heading>
<div class="mt-4">
<MapIsland />
</div>
</PageLayout>
);
});
14 changes: 9 additions & 5 deletions src/routes/markdowns/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Handlers, PageProps } from "$fresh/server.ts";
import { extract } from "$std/front_matter/yaml.ts";
import { CSS, render } from "$gfm";
import { Head } from "$fresh/runtime.ts";
import { Heading } from "#src/components/Heading.tsx";
import { PageLayout } from "#src/components/PageLayout.tsx";

const markdownText = `---
Expand Down Expand Up @@ -64,11 +65,14 @@ export default function MarkdownPage({ data }: PageProps<Page | null>) {
</Head>
<main>
<PageLayout>
<div>{JSON.stringify(data.data)}</div>
<div
class="markdown-body"
dangerouslySetInnerHTML={{ __html: render(data?.markdown) }}
/>
<Heading variant="h1" text={"Markdown"}></Heading>
<div class="mt-4">
<div>{JSON.stringify(data.data)}</div>
<div
class="markdown-body"
dangerouslySetInnerHTML={{ __html: render(data?.markdown) }}
/>
</div>
</PageLayout>
</main>
</>
Expand Down
3 changes: 2 additions & 1 deletion src/routes/partials/about/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineRoute, RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { Heading } from "#src/components/Heading.tsx";

const contents = [
"This is the documentation page. It should contain all the \n information you need to get started with the project.",
Expand All @@ -11,7 +12,7 @@ const loadContent = (id: string) => {

return (
<article class="prose lg:prose-xl">
<h1>Documentation: {id}</h1>
<Heading variant="h2" text={`Documentation: ${id}`}></Heading>
<p>
{currentContent}
</p>
Expand Down
9 changes: 7 additions & 2 deletions src/routes/projects/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Handlers, PageProps } from "$fresh/server.ts";
import { Heading } from "#src/components/Heading.tsx";
import { PageLayout } from "#src/components/PageLayout.tsx";

const projects = [{ id: 1, name: "Project 1", stars: 10 }, {
Expand Down Expand Up @@ -36,8 +37,12 @@ export default function ProjectPage(props: PageProps<Project>) {

return (
<PageLayout>
<h1 class="text-4xl font-bold">{props.data.name}</h1>
<p>{props.data.stars} stars</p>
<Heading variant="h1" text={"Project"}></Heading>

<div class="mt-4">
<Heading variant="h2" text={props.data.name}></Heading>
<p>{props.data.stars} stars</p>
</div>
</PageLayout>
);
}
3 changes: 2 additions & 1 deletion src/routes/search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Handlers, PageProps } from "$fresh/server.ts";
import { Heading } from "#src/components/Heading.tsx";
import { PageLayout } from "#src/components/PageLayout.tsx";

const NAMES = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Frank"];
Expand All @@ -21,7 +22,7 @@ export default function Page({ data }: PageProps<Data>) {
const { results, query } = data;
return (
<PageLayout>
<h1 class="text-4xl font-bold">Search</h1>
<Heading variant="h1" text={"Search"}></Heading>
<form class="mt-4">
<input
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
Expand Down
57 changes: 46 additions & 11 deletions tests/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ const CONN_INFO: ServeHandlerInfo = {
remoteAddr: { hostname: "127.0.0.1", port: 8000, transport: "tcp" },
};

const verifyPageContainsText = async (
handler: (req: Request, connInfo?: ServeHandlerInfo) => Promise<Response>,
pageUrl: string,
expectedText: string,
) => {
const resp = await handler(
new Request(`http://127.0.0.1/${pageUrl}`),
CONN_INFO,
);
const text = await resp.text();
modAssert.assert(text.includes(expectedText));
};

Deno.test("HTTP assert test.", async (t) => {
const handler = await createHandler(manifest, config);

Expand All @@ -17,20 +30,42 @@ Deno.test("HTTP assert test.", async (t) => {
});

await t.step("#2 GET /about", async () => {
const resp = await handler(
new Request("http://127.0.0.1/about"),
CONN_INFO,
);
const text = await resp.text();
modAssert.assert(text.includes("This is the about page."));
await verifyPageContainsText(handler, "about", "This is the about page.");
});

await t.step("#3 GET /404", async () => {
const resp = await handler(
new Request("http://127.0.0.1/404"),
CONN_INFO,
await verifyPageContainsText(handler, "404", "404 - Page not found");
});

await t.step("#4 GET /chart", async () => {
await verifyPageContainsText(handler, "chart", "Chart");
});

await t.step("#5 GET /countdown", async () => {
await verifyPageContainsText(
handler,
"countdown",
"The big event is happening",
);
const text = await resp.text();
modAssert.assert(text.includes("404 - Page not found"));
});

await t.step("#6 GET /map", async () => {
await verifyPageContainsText(handler, "map", "Map");
});

await t.step("#7 GET /search", async () => {
await verifyPageContainsText(handler, "search", "Search");
});

await t.step("#8 GET /greet", async () => {
await verifyPageContainsText(handler, "greet/test", "test");
});

await t.step("#9 GET /markdown", async () => {
await verifyPageContainsText(handler, "markdowns/string", "Markdown");
});

await t.step("#10 GET /project", async () => {
await verifyPageContainsText(handler, "projects/1", "Project 1");
});
});

0 comments on commit 110fe2d

Please sign in to comment.