mirror of
https://github.com/tiennm99/github-readme-stats.git
synced 2026-08-13 18:23:04 +00:00
fix: fetch all repos for for the stats card (#2100)
* fetch all stars * stop fetching when there are repos with zero stars * remove not needed parameters from the query * add docstring * removed not needed mock * style: update formatting Co-authored-by: rickstaa <rick.staa@outlook.com>
This commit is contained in:
co-authored by
rickstaa
parent
acbc03dc0f
commit
af97e5765b
@@ -19,13 +19,61 @@ const data = {
|
||||
followers: { totalCount: 100 },
|
||||
repositories: {
|
||||
totalCount: 5,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const firstRepositoriesData = {
|
||||
data: {
|
||||
user: {
|
||||
repositories: {
|
||||
nodes: [
|
||||
{ name: "test-repo-1", stargazers: { totalCount: 100 } },
|
||||
{ name: "test-repo-2", stargazers: { totalCount: 100 } },
|
||||
{ name: "test-repo-3", stargazers: { totalCount: 100 } },
|
||||
],
|
||||
pageInfo: {
|
||||
hasNextPage: true,
|
||||
cursor: "cursor",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const secondRepositoriesData = {
|
||||
data: {
|
||||
user: {
|
||||
repositories: {
|
||||
nodes: [
|
||||
{ name: "test-repo-4", stargazers: { totalCount: 50 } },
|
||||
{ name: "test-repo-5", stargazers: { totalCount: 50 } },
|
||||
],
|
||||
pageInfo: {
|
||||
hasNextPage: false,
|
||||
cursor: "cursor",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const repositoriesWithZeroStarsData = {
|
||||
data: {
|
||||
user: {
|
||||
repositories: {
|
||||
nodes: [
|
||||
{ name: "test-repo-1", stargazers: { totalCount: 100 } },
|
||||
{ name: "test-repo-2", stargazers: { totalCount: 100 } },
|
||||
{ name: "test-repo-3", stargazers: { totalCount: 100 } },
|
||||
{ name: "test-repo-4", stargazers: { totalCount: 0 } },
|
||||
{ name: "test-repo-5", stargazers: { totalCount: 0 } },
|
||||
],
|
||||
pageInfo: {
|
||||
hasNextPage: true,
|
||||
cursor: "cursor",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -44,14 +92,22 @@ const error = {
|
||||
|
||||
const mock = new MockAdapter(axios);
|
||||
|
||||
beforeEach(() => {
|
||||
mock
|
||||
.onPost("https://api.github.com/graphql")
|
||||
.replyOnce(200, data)
|
||||
.onPost("https://api.github.com/graphql")
|
||||
.replyOnce(200, firstRepositoriesData)
|
||||
.onPost("https://api.github.com/graphql")
|
||||
.replyOnce(200, secondRepositoriesData);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mock.reset();
|
||||
});
|
||||
|
||||
describe("Test fetchStats", () => {
|
||||
it("should fetch correct stats", async () => {
|
||||
mock.onPost("https://api.github.com/graphql").reply(200, data);
|
||||
|
||||
let stats = await fetchStats("anuraghazra");
|
||||
const rank = calculateRank({
|
||||
totalCommits: 100,
|
||||
@@ -74,7 +130,38 @@ describe("Test fetchStats", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should stop fetching when there are repos with zero stars", async () => {
|
||||
mock.reset();
|
||||
mock
|
||||
.onPost("https://api.github.com/graphql")
|
||||
.replyOnce(200, data)
|
||||
.onPost("https://api.github.com/graphql")
|
||||
.replyOnce(200, repositoriesWithZeroStarsData);
|
||||
|
||||
let stats = await fetchStats("anuraghazra");
|
||||
const rank = calculateRank({
|
||||
totalCommits: 100,
|
||||
totalRepos: 5,
|
||||
followers: 100,
|
||||
contributions: 61,
|
||||
stargazers: 300,
|
||||
prs: 300,
|
||||
issues: 200,
|
||||
});
|
||||
|
||||
expect(stats).toStrictEqual({
|
||||
contributedTo: 61,
|
||||
name: "Anurag Hazra",
|
||||
totalCommits: 100,
|
||||
totalIssues: 200,
|
||||
totalPRs: 300,
|
||||
totalStars: 300,
|
||||
rank,
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw error", async () => {
|
||||
mock.reset();
|
||||
mock.onPost("https://api.github.com/graphql").reply(200, error);
|
||||
|
||||
await expect(fetchStats("anuraghazra")).rejects.toThrow(
|
||||
@@ -83,8 +170,6 @@ describe("Test fetchStats", () => {
|
||||
});
|
||||
|
||||
it("should fetch and add private contributions", async () => {
|
||||
mock.onPost("https://api.github.com/graphql").reply(200, data);
|
||||
|
||||
let stats = await fetchStats("anuraghazra", true);
|
||||
const rank = calculateRank({
|
||||
totalCommits: 150,
|
||||
@@ -108,7 +193,6 @@ describe("Test fetchStats", () => {
|
||||
});
|
||||
|
||||
it("should fetch total commits", async () => {
|
||||
mock.onPost("https://api.github.com/graphql").reply(200, data);
|
||||
mock
|
||||
.onGet("https://api.github.com/search/commits?q=author:anuraghazra")
|
||||
.reply(200, { total_count: 1000 });
|
||||
@@ -136,7 +220,6 @@ describe("Test fetchStats", () => {
|
||||
});
|
||||
|
||||
it("should exclude stars of the `test-repo-1` repository", async () => {
|
||||
mock.onPost("https://api.github.com/graphql").reply(200, data);
|
||||
mock
|
||||
.onGet("https://api.github.com/search/commits?q=author:anuraghazra")
|
||||
.reply(200, { total_count: 1000 });
|
||||
|
||||
Reference in New Issue
Block a user