Skip to content

Commit c9c8f88

Browse files
committed
test(api-streak-theme): create specific integration test for theme parameter
1 parent b251324 commit c9c8f88

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

app/api/streak/tests/theme.test.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { describe, it, expect, vi, beforeEach } from 'vitest';
3+
import { GET } from '../route';
4+
import { fetchGitHubContributions, getOrgDashboardData } from '../../../../lib/github';
5+
import {
6+
getSecondsUntilUTCMidnight,
7+
getSecondsUntilMidnightInTimezone,
8+
} from '../../../../utils/time';
9+
import type { ContributionCalendar, ExtendedContributionData } from '../../../../types';
10+
11+
vi.mock('../../../../lib/github', () => ({
12+
fetchGitHubContributions: vi.fn(),
13+
getOrgDashboardData: vi.fn(),
14+
}));
15+
16+
vi.mock('../../../../utils/time', () => ({
17+
getSecondsUntilUTCMidnight: vi.fn(),
18+
getSecondsUntilMidnightInTimezone: vi.fn(),
19+
}));
20+
21+
const mockCalendar: ContributionCalendar = {
22+
totalContributions: 10,
23+
weeks: [
24+
{
25+
contributionDays: [
26+
{ contributionCount: 1, date: '2024-06-10' },
27+
{ contributionCount: 2, date: '2024-06-11' },
28+
{ contributionCount: 0, date: '2024-06-12' },
29+
{ contributionCount: 3, date: '2024-06-13' },
30+
{ contributionCount: 1, date: '2024-06-14' },
31+
{ contributionCount: 0, date: '2024-06-15' },
32+
{ contributionCount: 3, date: '2024-06-16' },
33+
],
34+
},
35+
],
36+
};
37+
38+
function makeRequest(params: Record<string, string> = {}): Request {
39+
const url = new URL('http://localhost/api/streak');
40+
for (const [key, value] of Object.entries(params)) {
41+
url.searchParams.set(key, value);
42+
}
43+
return new Request(url.toString());
44+
}
45+
46+
describe('Streak API - theme parameter integration tests', () => {
47+
beforeEach(() => {
48+
vi.clearAllMocks();
49+
vi.mocked(fetchGitHubContributions).mockResolvedValue({
50+
calendar: mockCalendar,
51+
repoContributions: [],
52+
} as unknown as ExtendedContributionData);
53+
vi.mocked(getOrgDashboardData).mockResolvedValue({
54+
profile: {},
55+
stats: {},
56+
calendar: mockCalendar,
57+
} as any);
58+
vi.mocked(getSecondsUntilUTCMidnight).mockReturnValue(3600);
59+
vi.mocked(getSecondsUntilMidnightInTimezone).mockReturnValue(7200);
60+
});
61+
62+
it('should return 200 OK and render dark theme by default', async () => {
63+
const response = await GET(makeRequest({ user: 'octocat', theme: 'dark' }));
64+
expect(response.status).toBe(200);
65+
const body = await response.text();
66+
expect(body).toContain('<svg');
67+
});
68+
69+
it('should return 200 OK and render light theme when theme is light', async () => {
70+
const response = await GET(makeRequest({ user: 'octocat', theme: 'light' }));
71+
expect(response.status).toBe(200);
72+
const body = await response.text();
73+
expect(body).toContain('<svg');
74+
});
75+
76+
it('should return 200 OK when theme is auto', async () => {
77+
const response = await GET(makeRequest({ user: 'octocat', theme: 'auto' }));
78+
expect(response.status).toBe(200);
79+
const body = await response.text();
80+
expect(body).toContain('<svg');
81+
});
82+
83+
it('should return 200 OK when theme is random', async () => {
84+
const response = await GET(makeRequest({ user: 'octocat', theme: 'random' }));
85+
expect(response.status).toBe(200);
86+
const body = await response.text();
87+
expect(body).toContain('<svg');
88+
});
89+
90+
it('should return 400 Bad Request when theme parameter is invalid', async () => {
91+
const response = await GET(makeRequest({ user: 'octocat', theme: 'not-a-valid-theme' }));
92+
expect(response.status).toBe(400);
93+
const body = await response.json();
94+
expect(body.error).toBe('Invalid parameters');
95+
expect(body.details.fieldErrors.theme[0]).toContain('Invalid theme');
96+
});
97+
98+
it('should produce different SVGs when theme is dark vs light', async () => {
99+
const resDark = await GET(makeRequest({ user: 'octocat', theme: 'dark' }));
100+
const resLight = await GET(makeRequest({ user: 'octocat', theme: 'light' }));
101+
102+
expect(resDark.status).toBe(200);
103+
expect(resLight.status).toBe(200);
104+
105+
const svgDark = await resDark.text();
106+
const svgLight = await resLight.text();
107+
108+
expect(svgDark).not.toEqual(svgLight);
109+
});
110+
});

0 commit comments

Comments
 (0)