1
+ import { queryElementByClassName } from "../../util/queryElement" ;
1
2
import {
2
- appendDummyElement ,
3
3
className ,
4
4
content ,
5
5
doneButton ,
@@ -8,12 +8,12 @@ import {
8
8
prevButton ,
9
9
skipButton ,
10
10
tooltipText ,
11
+ waitFor ,
11
12
} from "../../../tests/jest/helper" ;
12
13
import * as dontShowAgain from "./dontShowAgain" ;
13
14
import { getMockPartialSteps , getMockTour } from "./tests/mock" ;
14
15
import { Tour } from "./tour" ;
15
-
16
- const flushPromises = ( ) => new Promise ( setImmediate ) ;
16
+ import { helperLayerClassName , overlayClassName } from "./classNames" ;
17
17
18
18
describe ( "Tour" , ( ) => {
19
19
beforeEach ( ( ) => {
@@ -46,6 +46,13 @@ describe("Tour", () => {
46
46
47
47
beforeEach ( ( ) => {
48
48
mockTour = getMockTour ( ) ;
49
+
50
+ document . body . innerHTML = `<div>
51
+ <h1 id='title'>Title</h1>
52
+ <p id='paragraph'>Paragraph</p>
53
+ <div id='position-absolute' style='position: absolute;'>Position Absolute</div>
54
+ <div id='position-fixed' style='position: fixed;'>Position Fixed</div>
55
+ </div>` ;
49
56
} ) ;
50
57
51
58
afterEach ( async ( ) => {
@@ -113,12 +120,12 @@ describe("Tour", () => {
113
120
114
121
test ( "should highlight the target element" , async ( ) => {
115
122
// Arrange
116
- const p = appendDummyElement ( ) ;
123
+ const mockElement = document . querySelector ( "#paragraph" ) ;
117
124
mockTour . setOptions ( {
118
125
steps : [
119
126
{
120
127
intro : "step one" ,
121
- element : document . querySelector ( "p" ) ,
128
+ element : mockElement ,
122
129
} ,
123
130
] ,
124
131
} ) ;
@@ -127,18 +134,40 @@ describe("Tour", () => {
127
134
await mockTour . start ( ) ;
128
135
129
136
// Assert
130
- expect ( p . className ) . toContain ( "introjs-showElement" ) ;
131
- expect ( p . className ) . toContain ( "introjs-relativePosition" ) ;
137
+ expect ( mockElement ?. className ) . toContain ( "introjs-showElement" ) ;
138
+ expect ( mockElement ?. className ) . toContain ( "introjs-relativePosition" ) ;
139
+ } ) ;
140
+
141
+ test ( "should remove the container element after exit() is called" , async ( ) => {
142
+ // Arrange
143
+ const mockElement = document . querySelector ( "#paragraph" ) ;
144
+ mockTour . setOptions ( {
145
+ steps : [
146
+ {
147
+ intro : "step one" ,
148
+ element : mockElement ,
149
+ } ,
150
+ ] ,
151
+ } ) ;
152
+
153
+ // Act
154
+ await mockTour . start ( ) ;
155
+ await mockTour . exit ( ) ;
156
+
157
+ // Assert
158
+ expect ( mockElement ?. className ) . not . toContain ( "introjs-showElement" ) ;
159
+ expect ( queryElementByClassName ( helperLayerClassName ) ) . toBeNull ( ) ;
160
+ expect ( queryElementByClassName ( overlayClassName ) ) . toBeNull ( ) ;
132
161
} ) ;
133
162
134
163
test ( "should not highlight the target element if queryString is incorrect" , async ( ) => {
135
164
// Arrange
136
- const p = appendDummyElement ( ) ;
165
+ const mockElement = document . querySelector ( "#non-existing-element" ) ;
137
166
mockTour . setOptions ( {
138
167
steps : [
139
168
{
140
169
intro : "step one" ,
141
- element : document . querySelector ( "div" ) ,
170
+ element : mockElement ,
142
171
} ,
143
172
] ,
144
173
} ) ;
@@ -147,29 +176,19 @@ describe("Tour", () => {
147
176
await mockTour . start ( ) ;
148
177
149
178
// Assert
150
- expect ( p . className ) . not . toContain ( "introjs-showElement" ) ;
151
179
expect ( className ( ".introjs-showElement" ) ) . toContain (
152
180
"introjsFloatingElement"
153
181
) ;
154
182
} ) ;
155
183
156
- test ( "should not add relativePosition if target element is fixed or absolute " , async ( ) => {
184
+ test ( "should not add relativePosition if target element is fixed" , async ( ) => {
157
185
// Arrange
158
- const absolute = appendDummyElement (
159
- "h1" ,
160
- "hello world" ,
161
- "position: absolute"
162
- ) ;
163
- const fixed = appendDummyElement ( "h2" , "hello world" , "position: fixed" ) ;
186
+ const fixedMockElement = document . querySelector ( "#position-fixed" ) ;
164
187
mockTour . setOptions ( {
165
188
steps : [
166
189
{
167
190
intro : "step one" ,
168
- element : document . querySelector ( "h1" ) ,
169
- } ,
170
- {
171
- intro : "step two" ,
172
- element : document . querySelector ( "h2" ) ,
191
+ element : fixedMockElement ,
173
192
} ,
174
193
] ,
175
194
} ) ;
@@ -178,13 +197,32 @@ describe("Tour", () => {
178
197
await mockTour . start ( ) ;
179
198
180
199
// Assert
181
- expect ( absolute . className ) . toContain ( "introjs-showElement" ) ;
182
- expect ( absolute . className ) . not . toContain ( "introjs-relativePosition" ) ;
200
+ expect ( fixedMockElement ?. className ) . toContain ( "introjs-showElement" ) ;
201
+ expect ( fixedMockElement ?. className ) . not . toContain (
202
+ "introjs-relativePosition"
203
+ ) ;
204
+ } ) ;
205
+
206
+ test ( "should not add relativePosition if target element is fixed or absolute" , async ( ) => {
207
+ // Arrange
208
+ const absoluteMockElement = document . querySelector ( "#position-absolute" ) ;
209
+ mockTour . setOptions ( {
210
+ steps : [
211
+ {
212
+ intro : "step one" ,
213
+ element : absoluteMockElement ,
214
+ } ,
215
+ ] ,
216
+ } ) ;
183
217
184
- await mockTour . nextStep ( ) ;
218
+ // Act
219
+ await mockTour . start ( ) ;
185
220
186
- expect ( fixed . className ) . toContain ( "introjs-showElement" ) ;
187
- expect ( fixed . className ) . not . toContain ( "introjs-relativePosition" ) ;
221
+ // Assert
222
+ expect ( absoluteMockElement ?. className ) . toContain ( "introjs-showElement" ) ;
223
+ expect ( absoluteMockElement ?. className ) . not . toContain (
224
+ "introjs-relativePosition"
225
+ ) ;
188
226
} ) ;
189
227
190
228
test ( "should call the onstart callback" , async ( ) => {
@@ -228,7 +266,7 @@ describe("Tour", () => {
228
266
// Act
229
267
await mockTour . start ( ) ;
230
268
nextButton ( ) . click ( ) ;
231
- await flushPromises ( ) ;
269
+ await waitFor ( 1000 ) ;
232
270
233
271
// Assert
234
272
expect ( onexitMock ) . toBeCalledTimes ( 1 ) ;
@@ -254,7 +292,7 @@ describe("Tour", () => {
254
292
// Act
255
293
await mockTour . start ( ) ;
256
294
skipButton ( ) . click ( ) ;
257
- await flushPromises ( ) ;
295
+ await waitFor ( 1000 ) ;
258
296
259
297
// Assert
260
298
expect ( onexitMock ) . toBeCalledTimes ( 1 ) ;
@@ -282,7 +320,7 @@ describe("Tour", () => {
282
320
// Act
283
321
await mockTour . start ( ) ;
284
322
skipButton ( ) . click ( ) ;
285
- await flushPromises ( ) ;
323
+ await waitFor ( 1000 ) ;
286
324
287
325
// Assert
288
326
expect ( onexitMock ) . toBeCalledTimes ( 1 ) ;
0 commit comments