Skip to content

Commit

Permalink
tests: update the test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pplmx committed Aug 27, 2024
1 parent 62fbb4f commit b8864ec
Showing 1 changed file with 58 additions and 46 deletions.
104 changes: 58 additions & 46 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { run } from "../index";
jest.mock("@actions/core");
jest.mock("@actions/github");

describe("run", () => {
describe("GitHub Action", () => {
const mockGetInput = getInput as jest.MockedFunction<typeof getInput>;
const mockSetFailed = setFailed as jest.MockedFunction<typeof setFailed>;
const mockGetOctokit = getOctokit as jest.MockedFunction<typeof getOctokit>;
Expand Down Expand Up @@ -36,66 +36,78 @@ describe("run", () => {
(context as any).repo = { owner: "owner", repo: "repo" };
});

it("should set failed if not run on a pull request", async () => {
(context as any).payload = {};
describe("run function", () => {
it("should set failed if not run on a pull request", async () => {
(context as any).payload = {};

await run();
await run();

expect(mockSetFailed).toHaveBeenCalledWith("This action can only be run on Pull Requests");
});
expect(mockSetFailed).toHaveBeenCalledWith("This action can only be run on Pull Requests");
});

it("should add label to the pull request", async () => {
await run();

expect(mockGetInput).toHaveBeenCalledWith("gh-token", { required: true });
expect(mockGetInput).toHaveBeenCalledWith("label", { required: true });
expect(mockGetOctokit).toHaveBeenCalledWith("gh-token-value");
expect(mockAddLabels).toHaveBeenCalledWith({
owner: "owner",
repo: "repo",
issue_number: 1,
labels: ["label-value"],
it("should add label to the pull request", async () => {
await run();

expect(mockGetInput).toHaveBeenCalledWith("gh-token", { required: true });
expect(mockGetInput).toHaveBeenCalledWith("label", { required: true });
expect(mockGetOctokit).toHaveBeenCalledWith("gh-token-value");
expect(mockAddLabels).toHaveBeenCalledWith({
owner: "owner",
repo: "repo",
issue_number: 1,
labels: ["label-value"],
});
expect(mockSetFailed).not.toHaveBeenCalled();
});
expect(mockSetFailed).not.toHaveBeenCalled();
});

it("should handle error and set failed", async () => {
mockAddLabels.mockRejectedValueOnce(new Error("Test error"));
it("should handle error and set failed", async () => {
mockAddLabels.mockRejectedValueOnce(new Error("Test error"));

await run();
await run();

expect(mockAddLabels).toHaveBeenCalledWith({
owner: "owner",
repo: "repo",
issue_number: 1,
labels: ["label-value"],
expect(mockAddLabels).toHaveBeenCalledWith({
owner: "owner",
repo: "repo",
issue_number: 1,
labels: ["label-value"],
});
expect(mockSetFailed).toHaveBeenCalledWith("Test error");
});
expect(mockSetFailed).toHaveBeenCalledWith("Test error");
});

it("should set failed if gh-token is not provided", async () => {
mockGetInput.mockImplementation((name, options) => {
if (name === "gh-token" && options?.required) {
throw new Error("Input required and not supplied: gh-token");
}
return name === "label" ? "label-value" : "";
it("should set failed if gh-token is not provided", async () => {
mockGetInput.mockImplementation((name, options) => {
if (name === "gh-token" && options?.required) {
throw new Error("Input required and not supplied: gh-token");
}
return name === "label" ? "label-value" : "";
});

await run();

expect(mockSetFailed).toHaveBeenCalledWith("Input required and not supplied: gh-token");
});

await run();
it("should set failed if label is not provided", async () => {
mockGetInput.mockImplementation((name, options) => {
if (name === "label" && options?.required) {
throw new Error("Input required and not supplied: label");
}
return name === "gh-token" ? "gh-token-value" : "";
});

expect(mockSetFailed).toHaveBeenCalledWith("Input required and not supplied: gh-token");
});
await run();

it("should set failed if label is not provided", async () => {
mockGetInput.mockImplementation((name, options) => {
if (name === "label" && options?.required) {
throw new Error("Input required and not supplied: label");
}
return name === "gh-token" ? "gh-token-value" : "";
expect(mockSetFailed).toHaveBeenCalledWith("Input required and not supplied: label");
});

await run();
it("should set failed with generic message for unexpected errors", async () => {
mockGetOctokit.mockImplementation(() => {
throw new Error("Unexpected error");
});

expect(mockSetFailed).toHaveBeenCalledWith("Input required and not supplied: label");
await run();

expect(mockSetFailed).toHaveBeenCalledWith("Unexpected error");
});
});
});

0 comments on commit b8864ec

Please sign in to comment.