Skip to content

Commit d7ad995

Browse files
Merge branch 'main' into fix/debounce-preview-requests-clean
2 parents accaba1 + 4d2e3db commit d7ad995

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)