@instructure/ui-dialog 8.46.2-snapshot-7 → 8.46.2-snapshot-9

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.
package/CHANGELOG.md CHANGED
@@ -3,7 +3,7 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
- ## [8.46.2-snapshot-7](https://github.com/instructure/instructure-ui/compare/v8.46.1...v8.46.2-snapshot-7) (2023-10-20)
6
+ ## [8.46.2-snapshot-9](https://github.com/instructure/instructure-ui/compare/v8.46.1...v8.46.2-snapshot-9) (2023-10-24)
7
7
 
8
8
  **Note:** Version bump only for package @instructure/ui-dialog
9
9
 
@@ -0,0 +1,479 @@
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 React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
28
+ import { fireEvent, render, waitFor } from '@testing-library/react';
29
+ import { userEvent } from '@testing-library/user-event';
30
+ import '@testing-library/jest-dom';
31
+ import { Dialog } from '../index';
32
+ const TEST_TEXT = 'test-text';
33
+ const TEST_LABEL = 'test-label';
34
+ const renderDialog = props => {
35
+ const allProps = {
36
+ open: true,
37
+ label: TEST_LABEL,
38
+ ...props
39
+ };
40
+ return render( /*#__PURE__*/React.createElement(Dialog, allProps, _button || (_button = /*#__PURE__*/React.createElement("button", null, TEST_TEXT))));
41
+ };
42
+ const DialogExample = /*#__PURE__*/forwardRef((props, ref) => {
43
+ const inputRef = useRef(null);
44
+ const dialogRef = useRef(null);
45
+ useEffect(() => {
46
+ if (!props.open) {
47
+ inputRef.current.focus();
48
+ }
49
+ }, [props.open]);
50
+ useImperativeHandle(ref, () => ({
51
+ focusDialog: () => dialogRef.current.focus(),
52
+ blurDialog: () => dialogRef.current.blur()
53
+ }));
54
+ return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("input", {
55
+ "data-testid": "input-trigger",
56
+ type: "text",
57
+ ref: inputRef
58
+ }), /*#__PURE__*/React.createElement(Dialog, Object.assign({
59
+ shouldContainFocus: true,
60
+ shouldReturnFocus: true,
61
+ label: TEST_LABEL
62
+ }, props, {
63
+ ref: dialogRef
64
+ }), props.children || _div || (_div = /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("input", {
65
+ type: "text",
66
+ "data-testid": "input-one"
67
+ }), /*#__PURE__*/React.createElement("input", {
68
+ type: "text",
69
+ "data-testid": "input-two"
70
+ })))));
71
+ });
72
+ DialogExample.displayName = 'DialogExample';
73
+ const NestedDialogExample = props => {
74
+ const _useState = useState(false),
75
+ _useState2 = _slicedToArray(_useState, 2),
76
+ nestedOpen = _useState2[0],
77
+ setNestedOpen = _useState2[1];
78
+ const handleTriggerClick = () => setNestedOpen(true);
79
+ return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Dialog, Object.assign({
80
+ open: true,
81
+ shouldReturnFocus: true,
82
+ label: TEST_LABEL
83
+ }, props), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("input", {
84
+ onClick: handleTriggerClick,
85
+ type: "text",
86
+ "data-testid": "nested-input-one"
87
+ }), /*#__PURE__*/React.createElement("input", {
88
+ onClick: handleTriggerClick,
89
+ type: "text",
90
+ "data-testid": "nested-input-two"
91
+ })), /*#__PURE__*/React.createElement(Dialog, {
92
+ open: nestedOpen,
93
+ label: TEST_LABEL
94
+ }, TEST_TEXT))));
95
+ };
96
+ NestedDialogExample.displayName = 'NestedDialogExample';
97
+ describe('<Dialog />', () => {
98
+ it('should render nothing when closed', () => {
99
+ const _renderDialog = renderDialog({
100
+ open: false
101
+ }),
102
+ container = _renderDialog.container;
103
+ expect(container.firstChild).not.toBeInTheDocument();
104
+ });
105
+ it('should render children when open', async () => {
106
+ const _renderDialog2 = renderDialog({
107
+ open: true
108
+ }),
109
+ container = _renderDialog2.container;
110
+ expect(container.firstChild).toBeInTheDocument();
111
+ expect(container.firstChild).toHaveTextContent(TEST_TEXT);
112
+ });
113
+ it('should apply the a11y attributes', () => {
114
+ const _renderDialog3 = renderDialog({
115
+ label: TEST_LABEL
116
+ }),
117
+ getByRole = _renderDialog3.getByRole,
118
+ getByLabelText = _renderDialog3.getByLabelText;
119
+ const dialog = getByRole('dialog');
120
+ const label = getByLabelText(TEST_LABEL);
121
+ expect(dialog).toBeInTheDocument();
122
+ expect(label).toBeInTheDocument();
123
+ });
124
+ it('should apply the role attributes, if explicitly passed', () => {
125
+ const _renderDialog4 = renderDialog({
126
+ label: TEST_LABEL,
127
+ role: 'region'
128
+ }),
129
+ getByRole = _renderDialog4.getByRole,
130
+ getByLabelText = _renderDialog4.getByLabelText;
131
+ const regionRole = getByRole('region');
132
+ const label = getByLabelText(TEST_LABEL);
133
+ expect(regionRole).toBeInTheDocument();
134
+ expect(label).toBeInTheDocument();
135
+ });
136
+ it('should call onDismiss prop when Esc key pressed', async () => {
137
+ const onDismiss = jest.fn();
138
+ const _renderDialog5 = renderDialog({
139
+ onDismiss
140
+ }),
141
+ getByRole = _renderDialog5.getByRole;
142
+ const dialog = getByRole('dialog');
143
+ await waitFor(() => {
144
+ fireEvent.keyUp(dialog, {
145
+ key: 'Escape',
146
+ code: 'Escape',
147
+ keyCode: 27,
148
+ charCode: 27
149
+ });
150
+ expect(onDismiss).toHaveBeenCalled();
151
+ });
152
+ });
153
+ it('should call onDismiss prop when the document is clicked', async () => {
154
+ const onDismiss = jest.fn();
155
+ renderDialog({
156
+ onDismiss,
157
+ shouldCloseOnDocumentClick: true
158
+ });
159
+ await waitFor(() => {
160
+ fireEvent.click(document);
161
+ expect(onDismiss).toHaveBeenCalled();
162
+ });
163
+ });
164
+ describe('managed focus', () => {
165
+ it('should provide focus method', async () => {
166
+ const _render = render( /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(DialogExample, {
167
+ open: true,
168
+ contentElement: () => getByTestId('non-tabbable')
169
+ }, TEST_TEXT), _div2 || (_div2 = /*#__PURE__*/React.createElement("div", {
170
+ tabIndex: -1,
171
+ "data-testid": "non-tabbable"
172
+ }, TEST_TEXT)))),
173
+ getByTestId = _render.getByTestId;
174
+ const nonTabbableContent = getByTestId('non-tabbable');
175
+ await waitFor(() => {
176
+ userEvent.tab();
177
+ expect(document.activeElement).toBe(nonTabbableContent);
178
+ });
179
+ });
180
+ it('should warn when trying to focus or blur a closed dialog', () => {
181
+ const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {});
182
+ let ref;
183
+ render( /*#__PURE__*/React.createElement(DialogExample, {
184
+ open: false,
185
+ ref: el => ref = el
186
+ }));
187
+ ref.focusDialog();
188
+ expect(consoleError.mock.calls[0][0]).toBe("Warning: [Dialog] Can't focus a Dialog that isn't open.");
189
+ ref.blurDialog();
190
+ expect(consoleError.mock.calls[1][0]).toBe("Warning: [Dialog] Can't blur a Dialog that isn't open.");
191
+ consoleError.mockRestore();
192
+ });
193
+ it('should focus the first tabbable element by default', async () => {
194
+ const _render2 = render(_DialogExample || (_DialogExample = /*#__PURE__*/React.createElement(DialogExample, {
195
+ open: true
196
+ }))),
197
+ getByTestId = _render2.getByTestId;
198
+ const inputOne = getByTestId('input-one');
199
+ await waitFor(() => {
200
+ expect(document.activeElement).toBe(inputOne);
201
+ });
202
+ });
203
+ it('should focus the first tabbable element when open prop becomes true', async () => {
204
+ const _render3 = render(_DialogExample2 || (_DialogExample2 = /*#__PURE__*/React.createElement(DialogExample, {
205
+ open: false
206
+ }))),
207
+ rerender = _render3.rerender,
208
+ getByTestId = _render3.getByTestId;
209
+ const inputTrigger = getByTestId('input-trigger');
210
+ await waitFor(() => {
211
+ expect(document.activeElement).toBe(inputTrigger);
212
+ });
213
+ rerender(_DialogExample3 || (_DialogExample3 = /*#__PURE__*/React.createElement(DialogExample, {
214
+ open: true
215
+ })));
216
+ const inputOne = getByTestId('input-one');
217
+ await waitFor(() => {
218
+ expect(document.activeElement).toBe(inputOne);
219
+ });
220
+ });
221
+ it('should take a prop for finding default focus', async () => {
222
+ const _render4 = render( /*#__PURE__*/React.createElement(DialogExample, {
223
+ open: true,
224
+ defaultFocusElement: () => getByTestId('input-two')
225
+ })),
226
+ getByTestId = _render4.getByTestId;
227
+ const inputTwo = getByTestId('input-two');
228
+ await waitFor(() => {
229
+ expect(document.activeElement).toBe(inputTwo);
230
+ });
231
+ });
232
+ it('should still focus the defaultFocusElement when it is focusable but not tabbable', async () => {
233
+ const _render5 = render( /*#__PURE__*/React.createElement(DialogExample, {
234
+ open: true,
235
+ defaultFocusElement: () => getByTestId('non-tabbable')
236
+ }, _div3 || (_div3 = /*#__PURE__*/React.createElement("div", {
237
+ tabIndex: -1,
238
+ "data-testid": "non-tabbable"
239
+ }, TEST_TEXT)))),
240
+ getByTestId = _render5.getByTestId;
241
+ const nonTabbableContent = getByTestId('non-tabbable');
242
+ await waitFor(() => {
243
+ expect(document.activeElement).toBe(nonTabbableContent);
244
+ });
245
+ });
246
+ it('should focus the contentElement by default if focusable and no defaultFocusElement is provided', async () => {
247
+ const _render6 = render( /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(DialogExample, {
248
+ open: true,
249
+ contentElement: () => getByTestId('non-tabbable')
250
+ }, TEST_TEXT), _div4 || (_div4 = /*#__PURE__*/React.createElement("div", {
251
+ tabIndex: -1,
252
+ "data-testid": "non-tabbable"
253
+ }, TEST_TEXT)))),
254
+ getByTestId = _render6.getByTestId;
255
+ const nonTabbableContent = getByTestId('non-tabbable');
256
+ await waitFor(() => {
257
+ expect(document.activeElement).toBe(nonTabbableContent);
258
+ });
259
+ });
260
+ it('should focus the document body if there is no defaultFocusElement, tabbable elements, or focusable contentElement', async () => {
261
+ const _render7 = render(_DialogExample4 || (_DialogExample4 = /*#__PURE__*/React.createElement(DialogExample, {
262
+ open: false
263
+ }, TEST_TEXT))),
264
+ rerender = _render7.rerender,
265
+ getByTestId = _render7.getByTestId;
266
+ const inputTrigger = getByTestId('input-trigger');
267
+ inputTrigger.focus();
268
+ rerender(_DialogExample5 || (_DialogExample5 = /*#__PURE__*/React.createElement(DialogExample, {
269
+ open: true
270
+ }, TEST_TEXT)));
271
+ await waitFor(() => {
272
+ expect(document.activeElement).toBe(document.body);
273
+ });
274
+ });
275
+ it('should return focus', async () => {
276
+ const _render8 = render(_DialogExample6 || (_DialogExample6 = /*#__PURE__*/React.createElement(DialogExample, {
277
+ open: false
278
+ }))),
279
+ rerender = _render8.rerender,
280
+ getByTestId = _render8.getByTestId;
281
+ expect(document.activeElement).toBe(getByTestId('input-trigger'));
282
+ rerender(_DialogExample7 || (_DialogExample7 = /*#__PURE__*/React.createElement(DialogExample, {
283
+ open: true
284
+ })));
285
+ await waitFor(() => {
286
+ expect(document.activeElement).toBe(getByTestId('input-one'));
287
+ });
288
+ rerender(_DialogExample8 || (_DialogExample8 = /*#__PURE__*/React.createElement(DialogExample, {
289
+ open: false
290
+ })));
291
+ await waitFor(() => {
292
+ expect(document.activeElement).toBe(getByTestId('input-trigger'));
293
+ });
294
+ });
295
+ describe('when focus leaves the first and last tabbable', () => {
296
+ it(`should NOT call onBlur when shouldContainFocus=true and tab pressing last tabbable`, async () => {
297
+ const onBlur = jest.fn();
298
+ const _render9 = render( /*#__PURE__*/React.createElement(DialogExample, {
299
+ open: true,
300
+ onBlur: onBlur,
301
+ shouldContainFocus: true,
302
+ defaultFocusElement: () => getByTestId('input-two')
303
+ })),
304
+ getByTestId = _render9.getByTestId;
305
+ const inputOne = getByTestId('input-one');
306
+ const inputTwo = getByTestId('input-two');
307
+ await waitFor(() => {
308
+ expect(document.activeElement).toBe(inputTwo);
309
+ });
310
+ await waitFor(() => {
311
+ userEvent.tab();
312
+ expect(onBlur).not.toHaveBeenCalled();
313
+ expect(document.activeElement).toBe(inputOne);
314
+ });
315
+ });
316
+ it('should NOT call onBlur when shouldContainFocus=true and Shift+Tab pressing first tabbable', async () => {
317
+ const onBlur = jest.fn();
318
+ const _render10 = render( /*#__PURE__*/React.createElement(DialogExample, {
319
+ open: true,
320
+ shouldContainFocus: true,
321
+ defaultFocusElement: () => getByTestId('input-one'),
322
+ onBlur: onBlur
323
+ })),
324
+ getByTestId = _render10.getByTestId;
325
+ const inputOne = getByTestId('input-one');
326
+ const inputTwo = getByTestId('input-two');
327
+ await waitFor(() => {
328
+ expect(document.activeElement).toBe(inputOne);
329
+ fireEvent.keyDown(inputOne, {
330
+ key: 'Tab',
331
+ code: 'Tab',
332
+ keyCode: 9,
333
+ charCode: 9,
334
+ shiftKey: true
335
+ });
336
+ expect(onBlur).not.toHaveBeenCalled();
337
+ expect(document.activeElement).toBe(inputTwo);
338
+ });
339
+ });
340
+ it('should call onBlur when shouldContainFocus=false and tab pressing last tabbable', async () => {
341
+ const onBlur = jest.fn();
342
+ const _render11 = render( /*#__PURE__*/React.createElement(DialogExample, {
343
+ open: true,
344
+ shouldContainFocus: false,
345
+ defaultFocusElement: () => getByTestId('input-two'),
346
+ onBlur: onBlur
347
+ })),
348
+ getByTestId = _render11.getByTestId;
349
+ const inputTwo = getByTestId('input-two');
350
+ inputTwo.focus();
351
+ await waitFor(() => {
352
+ expect(document.activeElement).toBe(inputTwo);
353
+ fireEvent.keyDown(inputTwo, {
354
+ key: 'Tab',
355
+ code: 'Tab',
356
+ keyCode: 9,
357
+ charCode: 9
358
+ });
359
+ expect(onBlur).toHaveBeenCalled();
360
+ });
361
+ });
362
+ it('should call onBlur when shouldContainFocus=false and pressing Shift+Tab on the first tabbable', async () => {
363
+ const onBlur = jest.fn();
364
+ const _render12 = render( /*#__PURE__*/React.createElement(DialogExample, {
365
+ open: true,
366
+ shouldContainFocus: false,
367
+ defaultFocusElement: () => getByTestId('input-one'),
368
+ onBlur: onBlur
369
+ })),
370
+ getByTestId = _render12.getByTestId;
371
+ const inputOne = getByTestId('input-one');
372
+ inputOne.focus();
373
+ await waitFor(() => {
374
+ expect(document.activeElement).toBe(inputOne);
375
+ fireEvent.keyDown(inputOne, {
376
+ key: 'Tab',
377
+ code: 'Tab',
378
+ keyCode: 9,
379
+ charCode: 9,
380
+ shiftKey: true
381
+ });
382
+ expect(onBlur).toHaveBeenCalled();
383
+ });
384
+ });
385
+ describe('when launching a dialog w/out focusable content from another dialog', () => {
386
+ it(`should contain focus when last tabbable element triggers dialog w/out focusable content`, async () => {
387
+ const onBlur = jest.fn();
388
+ const _render13 = render( /*#__PURE__*/React.createElement(NestedDialogExample, {
389
+ onBlur: onBlur,
390
+ shouldContainFocus: true,
391
+ defaultFocusElement: () => getByTestId('nested-input-two')
392
+ })),
393
+ getByTestId = _render13.getByTestId;
394
+ const inputOne = getByTestId('nested-input-one');
395
+ const inputTwo = getByTestId('nested-input-two');
396
+ await waitFor(() => {
397
+ userEvent.click(inputTwo);
398
+ expect(document.activeElement).toBe(inputTwo);
399
+ fireEvent.keyDown(inputTwo, {
400
+ key: 'Tab',
401
+ code: 'Tab',
402
+ keyCode: 9,
403
+ charCode: 9
404
+ });
405
+ expect(onBlur).not.toHaveBeenCalled();
406
+ expect(document.activeElement).toBe(inputOne);
407
+ });
408
+ });
409
+ it('should contain focus when first tabbable element triggers dialog w/out focusable content', async () => {
410
+ const onBlur = jest.fn();
411
+ const _render14 = render( /*#__PURE__*/React.createElement(NestedDialogExample, {
412
+ onBlur: onBlur,
413
+ shouldContainFocus: true,
414
+ defaultFocusElement: () => getByTestId('nested-input-one')
415
+ })),
416
+ getByTestId = _render14.getByTestId;
417
+ const inputOne = getByTestId('nested-input-one');
418
+ const inputTwo = getByTestId('nested-input-two');
419
+ await waitFor(() => {
420
+ userEvent.click(inputOne);
421
+ expect(document.activeElement).toBe(inputOne);
422
+ fireEvent.keyDown(inputOne, {
423
+ key: 'Tab',
424
+ code: 'Tab',
425
+ keyCode: 9,
426
+ charCode: 9,
427
+ shiftKey: true
428
+ });
429
+ expect(onBlur).not.toHaveBeenCalled();
430
+ expect(document.activeElement).toBe(inputTwo);
431
+ });
432
+ });
433
+ it(`should call onBlur when shouldContainFocus=false and last tabbable element triggers dialog w/out focusable content`, async () => {
434
+ const onBlur = jest.fn();
435
+ const _render15 = render( /*#__PURE__*/React.createElement(NestedDialogExample, {
436
+ onBlur: onBlur,
437
+ shouldContainFocus: false,
438
+ defaultFocusElement: () => getByTestId('nested-input-two')
439
+ })),
440
+ getByTestId = _render15.getByTestId;
441
+ const inputTwo = getByTestId('nested-input-two');
442
+ await waitFor(() => {
443
+ userEvent.click(inputTwo);
444
+ expect(document.activeElement).toBe(inputTwo);
445
+ fireEvent.keyDown(inputTwo, {
446
+ key: 'Tab',
447
+ code: 'Tab',
448
+ keyCode: 9,
449
+ charCode: 9
450
+ });
451
+ expect(onBlur).toHaveBeenCalled();
452
+ });
453
+ });
454
+ it(`should call onBlur when shouldContainFocus=false and first tabbable element triggers dialog w/out focusable content`, async () => {
455
+ const onBlur = jest.fn();
456
+ const _render16 = render( /*#__PURE__*/React.createElement(NestedDialogExample, {
457
+ onBlur: onBlur,
458
+ shouldContainFocus: false,
459
+ defaultFocusElement: () => getByTestId('nested-input-one')
460
+ })),
461
+ getByTestId = _render16.getByTestId;
462
+ const inputOne = getByTestId('nested-input-one');
463
+ await waitFor(() => {
464
+ userEvent.click(inputOne);
465
+ expect(document.activeElement).toBe(inputOne);
466
+ fireEvent.keyDown(inputOne, {
467
+ key: 'Tab',
468
+ code: 'Tab',
469
+ keyCode: 9,
470
+ charCode: 9,
471
+ shiftKey: true
472
+ });
473
+ expect(onBlur).toHaveBeenCalled();
474
+ });
475
+ });
476
+ });
477
+ });
478
+ });
479
+ });