Skip to content

Commit

Permalink
test(utils): Clean up the test utils (#138)
Browse files Browse the repository at this point in the history
Make objects to collect similar constants.
  • Loading branch information
5ouma authored Dec 8, 2024
1 parent a4dc6bb commit c26f7e4
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 86 deletions.
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@octokit/rest": "npm:@octokit/[email protected]",
"@std/assert": "jsr:@std/[email protected]",
"@std/http": "jsr:@std/[email protected]",
"@std/path": "jsr:@std/[email protected]"
"@std/path": "jsr:@std/[email protected]",
"@std/testing": "jsr:@std/[email protected]"
}
}
45 changes: 44 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions src/libs/content.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { assertEquals, assertExists, assertStringIncludes } from "@std/assert";
import { STATUS_CODE } from "@std/http/status";
import { describe, test } from "@std/testing/bdd";

import { getContent } from "./content.ts";
import { testRef, testRepo, unknownRepo } from "./test_utils.ts";
import { testRef, testRepo } from "./test_utils.ts";

Deno.test("Get Content", async (t: Deno.TestContext) => {
await t.step("normal", async () => {
const [data, status] = await getContent(testRepo);
describe("Get Content", () => {
test("normal", async () => {
const [data, status] = await getContent(testRepo.normal);

assertExists(data);
assertEquals(status, STATUS_CODE.OK);
});

await t.step("with ref", async () => {
const [data, status] = await getContent(testRepo, testRef);
test("with ref", async () => {
const [data, status] = await getContent(testRepo.normal, testRef.normal);

assertExists(data);
assertEquals(status, STATUS_CODE.OK);
});

await t.step("not found", async () => {
const [data, status] = await getContent(unknownRepo);
test("not found", async () => {
const [data, status] = await getContent(testRepo.unknown);

assertStringIncludes(data, `⚠️ ${STATUS_CODE.NotFound}:`);
assertEquals(status, STATUS_CODE.NotFound);
Expand Down
40 changes: 33 additions & 7 deletions src/libs/redirect.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
import { assertEquals } from "@std/assert";
import { describe, test } from "@std/testing/bdd";
import { UserAgent } from "@std/http/user-agent";

import { checkRedirect } from "./redirect.ts";
import { testRef, testRepo, testUserAgent } from "./test_utils.ts";
import { getGitHubUrl } from "./utils.ts";

Deno.test("Redirect Detection", async (t: Deno.TestContext) => {
await t.step("normal", () => {
const url: URL | null = checkRedirect(testUserAgent, testRepo);
describe("Redirect Detection", () => {
describe("Direct", () => {
test("normal", () => {
const url: URL | null = checkRedirect(new UserAgent(""), testRepo.normal);

assertEquals(url, getGitHubUrl(testRepo));
assertEquals(url, null);
});

test("with ref", () => {
const url: URL | null = checkRedirect(
new UserAgent(""),
testRepo.normal,
testRef.normal,
);

assertEquals(url, null);
});
});

await t.step("with ref", () => {
const url: URL | null = checkRedirect(testUserAgent, testRepo, testRef);
describe("Redirect", () => {
test("normal", () => {
const url: URL | null = checkRedirect(testUserAgent, testRepo.normal);

assertEquals(url, getGitHubUrl(testRepo.normal));
});

test("with ref", () => {
const url: URL | null = checkRedirect(
testUserAgent,
testRepo.normal,
testRef.normal,
);

assertEquals(url, getGitHubUrl(testRepo, testRef));
assertEquals(url, getGitHubUrl(testRepo.normal, testRef.normal));
});
});
});
42 changes: 19 additions & 23 deletions src/libs/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,44 @@ import type { Repository } from "./types.ts";
*
* @example
* ```ts
* const repository = testRepo;
* const repository = testRepo.normal;
* ```
*/
export const testRepo: Repository = {
owner: "denoland",
name: "deno",
path: "README.md",
};

/**
* Sample unknown repository for test.
*
* @example
* ```ts
* const repository = unknownRepo;
* const repository = testRepo.unknown;
* ```
*/
export const unknownRepo: Repository = {
owner: "unknown-owner",
name: "unknown-repo",
path: "unknown-path",
export const testRepo: { normal: Repository; unknown: Repository } = {
normal: {
owner: "denoland",
name: "deno",
path: "README.md",
},
unknown: {
owner: "unknown-owner",
name: "unknown-repo",
path: "unknown-path",
},
};

/**
* Sample version reference for test.
*
* @example
* ```ts
* const ref = testRef;
* const ref = testRef.normal;
* ```
*/
export const testRef = "v1.0.0";

/**
* Sample version reference with slash for test.
*
* @example
* ```ts
* const ref = testRefSlash;
* const ref = testRef.slash;
* ```
*/
export const testRefSlash = "renovate/configure";
export const testRef: { normal: string; slash: string } = {
normal: "v1.0.0",
slash: "renovate/configure",
};

/**
* Sample user agent for test.
Expand Down
Loading

0 comments on commit c26f7e4

Please sign in to comment.