|
| 1 | +import * as assert from 'assert' |
| 2 | +import { calculateOverlayPosition } from './overlay_position' |
| 3 | + |
| 4 | +describe('overlay_position', () => { |
| 5 | + describe('calculateOverlayPosition()', () => { |
| 6 | + let relativeElement: HTMLElement |
| 7 | + let hoverOverlayElement: HTMLElement |
| 8 | + beforeEach(() => { |
| 9 | + const style = document.createElement('style') |
| 10 | + style.innerHTML = ` |
| 11 | + * { |
| 12 | + box-sizing: border-box; |
| 13 | + } |
| 14 | + .relative-element { |
| 15 | + background: lightgray; |
| 16 | + margin: 20px; |
| 17 | + width: 800px; |
| 18 | + height: 600px; |
| 19 | + position: relative; |
| 20 | + } |
| 21 | + .hover-overlay-element { |
| 22 | + background: gray; |
| 23 | + width: 350px; |
| 24 | + height: 150px; |
| 25 | + position: absolute; |
| 26 | + } |
| 27 | + .target { |
| 28 | + background: orange; |
| 29 | + width: 60px; |
| 30 | + height: 16px; |
| 31 | + position: absolute; |
| 32 | + } |
| 33 | + ` |
| 34 | + document.head.appendChild(style) |
| 35 | + |
| 36 | + relativeElement = document.createElement('div') |
| 37 | + relativeElement.className = 'relative-element' |
| 38 | + relativeElement.textContent = 'relativeElement' |
| 39 | + document.body.appendChild(relativeElement) |
| 40 | + |
| 41 | + hoverOverlayElement = document.createElement('div') |
| 42 | + hoverOverlayElement.className = 'hover-overlay-element' |
| 43 | + hoverOverlayElement.textContent = 'hoverOverlayElement' |
| 44 | + relativeElement.appendChild(hoverOverlayElement) |
| 45 | + }) |
| 46 | + afterEach(() => { |
| 47 | + relativeElement.remove() |
| 48 | + }) |
| 49 | + it('should return a position below the a given target in the middle of the page', () => { |
| 50 | + const target = document.createElement('div') |
| 51 | + target.className = 'target' |
| 52 | + target.style.left = '100px' |
| 53 | + target.style.top = '100px' |
| 54 | + target.textContent = 'target' |
| 55 | + relativeElement.appendChild(target) |
| 56 | + const position = calculateOverlayPosition({ relativeElement, target, hoverOverlayElement }) |
| 57 | + hoverOverlayElement.style.left = position.left + 'px' |
| 58 | + hoverOverlayElement.style.top = position.top + 'px' |
| 59 | + assert.deepStrictEqual(position, { left: 100, top: 116 }) |
| 60 | + }) |
| 61 | + }) |
| 62 | +}) |
0 commit comments