1
+ import * as getOffset from "../../src/util/getOffset" ;
2
+ import * as getWindowSize from "../../src/util/getWindowSize" ;
1
3
import placeTooltip from "../../src/core/placeTooltip" ;
2
4
3
5
4
6
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
+
5
48
test ( "should skip auto positioning when autoPosition is false" , ( ) => {
6
49
const targetElement = document . createElement ( "div" ) ;
7
50
const tooltipLayer = document . createElement ( "div" ) ;
@@ -22,24 +65,85 @@ describe("placeTooltip", () => {
22
65
expect ( tooltipLayer . className ) . toBe ( 'introjs-tooltip introjs-top' ) ;
23
66
} ) ;
24
67
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
+
26
90
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
+ } ) ;
28
108
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" ) ;
29
132
const tooltipLayer = document . createElement ( "div" ) ;
30
133
const arrowLayer = document . createElement ( "div" ) ;
31
134
32
135
placeTooltip . call ( {
33
136
_currentStep : 0 ,
34
137
_introItems : [ {
35
138
tooltip : 'hello' ,
36
- position : 'bottom '
139
+ position : 'left '
37
140
} ] ,
38
141
_options : {
39
- positionPrecedence : [ 'top' , 'bottom' ]
142
+ positionPrecedence : [ 'top' , 'bottom' , 'left' , 'right' ] ,
143
+ autoPosition : true
40
144
}
41
145
} , targetElement , tooltipLayer , arrowLayer , false ) ;
42
146
43
- expect ( tooltipLayer . className ) . toBe ( 'introjs-tooltip introjs-top ' ) ;
147
+ expect ( tooltipLayer . className ) . toBe ( 'introjs-tooltip introjs-bottom-middle-aligned ' ) ;
44
148
} ) ;
45
149
} ) ;
0 commit comments