forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowScroller.jest.js
410 lines (330 loc) · 11.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
/* global Element */
import React from 'react'
import { findDOMNode } from 'react-dom'
import { render } from '../TestUtils'
import { IS_SCROLLING_TIMEOUT } from './utils/onScroll'
import WindowScroller from './WindowScroller'
class ChildComponent extends React.Component {
render () {
const { scrollTop, isScrolling, height } = this.props
return (
<div>{`scrollTop:${scrollTop}, isScrolling:${isScrolling}, height:${height}`}</div>
)
}
}
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,
childRef,
...props
} = {}) {
const windowScroller = (
<WindowScroller {...props}>
{({ height, isScrolling, scrollTop, onChildScroll }) => (
<ChildComponent
ref={childRef}
height={height}
isScrolling={isScrolling}
scrollTop={scrollTop}
onScroll={onChildScroll}
/>
)}
</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)
})
// 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 component = render(getMarkup())
const rendered = findDOMNode(component)
expect(component.state.height).toEqual(window.innerHeight)
expect(component.state.height).toEqual(500)
expect(rendered.textContent).toContain('height:500')
})
it('should restore pointerEvents on body after IS_SCROLLING_TIMEOUT', async (done) => {
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))
expect(document.body.style.pointerEvents).toEqual('all')
done()
})
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 done => {
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
}
)
done()
})
it('should update :scrollTop when window is scrolled', async done => {
const component = render(getMarkup())
const rendered = findDOMNode(component)
// Initial load of the component should have 0 scrollTop
expect(rendered.textContent).toContain('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(rendered.textContent).toContain(`scrollTop:${componentScrollTop}`)
done()
})
it('should specify :isScrolling when scrolling and reset after scrolling', async (done) => {
const component = render(getMarkup())
const rendered = findDOMNode(component)
simulateWindowScroll({ scrollY: 5000 })
expect(rendered.textContent).toContain('isScrolling:true')
await new Promise(resolve => setTimeout(resolve, 250))
expect(rendered.textContent).toContain('isScrolling:false')
done()
})
})
describe('onResize', () => {
it('should trigger callback when window resizes', () => {
const onResizeCalls = []
render(getMarkup({
onResize: params => onResizeCalls.push(params)
}))
simulateWindowResize({ height: 1000, width: 1024 })
expect(onResizeCalls.length).toEqual(1)
expect(onResizeCalls[0]).toEqual({
height: 1000,
width: 1024
})
})
it('should update height when window resizes', () => {
const component = render(getMarkup())
const rendered = findDOMNode(component)
// 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(rendered.textContent).toContain('height:500')
simulateWindowResize({ height: 1000 })
expect(component.state.height).toEqual(window.innerHeight)
expect(component.state.height).toEqual(1000)
expect(rendered.textContent).toContain('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 }}></div>,
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 }}></div>,
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 }}></div>,
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', () => {
let windowScroller, childComponent
render(getMarkup({
ref: (ref) => {
windowScroller = ref
},
childRef: (ref) => {
childComponent = ref
}
}))
childComponent.props.onScroll({ scrollTop: 200 })
expect(window.scrollY).toEqual(200 + windowScroller._positionFromTop)
})
it('should not scroll the scrollElement if trying to scroll to where we already are', () => {
let childComponent
render(getMarkup({
childRef: (ref) => {
childComponent = ref
}
}))
simulateWindowScroll({ scrollY: 200 })
window.scrollTo = jest.fn()
childComponent.props.onScroll({ scrollTop: 200 })
expect(window.scrollTo).not.toHaveBeenCalled()
})
it('should scroll the scrollElement (when it is an element) the desired amount', () => {
let windowScroller, childComponent
const divEl = document.createElement('div')
render(getMarkup({
ref: (ref) => {
windowScroller = ref
},
scrollElement: divEl,
childRef: (ref) => {
childComponent = ref
}
}))
childComponent.props.onScroll({ scrollTop: 200 })
expect(divEl.scrollTop).toEqual(200 + windowScroller._positionFromTop)
})
it('should update own scrollTop', () => {
let windowScroller, childComponent
render(getMarkup({
ref: (ref) => {
windowScroller = ref
},
childRef: (ref) => {
childComponent = ref
}
}))
childComponent.props.onScroll({ scrollTop: 200 })
expect(windowScroller.state.scrollTop).toEqual(200)
})
})
})