@instructure/ui-dialog 10.19.2-snapshot-2 → 10.19.2-snapshot-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,420 +0,0 @@
1
- import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
- var _button, _div, _div2, _DialogExample, _DialogExample2, _DialogExample3, _div3, _div4, _DialogExample4, _DialogExample5, _DialogExample6, _DialogExample7, _DialogExample8;
3
- /*
4
- * The MIT License (MIT)
5
- *
6
- * Copyright (c) 2015 - present Instructure, Inc.
7
- *
8
- * Permission is hereby granted, free of charge, to any person obtaining a copy
9
- * of this software and associated documentation files (the "Software"), to deal
10
- * in the Software without restriction, including without limitation the rights
11
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
- * copies of the Software, and to permit persons to whom the Software is
13
- * furnished to do so, subject to the following conditions:
14
- *
15
- * The above copyright notice and this permission notice shall be included in all
16
- * copies or substantial portions of the Software.
17
- *
18
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- * SOFTWARE.
25
- */
26
-
27
- import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
28
- import { fireEvent, render, waitFor } from '@testing-library/react';
29
- import { vi } from 'vitest';
30
- import { userEvent } from '@testing-library/user-event';
31
- import '@testing-library/jest-dom';
32
- import { Dialog } from '../index';
33
- import { jsx as _jsx, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
34
- const TEST_TEXT = 'test-text';
35
- const TEST_LABEL = 'test-label';
36
- const renderDialog = props => {
37
- const allProps = {
38
- open: true,
39
- label: TEST_LABEL,
40
- ...props
41
- };
42
- return render(_jsx(Dialog, {
43
- ...allProps,
44
- children: _button || (_button = _jsx("button", {
45
- children: TEST_TEXT
46
- }))
47
- }));
48
- };
49
- const DialogExample = /*#__PURE__*/forwardRef((props, ref) => {
50
- const inputRef = useRef(null);
51
- const dialogRef = useRef(null);
52
- useEffect(() => {
53
- if (!props.open) {
54
- inputRef.current.focus();
55
- }
56
- }, [props.open]);
57
- useImperativeHandle(ref, () => ({
58
- focusDialog: () => dialogRef.current.focus(),
59
- blurDialog: () => dialogRef.current.blur()
60
- }));
61
- return _jsxs("div", {
62
- children: [_jsx("input", {
63
- "data-testid": "input-trigger",
64
- type: "text",
65
- ref: inputRef
66
- }), _jsx(Dialog, {
67
- shouldContainFocus: true,
68
- shouldReturnFocus: true,
69
- label: TEST_LABEL,
70
- ...props,
71
- ref: dialogRef,
72
- children: props.children || _div || (_div = _jsxs("div", {
73
- children: [_jsx("input", {
74
- type: "text",
75
- "data-testid": "input-one"
76
- }), _jsx("input", {
77
- type: "text",
78
- "data-testid": "input-two"
79
- })]
80
- }))
81
- })]
82
- });
83
- });
84
- DialogExample.displayName = 'DialogExample';
85
- const NestedDialogExample = props => {
86
- const _useState = useState(false),
87
- _useState2 = _slicedToArray(_useState, 2),
88
- nestedOpen = _useState2[0],
89
- setNestedOpen = _useState2[1];
90
- const handleTriggerClick = () => setNestedOpen(true);
91
- return _jsx("div", {
92
- children: _jsx(Dialog, {
93
- open: true,
94
- shouldReturnFocus: true,
95
- label: TEST_LABEL,
96
- ...props,
97
- children: _jsxs("div", {
98
- children: [_jsxs("div", {
99
- children: [_jsx("input", {
100
- onClick: handleTriggerClick,
101
- type: "text",
102
- "data-testid": "nested-input-one"
103
- }), _jsx("input", {
104
- onClick: handleTriggerClick,
105
- type: "text",
106
- "data-testid": "nested-input-two"
107
- })]
108
- }), _jsx(Dialog, {
109
- open: nestedOpen,
110
- label: TEST_LABEL,
111
- children: TEST_TEXT
112
- })]
113
- })
114
- })
115
- });
116
- };
117
- NestedDialogExample.displayName = 'NestedDialogExample';
118
- describe('<Dialog />', () => {
119
- it('should render nothing when closed', () => {
120
- const _renderDialog = renderDialog({
121
- open: false
122
- }),
123
- container = _renderDialog.container;
124
- expect(container.firstChild).not.toBeInTheDocument();
125
- });
126
- it('should render children when open', async () => {
127
- const _renderDialog2 = renderDialog({
128
- open: true
129
- }),
130
- container = _renderDialog2.container;
131
- expect(container.firstChild).toBeInTheDocument();
132
- expect(container.firstChild).toHaveTextContent(TEST_TEXT);
133
- });
134
- it('should apply the a11y attributes', () => {
135
- const _renderDialog3 = renderDialog({
136
- label: TEST_LABEL
137
- }),
138
- getByRole = _renderDialog3.getByRole,
139
- getByLabelText = _renderDialog3.getByLabelText;
140
- const dialog = getByRole('dialog');
141
- const label = getByLabelText(TEST_LABEL);
142
- expect(dialog).toBeInTheDocument();
143
- expect(label).toBeInTheDocument();
144
- });
145
- it('should apply the role attributes, if explicitly passed', () => {
146
- const _renderDialog4 = renderDialog({
147
- label: TEST_LABEL,
148
- role: 'region'
149
- }),
150
- getByRole = _renderDialog4.getByRole,
151
- getByLabelText = _renderDialog4.getByLabelText;
152
- const regionRole = getByRole('region');
153
- const label = getByLabelText(TEST_LABEL);
154
- expect(regionRole).toBeInTheDocument();
155
- expect(label).toBeInTheDocument();
156
- });
157
- it('should call onDismiss prop when Esc key pressed', async () => {
158
- const onDismiss = vi.fn();
159
- const _renderDialog5 = renderDialog({
160
- onDismiss
161
- }),
162
- getByRole = _renderDialog5.getByRole;
163
- const dialog = getByRole('dialog');
164
- await waitFor(() => {
165
- fireEvent.keyUp(dialog, {
166
- key: 'Escape',
167
- code: 'Escape',
168
- keyCode: 27,
169
- charCode: 27
170
- });
171
- expect(onDismiss).toHaveBeenCalled();
172
- });
173
- });
174
- it('should call onDismiss prop when the document is clicked', async () => {
175
- const onDismiss = vi.fn();
176
- renderDialog({
177
- onDismiss,
178
- shouldCloseOnDocumentClick: true
179
- });
180
- await waitFor(() => {
181
- userEvent.click(document.body);
182
- expect(onDismiss).toHaveBeenCalled();
183
- });
184
- });
185
- describe('managed focus', () => {
186
- it('should provide focus method', async () => {
187
- const _render = render(_jsxs("div", {
188
- children: [_jsx(DialogExample, {
189
- open: true,
190
- contentElement: () => getByTestId('non-tabbable'),
191
- children: TEST_TEXT
192
- }), _div2 || (_div2 = _jsx("div", {
193
- tabIndex: -1,
194
- "data-testid": "non-tabbable",
195
- children: TEST_TEXT
196
- }))]
197
- })),
198
- getByTestId = _render.getByTestId;
199
- const nonTabbableContent = getByTestId('non-tabbable');
200
- userEvent.tab();
201
- await waitFor(() => {
202
- expect(document.activeElement).toBe(nonTabbableContent);
203
- });
204
- });
205
- it('should warn when trying to focus or blur a closed dialog', () => {
206
- const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
207
- let ref;
208
- render(_jsx(DialogExample, {
209
- open: false,
210
- ref: el => ref = el
211
- }));
212
- ref.focusDialog();
213
- expect(consoleError.mock.calls[0][0]).toBe("Warning: [Dialog] Can't focus a Dialog that isn't open.");
214
- ref.blurDialog();
215
- expect(consoleError.mock.calls[1][0]).toBe("Warning: [Dialog] Can't blur a Dialog that isn't open.");
216
- consoleError.mockRestore();
217
- });
218
- it('should focus the first tabbable element by default', async () => {
219
- const _render2 = render(_DialogExample || (_DialogExample = _jsx(DialogExample, {
220
- open: true
221
- }))),
222
- getByTestId = _render2.getByTestId;
223
- const inputOne = getByTestId('input-one');
224
- await waitFor(() => {
225
- expect(document.activeElement).toBe(inputOne);
226
- });
227
- });
228
- it('should focus the first tabbable element when open prop becomes true', async () => {
229
- const _render3 = render(_DialogExample2 || (_DialogExample2 = _jsx(DialogExample, {
230
- open: false
231
- }))),
232
- rerender = _render3.rerender,
233
- getByTestId = _render3.getByTestId;
234
- const inputTrigger = getByTestId('input-trigger');
235
- await waitFor(() => {
236
- expect(document.activeElement).toBe(inputTrigger);
237
- });
238
- rerender(_DialogExample3 || (_DialogExample3 = _jsx(DialogExample, {
239
- open: true
240
- })));
241
- const inputOne = getByTestId('input-one');
242
- await waitFor(() => {
243
- expect(document.activeElement).toBe(inputOne);
244
- });
245
- });
246
- it('should take a prop for finding default focus', async () => {
247
- const _render4 = render(_jsx(DialogExample, {
248
- open: true,
249
- defaultFocusElement: () => getByTestId('input-two')
250
- })),
251
- getByTestId = _render4.getByTestId;
252
- const inputTwo = getByTestId('input-two');
253
- await waitFor(() => {
254
- expect(document.activeElement).toBe(inputTwo);
255
- });
256
- });
257
- it('should still focus the defaultFocusElement when it is focusable but not tabbable', async () => {
258
- const _render5 = render(_jsx(DialogExample, {
259
- open: true,
260
- defaultFocusElement: () => getByTestId('non-tabbable'),
261
- children: _div3 || (_div3 = _jsx("div", {
262
- tabIndex: -1,
263
- "data-testid": "non-tabbable",
264
- children: TEST_TEXT
265
- }))
266
- })),
267
- getByTestId = _render5.getByTestId;
268
- const nonTabbableContent = getByTestId('non-tabbable');
269
- await waitFor(() => {
270
- expect(document.activeElement).toBe(nonTabbableContent);
271
- });
272
- });
273
- it('should focus the contentElement by default if focusable and no defaultFocusElement is provided', async () => {
274
- const _render6 = render(_jsxs("div", {
275
- children: [_jsx(DialogExample, {
276
- open: true,
277
- contentElement: () => getByTestId('non-tabbable'),
278
- children: TEST_TEXT
279
- }), _div4 || (_div4 = _jsx("div", {
280
- tabIndex: -1,
281
- "data-testid": "non-tabbable",
282
- children: TEST_TEXT
283
- }))]
284
- })),
285
- getByTestId = _render6.getByTestId;
286
- const nonTabbableContent = getByTestId('non-tabbable');
287
- await waitFor(() => {
288
- expect(document.activeElement).toBe(nonTabbableContent);
289
- });
290
- });
291
- it('should focus the document body if there is no defaultFocusElement, tabbable elements, or focusable contentElement', async () => {
292
- const _render7 = render(_DialogExample4 || (_DialogExample4 = _jsx(DialogExample, {
293
- open: false,
294
- children: TEST_TEXT
295
- }))),
296
- rerender = _render7.rerender,
297
- getByTestId = _render7.getByTestId;
298
- const inputTrigger = getByTestId('input-trigger');
299
- inputTrigger.focus();
300
- rerender(_DialogExample5 || (_DialogExample5 = _jsx(DialogExample, {
301
- open: true,
302
- children: TEST_TEXT
303
- })));
304
- await waitFor(() => {
305
- expect(document.activeElement).toBe(document.body);
306
- });
307
- });
308
- it('should return focus', async () => {
309
- const _render8 = render(_DialogExample6 || (_DialogExample6 = _jsx(DialogExample, {
310
- open: false
311
- }))),
312
- rerender = _render8.rerender,
313
- getByTestId = _render8.getByTestId;
314
- expect(document.activeElement).toBe(getByTestId('input-trigger'));
315
- rerender(_DialogExample7 || (_DialogExample7 = _jsx(DialogExample, {
316
- open: true
317
- })));
318
- await waitFor(() => {
319
- expect(document.activeElement).toBe(getByTestId('input-one'));
320
- });
321
- rerender(_DialogExample8 || (_DialogExample8 = _jsx(DialogExample, {
322
- open: false
323
- })));
324
- await waitFor(() => {
325
- expect(document.activeElement).toBe(getByTestId('input-trigger'));
326
- });
327
- });
328
- describe('when focus leaves the first and last tabbable', () => {
329
- it(`should NOT call onBlur when shouldContainFocus=true and tab pressing last tabbable`, async () => {
330
- const onBlur = vi.fn();
331
- const _render9 = render(_jsx(DialogExample, {
332
- open: true,
333
- onBlur: onBlur,
334
- shouldContainFocus: true,
335
- defaultFocusElement: () => getByTestId('input-two')
336
- })),
337
- getByTestId = _render9.getByTestId;
338
- const inputOne = getByTestId('input-one');
339
- const inputTwo = getByTestId('input-two');
340
- await waitFor(() => {
341
- expect(document.activeElement).toBe(inputTwo);
342
- });
343
- await waitFor(() => {
344
- userEvent.tab();
345
- expect(onBlur).not.toHaveBeenCalled();
346
- expect(document.activeElement).toBe(inputOne);
347
- });
348
- });
349
- it('should NOT call onBlur when shouldContainFocus=true and Shift+Tab pressing first tabbable', async () => {
350
- const onBlur = vi.fn();
351
- const _render10 = render(_jsx(DialogExample, {
352
- open: true,
353
- shouldContainFocus: true,
354
- defaultFocusElement: () => getByTestId('input-one'),
355
- onBlur: onBlur
356
- })),
357
- getByTestId = _render10.getByTestId;
358
- const inputOne = getByTestId('input-one');
359
- const inputTwo = getByTestId('input-two');
360
- await waitFor(() => {
361
- expect(document.activeElement).toBe(inputOne);
362
- fireEvent.keyDown(inputOne, {
363
- key: 'Tab',
364
- code: 'Tab',
365
- keyCode: 9,
366
- charCode: 9,
367
- shiftKey: true
368
- });
369
- expect(onBlur).not.toHaveBeenCalled();
370
- expect(document.activeElement).toBe(inputTwo);
371
- });
372
- });
373
- it('should call onBlur when shouldContainFocus=false and tab pressing last tabbable', async () => {
374
- const onBlur = vi.fn();
375
- const _render11 = render(_jsx(DialogExample, {
376
- open: true,
377
- shouldContainFocus: false,
378
- defaultFocusElement: () => getByTestId('input-two'),
379
- onBlur: onBlur
380
- })),
381
- getByTestId = _render11.getByTestId;
382
- const inputTwo = getByTestId('input-two');
383
- inputTwo.focus();
384
- await waitFor(() => {
385
- expect(document.activeElement).toBe(inputTwo);
386
- fireEvent.keyDown(inputTwo, {
387
- key: 'Tab',
388
- code: 'Tab',
389
- keyCode: 9,
390
- charCode: 9
391
- });
392
- expect(onBlur).toHaveBeenCalled();
393
- });
394
- });
395
- it('should call onBlur when shouldContainFocus=false and pressing Shift+Tab on the first tabbable', async () => {
396
- const onBlur = vi.fn();
397
- const _render12 = render(_jsx(DialogExample, {
398
- open: true,
399
- shouldContainFocus: false,
400
- defaultFocusElement: () => getByTestId('input-one'),
401
- onBlur: onBlur
402
- })),
403
- getByTestId = _render12.getByTestId;
404
- const inputOne = getByTestId('input-one');
405
- inputOne.focus();
406
- await waitFor(() => {
407
- expect(document.activeElement).toBe(inputOne);
408
- fireEvent.keyDown(inputOne, {
409
- key: 'Tab',
410
- code: 'Tab',
411
- keyCode: 9,
412
- charCode: 9,
413
- shiftKey: true
414
- });
415
- expect(onBlur).toHaveBeenCalled();
416
- });
417
- });
418
- });
419
- });
420
- });