Debian Codegraph @

packages/popper/tests/functional/core.js

raw
1 import Popper from '../../src/index.js';
2
3 import '@popperjs/test-utils';
4 const jasmineWrapper = document.getElementById('jasmineWrapper');
5
6 // Utils
7 import appendNewPopper from '@popperjs/test-utils/utils/appendNewPopper';
8 import appendNewRef from '@popperjs/test-utils/utils/appendNewRef';
9 import simulateScroll from '@popperjs/test-utils/utils/simulateScroll';
10 import prepend from '@popperjs/test-utils/utils/prepend';
11 import isMSBrowser from '@popperjs/test-utils/utils/isMSBrowser';
12 import getRect from '../utils/getRect';
13
14 const isIE10 = navigator.appVersion.indexOf('MSIE 10') !== -1;
15 const isIPHONE = window.navigator.userAgent.match(/iPhone/i);
16 const arrowSize = 5;
17 [true, false].forEach(positionFixed => {
18
19
20 describe('[core]' + (positionFixed ? ' Fixed' : ''), () => {
21
22
23 beforeEach(function(){
24 Popper.Defaults.positionFixed = positionFixed;
25 });
26
27 afterEach(function() {
28 jasmineWrapper.scrollTop = 0;
29 jasmineWrapper.scrollLeft = 0;
30 });
31
32 it('can access the AMD module to create a new instance', () => {
33 // append popper element
34 const popper = document.createElement('div');
35 popper.style.width = '100px';
36 popper.style.height = '100px';
37 jasmineWrapper.appendChild(popper);
38
39 // append trigger element
40 const trigger = document.createElement('div');
41 jasmineWrapper.appendChild(trigger);
42
43 // initialize new popper instance
44 const pop = new Popper(trigger, popper);
45
46 expect(pop).toBeDefined();
47
48 pop.destroy();
49 });
50
51 it('inits a bottom popper', () => {
52 const reference = appendNewRef(1);
53 const popper = appendNewPopper(2);
54
55 const pop = new Popper(reference, popper);
56
57 const top = getRect(popper).top;
58 expect(top).toBeApprox(getRect(reference).bottom + arrowSize);
59
60 pop.destroy();
61 });
62
63 it('inits a bottom popper inside document with margins', done => {
64 const doc = document.documentElement;
65 doc.style.marginLeft = '300px';
66 doc.style.marginTop = '300px';
67
68 const wrp = document.createElement('div');
69 wrp.innerHTML = `
70 <div id="reference" style="
71 width: 100px;
72 height: 50px;
73 background-color: red;
74 "></div><div id="popper" style="background: black; color: white; width: 50px;">1</div>
75 `.trim();
76 const popper = wrp.childNodes[1];
77 const reference = wrp.childNodes[0];
78 prepend(reference, document.body);
79 prepend(popper, document.body);
80
81 new Popper(reference, popper, {
82 placement: 'top',
83 onCreate(data) {
84 const bottom = getRect(popper).bottom;
85 expect(bottom).toBeApprox(getRect(reference).top);
86
87 data.instance.destroy();
88 document.body.removeChild(popper);
89 document.body.removeChild(reference);
90 doc.style.cssText = null;
91 done();
92 },
93 });
94 });
95
96 it('inits a bottom popper inside document with margins, it should correctly flip', done => {
97 if (isIPHONE) {
98 pending();
99 }
100
101 const doc = document.documentElement;
102 doc.style.marginLeft = '50vw';
103 doc.style.marginTop = '100vh';
104
105 const wrp = document.createElement('div');
106 wrp.innerHTML = `
107 <div id="reference" style="
108 width: 100px;
109 height: 50px;
110 background-color: red;
111 "></div><div id="popper" style="background: black; color: white; width: 50px;">1</div>
112 `.trim();
113 const popper = wrp.childNodes[1];
114 const reference = wrp.childNodes[0];
115 prepend(reference, document.body);
116 prepend(popper, document.body);
117
118 new Popper(reference, popper, {
119 onCreate(data) {
120 const bottom = getRect(popper).bottom;
121 expect(bottom).toBeApprox(getRect(reference).top);
122
123 data.instance.destroy();
124 document.body.removeChild(popper);
125 document.body.removeChild(reference);
126 doc.style.cssText = null;
127 done();
128 },
129 });
130 });
131
132 it('inits a top popper inside body with margins relative to absolute reference rotated by 90 degs', done => {
133 const wrp = document.createElement('div');
134 wrp.innerHTML = `
135 <div id="reference" style="
136 position: absolute;
137 top: 100px;
138 width: 100px;
139 height: 50px;
140 background-color: red;
141 margin-left: 50px;
142 -ms-transform: rotate(90deg);
143 -webkit-transform: rotate(90deg);
144 transform: rotate(90deg);
145 "></div><div id="popper" style="background: black; color: white; width: 50px;">1</div>
146 `.trim();
147 const popper = wrp.childNodes[1];
148 const reference = wrp.childNodes[0];
149 prepend(reference, document.body);
150 prepend(popper, document.body);
151
152 new Popper(reference, popper, {
153 placement: 'top',
154 onCreate(data) {
155 const pop = getRect(popper);
156 const ref = getRect(reference);
157 expect(pop.bottom).toBeApprox(ref.top);
158 expect(pop.left).toBe(ref.left);
159
160 data.instance.destroy();
161 document.body.style.cssText = null;
162 document.body.removeChild(popper);
163 document.body.removeChild(reference);
164 done();
165 },
166 });
167 });
168
169 it('inits a right scrollable popper ', done => {
170 const reference = appendNewRef(1);
171 const popper = appendNewPopper(2);
172 popper.style.overflow = 'auto';
173
174 const pop = new Popper(reference, popper, {
175 placement: 'right',
176 onCreate() {
177 const left = getRect(popper).left;
178 expect(left).toBeApprox(getRect(reference).right + arrowSize);
179
180 pop.destroy();
181
182 done();
183 },
184 });
185 });
186
187 describe(['inner modifier'], () => {
188 it('inits a bottom inner popper', done => {
189 const reference = appendNewRef(1);
190 reference.style.height = '200px';
191 const popper = appendNewPopper(2);
192
193 const pop = new Popper(reference, popper, {
194 modifiers: { inner: { enabled: true } },
195 onCreate() {
196 const bottom = getRect(popper).bottom + arrowSize;
197 expect(bottom).toBeApprox(getRect(reference).bottom);
198 pop.destroy();
199 done();
200 },
201 });
202 });
203
204 it('inits a top inner popper', done => {
205 const reference = appendNewRef(1);
206 reference.style.height = '200px';
207 const popper = appendNewPopper(2);
208
209 const pop = new Popper(reference, popper, {
210 placement: 'top',
211 modifiers: { inner: { enabled: true } },
212 onCreate() {
213 const top = getRect(popper).top - arrowSize;
214 expect(top).toBeApprox(getRect(reference).top);
215 pop.destroy();
216 done();
217 },
218 });
219 });
220
221 it('inits a right inner popper', done => {
222 const reference = appendNewRef(1);
223 reference.style.height = '200px';
224 reference.style.width = '200px';
225 const popper = appendNewPopper(2);
226
227 const pop = new Popper(reference, popper, {
228 placement: 'right',
229 modifiers: { inner: { enabled: true } },
230 onCreate() {
231 const right = getRect(popper).right + arrowSize;
232 expect(right).toBeApprox(getRect(reference).right);
233 pop.destroy();
234 done();
235 },
236 });
237 });
238
239 it('inits a left inner popper', done => {
240 const reference = appendNewRef(1);
241 reference.style.height = '200px';
242 reference.style.width = '200px';
243 const popper = appendNewPopper(2);
244
245 const pop = new Popper(reference, popper, {
246 placement: 'left',
247 modifiers: { inner: { enabled: true } },
248 onCreate() {
249 const left = getRect(popper).left - arrowSize;
250 expect(left).toBeApprox(getRect(reference).left);
251 pop.destroy();
252 done();
253 },
254 });
255 });
256
257 it('inits a right-start inner popper', done => {
258 const reference = appendNewRef(1);
259 reference.style.height = '200px';
260 reference.style.width = '200px';
261 const popper = appendNewPopper(2);
262
263 const pop = new Popper(reference, popper, {
264 placement: 'right-start',
265 modifiers: { inner: { enabled: true } },
266 onCreate() {
267 const right = getRect(popper).right + arrowSize;
268 expect(right).toBeApprox(getRect(reference).right);
269 pop.destroy();
270 done();
271 },
272 });
273 });
274 });
275
276 describe('[auto placement]', () => {
277 it('should be computed to `top`', done => {
278 const parent = document.createElement('div');
279 parent.style.overflow = 'auto';
280 parent.style.position = 'relative';
281 parent.style.width = '500px';
282 parent.style.height = '500px';
283 parent.style.backgroundColor = 'green';
284 const reference = appendNewRef(1, 'ref', parent);
285 reference.style.position = 'absolute';
286 reference.style.bottom = '0';
287 reference.style.left = '50%';
288 const popper = appendNewPopper(2, 'pop', parent);
289 jasmineWrapper.appendChild(parent);
290
291 const pop = new Popper(reference, popper, {
292 placement: 'auto',
293 modifiers: { flip: { boundariesElement: 'scrollParent' } },
294 onCreate: () => {
295 const bottom = getRect(popper).bottom + arrowSize;
296 expect(bottom).toBeApprox(getRect(reference).top);
297 pop.destroy();
298 done();
299 },
300 });
301 });
302
303 it('should be computed to `right`', done => {
304 const parent = document.createElement('div');
305 parent.style.overflow = 'auto';
306 parent.style.position = 'relative';
307 parent.style.width = '500px';
308 parent.style.height = '500px';
309 parent.style.backgroundColor = 'green';
310 const reference = appendNewRef(1, 'ref', parent);
311 reference.style.position = 'absolute';
312 reference.style.left = '0';
313 reference.style.top = '50%';
314 const popper = appendNewPopper(2, 'pop', parent);
315 jasmineWrapper.appendChild(parent);
316
317 const pop = new Popper(reference, popper, {
318 placement: 'auto',
319 modifiers: { flip: { boundariesElement: 'scrollParent' } },
320 onCreate: () => {
321 const left = getRect(popper).left - arrowSize;
322 expect(left).toBeApprox(getRect(reference).right);
323 pop.destroy();
324 done();
325 },
326 });
327 });
328
329 it('should be computed to `bottom`', done => {
330 const parent = document.createElement('div');
331 parent.style.overflow = 'auto';
332 parent.style.position = 'relative';
333 parent.style.width = '500px';
334 parent.style.height = '500px';
335 parent.style.backgroundColor = 'green';
336 const reference = appendNewRef(1, 'ref', parent);
337 reference.style.position = 'absolute';
338 reference.style.top = '0';
339 reference.style.left = '50%';
340 const popper = appendNewPopper(2, 'pop', parent);
341 jasmineWrapper.appendChild(parent);
342
343 const pop = new Popper(reference, popper, {
344 placement: 'auto',
345 modifiers: { flip: { boundariesElement: 'scrollParent' } },
346 onCreate: () => {
347 const top = getRect(popper).top - arrowSize;
348 expect(top).toBeApprox(getRect(reference).bottom);
349 pop.destroy();
350 done();
351 },
352 });
353 });
354
355 it('should be computed to `left`', done => {
356 const parent = document.createElement('div');
357 parent.style.overflow = 'auto';
358 parent.style.position = 'relative';
359 parent.style.width = '500px';
360 parent.style.height = '500px';
361 parent.style.backgroundColor = 'green';
362 const reference = appendNewRef(1, 'ref', parent);
363 reference.style.position = 'absolute';
364 reference.style.right = '0';
365 reference.style.top = '50%';
366 const popper = appendNewPopper(2, 'pop', parent);
367 jasmineWrapper.appendChild(parent);
368
369 const pop = new Popper(reference, popper, {
370 placement: 'auto',
371 modifiers: { flip: { boundariesElement: 'scrollParent' } },
372 onCreate: () => {
373 const right = getRect(popper).right + arrowSize;
374 expect(right).toBeApprox(getRect(reference).left);
375 pop.destroy();
376 done();
377 },
378 });
379 });
380 });
381
382 it('inits a bottom popper attached to an inline reference', done => {
383 const reference = appendNewRef(1);
384 reference.style.display = 'inline';
385 const popper = appendNewPopper(2);
386
387 const pop = new Popper(reference, popper);
388
389 // give some time to the browser to render...
390 // otherwise `getBoundingClientRect` returns wrong values
391 setTimeout(() => {
392 const top = getRect(popper).top;
393 expect(top).toBeApprox(getRect(reference).bottom + arrowSize);
394 pop.destroy();
395 done();
396 }, 10);
397 });
398
399 it('inits a bottom-start popper', done => {
400 const reference = appendNewRef(1);
401 reference.style.marginLeft = '200px';
402 const popper = appendNewPopper(2);
403
404 new Popper(reference, popper, {
405 placement: 'bottom-start',
406 onCreate: data => {
407 expect(getRect(popper).left).toBeApprox(getRect(reference).left);
408
409 data.instance.destroy();
410 done();
411 },
412 });
413 });
414
415 it('inits a right popper', done => {
416 const reference = appendNewRef(1);
417 const popper = appendNewPopper(2);
418
419 new Popper(reference, popper, {
420 placement: 'right',
421 onCreate: data => {
422 expect(getRect(popper).left - arrowSize).toBeApprox(
423 getRect(reference).right
424 );
425
426 data.instance.destroy();
427 done();
428 },
429 });
430 });
431
432 it('inits a popper inside a scrolling div, contained in a relative div', done => {
433 const relative = document.createElement('div');
434 relative.style.width = '800px';
435 relative.style.height = '700px';
436 relative.style.position = 'relative';
437 relative.style.backgroundColor = 'green';
438 relative.style.border = '1px solid';
439 jasmineWrapper.appendChild(relative);
440
441 const scrolling = document.createElement('div');
442 scrolling.style.width = '800px';
443 scrolling.style.height = '500px';
444 scrolling.style.overflow = 'auto';
445 scrolling.style.backgroundColor = 'blue';
446 scrolling.style.marginTop = '100px';
447 relative.appendChild(scrolling);
448
449 const superHigh1 = document.createElement('div');
450 superHigh1.style.width = '800px';
451 superHigh1.style.height = '450px';
452 scrolling.appendChild(superHigh1);
453
454 const ref = appendNewRef(1, 'ref', scrolling);
455 const popper = appendNewPopper(2, 'popper', scrolling);
456
457 const superHigh2 = document.createElement('div');
458 superHigh2.style.width = '800px';
459 superHigh2.style.height = '500px';
460 scrolling.appendChild(superHigh2);
461
462 simulateScroll(scrolling, { scrollTop: 400 });
463 new Popper(ref, popper, {
464 placement: 'top',
465 onCreate: () => {
466 // placement should be top
467 expect(getRect(popper).bottom + arrowSize).toBeApprox(getRect(ref).top);
468
469 simulateScroll(scrolling, { scrollTop: 100, delay: 10 });
470 },
471 onUpdate: data => {
472 // placement should be top
473 expect(getRect(popper).bottom + arrowSize).toBeApprox(getRect(ref).top);
474
475 data.instance.destroy();
476 done();
477 },
478 });
479 });
480
481 it('inits a popper inside a scrolling div with an huge border', done => {
482 const scrolling = document.createElement('div');
483 scrolling.style.width = '800px';
484 scrolling.style.height = '500px';
485 scrolling.style.overflow = 'auto';
486 scrolling.style.backgroundColor = 'blue';
487 scrolling.style.border = '50px green solid';
488 jasmineWrapper.appendChild(scrolling);
489
490 const superHigh1 = document.createElement('div');
491 superHigh1.style.width = '800px';
492 superHigh1.style.height = '450px';
493 scrolling.appendChild(superHigh1);
494
495 const ref = appendNewRef(1, 'ref', scrolling);
496 const popper = appendNewPopper(2, 'popper', scrolling);
497
498 const superHigh2 = document.createElement('div');
499 superHigh2.style.width = '800px';
500 superHigh2.style.height = '500px';
501 scrolling.appendChild(superHigh2);
502
503 simulateScroll(scrolling, { scrollTop: 400 });
504 new Popper(ref, popper, {
505 placement: 'top',
506 onCreate: () => {
507 //placement should be top
508 expect(getRect(popper).bottom + arrowSize).toBeApprox(getRect(ref).top);
509
510 simulateScroll(scrolling, { scrollTop: 100, delay: 10 });
511 },
512 onUpdate: data => {
513 // placement should be top
514 expect(getRect(popper).bottom + arrowSize).toBeApprox(getRect(ref).top);
515
516 data.instance.destroy();
517 done();
518 },
519 });
520 });
521
522 it('inits a popper inside a body, with its reference element inside a relative div', done => {
523 const relative = document.createElement('div');
524 relative.style.position = 'relative';
525 relative.style.margin = '20px';
526 jasmineWrapper.appendChild(relative);
527
528 const ref = appendNewRef(1, 'ref', relative);
529 const popper = appendNewPopper(2, 'popper');
530
531 new Popper(ref, popper, {
532 onCreate: data => {
533 expect(getRect(popper).top - arrowSize).toBeApprox(getRect(ref).bottom);
534 expect(getRect(popper).left).toBeApprox(5);
535 data.instance.destroy();
536 done();
537 },
538 });
539 });
540
541 it('inits a popper inside a body without any positioned or scrollable parents and scrolls the body', done => {
542 const ref = appendNewRef(1, 'ref');
543 const popper = appendNewPopper(2, 'popper');
544 document.body.scrollTop = 100;
545
546 new Popper(ref, popper, {
547 onCreate: data => {
548 expect(getRect(popper).top - arrowSize).toBeApprox(getRect(ref).bottom);
549 expect(getRect(popper).left).toBeApprox(5);
550 data.instance.destroy();
551 done();
552 },
553 });
554 });
555
556 it('inits a popper inside a scrolled body, with its reference element inside a relative div', done => {
557 const relative = document.createElement('div');
558 relative.style.position = 'relative';
559 relative.style.margin = '20px';
560 relative.style.height = '300vh';
561 jasmineWrapper.appendChild(relative);
562 simulateScroll(document.body, { scrollTop: 300 });
563
564 const ref = appendNewRef(1, 'ref', relative);
565 ref.style.marginTop = '300px';
566 const popper = appendNewPopper(2, 'popper');
567
568 new Popper(ref, popper, {
569 onCreate: data => {
570 expect(getRect(popper).top - arrowSize).toBeApprox(getRect(ref).bottom);
571 expect(getRect(popper).left).toBeApprox(5);
572 data.instance.destroy();
573 done();
574 },
575 });
576 });
577
578 it('inits a popper near a reference element, both inside a fixed element, inside a scrolled body', done => {
579 //IE10 bug where elements who are checking for getBoundingClientRect too early throe exceptionn
580 if(isIE10 && positionFixed){
581 pending();
582 }
583 const fixed = document.createElement('div');
584 fixed.style.position = 'fixed';
585 fixed.style.margin = '20px';
586 fixed.style.height = '50px';
587 fixed.style.width = '100%';
588 fixed.style.backgroundColor = 'grey';
589 jasmineWrapper.appendChild(fixed);
590
591 const relative = document.createElement('div');
592 relative.style.position = 'relative';
593 relative.style.margin = '20px';
594 relative.style.height = '200vh';
595 jasmineWrapper.appendChild(relative);
596 simulateScroll(document.body, { scrollTop: 800 });
597
598 const ref = appendNewRef(1, 'ref', fixed);
599 const popper = appendNewPopper(2, 'popper', fixed);
600
601 new Popper(ref, popper, {
602 onCreate: data => {
603 expect(getRect(popper).top - arrowSize).toBeApprox(getRect(ref).bottom);
604 expect(getRect(popper).left).toBeApprox(5);
605 data.instance.destroy();
606 done();
607 },
608 });
609 });
610
611 it('inits a popper near a reference element, both inside a fixed element with CSS transforms, inside a scrolled body', done => {
612 //IE10 bug where elements who are checking for getBoundingClientRect too early throe exceptionn
613 if(isIE10 && positionFixed){
614 pending();
615 }
616 const relative = document.createElement('div');
617 relative.style.position = 'relative';
618 relative.style.margin = '20px';
619 relative.style.height = '200vh';
620 relative.style.background = 'rgba(100, 100, 100, 0.5)';
621
622 const fixed = document.createElement('div');
623 fixed.style.position = 'fixed';
624 fixed.style.margin = '20px';
625 fixed.style.height = '50px';
626 fixed.style.width = '100%';
627 fixed.style.transform = 'translateX(0.1)';
628 fixed.style.background = 'green';
629 relative.appendChild(fixed);
630
631 jasmineWrapper.appendChild(relative);
632
633 const ref = appendNewRef(1, 'ref', fixed);
634 const popper = appendNewPopper(2, 'popper', fixed);
635
636 simulateScroll(document.body, { scrollTop: 800 });
637
638 new Popper(ref, popper, {
639 onCreate: data => {
640 expect(getRect(popper).top - arrowSize).toBeApprox(getRect(ref).bottom);
641 expect(getRect(popper).left).toBeApprox(5);
642 data.instance.destroy();
643 done();
644 },
645 });
646 });
647
648 it('inits a popper near a reference element, both inside a fixed element on bottom of viewport, inside a scrolled body', done => {
649 const fixed = document.createElement('div');
650 fixed.style.position = 'fixed';
651 fixed.style.bottom = '5px';
652 fixed.style.height = '38px';
653 fixed.style.width = '100%';
654 jasmineWrapper.appendChild(fixed);
655
656 const relative = document.createElement('div');
657 relative.style.position = 'relative';
658 relative.style.margin = '20px';
659 relative.style.height = '200vh';
660 jasmineWrapper.appendChild(relative);
661 simulateScroll(document.body, { scrollTop: 800 });
662
663 const ref = appendNewRef(1, 'ref', fixed);
664 const popper = appendNewPopper(2, 'popper', fixed);
665
666 new Popper(ref, popper, {
667 placement: 'top',
668 onCreate: data => {
669 expect(getRect(popper).bottom + arrowSize).toBeApprox(getRect(ref).top);
670 expect(getRect(popper).left).toBeApprox(5);
671 expect(popper.getAttribute('x-placement')).toBe('top');
672 data.instance.destroy();
673 done();
674 },
675 });
676 });
677
678 it('inits a popper and destroy it using its callback', done => {
679 const reference = appendNewRef(1);
680 const popper = appendNewPopper(2);
681
682 new Popper(reference, popper, {
683 onCreate: data => {
684 data.instance.destroy();
685 expect(popper.style.top).toBe('');
686 done();
687 },
688 });
689 });
690
691 it('inits a popper with an empty form as parent, then auto remove it on destroy', done => {
692 const form = document.createElement('form');
693 const reference = appendNewRef(1, 'ref', form);
694 const popper = appendNewPopper(2, 'test', form);
695 jasmineWrapper.appendChild(form);
696
697 new Popper(reference, popper, {
698 removeOnDestroy: true,
699 onCreate: data => {
700 expect(data.instance.popper).toBeDefined();
701 expect(data.instance.popper.innerText.trim()).toBe('test');
702 data.instance.destroy();
703 expect(document.body.contains(data.instance.popper)).toBeFalsy();
704 done();
705 },
706 });
707 });
708
709 it('inits a popper with a not empty form as parent, then auto remove it on destroy', done => {
710 const form = document.createElement('form');
711 const input = document.createElement('input');
712 const popper = appendNewPopper(2, 'test', form);
713 form.appendChild(input);
714 const reference = appendNewRef(1, 'ref', form);
715 jasmineWrapper.appendChild(form);
716
717 new Popper(reference, popper, {
718 removeOnDestroy: true,
719 onCreate: data => {
720 expect(data.instance.popper).toBeDefined();
721 expect(data.instance.popper.innerText.trim()).toBe('test');
722 data.instance.destroy();
723 expect(document.body.contains(data.instance.popper)).toBeFalsy();
724 done();
725 },
726 });
727 });
728
729 it('inits a popper and make sure its position is correct on init', done => {
730 const reference = appendNewRef(1);
731 const popper = appendNewPopper(2, 'popper');
732
733 new Popper(reference, popper, {
734 placement: 'right',
735 removeOnDestroy: true,
736 onCreate: data => {
737 expect(getRect(popper).left - arrowSize).toBeApprox(
738 getRect(reference).right
739 );
740 data.instance.destroy();
741 done();
742 },
743 });
744 });
745
746 it('inits a popper inside a scrolled body, with its reference element inside a scrolling div, wrapped in a relative div', done => {
747
748 const relative = document.createElement('div');
749 relative.style.position = 'relative';
750 relative.style.margin = '20px';
751 relative.style.height = '200vh';
752 relative.style.paddingTop = '100px';
753 relative.style.backgroundColor = 'yellow';
754 jasmineWrapper.appendChild(relative);
755 simulateScroll(document.body, { scrollTop: 100 });
756
757 const scrolling = document.createElement('div');
758 scrolling.style.width = '100%';
759 scrolling.style.height = '100vh';
760 scrolling.style.overflow = 'auto';
761 scrolling.style.backgroundColor = 'green';
762 relative.appendChild(scrolling);
763
764 const superHigh = document.createElement('div');
765 superHigh.style.width = '1px';
766 superHigh.style.float = 'right';
767 superHigh.style.height = '300vh';
768 scrolling.appendChild(superHigh);
769
770 simulateScroll(scrolling, { scrollTop: 100 });
771
772 const ref = appendNewRef(1, 'ref', scrolling);
773 ref.style.width = '100px';
774 ref.style.height = '100px';
775 ref.style.marginTop = '100px';
776 const popper = appendNewPopper(2, 'popper', scrolling);
777
778 new Popper(ref, popper, {
779 placement: 'right-start',
780 onCreate: data => {
781 expect(getRect(popper).top).toBeApprox(getRect(ref).top + 5); // 5 is the boundaries margin
782 expect(getRect(popper).left - arrowSize).toBeApprox(getRect(ref).right);
783
784 data.instance.destroy();
785 done();
786 },
787 });
788 });
789
790 it('inits a popper and its reference element inside of an offsetParent, which is inside of an offsetParent', done => {
791 const outterWrapper = document.createElement('div');
792 outterWrapper.style.position = 'relative';
793 outterWrapper.style.left = '90vw';
794 outterWrapper.style.backgroundColor = 'yellow';
795 jasmineWrapper.appendChild(outterWrapper);
796
797 const wrapper = document.createElement('div');
798 wrapper.style.position = 'relative';
799 wrapper.style.left = '5vw';
800 wrapper.style.backgroundColor = 'green';
801 outterWrapper.appendChild(wrapper);
802
803 const ref = appendNewRef(1, 'ref', wrapper);
804 ref.style.width = '100px';
805 ref.style.height = '100px';
806 ref.style.marginTop = '100px';
807 wrapper.appendChild(ref);
808
809 const popper = document.createElement('div');
810 popper.style.width = '100px';
811 popper.style.height = '100px';
812 wrapper.appendChild(popper);
813
814 new Popper(ref, popper, {
815 modifiers: {
816 preventOverflow: {
817 boundariesElement: 'viewport',
818 },
819 },
820 onCreate: data => {
821 expect(getRect(popper).right).toBeApprox(window.innerWidth - 5); // 5 is the boundaries margin
822
823 data.instance.destroy();
824 done();
825 },
826 });
827 });
828
829
830 it('inits a popper with boundariesElement set to viewport, the popper should not be in the viewport', done => {
831 //IE10 bug where elements who are checking for getBoundingClientRect too early throe exceptionn
832 if (isIPHONE || (isIE10 && positionFixed)) {
833 pending();
834 }
835
836 const relative = document.createElement('div');
837 relative.style.position = 'relative';
838 relative.style.margin = '20px';
839 relative.style.paddingTop = '100px';
840 relative.style.backgroundColor = 'yellow';
841 jasmineWrapper.appendChild(relative);
842 simulateScroll(document.body, { scrollTop: 100 });
843
844 const ref = appendNewRef(1, 'ref', relative);
845 ref.style.width = '100px';
846 ref.style.height = '100px';
847 ref.style.marginTop = '2000px';
848 ref.style.marginBottom = '200px';
849 const popper = appendNewPopper(2, 'popper', relative);
850
851 new Popper(ref, popper, {
852 placement: 'bottom',
853 modifiers: {
854 flip: {
855 boundariesElement: 'viewport',
856 },
857 },
858 onCreate: () => {
859 expect(getRect(popper).bottom + arrowSize).toBeApprox(getRect(ref).top);
860 simulateScroll(document.body, { scrollTop: getRect(ref).top });
861 },
862 onUpdate: data => {
863 expect(getRect(popper).top - arrowSize).toBeApprox(getRect(ref).bottom);
864 data.instance.destroy();
865 done();
866 },
867 });
868 });
869
870
871 it('inits a popper with a custom modifier that should hide it', done => {
872 const reference = appendNewRef(1);
873 const popper = appendNewPopper(2);
874
875 function hidePopper(data) {
876 data.styles.display = 'none';
877 return data;
878 }
879
880 new Popper(reference, popper, {
881 modifiers: {
882 hidePopper: { order: 790, enabled: true, fn: hidePopper },
883 },
884 onCreate: data => {
885 expect(popper.style.display).toBe('none');
886 data.instance.destroy();
887 done();
888 },
889 });
890 });
891
892 it('inits a popper with a custom modifier that set its top to 3px', done => {
893 const reference = appendNewRef(1);
894 const popper = appendNewPopper(2);
895
896 function movePopper(data) {
897 data.styles.top = '3px';
898 return data;
899 }
900
901 new Popper(reference, popper, {
902 modifiers: {
903 movePopper: { order: 690, enabled: true, fn: movePopper },
904 },
905 onCreate: data => {
906 expect(popper.style.top).toBe('3px');
907 data.instance.destroy();
908 done();
909 },
910 });
911 });
912
913 it('inits a popper inside a Shadow DOM with its reference element inside DOM', done => {
914 if (!document.head.createShadowRoot) {
915 return done();
916 }
917
918 const reference = appendNewRef(1);
919
920 const shadowParent = document.createElement('div');
921 jasmineWrapper.appendChild(shadowParent);
922 const shadow = shadowParent.createShadowRoot();
923
924 const popper = appendNewPopper(2, 'popper', shadow);
925
926 new Popper(reference, popper, {
927 placement: 'right',
928 onCreate: data => {
929 expect(getRect(popper).left).toBeApprox(getRect(reference).right);
930 data.instance.destroy();
931 done();
932 },
933 });
934 });
935
936 it('inits a popper inside DOM with its reference element inside Shadow DOM', done => {
937 if (!document.head.createShadowRoot) {
938 return done();
939 }
940
941 const shadowParent1 = document.createElement('div');
942 jasmineWrapper.appendChild(shadowParent1);
943 const shadow1 = shadowParent1.createShadowRoot();
944
945 const reference = appendNewRef(1, 'reference', shadow1);
946 reference.style.display = 'block';
947 reference.style.width = '100px';
948
949 const popper = appendNewPopper(2, 'popper');
950
951 new Popper(reference, popper, {
952 placement: 'right',
953 onCreate: data => {
954 expect(getRect(popper).left - arrowSize).toBeApprox(
955 getRect(reference).right
956 );
957 data.instance.destroy();
958 done();
959 },
960 });
961 });
962
963 it('inits a popper inside a Shadow DOM with its reference element inside different Shadow DOM', done => {
964 if (!document.head.createShadowRoot) {
965 return done();
966 }
967
968 const shadowParent1 = document.createElement('div');
969 jasmineWrapper.appendChild(shadowParent1);
970 const shadow1 = shadowParent1.createShadowRoot();
971
972 const reference = appendNewRef(1, 'reference', shadow1);
973 reference.style.display = 'block';
974 reference.style.width = '100px';
975
976 const shadowParent2 = document.createElement('div');
977 jasmineWrapper.appendChild(shadowParent2);
978 const shadow2 = shadowParent2.createShadowRoot();
979
980 const popper = appendNewPopper(2, 'popper', shadow2);
981
982 new Popper(reference, popper, {
983 placement: 'right',
984 onCreate: data => {
985 expect(getRect(popper).left).toBeApprox(getRect(reference).right);
986 data.instance.destroy();
987 done();
988 },
989 });
990 });
991
992 it('init a bottom-end popper near the right side of its container and make sure it stays between boundaries', done => {
993 const container = document.createElement('div');
994 container.style.width = '800px';
995 container.style.height = '600px';
996 container.style.background = 'grey';
997 container.style.position = 'relative';
998 container.style.overflow = 'scroll';
999
1000 const scroller = document.createElement('div');
1001 scroller.style.width = '2000px';
1002 scroller.style.height = '1px';
1003
1004 const button = document.createElement('div');
1005 button.style.width = '20px';
1006 button.style.height = '50px';
1007 button.style.position = 'absolute';
1008 button.style.top = 0;
1009 button.style.right = 0;
1010 button.style.background = 'green';
1011
1012 const dropdown = document.createElement('div');
1013 dropdown.style.background = 'yellow';
1014 dropdown.style.position = 'absolute';
1015 dropdown.style.height = '200px';
1016 dropdown.style.width = '150px';
1017
1018 container.appendChild(button);
1019 container.appendChild(dropdown);
1020 container.appendChild(scroller);
1021 jasmineWrapper.appendChild(container);
1022
1023 const scrollLeft = 50;
1024
1025 new Popper(button, dropdown, {
1026 placement: 'bottom-end',
1027 onCreate: () => {
1028 expect(getRect(dropdown).right).toBeApprox(
1029 getRect(container).right - 5
1030 );
1031 container.scrollLeft = scrollLeft;
1032 },
1033 onUpdate: data => {
1034 expect(getRect(dropdown).right).toBeApprox(
1035 getRect(container).right - scrollLeft
1036 );
1037 data.instance.destroy();
1038 done();
1039 },
1040 });
1041 });
1042
1043 it('init a bottom-start popper near the right side of its container and make sure it stays between boundaries', done => {
1044 const container = document.createElement('div');
1045 container.style.width = '800px';
1046 container.style.height = '600px';
1047 container.style.background = 'grey';
1048 container.style.position = 'relative';
1049 container.style.overflow = 'scroll';
1050
1051 const scroller = document.createElement('div');
1052 scroller.style.width = '2000px';
1053 scroller.style.height = '1px';
1054
1055 const button = document.createElement('div');
1056 button.style.width = '20px';
1057 button.style.height = '50px';
1058 button.style.position = 'absolute';
1059 button.style.top = 0;
1060 button.style.right = 0;
1061 button.style.background = 'green';
1062
1063 const dropdown = document.createElement('div');
1064 dropdown.style.background = 'yellow';
1065 dropdown.style.position = 'absolute';
1066 dropdown.style.height = '200px';
1067 dropdown.style.width = '150px';
1068
1069 container.appendChild(button);
1070 container.appendChild(dropdown);
1071 container.appendChild(scroller);
1072 jasmineWrapper.appendChild(container);
1073
1074 const scrollLeft = 50;
1075
1076 new Popper(button, dropdown, {
1077 placement: 'bottom-start',
1078 modifiers: {
1079 //preventOverflow: { enabled: false },
1080 //flip: { enabled: false },
1081 },
1082 onCreate: () => {
1083 expect(getRect(dropdown).right).toBeApprox(
1084 getRect(container).right - 5
1085 );
1086 container.scrollLeft = scrollLeft;
1087 },
1088 onUpdate: data => {
1089 expect(getRect(dropdown).right).toBeApprox(
1090 getRect(container).right - 5
1091 );
1092 data.instance.destroy();
1093 done();
1094 },
1095 });
1096 });
1097
1098 it('init a popper near the right side of its container and make sure it stays between custom boundaries', done => {
1099 const container = document.createElement('div');
1100 container.style.width = '800px';
1101 container.style.height = '600px';
1102 container.style.background = 'grey';
1103 container.style.position = 'relative';
1104 container.style.overflow = 'scroll';
1105
1106 const scroller = document.createElement('div');
1107 scroller.style.width = '2000px';
1108 scroller.style.height = '1px';
1109
1110 const button = document.createElement('div');
1111 button.style.width = '20px';
1112 button.style.height = '50px';
1113 button.style.position = 'absolute';
1114 button.style.top = 0;
1115 button.style.right = 0;
1116 button.style.background = 'green';
1117
1118 const dropdown = document.createElement('div');
1119 dropdown.style.background = 'yellow';
1120 dropdown.style.position = 'absolute';
1121 dropdown.style.height = '200px';
1122 dropdown.style.width = '150px';
1123
1124 container.appendChild(button);
1125 container.appendChild(dropdown);
1126 container.appendChild(scroller);
1127 jasmineWrapper.appendChild(container);
1128
1129 const scrollLeft = 50;
1130
1131 new Popper(button, dropdown, {
1132 placement: 'bottom-end',
1133 modifiers: {
1134 preventOverflow: { boundariesElement: container },
1135 },
1136 onCreate: () => {
1137 expect(getRect(dropdown).right).toBeApprox(
1138 getRect(container).right - 5
1139 );
1140 container.scrollLeft = scrollLeft;
1141 },
1142 onUpdate: data => {
1143 expect(getRect(dropdown).right).toBeApprox(
1144 getRect(container).right - scrollLeft
1145 );
1146 data.instance.destroy();
1147 done();
1148 },
1149 });
1150 });
1151
1152 it('init a popper near the right side of a custom container and make sure it stays between boundaries', done => {
1153 const container = document.createElement('div');
1154 container.style.width = '800px';
1155 container.style.height = '600px';
1156 container.style.background = 'grey';
1157 container.style.position = 'relative';
1158 container.style.overflow = 'scroll';
1159
1160 const scroller = document.createElement('div');
1161 scroller.style.width = '2000px';
1162 scroller.style.height = '1px';
1163
1164 const button = document.createElement('div');
1165 button.style.width = '20px';
1166 button.style.height = '50px';
1167 button.style.position = 'absolute';
1168 button.style.top = 0;
1169 button.style.right = 0;
1170 button.style.background = 'green';
1171
1172 const dropdown = document.createElement('div');
1173 dropdown.style.background = 'yellow';
1174 dropdown.style.position = 'absolute';
1175 dropdown.style.height = '200px';
1176 dropdown.style.width = '150px';
1177
1178 container.appendChild(button);
1179 container.appendChild(scroller);
1180 jasmineWrapper.appendChild(container);
1181 jasmineWrapper.appendChild(dropdown);
1182
1183 const scrollLeft = 50;
1184
1185 new Popper(button, dropdown, {
1186 placement: 'bottom-end',
1187 modifiers: {
1188 preventOverflow: { boundariesElement: container },
1189 },
1190 onCreate: () => {
1191 expect(getRect(dropdown).right).toBe(getRect(container).right - 5);
1192 container.scrollLeft = scrollLeft;
1193 },
1194 onUpdate: data => {
1195 expect(getRect(dropdown).right).toBe(
1196 getRect(container).right - scrollLeft
1197 );
1198 data.instance.destroy();
1199 done();
1200 },
1201 });
1202 });
1203
1204 it('inits a popper near the left side of a custom absolute container contained in a relative parent', done => {
1205 const parent = document.createElement('div');
1206 parent.style.width = '600px';
1207 parent.style.height = '300px';
1208 parent.style.top = '50px';
1209 parent.style.left = '80px';
1210 parent.style.background = 'grey';
1211 parent.style.position = 'relative';
1212 parent.style.overflow = 'scroll';
1213
1214 const container = document.createElement('div');
1215 container.style.top = 0;
1216 container.style.right = 0;
1217 container.style.bottom = 0;
1218 container.style.left = 0;
1219 container.style.position = 'absolute';
1220
1221 const button = document.createElement('div');
1222 button.style.width = '20px';
1223 button.style.height = '50px';
1224 button.style.position = 'absolute';
1225 button.style.top = 0;
1226 button.style.left = 0;
1227 button.style.background = 'green';
1228
1229 const dropdown = document.createElement('div');
1230 dropdown.style.background = 'yellow';
1231 dropdown.style.position = 'absolute';
1232 dropdown.style.height = '200px';
1233 dropdown.style.width = '150px';
1234
1235 container.appendChild(button);
1236 parent.appendChild(container);
1237 jasmineWrapper.appendChild(parent);
1238 jasmineWrapper.appendChild(dropdown);
1239
1240 new Popper(button, dropdown, {
1241 placement: 'bottom-end',
1242 modifiers: {
1243 preventOverflow: { boundariesElement: container },
1244 },
1245 onCreate: data => {
1246 expect(getRect(dropdown).left).toBe(getRect(container).left + 5);
1247
1248 data.instance.destroy();
1249 done();
1250 },
1251 });
1252 });
1253
1254
1255 it('inits a popper near a reference element, both inside an element with CSS translateX', done => {
1256 //Not relevant for positionFixed cases
1257 if(positionFixed) {
1258 pending();
1259 }
1260 const fixed = document.createElement('div');
1261 fixed.style.position = 'fixed';
1262 fixed.style.left = '50%';
1263 fixed.style.top = '20px';
1264 fixed.style.transform = 'translateX(0)';
1265 fixed.style.background = 'green';
1266 jasmineWrapper.appendChild(fixed);
1267
1268 const popper = appendNewPopper(2, 'popper', fixed);
1269 const ref = appendNewRef(1, 'ref', fixed);
1270
1271 new Popper(ref, popper, {
1272 placement: 'bottom-end',
1273 onCreate: data => {
1274 expect(getRect(popper).right).toBeApprox(getRect(ref).right);
1275 data.instance.destroy();
1276 done();
1277 },
1278 });
1279 });
1280
1281
1282 it('inits a popper near the reference element when it is a child of ref and a parent is relatively positioned', done => {
1283 const relative = document.createElement('div');
1284 relative.style.position = 'relative';
1285 relative.style.left = '50%';
1286 relative.style.top = '20px';
1287 relative.style.background = 'green';
1288 jasmineWrapper.appendChild(relative);
1289
1290 const ref = appendNewRef(1, 'ref', relative);
1291 const popper = appendNewPopper(2, 'popper', ref);
1292
1293 new Popper(ref, popper, {
1294 placement: 'bottom-end',
1295 onCreate: data => {
1296 expect(getRect(popper).right).toBeApprox(getRect(ref).right);
1297 data.instance.destroy();
1298 done();
1299 },
1300 });
1301 });
1302
1303 it('inits a popper near the reference element when it is a child of ref and the ref is relatively positioned', done => {
1304 const ref = appendNewRef(1, 'ref');
1305 ref.style.position = 'relative';
1306 ref.style.left = '300px';
1307 ref.style.top = '20px';
1308 ref.style.background = 'green';
1309
1310 const popper = appendNewPopper(2, 'popper', ref);
1311
1312 new Popper(ref, popper, {
1313 placement: 'bottom-end',
1314 onCreate: data => {
1315 expect(getRect(popper).right).toBeApprox(getRect(ref).right);
1316 data.instance.destroy();
1317 done();
1318 },
1319 });
1320 });
1321
1322 it('inits a popper near the reference element when it is a child of ref and the ref is relatively positioned and the ref contains svg as a first child', done => {
1323 if (isIE10 && !positionFixed) {
1324 pending();
1325 }
1326 const ref = appendNewRef(1);
1327 ref.style.position = 'absolute';
1328 ref.style.margin = '0';
1329 ref.style.padding = '0';
1330 ref.style.top = '50px';
1331 ref.style.left = '50px';
1332 ref.style.height = '100px';
1333 ref.style.width = '100px';
1334 ref.style.background = 'green';
1335
1336 ref.innerHTML = `<svg fill="currentColor" preserveAspectRatio="xMidYMid meet" height="1.5em" width="1.5em" viewBox="0 0 40 40" style="vertical-align: middle;">
1337 <g>
1338 <path d="m20 0c-11 0-20 9-20 20 0 8.8 5.7 16.3 13.7 19 1 0.2 1.3-0.5 1.3-1 0-0.5 0-2 0-3.7-5.5 1.2-6.7-2.4-6.7-2.4-0.9-2.3-2.2-2.9-2.2-2.9-1.9-1.2 0.1-1.2 0.1-1.2 2 0.1 3.1 2.1 3.1 2.1 1.7 3 4.6 2.1 5.8 1.6 0.2-1.3 0.7-2.2 1.3-2.7-4.5-0.5-9.2-2.2-9.2-9.8 0-2.2 0.8-4 2.1-5.4-0.2-0.5-0.9-2.6 0.2-5.3 0 0 1.7-0.5 5.5 2 1.6-0.4 3.3-0.6 5-0.6 1.7 0 3.4 0.2 5 0.7 3.8-2.6 5.5-2.1 5.5-2.1 1.1 2.8 0.4 4.8 0.2 5.3 1.3 1.4 2.1 3.2 2.1 5.4 0 7.6-4.7 9.3-9.2 9.8 0.7 0.6 1.4 1.9 1.4 3.7 0 2.7 0 4.9 0 5.5 0 0.6 0.3 1.2 1.3 1 8-2.7 13.7-10.2 13.7-19 0-11-9-20-20-20z"></path>
1339 </g>
1340 </svg>`;
1341 ref.appendChild(document.createTextNode('ref'));
1342
1343 const popper = appendNewPopper(2, 'popper', ref);
1344 popper.style.margin = '0';
1345 popper.style.padding = '0';
1346
1347 new Popper(ref, popper, {
1348 placement: 'bottom-start',
1349 onCreate: data => {
1350 expect(getRect(popper).left).toBeApprox(getRect(ref).left);
1351 expect(getRect(popper).top).toBeApprox(getRect(ref).bottom);
1352 data.instance.destroy();
1353 done();
1354 },
1355 });
1356 });
1357
1358 it('checks that all the scrollable parents have an event listener attached', done => {
1359 jasmineWrapper.innerHTML = `
1360 <div id="s1" style="overflow: scroll; height: 300px; background: red;">
1361 <div id="s2" style="overflow: scroll; height: 300px; margin-top: 50px; background: green;">
1362 <div style="overflow: scroll; height: 300px; margin-top: 50px; background: yellow;">
1363 <div id="reference" style="background: pink">popper</div>
1364 </div>
1365 </div>
1366 </div>
1367 <div id="popper" style="background: purple">popper</div>
1368 `;
1369
1370 const reference = document.getElementById('reference');
1371 const popper = document.getElementById('popper');
1372 const s1 = document.getElementById('s1');
1373 const s2 = document.getElementById('s2');
1374
1375 new Popper(reference, popper, {
1376 onCreate() {
1377 simulateScroll(s1, { scrollTop: 50 });
1378 simulateScroll(s2, { scrollTop: 50 });
1379 },
1380 onUpdate(data) {
1381 expect(getRect(reference).bottom).toBeApprox(getRect(popper).top);
1382 data.instance.destroy();
1383 done();
1384 },
1385 });
1386 });
1387
1388 it('uses an object instead of the reference element to position its popper', done => {
1389 // Skip IE10 here because the current implementation doesn't work with it and
1390 // I don't want to add extra logic to support a "nice to have" feature on an
1391 // ancient browser
1392 if (isIE10) {
1393 pending();
1394 }
1395
1396 jasmineWrapper.innerHTML = `
1397 <div id="popper" style="background: purple;">popper</div>
1398 `;
1399
1400 const popper = document.getElementById('popper');
1401
1402 const reference = {
1403 getBoundingClientRect() {
1404 return {
1405 top: 10,
1406 left: 100,
1407 right: 150,
1408 bottom: 90,
1409 width: 50,
1410 height: 80,
1411 };
1412 },
1413 clientWidth: 50,
1414 clientHeight: 80,
1415 };
1416
1417 new Popper(reference, popper, {
1418 placement: 'bottom-start',
1419 onCreate() {
1420 expect(getRect(popper).top).toBe(
1421 reference.getBoundingClientRect().bottom
1422 );
1423 expect(getRect(popper).left).toBe(
1424 reference.getBoundingClientRect().left
1425 );
1426 done();
1427 },
1428 });
1429 });
1430
1431 it('uses a reference object with a referenceNode to position its popper', done => {
1432 // As above
1433 if (isIE10) {
1434 pending();
1435 }
1436
1437 jasmineWrapper.innerHTML = `
1438 <div id="reference" style="position: absolute; left: 10px; top: 10px; right: 10px; bottom: 10px;">
1439 <div id="popper" style="background: purple;">popper</div>
1440 </div>
1441 `;
1442
1443 const popper = document.getElementById('popper');
1444
1445 const reference = {
1446 getBoundingClientRect() {
1447 return {
1448 top: 10,
1449 left: 100,
1450 right: 150,
1451 bottom: 90,
1452 width: 50,
1453 height: 80,
1454 };
1455 },
1456 clientWidth: 50,
1457 clientHeight: 80,
1458 referenceNode: document.getElementById('reference'),
1459 };
1460
1461 new Popper(reference, popper, {
1462 placement: 'bottom-start',
1463 onCreate() {
1464 expect(getRect(popper).top).toBe(
1465 reference.getBoundingClientRect().bottom
1466 );
1467 expect(getRect(popper).left).toBe(
1468 reference.getBoundingClientRect().left
1469 );
1470 done();
1471 },
1472 });
1473 });
1474
1475 it('checks cases where the reference element is fixed', done => {
1476 jasmineWrapper.innerHTML = `
1477 <div id="reference" style="position: fixed; top: 100px; left: 100px; background: pink">reference</div>
1478 <div id="popper" style="background: purple">popper</div>
1479 `;
1480
1481 const reference = document.getElementById('reference');
1482 const popper = document.getElementById('popper');
1483
1484 new Popper(reference, popper, {
1485 positionFixed: true,
1486 placement: 'bottom-start',
1487 onCreate() {
1488 expect(popper.getBoundingClientRect().top).toBeApprox(
1489 reference.getBoundingClientRect().bottom
1490 );
1491 expect(popper.getBoundingClientRect().left).toBeApprox(
1492 reference.getBoundingClientRect().left
1493 );
1494 done();
1495 },
1496 });
1497 });
1498
1499 it('checks cases where the reference element is fixed in scrolling parent', done => {
1500 jasmineWrapper.innerHTML = `
1501 <div id="scroll" style="height: 100vh; overflow: scroll">
1502 <div id="reference" style="position: fixed; top: 100px; left: 100px; background: pink">reference</div>
1503 <div id="popper" style="background: purple">popper</div>
1504 <div style="height: 200vh"></div>
1505 </div>
1506 `;
1507
1508 const reference = document.getElementById('reference');
1509 const popper = document.getElementById('popper');
1510 const scrolling = document.getElementById('scroll');
1511
1512 new Popper(reference, popper, {
1513 placement: 'bottom-start',
1514 onCreate() {
1515 simulateScroll(scrolling, { scrollTop: 50 });
1516 },
1517 onUpdate() {
1518 expect(popper.getBoundingClientRect().top).toBeApprox(
1519 reference.getBoundingClientRect().bottom
1520 );
1521 expect(popper.getBoundingClientRect().left).toBeApprox(
1522 reference.getBoundingClientRect().left
1523 );
1524 done();
1525 },
1526 });
1527 });
1528
1529 it('checks that positionFixed works correctly', done => {
1530 jasmineWrapper.innerHTML = `
1531 <div id="reference" style="background: pink">reference</div>
1532 <div id="popper" style="background: purple">popper</div>
1533 `;
1534
1535 const reference = document.getElementById('reference');
1536 const popper = document.getElementById('popper');
1537
1538 new Popper(reference, popper, {
1539 positionFixed: true,
1540 placement: 'bottom-start',
1541 onCreate() {
1542 expect(popper.style.position).toEqual('fixed');
1543 expect(getRect(popper).top).toBeApprox(
1544 reference.getBoundingClientRect().bottom
1545 );
1546 expect(getRect(popper).left).toBeApprox(
1547 reference.getBoundingClientRect().left
1548 );
1549 done();
1550 },
1551 });
1552 });
1553
1554 it('init a popper with position fixed with a transformd parent', done => {
1555 const container = document.createElement('div');
1556 container.style.width = '800px';
1557 container.style.height = '600px';
1558 container.style.marginLeft = '200px';
1559 container.style.marginTop = '200px';
1560 container.style.background = 'grey';
1561 container.style.position = 'relative';
1562 container.style.transform = 'translateX(0)';
1563
1564 const popper = document.createElement('div');
1565 popper.style.width = '100px';
1566 popper.style.height = '100px';
1567 popper.style.background = 'yellow';
1568 popper.innerHTML = 'popper';
1569
1570 const ref = document.createElement('div');
1571 ref.style.width = '100px';
1572 ref.style.height = '100px';
1573 ref.style.background = 'green';
1574 ref.innerHTML = 'ref';
1575
1576 container.appendChild(popper);
1577 container.appendChild(ref);
1578 jasmineWrapper.appendChild(container);
1579
1580 new Popper(ref, popper, {
1581 placement: 'right',
1582 onCreate: data => {
1583 expect(getRect(popper).left).toBeApprox(
1584 getRect(ref).right
1585 );
1586 expect(getRect(popper).top).toBeApprox(
1587 getRect(ref).top
1588 );
1589
1590 data.instance.destroy();
1591 done();
1592 },
1593 });
1594 });
1595
1596 // test for #224
1597 it('checks that only the needed parents scroll offsets are included', done => {
1598 jasmineWrapper.innerHTML = `
1599 <div id="s1" style="overflow: scroll; height: 300px; background: red;">
1600 <div style="height: 100px;">
1601 <div id="reference" style="background: pink; margin-top: 100px; margin-bottom: 400px;">reference</div>
1602 </div>
1603 </div>
1604 <div style="height: 100vh;">spacer</div>
1605 <div id="popper" style="background: purple">popper</div>
1606 `;
1607
1608 const reference = document.getElementById('reference');
1609 const popper = document.getElementById('popper');
1610 const s1 = document.getElementById('s1');
1611
1612 new Popper(reference, popper, {
1613 onCreate() {
1614 simulateScroll(s1, { scrollTop: 20 });
1615 simulateScroll(document.body, { scrollTop: 50 });
1616 },
1617 onUpdate(data) {
1618 expect(getRect(reference).bottom).toBeApprox(getRect(popper).top);
1619 data.instance.destroy();
1620 done();
1621 },
1622 });
1623 });
1624
1625 // test for #310
1626 it('properly avoids overflow when parent is transformed', done => {
1627 //Not relevant for positionFixed cases
1628 if (positionFixed) {
1629 pending();
1630 }
1631 jasmineWrapper.innerHTML = `
1632 <style>
1633 .transform {
1634 will-change: transform;
1635 }
1636 #reference {
1637 width: 50px;
1638 height: 50px;
1639 background: lightgrey;
1640 }
1641 #popper {
1642 background: cyan;
1643 width: 200px;
1644 height: 100px;
1645 }
1646 </style>
1647 <div class="transform">
1648 <div id="reference">
1649 Box 2
1650 </div>
1651 <div id="popper">
1652 My Tooltip Content
1653 </div>
1654 </div>
1655 `;
1656
1657 const reference = document.getElementById('reference');
1658 const popper = document.getElementById('popper');
1659
1660 new Popper(reference, popper, {
1661 placement: 'bottom',
1662 onCreate(data) {
1663 expect(getRect(reference).bottom).toBeApprox(getRect(popper).top);
1664 expect(getRect(popper).left).toBeApprox(5);
1665 data.instance.destroy();
1666 done();
1667 },
1668 });
1669 });
1670
1671 // test for #305
1672 it('correct position if offset parent has borders', done => {
1673 jasmineWrapper.innerHTML = `
1674 <style>
1675 .container {
1676 align-items: center;
1677 background: blue;
1678 border: 20px solid red;
1679 display: flex;
1680 height: 400px;
1681 justify-content: center;
1682 margin: auto;
1683 position: relative;
1684 width: 400px;
1685 }
1686 #reference {
1687 background: orange;
1688 width: 50px;
1689 height: 50px;
1690 }
1691 #popper {
1692 background: green;
1693 width: 50px;
1694 height: 50px;
1695 }
1696 </style>
1697 <div class="container">
1698 <div id="reference">ref</div>
1699 <div id="popper">pop</div>
1700 </div>
1701 `;
1702
1703 const reference = document.getElementById('reference');
1704 const popper = document.getElementById('popper');
1705
1706 new Popper(reference, popper, {
1707 placement: 'bottom',
1708 onCreate(data) {
1709 expect(getRect(reference).bottom).toBeApprox(getRect(popper).top);
1710 expect(getRect(reference).left).toBeApprox(getRect(popper).left);
1711 data.instance.destroy();
1712 done();
1713 },
1714 });
1715 });
1716
1717 // Test for #253
1718 xit('sticky parent', done => {
1719 // MS Browsers (IE and Edge) don't support Sticky position
1720 if (isMSBrowser()) {
1721 pending();
1722 }
1723 jasmineWrapper.innerHTML = `
1724 <div id="s1" style="background: blue; height: 400px; overflow-y: scroll;">
1725 <div style="background: green; height: 50px; position: sticky; top: 0;">
1726 <div id="reference" style="background: orange; width: 50px; height: 50px;">ref</div>
1727 <div id="popper" style="background: steelblue; width: 50px; height: 50px;">pop</div>
1728 </div>
1729 <div style="height: 1000px;"></div>
1730 </div>
1731 `;
1732 const s1 = document.getElementById('s1');
1733 const reference = document.getElementById('reference');
1734 const popper = document.getElementById('popper');
1735
1736 simulateScroll(s1, { scrollTop: 100 });
1737
1738 new Popper(reference, popper, {
1739 onCreate(data) {
1740 expect(getRect(reference).bottom).toBeApprox(getRect(popper).top);
1741 data.instance.destroy();
1742 done();
1743 },
1744 });
1745 });
1746
1747 // test for #302
1748 it(
1749 'correct position on height:100% scrolled body with fixed popper parent',
1750 done => {
1751 jasmineWrapper.innerHTML = `
1752 <style>
1753 html, body {
1754 height: 100%;
1755 }
1756 .padder {
1757 height: 120vh;
1758 width: 10px;
1759 background-color: yellow;
1760 }
1761 .wrapper {
1762 position: fixed;
1763 bottom: 10px;
1764 left: 0px;
1765 right: 0px;
1766 }
1767 #reference {
1768 background: orange;
1769 width: 50px;
1770 height: 50px;
1771 }
1772 #popper {
1773 background: green;
1774 width: 50px;
1775 height: 50px;
1776 }
1777 </style>
1778 <div class="padder"></div>
1779 <div class="wrapper">
1780 <div id="reference">ref</div>
1781 <div id="popper">pop</div>
1782 </div>
1783 `;
1784
1785 const reference = document.getElementById('reference');
1786 const popper = document.getElementById('popper');
1787
1788 new Popper(reference, popper, {
1789 placement: 'top',
1790 onCreate() {
1791 simulateScroll(document.body, { scrollTop: 400, delay: 100 });
1792 },
1793 onUpdate(data) {
1794 expect(getRect(reference).top).toBeApprox(getRect(popper).bottom);
1795 data.instance.destroy();
1796 done();
1797 },
1798 });
1799 }
1800 );
1801
1802 // test for #276
1803 it('works inside table td elements', done => {
1804 jasmineWrapper.innerHTML = `
1805 <style>
1806 table {
1807 margin-top: 50px;
1808 }
1809 #reference {
1810 background: orange;
1811 width: 50px;
1812 height: 50px;
1813 }
1814 #popper {
1815 background: green;
1816 width: 50px;
1817 height: 50px;
1818 }
1819 </style>
1820 <table>
1821 <tbody>
1822 <tr>
1823 <td>
1824 <div id="reference">
1825 ref
1826 </div>
1827 <div id="popper">
1828 pop
1829 </div>
1830 </td>
1831 </tr>
1832 </tbody>
1833 </table>
1834 `;
1835
1836 const reference = document.getElementById('reference');
1837 const popper = document.getElementById('popper');
1838
1839 new Popper(reference, popper, {
1840 placement: 'bottom',
1841 onCreate(data) {
1842 expect(getRect(reference).bottom).toBeApprox(getRect(popper).top);
1843 expect(getRect(reference).left).toBeApprox(getRect(popper).left);
1844 data.instance.destroy();
1845 done();
1846 },
1847 });
1848 });
1849
1850 // test for #276
1851 it('works inside table th elements', done => {
1852 jasmineWrapper.innerHTML = `
1853 <style>
1854 table {
1855 margin-top: 50px;
1856 }
1857 #reference {
1858 background: orange;
1859 width: 50px;
1860 height: 50px;
1861 }
1862 #popper {
1863 background: green;
1864 width: 50px;
1865 height: 50px;
1866 }
1867 </style>
1868 <table>
1869 <tbody>
1870 <tr>
1871 <th>
1872 <div id="reference">
1873 ref
1874 </div>
1875 <div id="popper">
1876 pop
1877 </div>
1878 </th>
1879 </tr>
1880 </tbody>
1881 </table>
1882 `;
1883
1884 const reference = document.getElementById('reference');
1885 const popper = document.getElementById('popper');
1886
1887 new Popper(reference, popper, {
1888 placement: 'bottom',
1889 onCreate(data) {
1890 expect(getRect(reference).bottom).toBeApprox(getRect(popper).top);
1891 expect(getRect(reference).left).toBeApprox(getRect(popper).left);
1892 data.instance.destroy();
1893 done();
1894 },
1895 });
1896 });
1897
1898 // test for #453
1899 // When the reference is inside a scrollParent, it should be used as the
1900 // source for choosing the scrollParent rather than the popper.
1901 // This is particularly useful when the popper lives outside of the
1902 // scrollParent, e.g. in the body. In such cases, this allows the popper to
1903 // adjust (e.g. hide) based on the actual scrollParent boundaries rather than
1904 // that of the body.
1905 it(
1906 'reference is used as source for choosing scrollParent',
1907 done => {
1908 jasmineWrapper.innerHTML = `
1909 <style>
1910 body {
1911 padding: 100px;
1912 }
1913
1914 .scrollParent {
1915 height: 300px;
1916 width: 300px;
1917 overflow: auto;
1918 position: relative;
1919 }
1920
1921 .scrollContent {
1922 background: gray;
1923 padding: 1000px;
1924 }
1925
1926 #reference {
1927 background: lightgrey;
1928 height: 25px;
1929 width: 100px;
1930 }
1931
1932 #popper {
1933 background: cyan;
1934 height: 150px;
1935 width: 150px;
1936 }
1937
1938 [x-out-of-boundaries] {
1939 visibility: hidden;
1940 }
1941 </style>
1942
1943 <div class="scrollParent">
1944 <div class="scrollContent">
1945 <div id="reference">ref</div>
1946 </div>
1947 </div>
1948 <div id="popper">pop</div>
1949 `;
1950
1951 const reference = document.getElementById('reference');
1952 const popper = document.getElementById('popper');
1953 const scrollParent = document.querySelector('.scrollParent');
1954
1955 new Popper(reference, popper, {
1956 onCreate() {
1957 // Scroll reference just out of view to the right
1958 simulateScroll(scrollParent, { scrollTop: 950, scrollLeft: 700, delay: 100 });
1959 },
1960 onUpdate(data) {
1961 // Popper is hidden when reference is out of view in scrollParent
1962 expect(data.hide).toBe(true);
1963
1964 data.instance.destroy();
1965 done();
1966 },
1967 });
1968 }
1969 );
1970 });
1971 });