/
dom_spec.js
161 lines (135 loc) · 4.93 KB
/
dom_spec.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
goog.module('test.clulib.dom');
const {appendChild, removeNode, getViewportSize} = goog.require('goog.dom');
const {matches, closest, isElementVisible} = goog.require('clulib.dom');
exports = function () {
describe('clulib.dom', () => {
describe('matches', () => {
it('should return true if the element would be selected by the specified selector string', () => {
const element = document.createElement('div');
element.id = 'id-selector';
element.classList.add('class-selector');
expect(matches(element, '#id-selector.class-selector')).toBe(true);
});
it('should return false if the element would not be selected by the specified selector string', () => {
const element = document.createElement('div');
expect(matches(element, '#some-id')).toBe(false);
});
});
describe('closest', () => {
it('should return the element itself if the selector matches', () => {
const container = document.createElement('div');
container.innerHTML = `
<div class="cls">
<div class="cls origin" id="target"></div>
</div>
`;
const origin = container.querySelector('.origin');
const foundId = closest(origin, '.cls').id;
expect(foundId).toBe('target');
});
it('should return the closest ancestor that matches the selector', () => {
const container = document.createElement('div');
container.innerHTML = `
<div class="cls" id="target">
<div>
<div id="origin"></div>
</div>
</div>
`;
const origin = container.querySelector('#origin');
const foundId = closest(origin, '.cls').id;
expect(foundId).toBe('target');
});
it('should return null if nothing matches', () => {
const container = document.createElement('div');
container.innerHTML = `
<div>
<div>
<div id="origin"></div>
</div>
</div>
`;
const origin = container.querySelector('#origin');
const foundObject = closest(origin, '.cls');
expect(foundObject).toBe(null);
});
it('should return null if nothing matches in document context', () => {
const container = document.createElement('div');
container.innerHTML = `
<div>
<div>
<div id="origin"></div>
</div>
</div>
`;
appendChild(document.body, container);
const origin = container.querySelector('#origin');
const foundObject = closest(origin, '.cls');
expect(foundObject).toBe(null);
removeNode(container);
});
});
describe('isElementVisible', () => {
/**
* @type {goog.math.Size}
*/
const viewportSize = getViewportSize();
const fakeFullyVisible = {
['getBoundingClientRect'] () {
return {
'left': viewportSize.width / 4,
'top': viewportSize.height / 4,
'width': viewportSize.width / 2,
'height': viewportSize.height / 2
};
}
};
const fakeQuarterVisible = {
['getBoundingClientRect'] () {
return {
'left': viewportSize.width / 2,
'top': viewportSize.height / 2,
'width': viewportSize.width,
'height': viewportSize.height
};
}
};
const fakeNotVisible = {
['getBoundingClientRect'] () {
return {
'left': 0,
'top': viewportSize.height * 2,
'width': viewportSize.width,
'height': viewportSize.height
};
}
};
const fakeNotVisibleOnEdge = {
['getBoundingClientRect'] () {
return {
'left': 0,
'top': viewportSize.height,
'width': viewportSize.width,
'height': viewportSize.height
};
}
};
it('should detect fully visible elements', () => {
expect(isElementVisible(/** @type {?} */ (fakeFullyVisible))).toBe(true);
expect(isElementVisible(/** @type {?} */ (fakeFullyVisible), 1)).toBe(true);
});
it('should detect partially visible elements', () => {
expect(isElementVisible(/** @type {?} */ (fakeQuarterVisible))).toBe(true);
expect(isElementVisible(/** @type {?} */ (fakeQuarterVisible), .23)).toBe(true);
expect(isElementVisible(/** @type {?} */ (fakeQuarterVisible), .25)).toBe(true);
expect(isElementVisible(/** @type {?} */ (fakeQuarterVisible), .26)).toBe(false);
});
it('should not detect non visible elements', () => {
expect(isElementVisible(/** @type {?} */ (fakeNotVisible))).toBe(false);
});
it('should not detect non visible elements on edge', () => {
expect(isElementVisible(/** @type {?} */ (fakeNotVisibleOnEdge))).toBe(false);
});
});
});
};