Skip to content

Commit

Permalink
style(test): Write more clean test code
Browse files Browse the repository at this point in the history
Tests are meaningful and readable.
  • Loading branch information
5ouma committed Jun 7, 2024
1 parent 555d6d3 commit 93edb75
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
6 changes: 3 additions & 3 deletions test/libs/content_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import { testRepo, unknownRepo, testRef, exportRepo } from "../utils.ts";
Deno.test("Get Content", async <R extends string>() => {
const ctx: RouterContext<R> = testing.createMockContext();
exportRepo(testRepo);

await getContent(ctx);

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

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);
});

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.NotFound);
assertStringIncludes(
ctx.response.body!.toString(),
Expand Down
18 changes: 8 additions & 10 deletions test/libs/env_test.ts
Original file line number Diff line number Diff line change
@@ -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, "");
Expand Down
14 changes: 7 additions & 7 deletions test/libs/redirect_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", <R extends string>() => {
const ctx: RouterContext<R> = 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)", <R extends string>() => {
const ctx: RouterContext<R> = 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}`
);
});
6 changes: 6 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 93edb75

Please sign in to comment.