-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial-overlay.test.tsx
More file actions
375 lines (290 loc) · 10.4 KB
/
tutorial-overlay.test.tsx
File metadata and controls
375 lines (290 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import React from 'react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { TutorialOverlay } from '../src/components/tutorial-overlay';
import { tutorial } from '../src/core/tutorial';
import type { Options } from '../src/core/types';
const DEFAULT_HIGHLIGHT_PADDING = 8;
function renderOverlay() {
render(
<div>
<input aria-label="Page input" />
<div id="first-target">First target</div>
<div id="second-target">Second target</div>
<TutorialOverlay />
</div>
);
}
function openTutorial(options: Options = {}) {
act(() => {
tutorial.open({
steps: [
{
title: 'Step 1',
content: 'Step 1 content',
targetIds: ['first-target'],
},
{
title: 'Step 2',
content: 'Step 2 content',
targetIds: ['second-target'],
},
],
options,
});
});
}
function createDomRect({ left, top, width, height }: { left: number; top: number; width: number; height: number }): DOMRect {
return {
x: left,
y: top,
left,
top,
width,
height,
right: left + width,
bottom: top + height,
toJSON: () => '',
} as DOMRect;
}
function mockTargetRect(id: string, rect: { left: number; top: number; width: number; height: number }) {
const element = document.getElementById(id) as HTMLElement;
element.getBoundingClientRect = jest.fn(() => createDomRect(rect));
}
function getInfoBoxElement(): HTMLDivElement {
return screen.getByText('Step 1').closest('div')?.parentElement?.parentElement as HTMLDivElement;
}
describe('TutorialOverlay', () => {
test('stays mounted when a target element cannot be found', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(<TutorialOverlay />);
act(() => {
tutorial.open({
steps: [
{
title: 'Missing target',
content: 'Overlay stays open',
targetIds: ['does-not-exist'],
},
],
options: {},
});
});
expect(screen.getByText('Missing target')).toBeInTheDocument();
expect(screen.getByText('Overlay stays open')).toBeInTheDocument();
expect(screen.getByText('1 / 1')).toBeInTheDocument();
expect(errorSpy).toHaveBeenCalledWith('Highlighted element with id does-not-exist was not found.');
});
test('supports keyboard navigation by default', () => {
const onClose = jest.fn();
renderOverlay();
openTutorial({ onClose });
fireEvent.keyDown(window, { key: 'ArrowRight' });
expect(screen.getByText('Step 2 content')).toBeInTheDocument();
fireEvent.keyDown(window, { key: 'ArrowLeft' });
expect(screen.getByText('Step 1 content')).toBeInTheDocument();
fireEvent.keyDown(window, { key: 'Escape' });
expect(onClose).toHaveBeenCalledTimes(1);
expect(screen.queryByText('Step 1 content')).not.toBeInTheDocument();
});
test('exposes the info box as a labeled dialog', () => {
renderOverlay();
openTutorial();
const dialog = screen.getByRole('dialog', { name: 'Step 1' });
expect(dialog).toBeInTheDocument();
expect(dialog).toHaveAccessibleDescription('Step 1 content');
});
test('moves focus into the dialog when the tutorial opens', () => {
renderOverlay();
const input = screen.getByLabelText('Page input');
input.focus();
openTutorial();
expect(screen.getByRole('button', { name: '건너뛰기' })).toHaveFocus();
});
test('restores focus to the previously active element when the tutorial closes', () => {
const onClose = jest.fn();
renderOverlay();
const input = screen.getByLabelText('Page input');
input.focus();
openTutorial({ onClose });
fireEvent.keyDown(window, { key: 'Escape' });
expect(onClose).toHaveBeenCalledTimes(1);
expect(input).toHaveFocus();
});
test('keeps first step on ArrowLeft and closes after ArrowRight on the last step', () => {
const onClose = jest.fn();
renderOverlay();
openTutorial({ onClose });
fireEvent.keyDown(window, { key: 'ArrowLeft' });
expect(screen.getByText('Step 1 content')).toBeInTheDocument();
fireEvent.keyDown(window, { key: 'ArrowRight' });
fireEvent.keyDown(window, { key: 'ArrowRight' });
expect(onClose).toHaveBeenCalledTimes(1);
expect(screen.queryByText('Step 2 content')).not.toBeInTheDocument();
});
test('does not handle keyboard shortcuts when keyboardNavigation is disabled', () => {
const onClose = jest.fn();
renderOverlay();
openTutorial({ keyboardNavigation: false, onClose });
fireEvent.keyDown(window, { key: 'ArrowRight' });
fireEvent.keyDown(window, { key: 'Escape' });
expect(screen.getByText('Step 1 content')).toBeInTheDocument();
expect(onClose).not.toHaveBeenCalled();
});
test('keeps keyboard navigation working while focus is on a dialog button', () => {
renderOverlay();
openTutorial();
const skipButton = screen.getByRole('button', { name: '건너뛰기' });
expect(skipButton).toHaveFocus();
fireEvent.keyDown(skipButton, { key: 'ArrowRight' });
expect(screen.getByText('Step 2 content')).toBeInTheDocument();
});
test('ignores keyboard shortcuts while a text input has focus', () => {
const onClose = jest.fn();
renderOverlay();
openTutorial({ onClose });
const input = screen.getByLabelText('Page input');
input.focus();
fireEvent.keyDown(input, { key: 'ArrowRight' });
fireEvent.keyDown(input, { key: 'Escape' });
expect(screen.getByText('Step 1 content')).toBeInTheDocument();
expect(onClose).not.toHaveBeenCalled();
});
test('closes on backdrop click only when closeOnOverlayClick is enabled', () => {
const onClose = jest.fn();
renderOverlay();
openTutorial({ closeOnOverlayClick: true, onClose });
fireEvent.click(screen.getByTestId('tutorial-overlay-highlight-first-target'));
expect(screen.getByText('Step 1 content')).toBeInTheDocument();
fireEvent.click(screen.getByText('Step 1 content'));
expect(screen.getByText('Step 1 content')).toBeInTheDocument();
fireEvent.click(screen.getByTestId('tutorial-overlay-backdrop'));
expect(onClose).toHaveBeenCalledTimes(1);
expect(screen.queryByText('Step 1 content')).not.toBeInTheDocument();
});
test('does not close on backdrop click by default', () => {
const onClose = jest.fn();
renderOverlay();
openTutorial({ onClose });
fireEvent.click(screen.getByTestId('tutorial-overlay-backdrop'));
expect(screen.getByText('Step 1 content')).toBeInTheDocument();
expect(onClose).not.toHaveBeenCalled();
});
test('applies the default highlight padding to the calculated rect', () => {
renderOverlay();
mockTargetRect('first-target', { left: 100, top: 80, width: 120, height: 40 });
openTutorial();
expect(screen.getByTestId('tutorial-overlay-highlight-first-target')).toHaveStyle({
left: `${100 - DEFAULT_HIGHLIGHT_PADDING}px`,
top: `${80 - DEFAULT_HIGHLIGHT_PADDING}px`,
width: `${120 + DEFAULT_HIGHLIGHT_PADDING * 2}px`,
height: `${40 + DEFAULT_HIGHLIGHT_PADDING * 2}px`,
});
});
test('applies a custom highlight padding to the calculated rect', () => {
renderOverlay();
mockTargetRect('first-target', { left: 200, top: 160, width: 80, height: 32 });
openTutorial({ highLightPadding: 16 });
expect(screen.getByTestId('tutorial-overlay-highlight-first-target')).toHaveStyle({
left: '184px',
top: '144px',
width: '112px',
height: '64px',
});
});
test('keeps the info box inside the viewport when padding expands the target rect', () => {
jest.useFakeTimers();
Object.defineProperty(window, 'innerWidth', {
configurable: true,
value: 800,
});
Object.defineProperty(window, 'innerHeight', {
configurable: true,
value: 600,
});
renderOverlay();
mockTargetRect('first-target', { left: 760, top: 100, width: 40, height: 40 });
act(() => {
tutorial.open({
steps: [
{
title: 'Step 1',
content: 'Step 1 content',
targetIds: ['first-target'],
infoBoxAlignment: 'right',
},
],
options: {
highLightPadding: 8,
},
});
});
const infoBox = getInfoBoxElement();
Object.defineProperty(infoBox, 'clientWidth', {
configurable: true,
value: 320,
});
Object.defineProperty(infoBox, 'clientHeight', {
configurable: true,
value: 200,
});
act(() => {
window.dispatchEvent(new Event('resize'));
jest.advanceTimersByTime(300);
});
expect(infoBox.style.left).toBe('470px');
jest.useRealTimers();
});
test('clamps the info box vertically when neither side has enough space', () => {
jest.useFakeTimers();
Object.defineProperty(window, 'innerWidth', {
configurable: true,
value: 800,
});
Object.defineProperty(window, 'innerHeight', {
configurable: true,
value: 600,
});
renderOverlay();
mockTargetRect('first-target', { left: 120, top: 100, width: 80, height: 40 });
openTutorial({ infoBoxHeight: 520 });
const infoBox = getInfoBoxElement();
Object.defineProperty(infoBox, 'clientWidth', {
configurable: true,
value: 320,
});
Object.defineProperty(infoBox, 'clientHeight', {
configurable: true,
value: 520,
});
act(() => {
window.dispatchEvent(new Event('resize'));
jest.advanceTimersByTime(300);
});
expect(infoBox.style.top).toBe('70px');
jest.useRealTimers();
});
test('applies custom overlay, highlight, and info box styles from options', () => {
renderOverlay();
mockTargetRect('first-target', { left: 120, top: 96, width: 140, height: 48 });
openTutorial({
overlayColor: 'rgba(12, 34, 56, 0.7)',
highlightBorderColor: 'rgb(0, 255, 136)',
highlightBorderRadius: 24,
zIndex: 4321,
infoBoxWidth: '28rem',
});
expect(screen.getByTestId('tutorial-overlay-backdrop')).toHaveStyle({
backgroundColor: 'rgba(12, 34, 56, 0.7)',
zIndex: '4321',
});
expect(screen.getByRole('dialog', { name: 'Step 1' })).toHaveStyle({
width: '28rem',
zIndex: '4323',
});
expect(screen.getByTestId('tutorial-overlay-highlight-first-target')).toHaveStyle({
borderColor: 'rgb(0, 255, 136)',
borderRadius: '24px',
zIndex: '4322',
});
});
});