|
| 1 | +import axios from "axios"; |
| 2 | +import { getUserJoinYear, fetchContributions, stats } from "../src/fetcher/stats"; |
| 3 | + |
| 4 | +jest.mock("axios"); |
| 5 | + |
| 6 | +const mockedAxios = axios as jest.Mocked<typeof axios>; |
| 7 | + |
| 8 | +describe("GitHub Stats Fetcher Test", () => { |
| 9 | + const username = "FajarKim"; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + jest.clearAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + it("should fetch the correct join year for the user", async () => { |
| 16 | + mockedAxios.post.mockResolvedValueOnce({ |
| 17 | + data: { |
| 18 | + data: { |
| 19 | + user: { |
| 20 | + createdAt: "2015-06-15T00:00:00Z", |
| 21 | + }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + }); |
| 25 | + |
| 26 | + const joinYear = await getUserJoinYear(username); |
| 27 | + expect(joinYear).toBe(2015); |
| 28 | + expect(mockedAxios.post).toHaveBeenCalledTimes(1); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should throw an error if user data is missing", async () => { |
| 32 | + mockedAxios.post.mockResolvedValueOnce({ |
| 33 | + data: { |
| 34 | + data: { |
| 35 | + user: null, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + await expect(getUserJoinYear(username)).rejects.toThrow("User data is missing."); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should throw an error if there are errors in the API response", async () => { |
| 44 | + mockedAxios.post.mockResolvedValueOnce({ |
| 45 | + data: { |
| 46 | + errors: [{ message: "Some error occurred" }], |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + await expect(getUserJoinYear(username)).rejects.toThrow("Some error occurred"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should fetch contributions for the specified year", async () => { |
| 54 | + mockedAxios.post.mockResolvedValueOnce({ |
| 55 | + data: { |
| 56 | + data: { |
| 57 | + user: { |
| 58 | + contributionsCollection: { |
| 59 | + totalCommitContributions: 100, |
| 60 | + restrictedContributionsCount: 10, |
| 61 | + totalPullRequestReviewContributions: 5, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + const contributions = await fetchContributions(username, 2022); |
| 69 | + expect(contributions.totalCommitContributions).toBe(100); |
| 70 | + expect(contributions.restrictedContributionsCount).toBe(10); |
| 71 | + expect(contributions.totalPullRequestReviewContributions).toBe(5); |
| 72 | + expect(mockedAxios.post).toHaveBeenCalledTimes(1); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should throw an error if there are errors in the API response", async () => { |
| 76 | + mockedAxios.post.mockResolvedValueOnce({ |
| 77 | + data: { |
| 78 | + errors: [{ message: "Some error occurred" }], |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + await expect(fetchContributions(username, 2022)).rejects.toThrow("Some error occurred"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should fetch user stats with aggregated contributions", async () => { |
| 86 | + mockedAxios.post.mockImplementation((config) => { |
| 87 | + if (config.data.query.includes("createdAt")) { |
| 88 | + return Promise.resolve({ |
| 89 | + data: { |
| 90 | + data: { |
| 91 | + user: { |
| 92 | + createdAt: "2019-01-01T00:00:00Z", |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }); |
| 97 | + } else if (config.data.query.includes("contributionsCollection")) { |
| 98 | + return Promise.resolve({ |
| 99 | + data: { |
| 100 | + data: { |
| 101 | + user: { |
| 102 | + contributionsCollection: { |
| 103 | + totalCommitContributions: 50, |
| 104 | + restrictedContributionsCount: 5, |
| 105 | + totalPullRequestReviewContributions: 2, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }); |
| 111 | + } else { |
| 112 | + return Promise.resolve({ |
| 113 | + data: { |
| 114 | + data: { |
| 115 | + user: { |
| 116 | + name: "Rangga Fajar Oktariansyah", |
| 117 | + login: "FajarKim", |
| 118 | + avatarUrl: "https://example.com/avatar.jpg", |
| 119 | + repositories: { totalCount: 10 }, |
| 120 | + followers: { totalCount: 100 }, |
| 121 | + following: { totalCount: 50 }, |
| 122 | + openedIssues: { totalCount: 20 }, |
| 123 | + closedIssues: { totalCount: 30 }, |
| 124 | + pullRequests: { totalCount: 15 }, |
| 125 | + mergedPullRequests: { totalCount: 10 }, |
| 126 | + discussionStarted: { totalCount: 5 }, |
| 127 | + discussionAnswered: { totalCount: 3 }, |
| 128 | + repositoriesContributedTo: { totalCount: 7 }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + const userStats = await stats(username); |
| 137 | + |
| 138 | + expect(userStats.name).toBe("Rangga Fajar Oktariansyah"); |
| 139 | + expect(userStats.totalCommitContributions).toBe(100); // 50 + 50 |
| 140 | + expect(userStats.restrictedContributionsCount).toBe(10); // 5 + 5 |
| 141 | + expect(userStats.totalPullRequestReviewContributions).toBe(4); // 2 + 2 |
| 142 | + expect(userStats.repositories.totalCount).toBe(10); |
| 143 | + expect(userStats.followers.totalCount).toBe(100); |
| 144 | + expect(userStats.following.totalCount).toBe(50); |
| 145 | + }); |
| 146 | +}); |
0 commit comments