-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathscrollUtilsTests.tsx
350 lines (302 loc) · 14.7 KB
/
scrollUtilsTests.tsx
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright 2017 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { expect } from "chai";
import * as React from "react";
import * as ReactDOM from "react-dom";
import * as ScrollUtils from "../../../src/common/internal/scrollUtils";
import { type Region, Regions } from "../../../src/regions";
describe("scrollUtils", () => {
describe("getScrollPositionForRegion", () => {
const COLUMN_WIDTH = 150;
const ROW_HEIGHT = 20;
const INITIAL_SCROLL_LEFT = 17;
const INITIAL_SCROLL_TOP = 33;
const NUM_FROZEN_ROWS = 2;
const NUM_FROZEN_COLUMNS = 2;
describe("no frozen rows or columns", () => {
const TARGET_ROW = 2;
const TARGET_COLUMN = 3;
function fn(region: Region) {
return ScrollUtils.getScrollPositionForRegion(
region,
INITIAL_SCROLL_LEFT,
INITIAL_SCROLL_TOP,
getLeftOffset,
getTopOffset,
);
}
it("scrolling to cell", () => {
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT);
});
it("scrolling to row", () => {
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT);
});
it("scrolling to column", () => {
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH);
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});
it("scrolling to full table", () => {
const region = Regions.table();
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(0);
});
});
describe("with frozen rows", () => {
function fn(region: Region) {
return ScrollUtils.getScrollPositionForRegion(
region,
INITIAL_SCROLL_LEFT,
INITIAL_SCROLL_TOP,
getLeftOffset,
getTopOffset,
NUM_FROZEN_ROWS,
);
}
it("scrolling to a frozen cell", () => {
const TARGET_ROW = NUM_FROZEN_ROWS - 1;
const TARGET_COLUMN = 3;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH);
expect(scrollTop).to.equal(0);
});
it("scrolling to a non-frozen cell", () => {
const TARGET_ROW = NUM_FROZEN_ROWS; // 1 row beyond the last frozen row, b/c num is 1-indexed
const TARGET_COLUMN = 3;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT - NUM_FROZEN_ROWS * ROW_HEIGHT);
});
it("scrolling to a column", () => {
const TARGET_COLUMN = 3;
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH);
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});
it("scrolling to a frozen row", () => {
const TARGET_ROW = NUM_FROZEN_ROWS - 1;
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal(0);
});
it("scrolling to a non-frozen row", () => {
const TARGET_ROW = NUM_FROZEN_ROWS; // 1 row beyond the frozen region
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT - NUM_FROZEN_ROWS * ROW_HEIGHT);
});
it("scrolling to full table", () => {
const region = Regions.table();
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(0);
});
});
describe("with frozen columns", () => {
function fn(region: Region) {
return ScrollUtils.getScrollPositionForRegion(
region,
INITIAL_SCROLL_LEFT,
INITIAL_SCROLL_TOP,
getLeftOffset,
getTopOffset,
0,
NUM_FROZEN_COLUMNS,
);
}
it("scrolling to a frozen cell", () => {
const TARGET_ROW = 3;
const TARGET_COLUMN = NUM_FROZEN_COLUMNS - 1;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT);
});
it("scrolling to a non-frozen cell", () => {
const TARGET_ROW = 3;
const TARGET_COLUMN = NUM_FROZEN_COLUMNS;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH - NUM_FROZEN_COLUMNS * COLUMN_WIDTH);
});
it("scrolling to a frozen column", () => {
const TARGET_COLUMN = NUM_FROZEN_COLUMNS - 1;
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});
it("scrolling to a non-frozen column", () => {
const TARGET_COLUMN = NUM_FROZEN_COLUMNS; // 1 row beyond the frozen region
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH - NUM_FROZEN_COLUMNS * COLUMN_WIDTH);
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});
it("scrolling to a row", () => {
const TARGET_ROW = 3;
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT);
});
it("scrolling to full table", () => {
const region = Regions.table();
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(0);
});
});
describe("with frozen rows and columns", () => {
function fn(region: Region) {
return ScrollUtils.getScrollPositionForRegion(
region,
INITIAL_SCROLL_LEFT,
INITIAL_SCROLL_TOP,
getLeftOffset,
getTopOffset,
NUM_FROZEN_ROWS,
NUM_FROZEN_COLUMNS,
);
}
it("scrolling to a frozen cell", () => {
const TARGET_ROW = NUM_FROZEN_ROWS - 1;
const TARGET_COLUMN = NUM_FROZEN_COLUMNS - 1;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(0);
});
it("scrolling to a non-frozen cell", () => {
const TARGET_ROW = NUM_FROZEN_ROWS;
const TARGET_COLUMN = NUM_FROZEN_COLUMNS;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT - NUM_FROZEN_ROWS * ROW_HEIGHT);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH - NUM_FROZEN_COLUMNS * COLUMN_WIDTH);
});
it("scrolling to a frozen column", () => {
const TARGET_COLUMN = NUM_FROZEN_COLUMNS - 1;
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});
it("scrolling to a non-frozen column", () => {
const TARGET_COLUMN = NUM_FROZEN_COLUMNS; // 1 row beyond the frozen region
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH - NUM_FROZEN_COLUMNS * COLUMN_WIDTH);
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});
it("scrolling to a frozen row", () => {
const TARGET_ROW = NUM_FROZEN_ROWS - 1;
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal(0);
});
it("scrolling to a non-frozen row", () => {
const TARGET_ROW = NUM_FROZEN_ROWS; // 1 row beyond the frozen region
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT - NUM_FROZEN_ROWS * ROW_HEIGHT);
});
it("scrolling to full table", () => {
const region = Regions.table();
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(0);
});
});
function getTopOffset(rowIndex: number) {
return ROW_HEIGHT * rowIndex;
}
function getLeftOffset(columnIndex: number) {
return COLUMN_WIDTH * columnIndex;
}
});
describe("measureScrollBarThickness", () => {
const PARENT_WIDTH = 100;
const PARENT_HEIGHT = 100;
let containerElement: HTMLElement | undefined;
const baseStyles = { display: "block" };
const parentStyle: React.CSSProperties = {
...baseStyles,
background: "yellow",
height: PARENT_HEIGHT,
overflow: "auto",
width: PARENT_WIDTH,
};
const fn = ScrollUtils.measureScrollBarThickness;
const VERTICAL_ERROR = "measures vertical scrollbar correctly";
const HORIZONTAL_ERROR = "measures horizontal scrollbar correctly";
beforeEach(() => {
containerElement = document.createElement("div");
document.body.appendChild(containerElement);
});
afterEach(() => {
document.body.removeChild(containerElement!);
containerElement = undefined;
});
// NOTE: these tests will fail locally on OS X if you have your scrollbars set to "When scrolling"
// in System Preferences > General
it("measures correctly when neither scrollbar is showing", () => {
const element = mountElementsWithContentSize(PARENT_WIDTH / 2, PARENT_HEIGHT / 2);
expect(fn(element, "vertical"), VERTICAL_ERROR).to.equal(0);
expect(fn(element, "horizontal"), HORIZONTAL_ERROR).to.equal(0);
});
it("measures correctly when only vertical scrollbar is showing", () => {
const element = mountElementsWithContentSize(PARENT_WIDTH / 2, PARENT_HEIGHT * 2);
expect(fn(element, "vertical"), VERTICAL_ERROR).to.be.greaterThan(0);
expect(fn(element, "horizontal"), HORIZONTAL_ERROR).to.equal(0);
});
it("measures correctly when only horizontal scrollbar is showing", () => {
const element = mountElementsWithContentSize(PARENT_WIDTH * 2, PARENT_HEIGHT / 2);
expect(fn(element, "vertical"), VERTICAL_ERROR).to.equal(0);
expect(fn(element, "horizontal"), HORIZONTAL_ERROR).to.be.greaterThan(0);
});
it("measures correctly when both scrollbars are showing", () => {
const element = mountElementsWithContentSize(PARENT_WIDTH * 2, PARENT_HEIGHT * 2);
expect(fn(element, "vertical"), VERTICAL_ERROR).to.be.greaterThan(0);
expect(fn(element, "horizontal"), HORIZONTAL_ERROR).to.be.greaterThan(0);
});
function mountElementsWithContentSize(contentWidth: number, contentHeight: number) {
// HACKHACK: `as unknown as HTMLElement` cast is sketchy
return ReactDOM.render<React.HTMLProps<HTMLDivElement>>(
<div style={parentStyle}>
<div style={{ ...baseStyles, height: contentHeight, width: contentWidth }} />
</div>,
containerElement!,
) as unknown as HTMLElement;
}
});
});