|
| 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 massive scaling behavior', () => { |
| 13 | + beforeEach(() => { |
| 14 | + vi.clearAllMocks(); |
| 15 | + sessionStorage.clear(); |
| 16 | + mockUsePathname.mockReturnValue('/'); |
| 17 | + window.scrollTo = vi.fn(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('renders repeatedly without crashing under high mount volume', () => { |
| 21 | + expect(() => { |
| 22 | + for (let index = 0; index < 100; index++) { |
| 23 | + render(<ScrollRestoration />); |
| 24 | + } |
| 25 | + }).not.toThrow(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('handles many stored scroll positions without breaking restoration', () => { |
| 29 | + for (let index = 0; index < 500; index++) { |
| 30 | + sessionStorage.setItem(`scroll-position-/page-${index}`, String(index)); |
| 31 | + } |
| 32 | + |
| 33 | + mockUsePathname.mockReturnValue('/page-499'); |
| 34 | + |
| 35 | + render(<ScrollRestoration />); |
| 36 | + |
| 37 | + expect(window.scrollTo).toHaveBeenCalledWith(0, 499); |
| 38 | + }); |
| 39 | + |
| 40 | + it('stores scroll position correctly after many scroll events', () => { |
| 41 | + render(<ScrollRestoration />); |
| 42 | + |
| 43 | + for (let index = 0; index < 100; index++) { |
| 44 | + Object.defineProperty(window, 'scrollY', { |
| 45 | + value: index, |
| 46 | + configurable: true, |
| 47 | + }); |
| 48 | + |
| 49 | + window.dispatchEvent(new Event('scroll')); |
| 50 | + } |
| 51 | + |
| 52 | + expect(sessionStorage.getItem('scroll-position-/')).toBe('99'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('keeps pathname-specific keys isolated at scale', () => { |
| 56 | + for (let index = 0; index < 50; index++) { |
| 57 | + mockUsePathname.mockReturnValue(`/dashboard-${index}`); |
| 58 | + |
| 59 | + const { unmount } = render(<ScrollRestoration />); |
| 60 | + |
| 61 | + Object.defineProperty(window, 'scrollY', { |
| 62 | + value: index * 10, |
| 63 | + configurable: true, |
| 64 | + }); |
| 65 | + |
| 66 | + window.dispatchEvent(new Event('scroll')); |
| 67 | + unmount(); |
| 68 | + } |
| 69 | + |
| 70 | + expect(sessionStorage.getItem('scroll-position-/dashboard-0')).toBe('0'); |
| 71 | + expect(sessionStorage.getItem('scroll-position-/dashboard-49')).toBe('490'); |
| 72 | + }); |
| 73 | + it('removes scroll listener safely after many unmount cycles', () => { |
| 74 | + const removeEventListenerSpy = vi.spyOn(window, 'removeEventListener'); |
| 75 | + |
| 76 | + for (let index = 0; index < 25; index++) { |
| 77 | + const { unmount } = render(<ScrollRestoration />); |
| 78 | + unmount(); |
| 79 | + } |
| 80 | + |
| 81 | + expect(removeEventListenerSpy).toHaveBeenCalledTimes(25); |
| 82 | + }); |
| 83 | +}); |
0 commit comments