Skip to content

Commit 897e228

Browse files
authored
test: add theme contrast tests for ScrollRestoration (JhaSourav07#3179)
## Description Fixes JhaSourav07#2825 Adds theme contrast test coverage for the ScrollRestoration component by verifying consistent behavior in both dark and light theme environments. The tests validate rendering, scroll restoration, and scroll position persistence across theme modes. ## 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 change) ## Checklist before requesting a review: * [x] I have read the `CONTRIBUTING.md` file. * [ ] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). * [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 started the repo. * [x] I have made sure that i have only one commit to merge in this PR. * [ ] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). * [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents 349ec76 + 5484319 commit 897e228

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { render } from '@testing-library/react';
2+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
3+
4+
import ScrollRestoration from './ScrollRestoration';
5+
6+
const mockUsePathname = vi.fn();
7+
const originalScrollY = window.scrollY;
8+
9+
vi.mock('next/navigation', () => ({
10+
usePathname: () => mockUsePathname(),
11+
}));
12+
13+
describe('ScrollRestoration theme contrast behavior', () => {
14+
beforeEach(() => {
15+
vi.clearAllMocks();
16+
sessionStorage.clear();
17+
18+
document.documentElement.classList.remove('dark');
19+
20+
mockUsePathname.mockReturnValue('/');
21+
22+
window.scrollTo = vi.fn();
23+
});
24+
afterEach(() => {
25+
Object.defineProperty(window, 'scrollY', {
26+
value: originalScrollY,
27+
configurable: true,
28+
});
29+
});
30+
31+
it('renders safely in dark theme mode', () => {
32+
document.documentElement.classList.add('dark');
33+
34+
const { container } = render(<ScrollRestoration />);
35+
36+
expect(container.firstChild).toBeNull();
37+
});
38+
39+
it('renders safely in light theme mode', () => {
40+
document.documentElement.classList.remove('dark');
41+
42+
const { container } = render(<ScrollRestoration />);
43+
44+
expect(container.firstChild).toBeNull();
45+
});
46+
47+
it('restores saved scroll position in dark theme mode', () => {
48+
document.documentElement.classList.add('dark');
49+
50+
sessionStorage.setItem('scroll-position-/', '400');
51+
52+
render(<ScrollRestoration />);
53+
54+
expect(window.scrollTo).toHaveBeenCalledWith(0, 400);
55+
});
56+
57+
it('restores saved scroll position in light theme mode', () => {
58+
document.documentElement.classList.remove('dark');
59+
60+
sessionStorage.setItem('scroll-position-/', '200');
61+
62+
render(<ScrollRestoration />);
63+
64+
expect(window.scrollTo).toHaveBeenCalledWith(0, 200);
65+
});
66+
67+
it('stores scroll position consistently regardless of theme', () => {
68+
document.documentElement.classList.add('dark');
69+
70+
Object.defineProperty(window, 'scrollY', {
71+
value: 500,
72+
configurable: true,
73+
});
74+
75+
render(<ScrollRestoration />);
76+
77+
window.dispatchEvent(new Event('scroll'));
78+
79+
expect(sessionStorage.getItem('scroll-position-/')).toBe('500');
80+
});
81+
});

0 commit comments

Comments
 (0)