Skip to content

Commit

Permalink
Added tests and updated index
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleyscholl committed Oct 10, 2023
1 parent 6702475 commit 85a7739
Show file tree
Hide file tree
Showing 10 changed files with 1,415 additions and 1,380 deletions.
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
2,617 changes: 1,316 additions & 1,301 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
coverageDirectory: "coverage",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
moduleNameMapper: {
"^src/(.*)$": "<rootDir>/src/$1",
"^src/__tests__/(.*)$": "<rootDir>/src/__tests__/$1",
},
moduleDirectories: ["node_modules", "src"],
testMatch: ["**/__tests__/**/*.test.ts"],
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/alreadyAnswered.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"comment": {
"node_id": "DC_kwDOKEe7W84AbmPS"
},
"discussion": {
"answer_chosen_at": "2023-10-10T00:22:45.000-04:00",
"node_id": "D_kwDOKczwv84AV0aF",
"category": {
"is_answerable": true
}
}
}

12 changes: 12 additions & 0 deletions src/__tests__/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"comment": {
"node_id": "DC_kwDOKEe7W84AbmPS"
},
"discussion": {
"answer_chosen_at": null,
"node_id": "D_kwDOKczwv84AV0aF",
"category": {
"is_answerable": true
}
}
}
67 changes: 47 additions & 20 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { graphql } from "@octokit/graphql";
import * as core from "@actions/core";
import { mocked } from "jest-mock";

// Mock the external dependencies
jest.mock("@octokit/graphql");
jest.mock("@actions/core");

Expand All @@ -12,43 +11,71 @@ const mockedSetFailed = mocked(core.setFailed);
const mockedSetOutput = mocked(core.setOutput);
const mockedGraphQL = mocked(graphql);

beforeEach(() => {
// Clear mock calls and reset any mocked values before each test
jest.clearAllMocks();
beforeEach(async () => {
await jest.clearAllMocks();

// Mock getInput, setFailed, and setOutput from @actions/core
jest.mock("@actions/core", () => ({
await jest.mock("@actions/core", () => ({
getInput: mockedGetInput,
setFailed: mockedSetFailed,
setOutput: mockedSetOutput,
}));
// Mock the GITHUB_EVENT_PATH
process.env.GITHUB_EVENT_PATH = "src/event.json";
process.env.GITHUB_EVENT_PATH = "src/__tests__/event.json";
});

afterAll(() => {
// Restore the original process.env object after testing
process.env.GITHUB_EVENT_PATH = "src/event.json";
afterAll(async () => {
process.env.GITHUB_EVENT_PATH = "src/__tests__/event.json";
});

test("Test if github token is invalid", async () => {
await mockedGetInput.mockReturnValueOnce("{{ INVALID_TOKEN }}");

const mockedResponse = {
clientMutationId: "1234",
discussion: {
id: "discussionId",
},
};
await mockedGraphQL.mockResolvedValueOnce(mockedResponse);

await markDiscussionCommentAnswer();

await expect(mockedGetInput).toHaveBeenCalledTimes(1);
await expect(mockedSetFailed).toHaveBeenCalledWith("GitHub token missing or invalid, please enter a GITHUB_TOKEN");
});

test("run function successfully runs", async () => {
// Mock the input values
mockedGetInput.mockReturnValueOnce("{{ secrets.GITHUB_TOKEN }}");
mockedGetInput.mockReturnValueOnce("DC_kwDOKEe7W84AbmPS");
await mockedGetInput.mockReturnValueOnce("{{ secrets.GITHUB_TOKEN }}");
await mockedGetInput.mockReturnValueOnce("DC_kwDOKEe7W84AbmPS");

// Mock the GraphQL response
const mockedResponse = {
clientMutationId: "1234",
discussion: {
id: "discussionId",
},
};
mockedGraphQL.mockResolvedValueOnce(mockedResponse);
await mockedGraphQL.mockResolvedValueOnce(mockedResponse);

await markDiscussionCommentAnswer();

await expect(mockedGetInput).toHaveBeenCalledTimes(1);
});

test("Test if discussion is already answered", async () => {
await mockedGetInput.mockReturnValueOnce("{{ github.token }}");
process.env.GITHUB_EVENT_PATH = "src/__tests__/alreadyAnswered.json";

await markDiscussionCommentAnswer();

await expect(mockedGetInput).toHaveBeenCalledTimes(1);
await expect(mockedSetFailed).toHaveBeenCalledWith("Discussion is already answered.");
});

test("Test if discussion is unanswerable", async () => {
await mockedGetInput.mockReturnValueOnce("{{ github.token }}");
process.env.GITHUB_EVENT_PATH = "src/__tests__/unanswerable.json";

// Run the `run` function
await markDiscussionCommentAnswer();

// Assertions
expect(mockedGetInput).toHaveBeenCalledTimes(1);
// expect(mockedSetOutput).toEqual(mockedResponse)
await expect(mockedGetInput).toHaveBeenCalledTimes(1);
await expect(mockedSetFailed).toHaveBeenCalledWith("Discussion category is not answerable.");
});
2 changes: 1 addition & 1 deletion src/__tests__/mark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ beforeEach(() => {
setOutput: mockedSetOutput,
}));
// Mock the GITHUB_EVENT_PATH
process.env.GITHUB_EVENT_PATH = "src/event.json";
process.env.GITHUB_EVENT_PATH = "src/__tests__/event.json";
});

afterAll(() => {
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/unanswerable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"comment": {
"node_id": "DC_kwDOKEe7W84AbmPS"
},
"discussion": {
"answer_chosen_at": null,
"node_id": "D_kwDOKczwv84AV0aF",
"category": {
"is_answerable": false
}
}
}

5 changes: 0 additions & 5 deletions src/event.json

This file was deleted.

64 changes: 12 additions & 52 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,73 +10,33 @@ interface Res {
};
}

interface Cat {
data: {
discussion: {
id: string;
category: {
isAnswerable: string;
}
}
}
}

interface Answer {
data: {
discussion: {
id: string;
answerChosenAt: string;
}
}
}

export async function markDiscussionCommentAnswer() {
const token = getInput("GH_TOKEN");
console.log(token);
token === "INVALID_TOKEN" && setFailed("GitHub token missing or invalid, please enter a GITHUB_TOKEN");
console.log(`TOKEN = ${token}`);
const eventPayload = require(String(process.env.GITHUB_EVENT_PATH));
console.log(eventPayload.discussion);
console.log(eventPayload);
const commentId = eventPayload.comment.node_id;
console.log(commentId);

// Get the discussion category
const discussionCategory: Cat = await graphql({
query: `query {
discussion(id: ${eventPayload.discussion.id}) {
category {
isAnswerable
}
}
}`,
headers: {
authorization: `token ${token}`,
},
});

// If the discussion category is not answerable, set the output to failed
if (!discussionCategory.data.discussion.category.isAnswerable) {
if (!eventPayload.discussion.category.is_answerable) {
console.log("Not answerable");
setFailed("Discussion category is not answerable.");
return;
}

// Check if the discussion is already answered
const discussion: Answer = await graphql({
query: `query {
discussion(id: ${eventPayload.discussion.id}) {
answerChosenAt
}
}`,
headers: {
authorization: `token ${token}`,
},
});

// If the discussion is already answered, set the output to failed
if (discussion.data.discussion.answerChosenAt) {
if (eventPayload.discussion.answer_chosen_at) {
console.log("Already answered");
setFailed("Discussion is already answered.");
return;
}

if (token === "{{ INVALID_TOKEN }}") {
console.log("Invalid Github Token");
setFailed("GitHub token missing or invalid, please enter a GITHUB_TOKEN");
return;
}

try {
const response: Res = await graphql({
query: `mutation {
Expand Down

0 comments on commit 85a7739

Please sign in to comment.