Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit 378ea67

Browse files
committed
test: add second test for calculateOverlayPosition
1 parent 8efa73a commit 378ea67

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/overlay_position.test.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
import * as assert from 'assert'
2-
import { calculateOverlayPosition } from './overlay_position'
2+
import { calculateOverlayPosition, CSSOffsets } from './overlay_position'
33

44
describe('overlay_position', () => {
55
describe('calculateOverlayPosition()', () => {
66
let relativeElement: HTMLElement
77
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+
825
beforeEach(() => {
926
const style = document.createElement('style')
1027
style.innerHTML = `
@@ -46,16 +63,18 @@ describe('overlay_position', () => {
4663
afterEach(() => {
4764
relativeElement.remove()
4865
})
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 })
5676
const position = calculateOverlayPosition({ relativeElement, target, hoverOverlayElement })
57-
hoverOverlayElement.style.left = position.left + 'px'
58-
hoverOverlayElement.style.top = position.top + 'px'
77+
applyOffsets(hoverOverlayElement, position)
5978
assert.deepStrictEqual(position, { left: 100, top: 116 })
6079
})
6180
})

src/overlay_position.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ interface CalculateOverlayPositionOptions {
1313
hoverOverlayElement: HTMLElement
1414
}
1515

16+
export interface CSSOffsets {
17+
/** Offset from the left in pixel */
18+
left: number
19+
/** Offset from the top in pixel */
20+
top: number
21+
}
22+
1623
/**
1724
* Calculates the desired position of the hover overlay depending on the container,
1825
* the hover target and the size of the hover overlay
@@ -21,7 +28,7 @@ export const calculateOverlayPosition = ({
2128
relativeElement,
2229
target,
2330
hoverOverlayElement,
24-
}: CalculateOverlayPositionOptions): { left: number; top: number } => {
31+
}: CalculateOverlayPositionOptions): CSSOffsets => {
2532
// The scrollable element is the one with scrollbars. The scrolling element is the one with the content.
2633
const relativeElementBounds = relativeElement.getBoundingClientRect()
2734
const targetBound = target.getBoundingClientRect() // our target elements bounds

0 commit comments

Comments
 (0)