Skip to content

Commit

Permalink
test(step): Use step from Deno.TestContext
Browse files Browse the repository at this point in the history
It makes the result clean and readable.
  • Loading branch information
5ouma committed Jun 17, 2024
1 parent 719cb4c commit 042bb84
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 66 deletions.
41 changes: 21 additions & 20 deletions test/libs/content_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,31 @@ import { STATUS_CODE } from "@std/http/status";
import { getContent } from "../../src/libs/content.ts";
import { exportRepo, testRef, testRepo, unknownRepo } from "../utils.ts";

Deno.test("Get Content", async <R extends string>() => {
Deno.test("Get Content", async <R extends string>(t: Deno.TestContext) => {
const ctx: RouterContext<R> = testing.createMockContext();
exportRepo(testRepo);
await getContent(ctx);

assertEquals(ctx.response.status, STATUS_CODE.OK);
});
await t.step("normal", async () => {
exportRepo(testRepo);
await getContent(ctx);

Deno.test("Get Content (With ref)", async <R extends string>() => {
const ctx: RouterContext<R> = testing.createMockContext();
exportRepo(testRepo);
await getContent(ctx, testRef);
assertEquals(ctx.response.status, STATUS_CODE.OK);
});

assertEquals(ctx.response.status, STATUS_CODE.OK);
});
await t.step("with ref", async () => {
exportRepo(testRepo);
await getContent(ctx, testRef);

Deno.test("Get Content (Not found)", async <R extends string>() => {
const ctx: RouterContext<R> = testing.createMockContext();
exportRepo(unknownRepo);
await getContent(ctx);
assertEquals(ctx.response.status, STATUS_CODE.OK);
});

await t.step("not found", async () => {
exportRepo(unknownRepo);
await getContent(ctx);

assertEquals(ctx.response.status, STATUS_CODE.NotFound);
assertStringIncludes(
ctx.response.body!.toString(),
`⚠️ ${STATUS_CODE.NotFound}:`,
);
assertEquals(ctx.response.status, STATUS_CODE.NotFound);
assertStringIncludes(
ctx.response.body!.toString(),
`⚠️ ${STATUS_CODE.NotFound}:`,
);
});
});
28 changes: 15 additions & 13 deletions test/libs/env_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ import { assertEquals } from "@std/assert";
import { getRepository } from "../../src/libs/env.ts";
import { clearRepo, exportRepo, testRepo } from "../utils.ts";

Deno.test("Get Repository Env", () => {
exportRepo(testRepo);
const repository = getRepository();
Deno.test("Get Repository Env", async (t: Deno.TestContext) => {
await t.step("Normal", () => {
exportRepo(testRepo);
const repository = getRepository();

assertEquals(repository.owner, testRepo.owner);
assertEquals(repository.name, testRepo.name);
assertEquals(repository.path, testRepo.path);
});
assertEquals(repository.owner, testRepo.owner);
assertEquals(repository.name, testRepo.name);
assertEquals(repository.path, testRepo.path);
});

Deno.test("Get Repository Env (Not set)", () => {
clearRepo();
const repository = getRepository();
await t.step("Not set", () => {
clearRepo();
const repository = getRepository();

assertEquals(repository.owner, "");
assertEquals(repository.name, "");
assertEquals(repository.path, "");
assertEquals(repository.owner, "");
assertEquals(repository.name, "");
assertEquals(repository.path, "");
});
});
35 changes: 18 additions & 17 deletions test/libs/redirect_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ import { UserAgent } from "@std/http/user-agent";
import { redirect } from "../../src/libs/redirect.ts";
import { exportRepo, testRef, testRepo } from "../utils.ts";

Deno.test("Redirect Detection", <R extends string>() => {
Deno.test("Redirect Detection", async <R extends string>(t: Deno.TestContext) => {
const ctx: RouterContext<R> = testing.createMockContext();
exportRepo(testRepo);
redirect(ctx, new UserAgent("Chrome/1.2.3"));

assertEquals(ctx.response.status, STATUS_CODE.PermanentRedirect);
assertEquals(
ctx.response.headers.get("location"),
`https://github.com/${testRepo.owner}/${testRepo.name}/blob/master/${testRepo.path}`,
);
});
await t.step("normal", () => {
redirect(ctx, new UserAgent("Chrome/1.2.3"));

Deno.test("Redirect Detection (With ref)", <R extends string>() => {
const ctx: RouterContext<R> = testing.createMockContext();
exportRepo(testRepo);
redirect(ctx, new UserAgent("Chrome/1.2.3"), testRef);
assertEquals(ctx.response.status, STATUS_CODE.PermanentRedirect);
assertEquals(
ctx.response.headers.get("location"),
`https://github.com/${testRepo.owner}/${testRepo.name}/blob/master/${testRepo.path}`,
);
});

await t.step("with ref", () => {
redirect(ctx, new UserAgent("Chrome/1.2.3"), testRef);

assertEquals(ctx.response.status, STATUS_CODE.PermanentRedirect);
assertEquals(
ctx.response.headers.get("location"),
`https://github.com/${testRepo.owner}/${testRepo.name}/blob/${testRef}/${testRepo.path}`,
);
assertEquals(ctx.response.status, STATUS_CODE.PermanentRedirect);
assertEquals(
ctx.response.headers.get("location"),
`https://github.com/${testRepo.owner}/${testRepo.name}/blob/${testRef}/${testRepo.path}`,
);
});
});
34 changes: 18 additions & 16 deletions test/router_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import { STATUS_CODE } from "@std/http/status";
import { router } from "../src/router.ts";
import { exportRepo, testRef, testRepo } from "./utils.ts";

Deno.test("Serve (/)", async <R extends string>() => {
const ctx: RouterContext<R> = testing.createMockContext({
method: "GET",
path: "/",
Deno.test("Serve", async <R extends string>(t: Deno.TestContext) => {
await t.step("/", async () => {
const ctx: RouterContext<R> = testing.createMockContext({
method: "GET",
path: "/",
});
exportRepo(testRepo);
await router.routes()(ctx, () => Promise.resolve());

assertEquals(ctx.response.status, STATUS_CODE.OK);
});
exportRepo(testRepo);
await router.routes()(ctx, () => Promise.resolve());

assertEquals(ctx.response.status, STATUS_CODE.OK);
});
await t.step("/:ref", async () => {
const ctx: RouterContext<R> = testing.createMockContext({
method: "GET",
path: `/${testRef}`,
});
exportRepo(testRepo);
await router.routes()(ctx, () => Promise.resolve());

Deno.test("Serve (/:ref)", async <R extends string>() => {
const ctx: RouterContext<R> = testing.createMockContext({
method: "GET",
path: `/${testRef}`,
assertEquals(ctx.response.status, STATUS_CODE.OK);
});
exportRepo(testRepo);
await router.routes()(ctx, () => Promise.resolve());

assertEquals(ctx.response.status, STATUS_CODE.OK);
});

0 comments on commit 042bb84

Please sign in to comment.