Skip to content

Commit 4d2e3db

Browse files
authored
test(scroll-restoration): add empty fallback coverage (JhaSourav07#2999)
## Description Fixes JhaSourav07#2823 Added empty fallback test coverage for `app/components/ScrollRestoration.tsx`. ### Tests Added * Verifies component renders safely when no saved scroll position exists. * Verifies no scroll restoration occurs when session storage is empty. * Verifies saved scroll positions are restored correctly. * Verifies scroll events persist current scroll position. * Verifies pathname-specific storage keys are used safely. ## 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. * [ ] I have run `npm run format` and `npm run lint` locally and resolved all errors. * [x] My commits follow the Conventional Commits format. * [ ] I have updated `README.md` if I added a new theme or URL parameter. (Not applicable) * [x] I have starred 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. (Not applicable) * [ ] (Recommended) I joined the CommitPulse Discord community.
2 parents 22a94d5 + 89b42a5 commit 4d2e3db

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { render } from '@testing-library/react';
2+
import { beforeEach, describe, expect, it, vi } from 'vitest';
3+
4+
import ScrollRestoration from './ScrollRestoration';
5+
6+
const mockUsePathname = vi.fn();
7+
8+
vi.mock('next/navigation', () => ({
9+
usePathname: () => mockUsePathname(),
10+
}));
11+
12+
describe('ScrollRestoration empty fallback behavior', () => {
13+
beforeEach(() => {
14+
vi.clearAllMocks();
15+
sessionStorage.clear();
16+
mockUsePathname.mockReturnValue('/');
17+
window.scrollTo = vi.fn();
18+
});
19+
20+
it('renders null without crashing when no saved scroll position exists', () => {
21+
const { container } = render(<ScrollRestoration />);
22+
23+
expect(container.firstChild).toBeNull();
24+
});
25+
26+
it('does not scroll when sessionStorage has no saved position', () => {
27+
render(<ScrollRestoration />);
28+
29+
expect(window.scrollTo).not.toHaveBeenCalled();
30+
});
31+
32+
it('restores saved scroll position when value exists', () => {
33+
sessionStorage.setItem('scroll-position-/', '250');
34+
35+
render(<ScrollRestoration />);
36+
37+
expect(window.scrollTo).toHaveBeenCalledWith(0, 250);
38+
});
39+
40+
it('stores current scroll position on scroll event', () => {
41+
Object.defineProperty(window, 'scrollY', {
42+
value: 300,
43+
configurable: true,
44+
});
45+
46+
render(<ScrollRestoration />);
47+
48+
window.dispatchEvent(new Event('scroll'));
49+
50+
expect(sessionStorage.getItem('scroll-position-/')).toBe('300');
51+
});
52+
53+
it('uses pathname-specific storage keys for empty fallback safety', () => {
54+
mockUsePathname.mockReturnValue('/dashboard');
55+
56+
render(<ScrollRestoration />);
57+
58+
window.dispatchEvent(new Event('scroll'));
59+
60+
expect(sessionStorage.getItem('scroll-position-/dashboard')).toBeDefined();
61+
});
62+
});

0 commit comments

Comments
 (0)