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