-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathoffset.test.js
48 lines (46 loc) · 1.36 KB
/
offset.test.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
import '@testing-library/jest-dom';
import $utils from '../methods/vanilla-js-methods.js';
function createMockDiv(width, height) {
const div = document.createElement('div');
Object.assign(div.style, {
width: `${width }px`,
height: `${height }px`,
});
div.className = 'div-el';
// we have to mock this for jsdom.
div.getBoundingClientRect = () => ({
width,
height,
top: 50,
left: 100,
right: width,
bottom: height,
});
return div;
}
describe('offset method', () => {
test('Should be able to offset of the element', () => {
document.body.appendChild(createMockDiv(100, 100));
window.pageYOffset = 500;
window.pageXOffset = 500;
Object.defineProperties(window.HTMLElement.prototype, {
clientTop: {
get () {
return 200;
},
},
clientLeft: {
get () {
return 200;
},
},
});
const offset = $utils('.div-el').offset();
expect(offset).toEqual({ left: 400, top: 350 });
});
test('Should return 0 if element does not exist', () => {
document.body.innerHTML = '';
const offset = $utils('.div-el').offset();
expect(offset).toEqual({ left: 0, top: 0 });
});
});