From 93edb7587ba01f74944bf42db2bba22ed595fb2d Mon Sep 17 00:00:00 2001 From: 5ouma <101255979+5ouma@users.noreply.github.com> Date: Fri, 7 Jun 2024 19:06:09 +0900 Subject: [PATCH] style(test): Write more clean test code Tests are meaningful and readable. --- test/libs/content_test.ts | 6 +++--- test/libs/env_test.ts | 18 ++++++++---------- test/libs/redirect_test.ts | 14 +++++++------- test/utils.ts | 6 ++++++ 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/test/libs/content_test.ts b/test/libs/content_test.ts index 05e091b..b87c18e 100644 --- a/test/libs/content_test.ts +++ b/test/libs/content_test.ts @@ -7,24 +7,24 @@ import { testRepo, unknownRepo, testRef, exportRepo } from "../utils.ts"; Deno.test("Get Content", async () => { const ctx: RouterContext = testing.createMockContext(); exportRepo(testRepo); - await getContent(ctx); + assertEquals(ctx.response.status, STATUS_CODE.OK); }); Deno.test("Get Content (With ref)", async () => { const ctx: RouterContext = testing.createMockContext(); exportRepo(testRepo); - await getContent(ctx, testRef); + assertEquals(ctx.response.status, STATUS_CODE.OK); }); Deno.test("Get Content (Not found)", async () => { const ctx: RouterContext = testing.createMockContext(); exportRepo(unknownRepo); - await getContent(ctx); + assertEquals(ctx.response.status, STATUS_CODE.NotFound); assertStringIncludes( ctx.response.body!.toString(), diff --git a/test/libs/env_test.ts b/test/libs/env_test.ts index 996b7cd..48dff26 100644 --- a/test/libs/env_test.ts +++ b/test/libs/env_test.ts @@ -1,22 +1,20 @@ import { assertEquals } from "@std/assert"; import { getRepository } from "../../src/libs/env.ts"; -import { unknownRepo, exportRepo } from "../utils.ts"; +import { testRepo, exportRepo, clearRepo } from "../utils.ts"; Deno.test("Get Repository Env", () => { - exportRepo(unknownRepo); - + exportRepo(testRepo); const repository = getRepository(); - assertEquals(repository.owner, unknownRepo.owner); - assertEquals(repository.name, unknownRepo.name); - assertEquals(repository.path, unknownRepo.path); + + assertEquals(repository.owner, testRepo.owner); + assertEquals(repository.name, testRepo.name); + assertEquals(repository.path, testRepo.path); }); Deno.test("Get Repository Env (Not set)", () => { - Deno.env.delete("REPOSITORY_OWNER"); - Deno.env.delete("REPOSITORY_NAME"); - Deno.env.delete("REPOSITORY_PATH"); - + clearRepo(); const repository = getRepository(); + assertEquals(repository.owner, ""); assertEquals(repository.name, ""); assertEquals(repository.path, ""); diff --git a/test/libs/redirect_test.ts b/test/libs/redirect_test.ts index 588a565..979abac 100644 --- a/test/libs/redirect_test.ts +++ b/test/libs/redirect_test.ts @@ -3,28 +3,28 @@ import { assertEquals } from "@std/assert"; import { STATUS_CODE } from "@std/http/status"; import { UserAgent } from "@std/http/user-agent"; import { redirect } from "../../src/libs/redirect.ts"; -import { unknownRepo, testRef, exportRepo } from "../utils.ts"; +import { testRepo, testRef, exportRepo } from "../utils.ts"; Deno.test("Redirect Detection", () => { const ctx: RouterContext = testing.createMockContext(); - exportRepo(unknownRepo); - + 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/${unknownRepo.owner}/${unknownRepo.name}/blob/HEAD/${unknownRepo.path}` + `https://github.com/${testRepo.owner}/${testRepo.name}/blob/HEAD/${testRepo.path}` ); }); Deno.test("Redirect Detection (With ref)", () => { const ctx: RouterContext = testing.createMockContext(); - exportRepo(unknownRepo); - + 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/${unknownRepo.owner}/${unknownRepo.name}/blob/${testRef}/${unknownRepo.path}` + `https://github.com/${testRepo.owner}/${testRepo.name}/blob/${testRef}/${testRepo.path}` ); }); diff --git a/test/utils.ts b/test/utils.ts index b576b13..bccbd56 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -19,3 +19,9 @@ export function exportRepo(repository: Repository) { Deno.env.set("REPOSITORY_NAME", repository.name); Deno.env.set("REPOSITORY_PATH", repository.path); } + +export function clearRepo() { + Deno.env.delete("REPOSITORY_OWNER"); + Deno.env.delete("REPOSITORY_NAME"); + Deno.env.delete("REPOSITORY_PATH"); +}