forked from youzan/vant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.ts
57 lines (50 loc) · 1.28 KB
/
dom.ts
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
import { trigger } from './event';
function mockHTMLElementOffset() {
Object.defineProperties(HTMLElement.prototype, {
offsetParent: {
get() {
return this.parentNode || {};
},
},
offsetLeft: {
get() {
return parseFloat(window.getComputedStyle(this).marginLeft) || 0;
},
},
offsetTop: {
get() {
return parseFloat(window.getComputedStyle(this).marginTop) || 0;
},
},
offsetHeight: {
get() {
return parseFloat(window.getComputedStyle(this).height) || 0;
},
},
offsetWidth: {
get() {
return parseFloat(window.getComputedStyle(this).width) || 0;
},
},
});
}
export function mockScrollIntoView() {
const fn = jest.fn();
Element.prototype.scrollIntoView = fn;
return fn;
}
export function mockGetBoundingClientRect(
rect: DOMRect
): () => void {
const originMethod = Element.prototype.getBoundingClientRect;
Element.prototype.getBoundingClientRect = jest.fn(() => rect);
return function () {
Element.prototype.getBoundingClientRect = originMethod;
};
}
export function mockScrollTop(value: number) {
Object.defineProperty(window, 'scrollTop', { value, writable: true });
trigger(window, 'scroll');
}
mockScrollIntoView();
mockHTMLElementOffset();