Skip to content

Commit

Permalink
chore: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Aug 19, 2024
1 parent 9215562 commit c8c4039
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 54 deletions.
3 changes: 3 additions & 0 deletions tests/__mocks__/@octokit/graphql-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
validate: () => [],
}
3 changes: 0 additions & 3 deletions tests/__mocks__/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ export const db = factory({
url: String,
user: nullable(Object),
milestone: nullable(Object),
pull_request: {
html_url: String,
},
assignee: nullable({
avatar_url: String,
email: nullable(String),
Expand Down
55 changes: 34 additions & 21 deletions tests/__mocks__/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,38 @@ export const handlers = [
db.issue.update({ where: { owner: { login: { equals: owner as string } }, repo: { equals: repo as string }, id: { equals: Number(id) } }, data: { assignees: [] } });
return HttpResponse.json({ message: "Assignees removed" });
}),
http.get("https://api.github.com/search/issues", ({ request }) => {
const query = new URL(request.url);
const params = query.searchParams;
const q = params.get("q");

if (!q) {
return HttpResponse.json({ message: "No query" });
}

const issueNumber = q.match(/#(\d+)/)?.[1];
if (!issueNumber) {
return HttpResponse.json({ message: "No issue number" });
}

const issue = db.issue.findFirst({ where: { number: { equals: Number(issueNumber) } } });
if (!issue) {
return HttpResponse.json({ message: "Issue not found" });
}

return HttpResponse.json(issue);
})
http.post("https://api.github.com/graphql", () => {
return HttpResponse.json({
data: {
repository: {
issue: {
closedByPullRequestsReferences: {
edges: [
{
node: {
url: "https://github.com/ubiquity/test-repo/pull/1",
title: "test",
body: "test",
state: "OPEN",
number: 1,
author: { login: "ubiquity", id: 1 },
},
},
{
node: {
url: "https://github.com/ubiquity/test-repo/pull/1",
title: "test",
body: "test",
state: "CLOSED",
number: 2,
author: { login: "user2", id: 2 },
},
}
],
},
},
},
},
});
}),
];
1 change: 0 additions & 1 deletion tests/__mocks__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ export function createIssue(
number: id,
repo: repo || "test-repo",
body: body || "test",
pull_request: { html_url: `https://github.com/ubiquity/test-repo/pulls/${id}` },
});
}
85 changes: 56 additions & 29 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ describe("User start/stop", () => {
const infoSpy = jest.spyOn(context.logger, "info");
await runPlugin(context);

expect(infoSpy).toHaveBeenNthCalledWith(1, noAssignmentCommentFor(getIssueUrl(1)));
expect(infoSpy).toHaveBeenNthCalledWith(2, noAssignmentCommentFor(getIssueUrl(2)));
expect(infoSpy).toHaveBeenNthCalledWith(3, noAssignmentCommentFor(getIssueUrl(3)));
expect(infoSpy).toHaveBeenNthCalledWith(4, noAssignmentCommentFor(getIssueUrl(4)));
expect(infoSpy).toHaveBeenCalledTimes(4);
expect(infoSpy).toHaveBeenNthCalledWith(1, noAssignmentCommentFor(getIssueHtmlUrl(1)));
expect(infoSpy).toHaveBeenNthCalledWith(2, noAssignmentCommentFor(getIssueHtmlUrl(2)));
expect(infoSpy).toHaveBeenNthCalledWith(3, noAssignmentCommentFor(getIssueHtmlUrl(3)));
expect(infoSpy).toHaveBeenNthCalledWith(4, noAssignmentCommentFor(getIssueHtmlUrl(4)));
});

it("Should include the previously excluded repo", async () => {
Expand All @@ -69,29 +68,28 @@ describe("User start/stop", () => {
context.config.watch.optOut = [];
await runPlugin(context);

expect(infoSpy).toHaveBeenNthCalledWith(1, noAssignmentCommentFor(getIssueUrl(1)));
expect(infoSpy).toHaveBeenNthCalledWith(2, noAssignmentCommentFor(getIssueUrl(2)));
expect(infoSpy).toHaveBeenNthCalledWith(3, noAssignmentCommentFor(getIssueUrl(3)));
expect(infoSpy).toHaveBeenNthCalledWith(4, noAssignmentCommentFor(getIssueUrl(4)));
expect(infoSpy).toHaveBeenCalledTimes(4);
expect(infoSpy).toHaveBeenNthCalledWith(1, noAssignmentCommentFor(getIssueHtmlUrl(1)));
expect(infoSpy).toHaveBeenNthCalledWith(2, noAssignmentCommentFor(getIssueHtmlUrl(2)));
expect(infoSpy).toHaveBeenNthCalledWith(3, noAssignmentCommentFor(getIssueHtmlUrl(3)));
expect(infoSpy).toHaveBeenNthCalledWith(4, noAssignmentCommentFor(getIssueHtmlUrl(4)));
});

it("Should eject the user after the disqualification period", async () => {
const context = createContext(4, 2);
const infoSpy = jest.spyOn(context.logger, "info");

createComment(3, 3, STRINGS.BOT, "Bot", botAssignmentComment(2, daysPriorToNow(9)), daysPriorToNow(9));
const timestamp = daysPriorToNow(9);
createComment(3, 3, STRINGS.BOT, "Bot", botAssignmentComment(2, timestamp), timestamp);

const issue = db.issue.findFirst({ where: { id: { equals: 4 } } });
expect(issue?.assignees).toEqual([{ login: STRINGS.USER, id: 2 }]);

await runPlugin(context);

expect(infoSpy).toHaveBeenNthCalledWith(1, `Assignees mismatch found for ${getIssueUrl(1)}`, { caller: STRINGS.LOGS_ANON_CALLER, issue: [1], metadata: [2] });
expect(infoSpy).toHaveBeenNthCalledWith(2, `Passed the deadline on ${getIssueUrl(2)} and no activity is detected, removing assignees.`);
expect(infoSpy).toHaveBeenNthCalledWith(3, `Passed the deadline on ${getIssueUrl(3)} and no activity is detected, removing assignees.`);
expect(infoSpy).toHaveBeenNthCalledWith(4, `Passed the deadline on ${getIssueUrl(4)} and no activity is detected, removing assignees.`);
expect(infoSpy).toHaveBeenCalledTimes(4);
expect(infoSpy).toHaveBeenNthCalledWith(1, `Assignees mismatch found for ${getIssueHtmlUrl(1)}`, { caller: STRINGS.LOGS_ANON_CALLER, assigneeIds: [1], metadata: { taskDeadline: timestamp, taskAssignees: [2] } });
expect(infoSpy).toHaveBeenNthCalledWith(2, `Passed the deadline on ${getIssueHtmlUrl(1)} and no activity is detected, removing assignees.`);
expect(infoSpy).toHaveBeenNthCalledWith(3, `Passed the deadline on ${getIssueHtmlUrl(2)} and no activity is detected, removing assignees.`);
expect(infoSpy).toHaveBeenNthCalledWith(4, `Passed the deadline on ${getIssueHtmlUrl(3)} and no activity is detected, removing assignees.`);

const updatedIssue = db.issue.findFirst({ where: { id: { equals: 4 } } });
expect(updatedIssue?.assignees).toEqual([]);
Expand All @@ -101,15 +99,21 @@ describe("User start/stop", () => {
const context = createContext(4, 2);
const infoSpy = jest.spyOn(context.logger, "info");

createComment(3, 3, STRINGS.BOT, "Bot", botAssignmentComment(2, daysPriorToNow(5)), daysPriorToNow(5));
const timestamp = daysPriorToNow(5);

createComment(3, 3, STRINGS.BOT, "Bot", botAssignmentComment(2, timestamp), timestamp);

const issue = db.issue.findFirst({ where: { id: { equals: 4 } } });
expect(issue?.assignees).toEqual([{ login: STRINGS.USER, id: 2 }]);

await runPlugin(context);

expect(infoSpy).toHaveBeenNthCalledWith(1, `Assignees mismatch found for ${getIssueUrl(1)}`, { caller: STRINGS.LOGS_ANON_CALLER, issue: [1], metadata: [2] });
expect(infoSpy).toHaveBeenCalledTimes(1);
expect(infoSpy).toHaveBeenNthCalledWith(1, `Assignees mismatch found for ${getIssueHtmlUrl(1)}`, {
caller: STRINGS.LOGS_ANON_CALLER, assigneeIds: [1], metadata: {
taskDeadline: timestamp,
taskAssignees: [2],
}
});

const updatedIssue = db.issue.findFirst({ where: { id: { equals: 4 } } });
expect(updatedIssue?.assignees).toEqual([{ login: STRINGS.USER, id: 2 }]);
Expand All @@ -122,30 +126,53 @@ describe("User start/stop", () => {
const context = createContext(4, 2);
const infoSpy = jest.spyOn(context.logger, "info");

createComment(3, 3, STRINGS.BOT, "Bot", botAssignmentComment(2, daysPriorToNow(2)), daysPriorToNow(2));
const timestamp = daysPriorToNow(2)
createComment(3, 3, STRINGS.BOT, "Bot", botAssignmentComment(2, timestamp), timestamp);

const issue = db.issue.findFirst({ where: { id: { equals: 4 } } });
expect(issue?.assignees).toEqual([{ login: STRINGS.USER, id: 2 }]);

await runPlugin(context);

expect(infoSpy).toHaveBeenNthCalledWith(1, `Assignees mismatch found for ${getIssueUrl(1)}`, { caller: STRINGS.LOGS_ANON_CALLER, issue: [1], metadata: [2] });
expect(infoSpy).toHaveBeenNthCalledWith(2, `Nothing to do for ${getIssueHtmlUrl(2)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(4, `Nothing to do for ${getIssueHtmlUrl(3)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(6, `Nothing to do for ${getIssueHtmlUrl(4)}, still within due-time.`);
expect(infoSpy).toHaveBeenCalledTimes(7);
expect(infoSpy).toHaveBeenNthCalledWith(1, `Assignees mismatch found for ${getIssueHtmlUrl(1)}`, {
caller: STRINGS.LOGS_ANON_CALLER,
assigneeIds: [1],
metadata: {
taskDeadline: timestamp,
taskAssignees: [2],
}
});
expect(infoSpy).toHaveBeenNthCalledWith(2, `Nothing to do for ${getIssueHtmlUrl(1)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(4, `Nothing to do for ${getIssueHtmlUrl(2)}, still within due-time.`);
expect(infoSpy).toHaveBeenNthCalledWith(6, `Nothing to do for ${getIssueHtmlUrl(3)}, still within due-time.`);

const updatedIssue = db.issue.findFirst({ where: { id: { equals: 4 } } });
expect(updatedIssue?.assignees).toEqual([{ login: STRINGS.USER, id: 2 }]);
});

it("Should handle collecting linked PR with # format", async () => {
it("Should handle collecting linked PRs", async () => {
const context = createContext(1, 1);
const issue = db.issue.findFirst({ where: { id: { equals: 1 } } });
const result = await collectLinkedPullRequests(context, { issue_number: issue?.number as number, repo: issue?.repo as string, owner: issue?.owner.login as string });
expect(result).toHaveLength(1);
expect(result[0].number).toEqual(1);
expect(result[0].body).toMatch(/Resolves #1/gi);
expect(result).toHaveLength(2);
expect(result).toEqual([
{
url: "https://github.com/ubiquity/test-repo/pull/1",
title: "test",
body: "test",
state: "OPEN",
number: 1,
author: { login: "ubiquity", id: 1 },
},
{
url: "https://github.com/ubiquity/test-repo/pull/1",
title: "test",
body: "test",
state: "CLOSED",
number: 2,
author: { login: "user2", id: 2 },
}
]);
});
});

Expand Down

0 comments on commit c8c4039

Please sign in to comment.