Skip to content

Commit 21385dc

Browse files
test: compute position
1 parent 008090f commit 21385dc

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/test/__snapshots__/utils.spec.js.snap

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,41 @@ exports[`compute positions all reference elements 1`] = `
1616
},
1717
}
1818
`;
19+
20+
exports[`compute positions all reference elements with border 1`] = `
21+
{
22+
"place": "top",
23+
"tooltipArrowStyles": {
24+
"borderBottom": "1px solid red",
25+
"borderRight": "1px solid red",
26+
"bottom": "-5px",
27+
"left": "-1px",
28+
"right": "",
29+
"top": "",
30+
},
31+
"tooltipStyles": {
32+
"border": "1px solid red",
33+
"left": "5px",
34+
"top": "-10px",
35+
},
36+
}
37+
`;
38+
39+
exports[`compute positions all reference elements with non-px border 1`] = `
40+
{
41+
"place": "top",
42+
"tooltipArrowStyles": {
43+
"borderBottom": "medium solid red",
44+
"borderRight": "medium solid red",
45+
"bottom": "-5px",
46+
"left": "-1px",
47+
"right": "",
48+
"top": "",
49+
},
50+
"tooltipStyles": {
51+
"border": "medium solid red",
52+
"left": "5px",
53+
"top": "-10px",
54+
},
55+
}
56+
`;

src/test/utils.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,34 @@ describe('compute positions', () => {
5858

5959
expect(value).toMatchSnapshot()
6060
})
61+
62+
test('all reference elements with border', async () => {
63+
const element = document.createElement('div')
64+
const elementTooltip = document.createElement('div')
65+
const elementTooltipArrow = document.createElement('div')
66+
const value = await computeTooltipPosition({
67+
elementReference: element,
68+
tooltipReference: elementTooltip,
69+
tooltipArrowReference: elementTooltipArrow,
70+
border: '1px solid red',
71+
})
72+
73+
expect(value).toMatchSnapshot()
74+
})
75+
76+
test('all reference elements with non-px border', async () => {
77+
const element = document.createElement('div')
78+
const elementTooltip = document.createElement('div')
79+
const elementTooltipArrow = document.createElement('div')
80+
const value = await computeTooltipPosition({
81+
elementReference: element,
82+
tooltipReference: elementTooltip,
83+
tooltipArrowReference: elementTooltipArrow,
84+
border: 'medium solid red',
85+
})
86+
87+
expect(value).toMatchSnapshot()
88+
})
6189
})
6290

6391
describe('debounce', () => {

src/utils/compute-positions.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const computeTooltipPosition = async ({
4040
}).then(({ x, y, placement, middlewareData }) => {
4141
const styles = { left: `${x}px`, top: `${y}px`, border }
4242

43+
/* c8 ignore start */
4344
const { x: arrowX, y: arrowY } = middlewareData.arrow ?? { x: 0, y: 0 }
4445

4546
const staticSide =
@@ -49,6 +50,7 @@ export const computeTooltipPosition = async ({
4950
bottom: 'top',
5051
left: 'right',
5152
}[placement.split('-')[0]] ?? 'bottom'
53+
/* c8 ignore end */
5254

5355
const borderSide = border && {
5456
borderBottom: border,
@@ -62,12 +64,14 @@ export const computeTooltipPosition = async ({
6264
borderWidth = Number(match[1])
6365
} else {
6466
/**
65-
* this means `border` was set without `width`, or non-px value
67+
* this means `border` was set without `width`,
68+
* or non-px value (such as `medium`, `thick`, ...)
6669
*/
6770
borderWidth = 1
6871
}
6972
}
7073

74+
/* c8 ignore start */
7175
const arrowStyle = {
7276
left: arrowX != null ? `${arrowX}px` : '',
7377
top: arrowY != null ? `${arrowY}px` : '',
@@ -76,6 +80,7 @@ export const computeTooltipPosition = async ({
7680
...borderSide,
7781
[staticSide]: `-${4 + borderWidth}px`,
7882
}
83+
/* c8 ignore end */
7984

8085
return { tooltipStyles: styles, tooltipArrowStyles: arrowStyle, place: placement }
8186
})

0 commit comments

Comments
 (0)