Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jbosse committed Jun 21, 2024
1 parent e52a87b commit 7153d6a
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 184 deletions.
10 changes: 7 additions & 3 deletions .github/linters/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ rules:
"import/no-namespace": "off",
"no-console": "off",
"no-unused-vars": "off",
"prettier/prettier": "error",
"semi": "off",
"quotes": ["error", "double", {"allowTemplateLiterals": true }]
"prettier/prettier": ["error",
{
"singleQuote": false,
"avoidEscape": false,
"semi": true,
}],
"semi": "off"
}
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

Check warning on line 1 in .prettierrc.json

View workflow job for this annotation

GitHub Actions / Lint Codebase

File ignored by default.
"printWidth": 80,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": false,
Expand Down
74 changes: 42 additions & 32 deletions __tests__/fogbogz-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,53 @@ describe("createCase", () => {
const category = "My Category";

beforeEach(() => {
jest.clearAllMocks()
jest.clearAllMocks();
});

it("posts to the FogBugz API", async () => {
postMock.mockImplementation((_url, _payload, _config) => { return validResponse; });
postMock.mockImplementation((_url, _payload, _config) => {
return validResponse;
});

await fogbugzClient.createCase(title, project, text, category);

expect(postMock)
.toHaveBeenCalledWith(
"https://example.com",
expect.anything(),
expect.objectContaining({
headers: {
"Accept": "application/json",
}
})
);
expect(postMock).toHaveBeenCalledWith(
"https://example.com",
expect.anything(),
expect.objectContaining({
headers: {
Accept: "application/json"
}
})
);
});

it("posts the correct payload", async () => {
postMock.mockImplementation((_url, _payload, _config) => { return validResponse; });
postMock.mockImplementation((_url, _payload, _config) => {
return validResponse;
});

await fogbugzClient.createCase(title, project, text, category);

expect(postMock)
.toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
token: "sometoken",
cmd: "new",
sTitle: title,
sProject: project,
sCategory: category,
sEvent: text,
fRichText: 1
}),
expect.anything()
);
expect(postMock).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
token: "sometoken",
cmd: "new",
sTitle: title,
sProject: project,
sCategory: category,
sEvent: text,
fRichText: 1
}),
expect.anything()
);
});

