Skip to content

Commit a120dcf

Browse files
authored
test(contributors): add mock integration coverage (JhaSourav07#3084)
## Description Fixes JhaSourav07#2861 Added isolated unit and integration tests for `app/contributors/page.tsx` focusing on asynchronous service mocking, data propagation, and fallback handling. ### Changes - Added `app/contributors/page.mock-integrations.test.tsx` - Mocked `ContributorsClient` to isolate server-side page logic - Verified successful rendering using mocked asynchronous service responses - Tested contributor data propagation from the service layer to the client component - Verified computed `totalContributions` values are passed correctly - Confirmed `topContributors` collections are generated and forwarded properly - Tested fallback behavior when contributor endpoint requests fail - Improved coverage around asynchronous data loading and integration boundaries ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview N/A (test-only changes) ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally. - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have starred the repo. - [x] I have made sure that I have only one commit to merge in this PR. - [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no SVG changes made). - [ ] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents 5494de2 + f338efb commit a120dcf

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// app/contributors/page.mock-integrations.test.tsx
2+
3+
import { beforeEach, describe, expect, it, vi } from 'vitest';
4+
import { render, screen } from '@testing-library/react';
5+
import '@testing-library/jest-dom';
6+
7+
type ContributorsClientProps = {
8+
contributors: unknown[];
9+
totalContributions: number;
10+
topContributors: unknown[];
11+
};
12+
13+
const mockContributorsClient = vi.fn((_props?: ContributorsClientProps) => (
14+
<div data-testid="contributors-client">Contributors Client</div>
15+
));
16+
17+
vi.mock('./ContributorsClient', () => ({
18+
default: (props: ContributorsClientProps) => mockContributorsClient(props),
19+
}));
20+
21+
describe('ContributorsPage Mock Integrations', () => {
22+
beforeEach(() => {
23+
vi.clearAllMocks();
24+
});
25+
26+
it('renders successfully using mocked service data', async () => {
27+
const { default: ContributorsPage } = await import('./page');
28+
29+
const page = await ContributorsPage();
30+
31+
render(page);
32+
33+
expect(screen.getByTestId('contributors-client')).toBeInTheDocument();
34+
});
35+
36+
it('passes contributor data from mocked fetch layer', async () => {
37+
const { default: ContributorsPage } = await import('./page');
38+
39+
const page = await ContributorsPage();
40+
41+
render(page);
42+
43+
expect(mockContributorsClient).toHaveBeenCalled();
44+
45+
const props = mockContributorsClient.mock.calls[0][0] as ContributorsClientProps;
46+
47+
expect(Array.isArray(props.contributors)).toBe(true);
48+
});
49+
50+
it('passes computed contribution totals to client component', async () => {
51+
const { default: ContributorsPage } = await import('./page');
52+
53+
const page = await ContributorsPage();
54+
55+
render(page);
56+
57+
const props = mockContributorsClient.mock.calls[0][0] as ContributorsClientProps;
58+
59+
expect(typeof props.totalContributions).toBe('number');
60+
});
61+
62+
it('passes top contributors collection to client component', async () => {
63+
const { default: ContributorsPage } = await import('./page');
64+
65+
const page = await ContributorsPage();
66+
67+
render(page);
68+
69+
const props = mockContributorsClient.mock.calls[0][0] as ContributorsClientProps;
70+
71+
expect(Array.isArray(props.topContributors)).toBe(true);
72+
});
73+
74+
it('falls back to empty contributor data on failed endpoint responses', async () => {
75+
const originalFetch = global.fetch;
76+
77+
global.fetch = vi.fn().mockResolvedValue({
78+
ok: false,
79+
status: 500,
80+
headers: {
81+
get: () => null,
82+
},
83+
} as unknown as Response);
84+
85+
const { default: ContributorsPage } = await import('./page');
86+
87+
const page = await ContributorsPage();
88+
89+
render(page);
90+
91+
expect(screen.getByTestId('contributors-client')).toBeInTheDocument();
92+
93+
global.fetch = originalFetch;
94+
});
95+
});

0 commit comments

Comments
 (0)