Skip to content

Commit b1489be

Browse files
committed
chore(placeTooltip): adding placeTooltip tests
1 parent 8348f66 commit b1489be

File tree

1 file changed

+109
-5
lines changed

1 file changed

+109
-5
lines changed

tests/core/placeTooltip.test.js

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,50 @@
1+
import * as getOffset from "../../src/util/getOffset";
2+
import * as getWindowSize from "../../src/util/getWindowSize";
13
import placeTooltip from "../../src/core/placeTooltip";
24

35

46
describe("placeTooltip", () => {
7+
test("should automatically place the tooltip position when there is enough space", () => {
8+
jest.spyOn(getOffset, 'default').mockReturnValue({
9+
height: 100,
10+
width: 100,
11+
top: 0,
12+
left: 0
13+
});
14+
15+
jest.spyOn(getWindowSize, 'default').mockReturnValue({
16+
height: 1000,
17+
width: 1000
18+
});
19+
20+
jest.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({
21+
width: 100,
22+
height: 100,
23+
top: 200,
24+
left: 200,
25+
bottom: 300,
26+
right: 300,
27+
});
28+
29+
const targetElement = document.createElement("div");
30+
const tooltipLayer = document.createElement("div");
31+
const arrowLayer = document.createElement("div");
32+
33+
placeTooltip.call({
34+
_currentStep: 0,
35+
_introItems: [{
36+
tooltip: 'hello',
37+
position: 'top'
38+
}],
39+
_options: {
40+
positionPrecedence: ['top', 'bottom', 'left', 'right'],
41+
autoPosition: true
42+
}
43+
}, targetElement, tooltipLayer, arrowLayer, false);
44+
45+
expect(tooltipLayer.className).toBe('introjs-tooltip introjs-top-right-aligned');
46+
});
47+
548
test("should skip auto positioning when autoPosition is false", () => {
649
const targetElement = document.createElement("div");
750
const tooltipLayer = document.createElement("div");
@@ -22,24 +65,85 @@ describe("placeTooltip", () => {
2265
expect(tooltipLayer.className).toBe('introjs-tooltip introjs-top');
2366
});
2467

25-
test("should determine tooltip positions automatically", () => {
68+
test("should use floating tooltips when height/width is limited", () => {
69+
jest.spyOn(getOffset, 'default').mockReturnValue({
70+
height: 100,
71+
width: 100,
72+
top: 0,
73+
left: 0
74+
});
75+
76+
jest.spyOn(getWindowSize, 'default').mockReturnValue({
77+
height: 100,
78+
width: 100
79+
});
80+
81+
jest.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({
82+
width: 100,
83+
height: 100,
84+
top: 0,
85+
left: 0,
86+
bottom: 0,
87+
right: 0,
88+
});
89+
2690
const targetElement = document.createElement("div");
27-
targetElement.clientHeight = '100px';
91+
const tooltipLayer = document.createElement("div");
92+
const arrowLayer = document.createElement("div");
93+
94+
placeTooltip.call({
95+
_currentStep: 0,
96+
_introItems: [{
97+
tooltip: 'hello',
98+
position: 'left'
99+
}],
100+
_options: {
101+
positionPrecedence: ['top', 'bottom', 'left', 'right'],
102+
autoPosition: true
103+
}
104+
}, targetElement, tooltipLayer, arrowLayer, false);
105+
106+
expect(tooltipLayer.className).toBe('introjs-tooltip introjs-floating');
107+
});
28108

109+
test("should use bottom middle aligned when there is enough vertical space", () => {
110+
jest.spyOn(getOffset, 'default').mockReturnValue({
111+
height: 100,
112+
width: 100,
113+
top: 0,
114+
left: 0
115+
});
116+
117+
jest.spyOn(getWindowSize, 'default').mockReturnValue({
118+
height: 500,
119+
width: 100
120+
});
121+
122+
jest.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({
123+
width: 100,
124+
height: 100,
125+
top: 0,
126+
left: 0,
127+
bottom: 0,
128+
right: 0,
129+
});
130+
131+
const targetElement = document.createElement("div");
29132
const tooltipLayer = document.createElement("div");
30133
const arrowLayer = document.createElement("div");
31134

32135
placeTooltip.call({
33136
_currentStep: 0,
34137
_introItems: [{
35138
tooltip: 'hello',
36-
position: 'bottom'
139+
position: 'left'
37140
}],
38141
_options: {
39-
positionPrecedence: ['top', 'bottom']
142+
positionPrecedence: ['top', 'bottom', 'left', 'right'],
143+
autoPosition: true
40144
}
41145
}, targetElement, tooltipLayer, arrowLayer, false);
42146

43-
expect(tooltipLayer.className).toBe('introjs-tooltip introjs-top');
147+
expect(tooltipLayer.className).toBe('introjs-tooltip introjs-bottom-middle-aligned');
44148
});
45149
});

0 commit comments

Comments
 (0)