Skip to content

Commit eb445fa

Browse files
committed
test: fix tests
1 parent afb417b commit eb445fa

File tree

3 files changed

+32
-55
lines changed

3 files changed

+32
-55
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ You can specify a year and fetch only the commits that were made in that year by
137137
```md
138138
![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&year=2020)
139139
```
140+
140141
### Showing icons
141142

142143
To enable icons, you can pass `show_icons=true` in the query param, like so:

src/fetchers/stats-fetcher.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ const fetcher = (variables, token) => {
2525
return request(
2626
{
2727
query: `
28-
query userInfo($login: String!, $starttime: DateTime!) {
28+
query userInfo($login: String!${
29+
variables.starttime ? ", $starttime: DateTime!" : ""
30+
}) {
2931
user(login: $login) {
3032
name
3133
login
32-
contributionsCollection(from: $starttime) {
34+
contributionsCollection${
35+
variables.starttime ? "(from: $starttime)" : ""
36+
} {
3337
totalCommitContributions
3438
restrictedContributionsCount
3539
}
@@ -208,9 +212,9 @@ async function fetchStats(
208212
rank: { level: "C", score: 0 },
209213
};
210214

211-
let res = await retryer(fetcher, {
212-
login: username,
213-
starttime: year ? `${year}-01-01T00:00:00Z` : undefined
215+
let res = await retryer(fetcher, {
216+
login: username,
217+
starttime: year ? `${year}-01-01T00:00:00Z` : undefined,
214218
});
215219

216220
// Catch GraphQL errors.

tests/fetchStats.test.js

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const data = {
2424
},
2525
};
2626

27+
const dataYear = JSON.parse(JSON.stringify(data));
28+
dataYear.data.user.contributionsCollection.totalCommitContributions = 2003;
29+
dataYear.data.user.contributionsCollection.restrictedContributionsCount = 3;
30+
2731
const firstRepositoriesData = {
2832
data: {
2933
user: {
@@ -92,10 +96,23 @@ const error = {
9296

9397
const mock = new MockAdapter(axios);
9498

99+
/**
100+
* Mocks the GraphQL API based on certain conditions.
101+
*
102+
* @param {*} config Axios config object.
103+
* @returns Axios response object.
104+
*/
105+
const mockData = (config) => {
106+
// If year is 2003, return dataYear.
107+
if (config.data.includes("contributionsCollection(from: $starttime)"))
108+
return [200, dataYear];
109+
return [200, data];
110+
};
111+
95112
beforeEach(() => {
96113
mock
97114
.onPost("https://api.github.com/graphql")
98-
.replyOnce(200, data)
115+
.replyOnce(mockData)
99116
.onPost("https://api.github.com/graphql")
100117
.replyOnce(200, firstRepositoriesData);
101118
// .onPost("https://api.github.com/graphql") // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
@@ -254,61 +271,15 @@ describe("Test fetchStats", () => {
254271
});
255272
});
256273

257-
it("should get present year commits when provide no year", async () => {
258-
const data2003 = {...data, data:
259-
{...data.data, user:
260-
{...data.data.user, contributionsCollection: {
261-
totalCommitContributions: 2003,
262-
restrictedContributionsCount: 3,
263-
}}}}
264-
mock.onPost("https://api.github.com/graphql").reply((cfg) => {
265-
if (cfg.data.includes("contributionsCollection(from: 2003-01-01T00:00:00Z)"))
266-
return [200, data2003];
267-
return [200, data];
268-
});
269-
270-
let stats = await fetchStats("anuraghazra", true, false, []);
271-
const rank = calculateRank({
272-
totalCommits: 150,
273-
totalRepos: 5,
274-
followers: 100,
275-
contributions: 61,
276-
stargazers: 400,
277-
prs: 300,
278-
issues: 200,
279-
});
280-
281-
expect(stats).toStrictEqual({
282-
contributedTo: 61,
283-
name: "Anurag Hazra",
284-
totalCommits: 150,
285-
totalIssues: 200,
286-
totalPRs: 300,
287-
totalStars: 400,
288-
rank,
289-
});
290-
});
291-
292274
it("should get commits of provided year", async () => {
293-
const data2003 = {...data, data:
294-
{...data.data, user:
295-
{...data.data.user, contributionsCollection: {
296-
totalCommitContributions: 2003,
297-
restrictedContributionsCount: 3,
298-
}}}}
299-
mock.onPost("https://api.github.com/graphql").reply((cfg) => {
300-
if (cfg.data.includes(`"starttime":"2003-01-01T00:00:00Z"`))
301-
return [200, data2003];
302-
return [200, data];
303-
});
304-
305275
let stats = await fetchStats("anuraghazra", true, false, [], 2003);
306276
const rank = calculateRank({
307277
totalCommits: 2006,
308278
totalRepos: 5,
309279
followers: 100,
310280
contributions: 61,
311-
stargazers: 400,
281+
// stargazers: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
282+
stargazers: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
312283
prs: 300,
313284
issues: 200,
314285
});
@@ -319,7 +290,8 @@ describe("Test fetchStats", () => {
319290
totalCommits: 2006,
320291
totalIssues: 200,
321292
totalPRs: 300,
322-
totalStars: 400,
293+
// totalStars: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
294+
totalStars: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
323295
rank,
324296
});
325297
});

0 commit comments

Comments
 (0)