Skip to content

Commit 4dc3d98

Browse files
test: improve GitHub API utility test coverage and fixes
1 parent b124f6b commit 4dc3d98

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

lib/github.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,50 @@ describe('GitHub API cache behavior', () => {
812812
expect(results.map((result) => result.totalContributions)).toEqual([42, 42, 42]);
813813
});
814814

815+
it('dedupes rapid synchronous contribution requests until the delayed fetch resolves once', async () => {
816+
vi.useFakeTimers();
817+
const resolveFetchSpy = vi.fn();
818+
819+
vi.mocked(fetch).mockImplementation(
820+
() =>
821+
new Promise<Response>((resolve) => {
822+
setTimeout(() => {
823+
resolveFetchSpy();
824+
resolve(
825+
mockResponse({
826+
data: {
827+
user: {
828+
contributionsCollection: { contributionCalendar: mockCalendar },
829+
},
830+
},
831+
})
832+
);
833+
}, 250);
834+
})
835+
);
836+
837+
const requests = [
838+
fetchGitHubContributions('octocat'),
839+
fetchGitHubContributions('octocat'),
840+
fetchGitHubContributions('octocat'),
841+
];
842+
843+
await Promise.resolve();
844+
845+
expect(fetch).toHaveBeenCalledTimes(1);
846+
expect(resolveFetchSpy).not.toHaveBeenCalled();
847+
848+
await vi.advanceTimersByTimeAsync(249);
849+
expect(resolveFetchSpy).not.toHaveBeenCalled();
850+
851+
await vi.advanceTimersByTimeAsync(1);
852+
853+
const results = await Promise.all(requests);
854+
855+
expect(resolveFetchSpy).toHaveBeenCalledTimes(1);
856+
expect(results.map((result) => result.totalContributions)).toEqual([42, 42, 42]);
857+
});
858+
815859
it('refresh bypass: bypassCache=true forces a fresh fetch', async () => {
816860
vi.mocked(fetch).mockImplementation(async () =>
817861
mockResponse({

0 commit comments

Comments
 (0)