Debian Codegraph @

packages/domutils/tests/src/element.spec.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 /*-----------------------------------------------------------------------------
4 | Copyright (c) 2014-2017, PhosphorJS Contributors
5 |
6 | Distributed under the terms of the BSD 3-Clause License.
7 |
8 | The full license is in the file LICENSE, distributed with this software.
9 |----------------------------------------------------------------------------*/
10 import { expect } from 'chai';
11
12 import { ElementExt } from '@lumino/domutils';
13
14 const STYLE_TEXT = `
15 .box-sizing {
16 border-top: solid 10px black;
17 border-left: solid 15px black;
18 padding: 15px 8px 9px 12px;
19 }
20
21 .size-limits {
22 min-width: 90px;
23 min-height: 95px;
24 max-width: 100px;
25 max-height: 105px;
26 }
27
28 .hit-test {
29 position: absolute;
30 top: 0;
31 left: 0;
32 width: 100px;
33 height: 100px;
34 }
35
36 .scroll-area {
37 position: absolute;
38 overflow: auto;
39 top: 0px;
40 left: 0px;
41 width: 300px;
42 height: 600px;
43 }
44
45 .scroll-elemA {
46 position: absolute;
47 top: 50px;
48 left: 50px;
49 width: 100px;
50 height: 700px;
51 }
52
53 .scroll-elemB {
54 position: absolute;
55 top: 70px;
56 left: 100px;
57 width: 100px;
58 height: 100px;
59 }
60 `;
61
62 describe('@lumino/domutils', () => {
63 describe('ElementExt', () => {
64 const styleNode = document.createElement('style');
65
66 before(() => {
67 styleNode.textContent = STYLE_TEXT;
68 document.head.appendChild(styleNode);
69 });
70
71 after(() => {
72 document.head.removeChild(styleNode);
73 });
74
75 let div: HTMLElement = null!;
76
77 beforeEach(() => {
78 div = document.createElement('div');
79 document.body.appendChild(div);
80 });
81
82 afterEach(() => {
83 document.body.removeChild(div);
84 });
85
86 describe('boxSizing()', () => {
87 it('should return a box sizing with correct values', () => {
88 div.className = 'box-sizing';
89 let box = ElementExt.boxSizing(div);
90 expect(box.borderTop).to.equal(10);
91 expect(box.borderLeft).to.equal(15);
92 expect(box.borderRight).to.equal(0);
93 expect(box.borderBottom).to.equal(0);
94 expect(box.paddingTop).to.equal(15);
95 expect(box.paddingLeft).to.equal(12);
96 expect(box.paddingRight).to.equal(8);
97 expect(box.paddingBottom).to.equal(9);
98 expect(box.verticalSum).to.equal(34);
99 expect(box.horizontalSum).to.equal(35);
100 });
101
102 it('should use defaults if parameters are not set', () => {
103 let sizing = ElementExt.boxSizing(div);
104 expect(sizing.borderTop).to.equal(0);
105 expect(sizing.borderLeft).to.equal(0);
106 expect(sizing.borderRight).to.equal(0);
107 expect(sizing.borderBottom).to.equal(0);
108 expect(sizing.paddingTop).to.equal(0);
109 expect(sizing.paddingLeft).to.equal(0);
110 expect(sizing.paddingRight).to.equal(0);
111 expect(sizing.paddingBottom).to.equal(0);
112 expect(sizing.verticalSum).to.equal(0);
113 expect(sizing.horizontalSum).to.equal(0);
114 });
115 });
116
117 describe('sizeLimits()', () => {
118 it('should return a size limits object with correct parameters', () => {
119 div.className = 'size-limits';
120 let limits = ElementExt.sizeLimits(div);
121 expect(limits.minWidth).to.equal(90);
122 expect(limits.minHeight).to.equal(95);
123 expect(limits.maxWidth).to.equal(100);
124 expect(limits.maxHeight).to.equal(105);
125 });
126
127 it('should use defaults if parameters are not set', () => {
128 let limits = ElementExt.sizeLimits(div);
129 expect(limits.minWidth).to.equal(0);
130 expect(limits.minHeight).to.equal(0);
131 expect(limits.maxWidth).to.equal(Infinity);
132 expect(limits.maxHeight).to.equal(Infinity);
133 });
134 });
135
136 describe('hitTest()', () => {
137 it('should return `true` when point is inside the node', () => {
138 div.className = 'hit-test';
139 expect(ElementExt.hitTest(div, 50, 50)).to.equal(true);
140 });
141
142 it('should return `false` when point is outside the node', () => {
143 div.className = 'hit-test';
144 expect(ElementExt.hitTest(div, 150, 150)).to.equal(false);
145 });
146
147 it('should use closed intervals for left and top only', () => {
148 div.className = 'hit-test';
149 expect(ElementExt.hitTest(div, 0, 0)).to.equal(true);
150 expect(ElementExt.hitTest(div, 100, 0)).to.equal(false);
151 expect(ElementExt.hitTest(div, 99, 0)).to.equal(true);
152 expect(ElementExt.hitTest(div, 0, 100)).to.equal(false);
153 expect(ElementExt.hitTest(div, 0, 99)).to.equal(true);
154 expect(ElementExt.hitTest(div, 100, 100)).to.equal(false);
155 expect(ElementExt.hitTest(div, 99, 99)).to.equal(true);
156 });
157 });
158
159 describe('scrollIntoViewIfNeeded()', () => {
160 let elemA: HTMLElement = null!;
161 let elemB: HTMLElement = null!;
162
163 beforeEach(() => {
164 div.className = 'scroll-area';
165 elemA = document.createElement('div');
166 elemB = document.createElement('div');
167 elemA.className = 'scroll-elemA';
168 elemB.className = 'scroll-elemB';
169 div.appendChild(elemA);
170 div.appendChild(elemB);
171 });
172
173 it('should do nothing if the element covers the viewport', () => {
174 elemB.style.top = '1000px';
175 div.scrollTop = 75;
176 ElementExt.scrollIntoViewIfNeeded(div, elemA);
177 expect(div.scrollTop).to.equal(75);
178 });
179
180 it('should do nothing if the element fits within the viewport', () => {
181 div.scrollTop = 25;
182 ElementExt.scrollIntoViewIfNeeded(div, elemB);
183 expect(div.scrollTop).to.equal(25);
184 });
185
186 it('should align the top edge for smaller size elements overlapping the top', () => {
187 elemA.style.top = '1000px';
188 div.scrollTop = 90;
189 ElementExt.scrollIntoViewIfNeeded(div, elemB);
190 expect(div.scrollTop).to.equal(70);
191 });
192
193 it('should align the top edge for equal size elements overlapping the top', () => {
194 elemA.style.height = '600px';
195 elemB.style.top = '1000px';
196 div.scrollTop = 90;
197 ElementExt.scrollIntoViewIfNeeded(div, elemA);
198 expect(div.scrollTop).to.equal(50);
199 });
200
201 it('should align the top edge for larger size elements overlapping the bottom', () => {
202 elemB.style.top = '1000px';
203 ElementExt.scrollIntoViewIfNeeded(div, elemA);
204 expect(div.scrollTop).to.equal(50);
205 });
206
207 it('should align the top edge for equal size elements overlapping the bottom', () => {
208 elemA.style.height = '600px';
209 elemB.style.top = '1000px';
210 ElementExt.scrollIntoViewIfNeeded(div, elemA);
211 expect(div.scrollTop).to.equal(50);
212 });
213
214 it('should align the bottom edge for larger size elements overlapping the top', () => {
215 elemB.style.top = '1000px';
216 div.scrollTop = 200;
217 ElementExt.scrollIntoViewIfNeeded(div, elemA);
218 expect(div.scrollTop).to.equal(150);
219 });
220
221 it('should align the bottom edge for smaller size elements overlapping the bottom', () => {
222 elemB.style.top = '600px';
223 div.scrollTop = 50;
224 ElementExt.scrollIntoViewIfNeeded(div, elemB);
225 expect(div.scrollTop).to.equal(100);
226 });
227 });
228 });
229 });