Debian Codegraph @

packages/popper/tests/functional/offset.js

raw
1 import Popper from '../../src/index.js';
2 import '@popperjs/test-utils/setup';
3
4 // Utils
5 import appendNewPopper from '@popperjs/test-utils/utils/appendNewPopper';
6 import appendNewRef from '@popperjs/test-utils/utils/appendNewRef';
7
8 [true, false].forEach((positionFixed) => {
9 describe('[offset]' + (positionFixed ? ' Fixed' : ''), () => {
10
11 beforeEach(function(){
12 Popper.Defaults.positionFixed = positionFixed;
13 });
14
15 it('creates a popper with single implicit px offset', done => {
16 const reference = appendNewRef(1);
17 reference.style.marginLeft = '100px';
18 const popper = appendNewPopper(2);
19
20 const offset = 10;
21
22 new Popper(reference, popper, {
23 placement: 'bottom',
24 modifiers: {
25 offset: {
26 offset: offset,
27 },
28 },
29 onCreate: data => {
30 const refLeft = reference.getBoundingClientRect().left;
31 const refWidth = reference.offsetWidth;
32 const popperLeft = popper.getBoundingClientRect().left;
33 const popperWidth = popper.offsetWidth;
34 const expectedPopperLeft =
35 refLeft + refWidth / 2 - popperWidth / 2 + offset;
36
37 expect(popperLeft).toBeApprox(expectedPopperLeft);
38 data.instance.destroy();
39 done();
40 },
41 });
42 });
43
44 it('creates a popper with double implicit px offset', done => {
45 const reference = appendNewRef(1);
46 reference.style.marginLeft = '100px';
47 const popper = appendNewPopper(2);
48
49 const offset = '10,10';
50 const arrowHeight = 5;
51
52 new Popper(reference, popper, {
53 placement: 'bottom',
54 modifiers: {
55 offset: {
56 offset: offset,
57 },
58 },
59 onCreate: data => {
60 const refLeft = reference.getBoundingClientRect().left;
61 const refBottom = reference.getBoundingClientRect().bottom;
62 const refWidth = reference.offsetWidth;
63 const popperLeft = popper.getBoundingClientRect().left;
64 const popperTop = popper.getBoundingClientRect().top;
65 const popperWidth = popper.offsetWidth;
66 const expectedPopperLeft =
67 refLeft + refWidth / 2 - popperWidth / 2 + +offset.split(',')[0];
68
69 expect(popperLeft).toBeApprox(expectedPopperLeft);
70 expect(popperTop - arrowHeight).toBeApprox(
71 refBottom + +offset.split(',')[1]
72 );
73 data.instance.destroy();
74 done();
75 },
76 });
77 });
78
79 it('creates a popper with single explicit % offset', done => {
80 const reference = appendNewRef(1);
81 reference.style.marginLeft = '100px';
82 const popper = appendNewPopper(2);
83
84 const offset = '25%';
85
86 new Popper(reference, popper, {
87 placement: 'bottom',
88 modifiers: {
89 offset: {
90 offset: offset,
91 },
92 },
93 onCreate: data => {
94 const refLeft = reference.getBoundingClientRect().left;
95 const refWidth = reference.offsetWidth;
96 const popperLeft = popper.getBoundingClientRect().left;
97 const popperWidth = popper.offsetWidth;
98 const expectedPopperLeft =
99 refLeft + refWidth / 2 - popperWidth / 2 + refWidth / 4;
100
101 expect(popperLeft).toBeApprox(expectedPopperLeft);
102 data.instance.destroy();
103 done();
104 },
105 });
106 });
107
108 it('creates a popper with single explicit negative % offset', done => {
109 const reference = appendNewRef(1);
110 reference.style.marginLeft = '100px';
111 const popper = appendNewPopper(2);
112
113 const offset = '-25%';
114
115 new Popper(reference, popper, {
116 placement: 'bottom',
117 modifiers: {
118 offset: {
119 offset: offset,
120 },
121 },
122 onCreate: data => {
123 const refLeft = reference.getBoundingClientRect().left;
124 const refWidth = reference.offsetWidth;
125 const popperLeft = popper.getBoundingClientRect().left;
126 const popperWidth = popper.offsetWidth;
127 const expectedPopperLeft =
128 refLeft + refWidth / 2 - popperWidth / 2 - refWidth / 4;
129
130 expect(popperLeft).toBeApprox(expectedPopperLeft);
131 data.instance.destroy();
132 done();
133 },
134 });
135 });
136
137 it('creates a popper with double explicit % offset', done => {
138 const reference = appendNewRef(1);
139 reference.style.marginLeft = '100px';
140 const popper = appendNewPopper(2);
141
142 const offset = '25%, 25%';
143 const arrowHeight = 5;
144
145 new Popper(reference, popper, {
146 placement: 'bottom',
147 modifiers: {
148 offset: {
149 offset: offset,
150 },
151 },
152 onCreate: data => {
153 const refLeft = reference.getBoundingClientRect().left;
154 const refBottom = reference.getBoundingClientRect().bottom;
155 const refWidth = reference.offsetWidth;
156 const refHeight = reference.offsetHeight;
157 const popperLeft = popper.getBoundingClientRect().left;
158 const popperTop = popper.getBoundingClientRect().top;
159 const popperWidth = popper.offsetWidth;
160 const expectedPopperLeft =
161 refLeft + refWidth / 2 - popperWidth / 2 + refWidth / 4;
162
163 expect(popperLeft).toBeApprox(expectedPopperLeft);
164 expect(popperTop - arrowHeight).toBeApprox(refBottom + refHeight / 4);
165 data.instance.destroy();
166 done();
167 },
168 });
169 });
170
171 it('creates a popper with double explicit negative % offset', done => {
172 const reference = appendNewRef(1);
173 reference.style.marginLeft = '100px';
174 reference.style.marginTop = '100px';
175 const popper = appendNewPopper(2);
176
177 const offset = '-25%, -25%';
178 const arrowHeight = 5;
179
180 new Popper(reference, popper, {
181 placement: 'bottom',
182 modifiers: {
183 offset: {
184 offset: offset,
185 },
186 flip: { enabled: false },
187 },
188 onCreate: data => {
189 const refLeft = reference.getBoundingClientRect().left;
190 const refBottom = reference.getBoundingClientRect().bottom;
191 const refWidth = reference.offsetWidth;
192 const refHeight = reference.offsetHeight;
193 const popperLeft = popper.getBoundingClientRect().left;
194 const popperTop = popper.getBoundingClientRect().top;
195 const popperWidth = popper.offsetWidth;
196 const expectedPopperLeft =
197 refLeft + refWidth / 2 - popperWidth / 2 - refWidth / 4;
198
199 expect(popperLeft).toBeApprox(expectedPopperLeft);
200 expect(popperTop - arrowHeight).toBeApprox(refBottom - refHeight / 4);
201 data.instance.destroy();
202 done();
203 },
204 });
205 });
206
207 it('creates a popper with math operation as offset value', done => {
208 const reference = appendNewRef(1);
209 reference.style.marginLeft = '100px';
210 reference.style.marginTop = '100px';
211 const popper = appendNewPopper(2);
212
213 const offset = '5 - 25%';
214 const arrowHeight = 5;
215
216 new Popper(reference, popper, {
217 placement: 'bottom',
218 modifiers: {
219 offset: {
220 offset: offset,
221 },
222 flip: { enabled: false },
223 },
224 onCreate: data => {
225 const refLeft = reference.getBoundingClientRect().left;
226 const refBottom = reference.getBoundingClientRect().bottom;
227 const refWidth = reference.offsetWidth;
228 const popperLeft = popper.getBoundingClientRect().left;
229 const popperTop = popper.getBoundingClientRect().top;
230 const popperWidth = popper.offsetWidth;
231 const expectedPopperLeft =
232 refLeft + refWidth / 2 - popperWidth / 2 - refWidth / 4 + 5;
233
234 expect(popperLeft).toBeApprox(expectedPopperLeft);
235 expect(popperTop - arrowHeight).toBeApprox(refBottom);
236 data.instance.destroy();
237 done();
238 },
239 });
240 });
241
242 it('creates a popper with a couple of math operations as offset values', done => {
243 const reference = appendNewRef(1);
244 reference.style.marginLeft = '100px';
245 reference.style.marginTop = '100px';
246 const popper = appendNewPopper(2);
247
248 const offset = '5 - 25%, 10px + 25%';
249 const arrowHeight = 5;
250
251 new Popper(reference, popper, {
252 placement: 'bottom',
253 modifiers: {
254 offset: {
255 offset: offset,
256 },
257 flip: { enabled: false },
258 },
259 onCreate: data => {
260 const refLeft = reference.getBoundingClientRect().left;
261 const refHeight = reference.getBoundingClientRect().height;
262 const refBottom = reference.getBoundingClientRect().bottom;
263 const refWidth = reference.offsetWidth;
264 const popperLeft = popper.getBoundingClientRect().left;
265 const popperTop = popper.getBoundingClientRect().top;
266 const popperWidth = popper.offsetWidth;
267 const expectedPopperLeft =
268 refLeft + refWidth / 2 - popperWidth / 2 - refWidth / 4 + 5;
269
270 expect(popperLeft).toBeApprox(expectedPopperLeft);
271 expect(popperTop - arrowHeight).toBeApprox(
272 refBottom + refHeight / 4 + 10
273 );
274 data.instance.destroy();
275 done();
276 },
277 });
278 });
279 });
280 });