|
1 | 1 | import * as assert from 'assert' |
2 | | -import { calculateOverlayPosition } from './overlay_position' |
| 2 | +import { calculateOverlayPosition, CSSOffsets } from './overlay_position' |
3 | 3 |
|
4 | 4 | describe('overlay_position', () => { |
5 | 5 | describe('calculateOverlayPosition()', () => { |
6 | 6 | let relativeElement: HTMLElement |
7 | 7 | let hoverOverlayElement: HTMLElement |
| 8 | + |
| 9 | + /** Puts a target `div` into the relativeElement at the given px offsets */ |
| 10 | + const createTarget = (position: CSSOffsets): HTMLElement => { |
| 11 | + const target = document.createElement('div') |
| 12 | + target.className = 'target' |
| 13 | + target.textContent = 'target' |
| 14 | + applyOffsets(target, position) |
| 15 | + relativeElement.appendChild(target) |
| 16 | + return target |
| 17 | + } |
| 18 | + |
| 19 | + /** Positions an element at the given px offsets */ |
| 20 | + const applyOffsets = (element: HTMLElement, { left, top }: CSSOffsets): void => { |
| 21 | + element.style.left = left + 'px' |
| 22 | + element.style.top = top + 'px' |
| 23 | + } |
| 24 | + |
8 | 25 | beforeEach(() => { |
9 | 26 | const style = document.createElement('style') |
10 | 27 | style.innerHTML = ` |
@@ -46,16 +63,18 @@ describe('overlay_position', () => { |
46 | 63 | afterEach(() => { |
47 | 64 | relativeElement.remove() |
48 | 65 | }) |
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) |
| 66 | + |
| 67 | + it('should return a position above the given target if the overlay fits above', () => { |
| 68 | + const target = createTarget({ left: 100, top: 200 }) |
| 69 | + const position = calculateOverlayPosition({ relativeElement, target, hoverOverlayElement }) |
| 70 | + applyOffsets(hoverOverlayElement, position) |
| 71 | + assert.deepStrictEqual(position, { left: 100, top: 50 }) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should return a position below the a given target if the overlay does not fit above', () => { |
| 75 | + const target = createTarget({ left: 100, top: 100 }) |
56 | 76 | const position = calculateOverlayPosition({ relativeElement, target, hoverOverlayElement }) |
57 | | - hoverOverlayElement.style.left = position.left + 'px' |
58 | | - hoverOverlayElement.style.top = position.top + 'px' |
| 77 | + applyOffsets(hoverOverlayElement, position) |
59 | 78 | assert.deepStrictEqual(position, { left: 100, top: 116 }) |
60 | 79 | }) |
61 | 80 | }) |
|
0 commit comments