|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import { afterEach, beforeEach, describe, it, mock } from 'node:test'; |
| 3 | +import assert from 'node:assert/strict'; |
| 4 | + |
| 5 | +import useScrollToElement from '#site/hooks/client/useScrollToElement.js'; |
| 6 | +import { NavigationStateContext } from '#site/providers/navigationStateProvider'; |
| 7 | + |
| 8 | +describe('useScrollToElement', () => { |
| 9 | + let mockElement; |
| 10 | + let mockRef; |
| 11 | + let navigationState; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + navigationState = {}; |
| 15 | + |
| 16 | + mockElement = { |
| 17 | + scrollTop: 0, |
| 18 | + scrollLeft: 0, |
| 19 | + scroll: mock.fn(), |
| 20 | + addEventListener: mock.fn(), |
| 21 | + removeEventListener: mock.fn(), |
| 22 | + }; |
| 23 | + |
| 24 | + mockRef = { current: mockElement }; |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + mock.reset(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should handle scroll restoration with various scenarios', () => { |
| 32 | + const wrapper = ({ children }) => ( |
| 33 | + <NavigationStateContext.Provider value={navigationState}> |
| 34 | + {children} |
| 35 | + </NavigationStateContext.Provider> |
| 36 | + ); |
| 37 | + |
| 38 | + // Should restore scroll position on mount if saved state exists |
| 39 | + navigationState.sidebar = { x: 100, y: 200 }; |
| 40 | + const { unmount: unmount1 } = renderHook(() => useScrollToElement('sidebar', mockRef), { wrapper }); |
| 41 | + |
| 42 | + assert.equal(mockElement.scroll.mock.callCount(), 1); |
| 43 | + assert.deepEqual(mockElement.scroll.mock.calls[0].arguments, [ |
| 44 | + { top: 200, behavior: 'auto' }, |
| 45 | + ]); |
| 46 | + |
| 47 | + unmount1(); |
| 48 | + mock.reset(); |
| 49 | + mockElement.scroll = mock.fn(); |
| 50 | + |
| 51 | + // Should not restore if no saved state exists |
| 52 | + navigationState = {}; |
| 53 | + const { unmount: unmount2 } = renderHook(() => useScrollToElement('sidebar', mockRef), { wrapper }); |
| 54 | + assert.equal(mockElement.scroll.mock.callCount(), 0); |
| 55 | + |
| 56 | + unmount2(); |
| 57 | + mock.reset(); |
| 58 | + mockElement.scroll = mock.fn(); |
| 59 | + |
| 60 | + // Should not restore if current position matches saved state |
| 61 | + navigationState.sidebar = { x: 0, y: 0 }; |
| 62 | + mockElement.scrollTop = 0; |
| 63 | + const { unmount: unmount3 } = renderHook(() => useScrollToElement('sidebar', mockRef), { wrapper }); |
| 64 | + assert.equal(mockElement.scroll.mock.callCount(), 0); |
| 65 | + |
| 66 | + unmount3(); |
| 67 | + mock.reset(); |
| 68 | + mockElement.scroll = mock.fn(); |
| 69 | + |
| 70 | + // Should restore scroll to element that was outside viewport (deep scroll) |
| 71 | + navigationState.sidebar = { x: 0, y: 1500 }; |
| 72 | + mockElement.scrollTop = 0; |
| 73 | + renderHook(() => useScrollToElement('sidebar', mockRef), { wrapper }); |
| 74 | + |
| 75 | + assert.equal(mockElement.scroll.mock.callCount(), 1); |
| 76 | + assert.deepEqual(mockElement.scroll.mock.calls[0].arguments, [ |
| 77 | + { top: 1500, behavior: 'auto' }, |
| 78 | + ]); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should persist and restore scroll position across navigation', async () => { |
| 82 | + const wrapper = ({ children }) => ( |
| 83 | + <NavigationStateContext.Provider value={navigationState}> |
| 84 | + {children} |
| 85 | + </NavigationStateContext.Provider> |
| 86 | + ); |
| 87 | + |
| 88 | + // First render: user scrolls to position 800 |
| 89 | + const { unmount } = renderHook(() => useScrollToElement('sidebar', mockRef), { wrapper }); |
| 90 | + |
| 91 | + const scrollHandler = mockElement.addEventListener.mock.calls[0].arguments[1]; |
| 92 | + mockElement.scrollTop = 800; |
| 93 | + mockElement.scrollLeft = 0; |
| 94 | + scrollHandler(); |
| 95 | + |
| 96 | + // Wait for debounce |
| 97 | + await new Promise(resolve => setTimeout(resolve, 350)); |
| 98 | + |
| 99 | + // Position should be saved |
| 100 | + assert.deepEqual(navigationState.sidebar, { x: 0, y: 800 }); |
| 101 | + |
| 102 | + // Simulate navigation (unmount) |
| 103 | + unmount(); |
| 104 | + |
| 105 | + // Simulate navigation back (remount with element at top) |
| 106 | + mockElement.scrollTop = 0; |
| 107 | + mock.reset(); |
| 108 | + mockElement.scroll = mock.fn(); |
| 109 | + mockElement.addEventListener = mock.fn(); |
| 110 | + mockElement.removeEventListener = mock.fn(); |
| 111 | + mockRef.current = mockElement; |
| 112 | + |
| 113 | + renderHook(() => useScrollToElement('sidebar', mockRef), { wrapper }); |
| 114 | + |
| 115 | + // Should restore to position 800 |
| 116 | + assert.equal(mockElement.scroll.mock.callCount(), 1); |
| 117 | + assert.deepEqual(mockElement.scroll.mock.calls[0].arguments, [ |
| 118 | + { top: 800, behavior: 'auto' }, |
| 119 | + ]); |
| 120 | + |
| 121 | + // Also test that scroll position is saved to navigation state during scroll |
| 122 | + mock.reset(); |
| 123 | + mockElement.addEventListener = mock.fn(); |
| 124 | + mockElement.scroll = mock.fn(); |
| 125 | + navigationState = {}; |
| 126 | + |
| 127 | + renderHook(() => useScrollToElement('sidebar', mockRef), { wrapper }); |
| 128 | + |
| 129 | + // Get the scroll handler that was registered |
| 130 | + const scrollHandler2 = mockElement.addEventListener.mock.calls[0].arguments[1]; |
| 131 | + |
| 132 | + // Simulate scroll |
| 133 | + mockElement.scrollTop = 150; |
| 134 | + mockElement.scrollLeft = 50; |
| 135 | + |
| 136 | + // Call the handler |
| 137 | + scrollHandler2(); |
| 138 | + |
| 139 | + // Wait for debounce (default 300ms) |
| 140 | + await new Promise(resolve => setTimeout(resolve, 350)); |
| 141 | + |
| 142 | + // Check that navigation state was updated |
| 143 | + assert.deepEqual(navigationState.sidebar, { x: 50, y: 150 }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments