Skip to content

Commit 4b15721

Browse files
author
Touch Sungkawichai
committed
add tests
1 parent d2c4dfd commit 4b15721

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

tests/fetchStats.test.js

+97
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ const data = {
3131
},
3232
};
3333

34+
const data2 = {
35+
data: {
36+
user: {
37+
name: "Anurag Hazra",
38+
repositoriesContributedTo: { totalCount: 61 },
39+
contributionsCollection: {
40+
totalCommitContributions: 1000,
41+
restrictedContributionsCount: 50,
42+
},
43+
pullRequests: { totalCount: 300 },
44+
openIssues: { totalCount: 100 },
45+
closedIssues: { totalCount: 100 },
46+
followers: { totalCount: 100 },
47+
repositories: {
48+
totalCount: 5,
49+
nodes: [
50+
{ name: "test-repo-1", stargazers: { totalCount: 100 } },
51+
{ name: "test-repo-2", stargazers: { totalCount: 100 } },
52+
{ name: "test-repo-3", stargazers: { totalCount: 100 } },
53+
{ name: "test-repo-4", stargazers: { totalCount: 50 } },
54+
{ name: "test-repo-5", stargazers: { totalCount: 50 } },
55+
],
56+
},
57+
},
58+
},
59+
};
60+
3461
const error = {
3562
errors: [
3663
{
@@ -162,4 +189,74 @@ describe("Test fetchStats", () => {
162189
rank,
163190
});
164191
});
192+
193+
it("should get present year commits when provide no year", async () => {
194+
const data2003 = {...data, data:
195+
{...data.data, user:
196+
{...data.data.user, contributionsCollection: {
197+
totalCommitContributions: 2003,
198+
restrictedContributionsCount: 3,
199+
}}}}
200+
mock.onPost("https://api.github.com/graphql").reply((cfg) => {
201+
if (cfg.data.includes("contributionsCollection(from: 2003-01-01T00:00:00Z)"))
202+
return [200, data2003];
203+
return [200, data];
204+
});
205+
206+
let stats = await fetchStats("anuraghazra", true, false, []);
207+
const rank = calculateRank({
208+
totalCommits: 150,
209+
totalRepos: 5,
210+
followers: 100,
211+
contributions: 61,
212+
stargazers: 400,
213+
prs: 300,
214+
issues: 200,
215+
});
216+
217+
expect(stats).toStrictEqual({
218+
contributedTo: 61,
219+
name: "Anurag Hazra",
220+
totalCommits: 150,
221+
totalIssues: 200,
222+
totalPRs: 300,
223+
totalStars: 400,
224+
rank,
225+
});
226+
});
227+
228+
it("should get commits of provided year", async () => {
229+
const data2003 = {...data, data:
230+
{...data.data, user:
231+
{...data.data.user, contributionsCollection: {
232+
totalCommitContributions: 2003,
233+
restrictedContributionsCount: 3,
234+
}}}}
235+
mock.onPost("https://api.github.com/graphql").reply((cfg) => {
236+
if (cfg.data.includes(`"starttime":"2003-01-01T00:00:00Z"`))
237+
return [200, data2003];
238+
return [200, data];
239+
});
240+
241+
let stats = await fetchStats("anuraghazra", true, false, [], 2003);
242+
const rank = calculateRank({
243+
totalCommits: 2006,
244+
totalRepos: 5,
245+
followers: 100,
246+
contributions: 61,
247+
stargazers: 400,
248+
prs: 300,
249+
issues: 200,
250+
});
251+
252+
expect(stats).toStrictEqual({
253+
contributedTo: 61,
254+
name: "Anurag Hazra",
255+
totalCommits: 2006,
256+
totalIssues: 200,
257+
totalPRs: 300,
258+
totalStars: 400,
259+
rank,
260+
});
261+
});
165262
});

0 commit comments

Comments
 (0)