| 1 |
import getBoundaries from '../../src/utils/getBoundaries'; |
| 2 |
|
| 3 |
describe('utils/getBoundaries', () => { |
| 4 |
let node; |
| 5 |
let spacing; |
| 6 |
let scrolling; |
| 7 |
let scrollingChild; |
| 8 |
let popper; |
| 9 |
let scrollingPopper; |
| 10 |
|
| 11 |
function setCss(element, css) { |
| 12 |
for (const key in css) { |
| 13 |
if (css.hasOwnProperty(key)) { |
| 14 |
element.style[key] = css[key]; |
| 15 |
} |
| 16 |
} |
| 17 |
} |
| 18 |
function expectBoundary(result, expected) { |
| 19 |
const tolerance = 2; |
| 20 |
expect(Math.abs(result.top - expected.top) <= tolerance).toBeTruthy(); |
| 21 |
expect(Math.abs(result.right - expected.right) <= tolerance).toBeTruthy(); |
| 22 |
expect(Math.abs(result.bottom - expected.bottom) <= tolerance).toBeTruthy(); |
| 23 |
expect(Math.abs(result.left - expected.left) <= tolerance).toBeTruthy(); |
| 24 |
} |
| 25 |
|
| 26 |
beforeEach(() => { |
| 27 |
node = document.createElement('div'); |
| 28 |
spacing = document.createElement('div'); |
| 29 |
popper = document.createElement('div'); |
| 30 |
scrollingPopper = document.createElement('div'); |
| 31 |
|
| 32 |
scrolling = document.createElement('div'); |
| 33 |
setCss(scrolling, { |
| 34 |
top: '150px', |
| 35 |
left: '160px', |
| 36 |
right: '300px', |
| 37 |
bottom: '400px', |
| 38 |
height: '150px', |
| 39 |
width: '125px', |
| 40 |
}); |
| 41 |
|
| 42 |
scrollingChild = document.createElement('div'); |
| 43 |
setCss(scrollingChild, { |
| 44 |
overflow: 'scroll', |
| 45 |
top: '50px', |
| 46 |
left: '60px', |
| 47 |
right: '-100px', |
| 48 |
bottom: '-200px', |
| 49 |
height: '350px', |
| 50 |
width: '625px', |
| 51 |
position: 'absolute', |
| 52 |
transform: 'translate3d(100px, 100px, 0)', |
| 53 |
willChange: 'transform', |
| 54 |
}); |
| 55 |
|
| 56 |
document.body.appendChild(node); |
| 57 |
node.appendChild(spacing); |
| 58 |
node.appendChild(scrolling); |
| 59 |
node.appendChild(popper); |
| 60 |
scrolling.appendChild(scrollingChild); |
| 61 |
scrollingChild.appendChild(scrollingPopper); |
| 62 |
}); |
| 63 |
|
| 64 |
afterEach(() => { |
| 65 |
document.body.removeChild(node); |
| 66 |
}); |
| 67 |
|
| 68 |
it('returns a boundary defined by the document element.', () => { |
| 69 |
const result = getBoundaries(popper, node, 0, 'window', true); |
| 70 |
expectBoundary(result, { |
| 71 |
top: 0, |
| 72 |
right: window.innerWidth, |
| 73 |
bottom: window.innerHeight, |
| 74 |
left: 0, |
| 75 |
}); |
| 76 |
}); |
| 77 |
|
| 78 |
it('returns a boundary defined by the document element by way of a child reference.', () => { |
| 79 |
const result = getBoundaries(popper, spacing, 0, 'scrollParent', true); |
| 80 |
expectBoundary(result, { |
| 81 |
top: 0, |
| 82 |
right: window.innerWidth, |
| 83 |
bottom: window.innerHeight, |
| 84 |
left: 0, |
| 85 |
}); |
| 86 |
}); |
| 87 |
|
| 88 |
it('returns a custom defined boundary within the page.', () => { |
| 89 |
const result = getBoundaries(scrollingPopper, scrollingChild, 0, 'scrollParent', false); |
| 90 |
expectBoundary(result, { |
| 91 |
top: -150, |
| 92 |
right: window.innerWidth - 160, |
| 93 |
bottom: window.innerHeight - 150, |
| 94 |
left: -160, |
| 95 |
}); |
| 96 |
}); |
| 97 |
}); |