it("returns the case number if case is created", async () => {
postMock.mockImplementation((_url, _payload, _config) => { return validResponse; });
postMock.mockImplementation((_url, _payload, _config) => {
return validResponse;
});

const result = await fogbugzClient.createCase(title, project, text, category);

Expand All @@ -81,8 +85,10 @@ describe("createCase", () => {
warnings: ["warning"],
data: {}
}
}
postMock.mockImplementation((_url, _payload, _config) => { return invalidResponse; });
};
postMock.mockImplementation((_url, _payload, _config) => {
return invalidResponse;
});

const result = await fogbugzClient.createCase(title, project, text, category);

Expand All @@ -93,7 +99,9 @@ describe("createCase", () => {

it("returns an error if the response is not 200", async () => {
validResponse.status = 500;
postMock.mockImplementation((_url, _payload, _config) => { return validResponse; });
postMock.mockImplementation((_url, _payload, _config) => {
return validResponse;
});

const result = await fogbugzClient.createCase(title, project, text, category);

Expand All @@ -103,7 +111,9 @@ describe("createCase", () => {
});

it("returns an error if there is an eunexpected error", async () => {
postMock.mockImplementation((_url, _payload, _config) => { throw new Error("just no"); });
postMock.mockImplementation((_url, _payload, _config) => {
throw new Error("just no");
});

const result = await fogbugzClient.createCase(title, project, text, category);

Expand Down
23 changes: 9 additions & 14 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
/**
* Unit tests for the action's entrypoint, src/index.js
*/
const { run } = require("../src/main");

const { run } = require('../src/main')

// Mock the action's entrypoint
jest.mock('../src/main', () => ({
jest.mock("../ src / main", () => ({
run: jest.fn()
}))
}));

describe('index', () => {
it('calls run when imported', async () => {
require('../src/index')
describe("index", () => {
it("calls run when imported", async () => {
require("../src/index");

expect(run).toHaveBeenCalled()
})
})
expect(run).toHaveBeenCalled();
});
});
112 changes: 65 additions & 47 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Unit tests for the action's main functionality, src/main.js
* Unit tests for the action"s main functionality, src/main.js
*/
const core = require('@actions/core');
const core = require("@actions/core");
const github = require("@actions/github");

const FogBugzClient = require("../src/fogbugz-client");
Expand All @@ -12,69 +12,69 @@ const PlanviewClient = require("../src/planview-client");
jest.mock("../src/planview-client", () => jest.fn());
const createCard = jest.fn();

const dependabotPr = require("./dependabot_pr_sample.json");
const regularPr = require("./regular_pr_sample.json");
const syncPr = require("./sync_pr_sample.json");
const notAPr = require("./not_a_pr_sample.json");
const main = require('../src/main');
const dependabotPr = require("./dependabot_pr_sample");
const regularPr = require("./regular_pr_sample");
const syncPr = require("./sync_pr_sample");
const notAPr = require("./not_a_pr_sample");
const main = require("../src/main");

const debugMock = jest.spyOn(core, 'debug').mockImplementation();
const getInputMock = jest.spyOn(core, 'getInput').mockImplementation();
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation();
const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation();
const debugMock = jest.spyOn(core, "debug").mockImplementation();
const getInputMock = jest.spyOn(core, "getInput").mockImplementation();
const setFailedMock = jest.spyOn(core, "setFailed").mockImplementation();
const setOutputMock = jest.spyOn(core, "setOutput").mockImplementation();

const invalidFogBugzResponse = { success: false, error: "hi" };
const validFogBugzResponse = { success: true, case: { ixBug: 123 } };

const invalidPlanviewResponse = { success: false, result: "hi" };
const validPlanviewResponse = { success: true, data: { id: 42 } };

describe('action', () => {
describe("action", () => {
let debugMessages = [];

beforeEach(() => {
jest.clearAllMocks();

debugMessages = [];
debugMock.mockImplementation(msg => {
debugMessages.push(msg)
debugMessages.push(msg);
});

getInputMock.mockImplementation(name => {
switch (name) {
case "fogbugz_api_url":
return "https://my.fb.com/api"
return "https://my.fb.com/api";
case "fogbugz_token":
return "myfbtoken"
return "myfbtoken";
case "fogbugz_project":
return "My Project"
return "My Project";
case "fogbugz_subproject":
return "My Subproject"
return "My Subproject";
case "fogbugz_category":
return "My Category"
return "My Category";
case "planview_api_url":
return "https://my.pv.com/io"
return "https://my.pv.com/io";
case "planview_auth":
return "myplanviewauth"
return "myplanviewauth";
case "planview_board_id":
return 123456
return 123456;
case "planview_lane_id":
return 654321
return 654321;
case "planview_type_id":
return 987654
return 987654;
case "users":
return "dependabot[bot]"
return "dependabot[bot]";
default:
return ""
return "";
}
});

FogBugzClient.mockImplementation((_baseUrl, _token) => {
return { createCase: createCase };
return { createCase };
});

PlanviewClient.mockImplementation((_baseUrl, _auth) => {
return { createCard: createCard };
return { createCard };
});
});

Expand Down Expand Up @@ -125,9 +125,14 @@ describe('action', () => {

await main.run();

expect(createCase).toHaveBeenCalledWith("My Subproject - Dependabot PR title", "My Project", "Dependabot PR body", "My Category");
expect(createCase).toHaveBeenCalledWith(
"My Subproject - Dependabot PR title",
"My Project",
"Dependabot PR body",
"My Category"
);
expect(debugMessages).toContain("Creating FB case for My Subproject - Dependabot PR title");
expect(debugMessages).toContain("fbt_result: \"myresult\"");
expect(debugMessages).toContain('fbt_result: "myresult"');
});

it("fails if the FogBugz case isn't created", async () => {
Expand Down Expand Up @@ -165,9 +170,15 @@ describe('action', () => {

await main.run();

expect(createCard).toHaveBeenCalledWith(123456, 654321, 987654,
"My Subproject - Dependabot PR title", 123, "https://github.com/MyOrg/my_repo/pull/123");
expect(debugMessages).toContain("pvc_result: \"myresult\"");
expect(createCard).toHaveBeenCalledWith(
123456,
654321,
987654,
"My Subproject - Dependabot PR title",
123,
"https://github.com/MyOrg/my_repo/pull/123"
);
expect(debugMessages).toContain('pvc_result: "myresult"');
});

it("fails if the card can't be created", async () => {
Expand All @@ -190,8 +201,10 @@ describe('action', () => {
});

it("handles and unecpected exception", async () => {
error = new Error("just no");
getInputMock.mockImplementation(_name => { throw error });
const error = new Error("just no");
getInputMock.mockImplementation(_name => {
throw error;
});

await main.run();

Expand All @@ -203,32 +216,37 @@ describe('action', () => {
getInputMock.mockImplementation(name => {
switch (name) {
case "fogbugz_api_url":
return "https://my.fb.com/api"
return "https://my.fb.com/api";
case "fogbugz_token":
return "myfbtoken"
return "myfbtoken";
case "fogbugz_project":
return "My Project"
return "My Project";
case "fogbugz_category":
return "My Category"
return "My Category";
case "planview_api_url":
return "https://my.pv.com/io"
return "https://my.pv.com/io";
case "planview_auth":
return "myplanviewauth"
return "myplanviewauth";
case "planview_board_id":
return 123456
return 123456;
case "planview_lane_id":
return 654321
return 654321;
case "planview_type_id":
return 987654
return 987654;
case "users":
return "dependabot[bot]"
return "dependabot[bot]";
default:
return ""
return "";
}
});

await main.run();

expect(createCase).toHaveBeenCalledWith("Dependabot PR title", "My Project", "Dependabot PR body", "My Category");
expect(createCase).toHaveBeenCalledWith(
"Dependabot PR title",
"My Project",
"Dependabot PR body",
"My Category"
);
});
})
});
Loading

0 comments on commit 7153d6a

Please sign in to comment.