From 2faadf008bf069c0e2d60224dbc246612afa01b3 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 6 Jan 2024 10:19:07 -0500 Subject: [PATCH 1/2] test: added end-to-end-style unit test --- src/index.test.ts | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/index.test.ts diff --git a/src/index.test.ts b/src/index.test.ts new file mode 100644 index 00000000..a8b9d3e8 --- /dev/null +++ b/src/index.test.ts @@ -0,0 +1,104 @@ +import { describe, expect, test, vi } from "vitest"; + +import { getAllContributorsForRepository } from "./index.js"; + +const mockRequest = (url: string) => { + switch (url) { + case "GET /repos/{owner}/{repo}/issues": + return { + data: [ + { + labels: ["type: bug"], + number: 111, + user: { + login: "AcceptedIssueUser", + }, + }, + ], + }; + case "GET /repos/{owner}/{repo}/issues/events": + return { + data: [ + { + actor: { + login: "IssueEventUser", + }, + event: "assigned", + issue: { + number: 222, + }, + }, + ], + }; + case "GET /search/issues": + return { + data: { + items: [ + { + number: 333, + title: "feat: Merged PR", + user: { + login: "MergedPullUser", + }, + }, + ], + }, + }; + case "GET /repos/{owner}/{repo}/events": + return { + data: [ + { + actor: { + login: "IssueEventUser", + }, + issue: { + number: 222, + }, + type: "PullRequestReviewEvent", + }, + ], + }; + } +}; + +vi.mock("octokit", () => ({ + get Octokit() { + return class MockOctokit { + static defaults = () => MockOctokit; + + request = mockRequest; + }; + }, +})); + +describe("end-to-end", () => { + test("getAllContributorsForRepository", async () => { + const actual = await getAllContributorsForRepository({ + owner: "TestOwner", + repo: "test-repository", + }); + + expect(actual).toMatchInlineSnapshot(` + { + "acceptedissueuser": { + "bug": [ + 111, + ], + }, + "issueeventuser": { + "maintenance": [ + 222, + ], + "review": [ + 222, + ], + }, + "mergedpulluser": { + "code": [ + 333, + ], + }, + } + `); + }); +}); From 6622e8f6d9b7275a79029044ce3b2af3a1b549f4 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 6 Jan 2024 10:21:15 -0500 Subject: [PATCH 2/2] Adjusted for spellchecker --- src/index.test.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index a8b9d3e8..b4296902 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -11,7 +11,7 @@ const mockRequest = (url: string) => { labels: ["type: bug"], number: 111, user: { - login: "AcceptedIssueUser", + login: "Accepted-Issue-User", }, }, ], @@ -21,7 +21,7 @@ const mockRequest = (url: string) => { data: [ { actor: { - login: "IssueEventUser", + login: "Issue-Event-User", }, event: "assigned", issue: { @@ -38,7 +38,7 @@ const mockRequest = (url: string) => { number: 333, title: "feat: Merged PR", user: { - login: "MergedPullUser", + login: "Merged-Pull-User", }, }, ], @@ -49,12 +49,12 @@ const mockRequest = (url: string) => { data: [ { actor: { - login: "IssueEventUser", + login: "Issue-Event-User", }, issue: { number: 222, }, - type: "PullRequestReviewEvent", + type: "Pull-Request-Review-Event", }, ], }; @@ -80,20 +80,17 @@ describe("end-to-end", () => { expect(actual).toMatchInlineSnapshot(` { - "acceptedissueuser": { + "accepted-issue-user": { "bug": [ 111, ], }, - "issueeventuser": { + "issue-event-user": { "maintenance": [ 222, ], - "review": [ - 222, - ], }, - "mergedpulluser": { + "merged-pull-user": { "code": [ 333, ],