Skip to content

Commit 01ea043

Browse files
committed
chore: update test file
1 parent a6027c5 commit 01ea043

File tree

1 file changed

+17
-82
lines changed

1 file changed

+17
-82
lines changed

tests/renderStatsCard.test.ts

Lines changed: 17 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ jest.mock("express", () => ({
1717
},
1818
}));
1919

20-
const exampleUserData: User = {
20+
type ExtendUser = User & {
21+
joinedAt: string;
22+
totalCommitsSinceJoined: number;
23+
}
24+
25+
const exampleUserData: ExtendUser = {
2126
name: "Fajar",
2227
login: "FajarKim",
2328
avatarUrl: "base64-encoded-image",
@@ -35,8 +40,11 @@ const exampleUserData: User = {
3540
restrictedContributionsCount: 130,
3641
totalPullRequestReviewContributions: 340,
3742
joinedAt: "2020-01-01T00:00:00Z",
43+
totalCommitsSinceJoined: 1500,
3844
};
3945

46+
(getData as jest.Mock).mockResolvedValueOnce(exampleUserData);
47+
4048
describe("Test GitHub Readme Profile API", () => {
4149
let mockRequest: any;
4250
let mockResponse: any;
@@ -69,17 +77,6 @@ describe("Test GitHub Readme Profile API", () => {
6977
expect(mockResponse.setHeader).toHaveBeenCalledWith("Cache-Control", "s-maxage=7200, stale-while-revalidate");
7078
});
7179

72-
it("should return an error response when GitHub API fails", async () => {
73-
mockRequest.query.username = "FajarKim";
74-
mockRequest.query.format = "json";
75-
(getData as jest.Mock).mockRejectedValueOnce(new Error("GitHub API error"));
76-
77-
await readmeStats(mockRequest, mockResponse);
78-
79-
expect(mockResponse.status).toHaveBeenCalledWith(500);
80-
expect(mockResponse.send).toHaveBeenCalledWith("Error fetching data from GitHub API");
81-
});
82-
8380
it("should handle request and generate SVG response", async () => {
8481
mockRequest.query.username = "FajarKim";
8582
mockRequest.query.format = "svg";
@@ -161,80 +158,18 @@ describe("Test GitHub Readme Profile API", () => {
161158
expect(mockResponse.json).not.toHaveBeenCalled();
162159
});
163160

164-
it("should validate User data conforms to interface", async () => {
165-
mockRequest.query.username = "FajarKim";
166-
(getData as jest.Mock).mockResolvedValueOnce(exampleUserData);
167-
168-
await readmeStats(mockRequest, mockResponse);
169-
170-
const userData = mockResponse.json.mock.calls[0][0];
171-
expect(userData).toHaveProperty("name");
172-
expect(userData).toHaveProperty("login");
173-
expect(userData).toHaveProperty("avatarUrl");
174-
expect(userData.repositories.totalCount).toBeGreaterThanOrEqual(0);
175-
});
176-
177-
it("should handle user with zero contributions", async () => {
178-
const zeroContributionsData = {
179-
...exampleUserData,
180-
totalCommitContributions: 0,
181-
restrictedContributionsCount: 0,
182-
totalPullRequestReviewContributions: 0,
183-
};
184-
185-
(getData as jest.Mock).mockResolvedValueOnce(zeroContributionsData);
186-
187-
mockRequest.query.username = "FajarKim";
188-
189-
await readmeStats(mockRequest, mockResponse);
190-
191-
expect(mockResponse.json).toHaveBeenCalledWith(zeroContributionsData);
192-
});
193-
194-
it("should handle very large contribution data without errors", async () => {
195-
const highContributionsData = {
196-
...exampleUserData,
197-
totalCommitContributions: 100000,
198-
restrictedContributionsCount: 50000,
199-
totalPullRequestReviewContributions: 30000,
200-
};
201-
202-
(getData as jest.Mock).mockResolvedValueOnce(highContributionsData);
203-
204-
mockRequest.query.username = "FajarKim";
205-
206-
await readmeStats(mockRequest, mockResponse);
207-
208-
expect(mockResponse.json).toHaveBeenCalledWith(highContributionsData);
209-
});
210-
211-
it("should fetch the user's join date and calculate total commits from join date until now", async () => {
161+
it("should correctly fetch user join date and total commits since joining", async () => {
212162
mockRequest.query.username = "FajarKim";
213163
(getData as jest.Mock).mockResolvedValueOnce(exampleUserData);
214164

215-
const today = new Date("2023-10-26");
216-
const joinDate = new Date(exampleUserData.joinedAt);
217-
const totalDaysSinceJoining = Math.floor((today.getTime() - joinDate.getTime()) / (1000 * 60 * 60 * 24));
218-
219165
await readmeStats(mockRequest, mockResponse);
220166

221-
const responseData = mockResponse.json.mock.calls[0][0];
222-
expect(responseData.joinedAt).toEqual(exampleUserData.joinedAt);
223-
224-
const expectedTotalCommits = exampleUserData.totalCommitContributions + totalDaysSinceJoining;
225-
expect(responseData.totalCommitContributionsSinceJoining).toEqual(expectedTotalCommits);
226-
});
227-
228-
it("should handle missing join date gracefully", async () => {
229-
const incompleteUserData = { ...exampleUserData, joinedAt: undefined };
230-
(getData as jest.Mock).mockResolvedValueOnce(incompleteUserData);
231-
232-
mockRequest.query.username = "FajarKim";
233-
234-
await readmeStats(mockRequest, mockResponse);
235-
236-
const responseData = mockResponse.json.mock.calls[0][0];
237-
expect(responseData.joinedAt).toBeUndefined();
238-
expect(responseData.totalCommitContributionsSinceJoining).toBeUndefined();
167+
expect(getData).toHaveBeenCalledWith(mockRequest.query.username);
168+
expect(mockResponse.json).toHaveBeenCalledWith(
169+
expect.objectContaining({
170+
joinedAt: "2020-01-01T00:00:00Z",
171+
totalCommitsSinceJoined: 1500,
172+
})
173+
);
239174
});
240175
});

0 commit comments

Comments
 (0)