forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWindowScroller.jest.js
476 lines (387 loc) · 13.5 KB
/
WindowScroller.jest.js
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import * as React from 'react';
import {findDOMNode} from 'react-dom';
import {render} from '../TestUtils';
import WindowScroller, {IS_SCROLLING_TIMEOUT} from './WindowScroller';
function mockGetBoundingClientRectForHeader({
documentOffset = 0,
height,
width,
}) {
// Mock the WindowScroller element and window separately
// The only way to mock the former (before its created) is globally
Element.prototype.getBoundingClientRect = jest.fn(() => ({
top: height,
left: width,
}));
document.documentElement.getBoundingClientRect = jest.fn(() => ({
top: documentOffset,
left: documentOffset,
}));
}
function getMarkup({headerElements, documentOffset, renderFn, ...props} = {}) {
const windowScroller = (
<WindowScroller {...props}>
{params => <div>{renderFn && renderFn(params)}</div>}
</WindowScroller>
);
// JSDome doesn't implement a working getBoundingClientRect()
// But WindowScroller requires it
mockGetBoundingClientRectForHeader({
documentOffset,
height: headerElements ? headerElements.props.style.height : 0,
width: headerElements ? headerElements.props.style.width : 0,
});
if (headerElements) {
return (
<div>
{headerElements}
{windowScroller}
</div>
);
} else {
return windowScroller;
}
}
function simulateWindowScroll({scrollX = 0, scrollY = 0}) {
document.body.style.height = '10000px';
window.scrollX = scrollX;
window.scrollY = scrollY;
document.dispatchEvent(new window.Event('scroll', {bubbles: true}));
document.body.style.height = '';
}
function simulateWindowResize({height = 0, width = 0}) {
window.innerHeight = height;
window.innerWidth = width;
document.dispatchEvent(new window.Event('resize', {bubbles: true}));
}
describe('WindowScroller', () => {
// Set default window height and scroll position between tests
beforeEach(() => {
window.scrollY = 0;
window.scrollX = 0;
window.innerHeight = 500;
window.innerWidth = 500;
});
// Starts updating scrollTop only when the top position is reached
it('should have correct top and left properties to be defined on :_positionFromTop and :_positionFromLeft', () => {
const component = render(getMarkup());
const rendered = findDOMNode(component);
const {top, left} = rendered.getBoundingClientRect();
expect(component._positionFromTop).toEqual(top);
expect(component._positionFromLeft).toEqual(left);
});
it('should allow passing child element with registerChild of children function param', () => {
const scrollElement = document.createElement('div');
scrollElement.scrollTop = 100;
scrollElement.scrollLeft = 150;
scrollElement.getBoundingClientRect = () => ({
top: 200,
left: 250,
});
const child = document.createElement('div');
child.getBoundingClientRect = () => ({
top: 300,
left: 350,
});
const renderFn = jest.fn();
const component = render(getMarkup({scrollElement, renderFn}));
renderFn.mock.calls[0][0].registerChild(child);
expect(component._positionFromTop).toEqual(300 + 100 - 200);
expect(component._positionFromLeft).toEqual(350 + 150 - 250);
});
it('should warn on passing non-element or not null', () => {
const warnFn = jest.spyOn(console, 'warn');
const renderFn = jest.fn();
render(getMarkup({renderFn}));
renderFn.mock.calls[0][0].registerChild(1);
renderFn.mock.calls[0][0].registerChild(document.createElement('div'));
renderFn.mock.calls[0][0].registerChild(null);
expect(warnFn).toHaveBeenCalledTimes(1);
warnFn.mockRestore();
});
// Test edge-case reported in bvaughn/react-virtualized/pull/346
it('should have correct top and left properties to be defined on :_positionFromTop and :_positionFromLeft if documentElement is scrolled', () => {
render.unmount();
// Simulate scrolled documentElement
const component = render(
getMarkup({
documentOffset: -100,
}),
);
const rendered = findDOMNode(component);
const {top, left} = rendered.getBoundingClientRect();
expect(component._positionFromTop).toEqual(top + 100);
expect(component._positionFromLeft).toEqual(left + 100);
// Reset override
delete document.documentElement.getBoundingClientRect;
});
it('inherits the window height and passes it to child component', () => {
const renderFn = jest.fn();
const component = render(getMarkup({renderFn}));
expect(component.state.height).toEqual(window.innerHeight);
expect(component.state.height).toEqual(500);
expect(renderFn).lastCalledWith(
expect.objectContaining({
height: 500,
}),
);
});
it('should restore pointerEvents on body after IS_SCROLLING_TIMEOUT', async () => {
render(getMarkup());
document.body.style.pointerEvents = 'all';
simulateWindowScroll({scrollY: 5000});
expect(document.body.style.pointerEvents).toEqual('none');
await new Promise(resolve =>
setTimeout(resolve, IS_SCROLLING_TIMEOUT + 100),
);
expect(document.body.style.pointerEvents).toEqual('all');
});
it('should restore pointerEvents on body after unmount', () => {
render(getMarkup());
document.body.style.pointerEvents = 'all';
simulateWindowScroll({scrollY: 5000});
expect(document.body.style.pointerEvents).toEqual('none');
render.unmount();
expect(document.body.style.pointerEvents).toEqual('all');
});
describe('onScroll', () => {
it('should trigger callback when window scrolls', async () => {
const onScroll = jest.fn();
render(getMarkup({onScroll}));
simulateWindowScroll({scrollY: 5000});
// Allow scrolling timeout to complete so that the component computes state
await new Promise(resolve => setTimeout(resolve, 150));
expect(onScroll).toHaveBeenCalledWith({
scrollLeft: 0,
scrollTop: 5000,
});
simulateWindowScroll({
scrollX: 2500,
scrollY: 5000,
});
// Allow scrolling timeout to complete so that the component computes state
await new Promise(resolve => setTimeout(resolve, 150));
expect(onScroll).toHaveBeenCalledWith({
scrollLeft: 2500,
scrollTop: 5000,
});
});
it('should update :scrollTop when window is scrolled', async () => {
const renderFn = jest.fn();
const component = render(getMarkup({renderFn}));
// Initial load of the component should have 0 scrollTop
expect(renderFn).lastCalledWith(
expect.objectContaining({
scrollTop: 0,
}),
);
simulateWindowScroll({scrollY: 5000});
// Allow scrolling timeout to complete so that the component computes state
await new Promise(resolve => setTimeout(resolve, 150));
const componentScrollTop = window.scrollY - component._positionFromTop;
expect(component.state.scrollTop).toEqual(componentScrollTop);
expect(renderFn).lastCalledWith(
expect.objectContaining({
scrollTop: componentScrollTop,
}),
);
});
it('should specify :isScrolling when scrolling and reset after scrolling', async () => {
const renderFn = jest.fn();
render(getMarkup({renderFn}));
simulateWindowScroll({scrollY: 5000});
expect(renderFn).lastCalledWith(
expect.objectContaining({
isScrolling: true,
}),
);
await new Promise(resolve => setTimeout(resolve, 250));
expect(renderFn).lastCalledWith(
expect.objectContaining({
isScrolling: false,
}),
);
});
it('should support a custom :scrollingResetTimeInterval prop', async () => {
const renderFn = jest.fn();
render(
getMarkup({
scrollingResetTimeInterval: 500,
renderFn,
}),
);
expect(renderFn).lastCalledWith(
expect.objectContaining({
isScrolling: false,
}),
);
simulateWindowScroll({scrollY: 5000});
expect(renderFn).lastCalledWith(
expect.objectContaining({
isScrolling: true,
}),
);
await new Promise(resolve => setTimeout(resolve, 100));
expect(renderFn).lastCalledWith(
expect.objectContaining({
isScrolling: true,
}),
);
await new Promise(resolve => setTimeout(resolve, 100));
expect(renderFn).lastCalledWith(
expect.objectContaining({
isScrolling: true,
}),
);
await new Promise(resolve => setTimeout(resolve, 400));
expect(renderFn).lastCalledWith(
expect.objectContaining({
isScrolling: false,
}),
);
});
});
describe('onResize', () => {
it('should trigger callback on init and when window resizes', () => {
const resizeFn = jest.fn();
render(getMarkup({onResize: resizeFn}));
simulateWindowResize({height: 1000, width: 1024});
expect(resizeFn).toHaveBeenCalledTimes(1);
expect(resizeFn).lastCalledWith({height: 1000, width: 1024});
});
it('should update height when window resizes', () => {
const renderFn = jest.fn();
const component = render(getMarkup({renderFn}));
// Initial load of the component should have the same window height = 500
expect(component.state.height).toEqual(window.innerHeight);
expect(component.state.height).toEqual(500);
expect(renderFn).lastCalledWith(
expect.objectContaining({
height: 500,
}),
);
simulateWindowResize({height: 1000});
expect(component.state.height).toEqual(window.innerHeight);
expect(component.state.height).toEqual(1000);
expect(renderFn).lastCalledWith(
expect.objectContaining({
height: 1000,
}),
);
});
});
describe('updatePosition', () => {
it('should calculate the initial offset from the top of the page when mounted', () => {
let windowScroller;
render(
getMarkup({
headerElements: <div style={{height: 100}} />,
ref: ref => {
windowScroller = ref;
},
}),
);
expect(windowScroller._positionFromTop).toBe(100);
});
it('should recalculate the offset from the top when the window resizes', () => {
let windowScroller;
render(
getMarkup({
headerElements: <div id="header" style={{height: 100, width: 150}} />,
ref: ref => {
windowScroller = ref;
},
}),
);
expect(windowScroller._positionFromTop).toBe(100);
expect(windowScroller._positionFromLeft).toBe(150);
mockGetBoundingClientRectForHeader({
height: 200,
width: 300,
});
expect(windowScroller._positionFromTop).toBe(100);
expect(windowScroller._positionFromLeft).toBe(150);
simulateWindowResize({height: 1000, width: 1000});
expect(windowScroller._positionFromTop).toBe(200);
expect(windowScroller._positionFromLeft).toBe(300);
});
it('should recalculate the offset from the top if called externally', () => {
let windowScroller;
render(
getMarkup({
headerElements: <div id="header" style={{height: 100, width: 150}} />,
ref: ref => {
windowScroller = ref;
},
}),
);
expect(windowScroller._positionFromTop).toBe(100);
expect(windowScroller._positionFromLeft).toBe(150);
mockGetBoundingClientRectForHeader({
height: 200,
width: 300,
});
windowScroller.updatePosition();
expect(windowScroller._positionFromTop).toBe(200);
expect(windowScroller._positionFromLeft).toBe(300);
});
});
describe('when child scrolls', () => {
let originalScrollTo;
beforeEach(() => {
originalScrollTo = window.scrollTo;
window.scrollTo = (scrollX, scrollY) =>
simulateWindowScroll({scrollX, scrollY});
});
afterEach(() => {
window.scrollTo = originalScrollTo;
render.unmount();
});
it('should scroll the scrollElement (when it is window) the desired amount', () => {
const renderFn = jest.fn();
let windowScroller;
render(
getMarkup({
ref: ref => {
windowScroller = ref;
},
renderFn,
}),
);
renderFn.mock.calls[0][0].onChildScroll({scrollTop: 200});
expect(window.scrollY).toEqual(200 + windowScroller._positionFromTop);
});
it('should not scroll the scrollElement if trying to scroll to where we already are', () => {
const renderFn = jest.fn();
render(getMarkup({renderFn}));
simulateWindowScroll({scrollY: 200});
window.scrollTo = jest.fn();
renderFn.mock.calls[0][0].onChildScroll({scrollTop: 200});
expect(window.scrollTo).not.toHaveBeenCalled();
});
it('should scroll the scrollElement (when it is an element) the desired amount', () => {
let windowScroller;
const renderFn = jest.fn();
const divEl = document.createElement('div');
render(
getMarkup({
ref: ref => {
windowScroller = ref;
},
renderFn,
scrollElement: divEl,
}),
);
renderFn.mock.calls[0][0].onChildScroll({scrollTop: 200});
expect(divEl.scrollTop).toEqual(200 + windowScroller._positionFromTop);
});
it('should update own scrollTop', () => {
const renderFn = jest.fn();
render(getMarkup({renderFn}));
renderFn.mock.calls[0][0].onChildScroll({scrollTop: 200});
expect(renderFn).lastCalledWith(
expect.objectContaining({scrollTop: 200}),
);
});
});
});