@primer/behaviors 0.0.0-202201881944 → 0.0.0-202201892424

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,243 +0,0 @@
1
- import { scrollIntoView } from '../scroll-into-view.js';
2
-
3
- function scrollPositionFormula(positionData, isChildAboveViewingArea) {
4
- const {
5
- viewingAreaEdgePosition,
6
- childEdgePosition,
7
- margin
8
- } = positionData;
9
- const marginOffset = margin * (isChildAboveViewingArea ? -1 : 1);
10
- return childEdgePosition - viewingAreaEdgePosition + marginOffset;
11
- } // The DOMRect constructor isn't available in JSDOM, so we improvise here.
12
-
13
-
14
- function makeDOMRect(x, y, width, height) {
15
- return {
16
- x,
17
- y,
18
- width,
19
- height,
20
- top: y,
21
- left: x,
22
- right: x + width,
23
- bottom: y + height,
24
-
25
- toJSON() {
26
- return this;
27
- }
28
-
29
- };
30
- } // Since Jest/JSDOM doesn't support layout, we can stub out getBoundingClientRect if we know the
31
- // correct dimensions. JSDOM will handle the rest of the DOM API used by getAnchoredPosition.
32
-
33
-
34
- function createVirtualDOM(viewingAreaRect, childRect) {
35
- const viewingArea = document.createElement('div');
36
- viewingArea.style.overflow = 'auto';
37
- viewingArea.id = 'viewingArea';
38
- viewingArea.innerHTML = '<div id="child"></div>';
39
- const child = viewingArea.querySelector('#child');
40
-
41
- child.getBoundingClientRect = () => childRect;
42
-
43
- viewingArea.getBoundingClientRect = () => viewingAreaRect;
44
-
45
- return {
46
- viewingArea,
47
- child
48
- };
49
- }
50
-
51
- describe('scrollIntoView', () => {
52
- it('scrolls the expected amount when only the viewingArea element and child element are passed to the function', () => {
53
- const scrollToMock = jest.fn();
54
- Object.defineProperty(window.Element.prototype, 'scrollTo', {
55
- writable: true,
56
- value: scrollToMock
57
- });
58
- const childHeight = 50;
59
- const viewAreaHeight = 100;
60
- const childStart = viewAreaHeight + 10;
61
- const expectedScrollPosition = scrollPositionFormula({
62
- viewingAreaEdgePosition: viewAreaHeight,
63
- childEdgePosition: childStart + childHeight,
64
- margin: 0
65
- }, false);
66
- const viewingAreaRect = makeDOMRect(0, 0, 100, viewAreaHeight);
67
- const childRect = makeDOMRect(0, childStart, 100, childHeight);
68
- const {
69
- viewingArea,
70
- child
71
- } = createVirtualDOM(viewingAreaRect, childRect);
72
-
73
- viewingArea.getBoundingClientRect = () => viewingAreaRect;
74
-
75
- viewingArea.scrollTop = 0;
76
-
77
- child.getBoundingClientRect = () => childRect;
78
-
79
- scrollIntoView(child, viewingArea);
80
- expect(scrollToMock).toHaveBeenCalledWith({
81
- behavior: 'smooth',
82
- top: expectedScrollPosition
83
- });
84
- });
85
- describe('y-axis', () => {
86
- it('scrolls the child into the viewing area when it is AFTER the overflow cutoff point', () => {
87
- const scrollToMock = jest.fn();
88
- Object.defineProperty(window.Element.prototype, 'scrollTo', {
89
- writable: true,
90
- value: scrollToMock
91
- });
92
- const childHeight = 50;
93
- const viewAreaHeight = 100;
94
- const childStart = viewAreaHeight + 10;
95
- const scrollMargin = 10;
96
- const expectedScrollPosition = scrollPositionFormula({
97
- viewingAreaEdgePosition: viewAreaHeight,
98
- childEdgePosition: childStart + childHeight,
99
- margin: scrollMargin
100
- }, false);
101
- const viewingAreaRect = makeDOMRect(0, 0, 100, viewAreaHeight);
102
- const childRect = makeDOMRect(0, childStart, 100, childHeight);
103
- const {
104
- viewingArea,
105
- child
106
- } = createVirtualDOM(viewingAreaRect, childRect);
107
-
108
- viewingArea.getBoundingClientRect = () => viewingAreaRect;
109
-
110
- viewingArea.scrollTop = 0;
111
-
112
- child.getBoundingClientRect = () => childRect;
113
-
114
- scrollIntoView(child, viewingArea, {
115
- direction: 'vertical',
116
- startMargin: scrollMargin,
117
- endMargin: scrollMargin,
118
- behavior: 'auto'
119
- });
120
- expect(scrollToMock).toHaveBeenCalledWith({
121
- behavior: 'auto',
122
- top: expectedScrollPosition
123
- });
124
- });
125
- it('scrolls the child into the viewing area when it is BEFORE the overflow cutoff point', () => {
126
- const scrollToMock = jest.fn();
127
- Object.defineProperty(window.Element.prototype, 'scrollTo', {
128
- writable: true,
129
- value: scrollToMock
130
- });
131
- const childHeight = 50;
132
- const childStart = childHeight * -1 - 10;
133
- const scrollMargin = 10;
134
- const expectedScrollPosition = scrollPositionFormula({
135
- viewingAreaEdgePosition: 0,
136
- childEdgePosition: childStart,
137
- margin: scrollMargin
138
- }, true);
139
- const viewingAreaRect = makeDOMRect(0, 0, 100, 100);
140
- const childRect = makeDOMRect(0, childStart, 100, childHeight);
141
- const {
142
- viewingArea,
143
- child
144
- } = createVirtualDOM(viewingAreaRect, childRect);
145
-
146
- viewingArea.getBoundingClientRect = () => viewingAreaRect;
147
-
148
- viewingArea.scrollTop = 0;
149
-
150
- child.getBoundingClientRect = () => childRect;
151
-
152
- scrollIntoView(child, viewingArea, {
153
- direction: 'vertical',
154
- startMargin: scrollMargin,
155
- endMargin: scrollMargin,
156
- behavior: 'auto'
157
- });
158
- expect(scrollToMock).toHaveBeenCalledWith({
159
- behavior: 'auto',
160
- top: expectedScrollPosition
161
- });
162
- });
163
- });
164
- describe('x-axis', () => {
165
- it('scrolls the child into the viewing area when it is AFTER the overflow cutoff point', () => {
166
- const scrollToMock = jest.fn();
167
- Object.defineProperty(window.Element.prototype, 'scrollTo', {
168
- writable: true,
169
- value: scrollToMock
170
- });
171
- const childWidth = 50;
172
- const viewAreaWidth = 100;
173
- const childStart = viewAreaWidth + 10;
174
- const scrollMargin = 10;
175
- const expectedScrollPosition = scrollPositionFormula({
176
- viewingAreaEdgePosition: viewAreaWidth,
177
- childEdgePosition: childStart + childWidth,
178
- margin: scrollMargin
179
- }, false);
180
- const viewingAreaRect = makeDOMRect(0, 0, 100, viewAreaWidth);
181
- const childRect = makeDOMRect(childStart, 0, childWidth, 100);
182
- const {
183
- viewingArea,
184
- child
185
- } = createVirtualDOM(viewingAreaRect, childRect);
186
-
187
- viewingArea.getBoundingClientRect = () => viewingAreaRect;
188
-
189
- viewingArea.scrollLeft = 0;
190
-
191
- child.getBoundingClientRect = () => childRect;
192
-
193
- scrollIntoView(child, viewingArea, {
194
- direction: 'horizontal',
195
- startMargin: scrollMargin,
196
- endMargin: scrollMargin,
197
- behavior: 'auto'
198
- });
199
- expect(scrollToMock).toHaveBeenCalledWith({
200
- behavior: 'auto',
201
- left: expectedScrollPosition
202
- });
203
- });
204
- it('scrolls the child into the viewing area when it is BEFORE the overflow cutoff point', () => {
205
- const scrollToMock = jest.fn();
206
- Object.defineProperty(window.Element.prototype, 'scrollTo', {
207
- writable: true,
208
- value: scrollToMock
209
- });
210
- const childWidth = 50;
211
- const childStart = childWidth * -1 - 10;
212
- const scrollMargin = 10;
213
- const expectedScrollPosition = scrollPositionFormula({
214
- viewingAreaEdgePosition: 0,
215
- childEdgePosition: childStart,
216
- margin: scrollMargin
217
- }, true);
218
- const viewingAreaRect = makeDOMRect(0, 0, 100, 100);
219
- const childRect = makeDOMRect(childStart, 0, childWidth, 100);
220
- const {
221
- viewingArea,
222
- child
223
- } = createVirtualDOM(viewingAreaRect, childRect);
224
-
225
- viewingArea.getBoundingClientRect = () => viewingAreaRect;
226
-
227
- viewingArea.scrollTop = 0;
228
-
229
- child.getBoundingClientRect = () => childRect;
230
-
231
- scrollIntoView(child, viewingArea, {
232
- direction: 'horizontal',
233
- startMargin: scrollMargin,
234
- endMargin: scrollMargin,
235
- behavior: 'auto'
236
- });
237
- expect(scrollToMock).toHaveBeenCalledWith({
238
- behavior: 'auto',
239
- left: expectedScrollPosition
240
- });
241
- });
242
- });
243
- });