@instructure/ui-focusable 10.19.2-snapshot-3 → 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,412 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- var _react = require("react");
5
- var _react2 = require("@testing-library/react");
6
- var _userEvent = _interopRequireDefault(require("@testing-library/user-event"));
7
- var _vitest = require("vitest");
8
- require("@testing-library/jest-dom");
9
- var _index = require("../index");
10
- var _jsxRuntime = require("@emotion/react/jsx-runtime");
11
- var _button, _button2, _input, _a, _a2, _button3, _button4, _span, _span2, _span3, _span4, _button5, _button6;
12
- /*
13
- * The MIT License (MIT)
14
- *
15
- * Copyright (c) 2015 - present Instructure, Inc.
16
- *
17
- * Permission is hereby granted, free of charge, to any person obtaining a copy
18
- * of this software and associated documentation files (the "Software"), to deal
19
- * in the Software without restriction, including without limitation the rights
20
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21
- * copies of the Software, and to permit persons to whom the Software is
22
- * furnished to do so, subject to the following conditions:
23
- *
24
- * The above copyright notice and this permission notice shall be included in all
25
- * copies or substantial portions of the Software.
26
- *
27
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33
- * SOFTWARE.
34
- */
35
- describe('<Focusable />', () => {
36
- let consoleWarningMock;
37
- let consoleErrorMock;
38
- beforeEach(() => {
39
- // Mocking console to prevent test output pollution and expect for messages
40
- consoleWarningMock = _vitest.vi.spyOn(console, 'warn').mockImplementation(() => {});
41
- consoleErrorMock = _vitest.vi.spyOn(console, 'error').mockImplementation(() => {});
42
- });
43
- afterEach(() => {
44
- consoleWarningMock.mockRestore();
45
- consoleErrorMock.mockRestore();
46
- });
47
- it('should render', async () => {
48
- (0, _react2.render)((0, _jsxRuntime.jsx)(_index.Focusable, {
49
- children: () => _button || (_button = (0, _jsxRuntime.jsx)("button", {
50
- children: "hello world"
51
- }))
52
- }));
53
- const button = _react2.screen.getByRole('button');
54
- expect(button).toBeInTheDocument();
55
- expect(button).toHaveTextContent('hello world');
56
- });
57
- it('should call children function with focused when element receives focus', async () => {
58
- const renderSpy = _vitest.vi.fn();
59
- (0, _react2.render)((0, _jsxRuntime.jsx)(_index.Focusable, {
60
- children: args => {
61
- renderSpy(args);
62
- return _button2 || (_button2 = (0, _jsxRuntime.jsx)("button", {
63
- type: "button",
64
- children: "foo"
65
- }));
66
- }
67
- }));
68
- const focusable = _react2.screen.getByRole('button');
69
- await (0, _react2.waitFor)(() => {
70
- const args = renderSpy.mock.lastCall[0];
71
- expect(args).toHaveProperty('focused', false);
72
- expect(args).toHaveProperty('focusable', focusable);
73
- expect(args).toHaveProperty('focusVisible', false);
74
- });
75
- await _userEvent.default.type(focusable, '{enter}');
76
- await focusable.focus();
77
- await (0, _react2.waitFor)(() => {
78
- const args = renderSpy.mock.lastCall[0];
79
- expect(document.activeElement).toBe(focusable);
80
- expect(args).toHaveProperty('focused', true);
81
- expect(args).toHaveProperty('focusable', focusable);
82
- expect(args).toHaveProperty('focusVisible', true);
83
- });
84
- await focusable.blur();
85
- await (0, _react2.waitFor)(() => {
86
- const args = renderSpy.mock.lastCall[0];
87
- expect(document.activeElement).not.toBe(focusable);
88
- expect(args).toHaveProperty('focused', false);
89
- expect(args).toHaveProperty('focusable', focusable);
90
- expect(args).toHaveProperty('focusVisible', false);
91
- });
92
- });
93
- it('should handle conditionally rendered focus elements', async () => {
94
- const renderSpy = _vitest.vi.fn(({
95
- focused
96
- }) => (0, _jsxRuntime.jsx)("div", {
97
- children: focused ? _input || (_input = (0, _jsxRuntime.jsx)("input", {
98
- type: "text"
99
- })) : _a || (_a = (0, _jsxRuntime.jsx)("a", {
100
- href: "http://focus.net",
101
- children: "Click"
102
- }))
103
- }));
104
- (0, _react2.render)((0, _jsxRuntime.jsx)(_index.Focusable, {
105
- render: renderSpy
106
- }));
107
- const firstFocusable = _react2.screen.getByText('Click');
108
- expect(firstFocusable.tagName).toBe('A');
109
- expect(firstFocusable).not.toHaveFocus();
110
- await (0, _react2.waitFor)(() => {
111
- const args = renderSpy.mock.lastCall[0];
112
- expect(args.focused).toBe(false);
113
- expect(args.focusable).toBe(firstFocusable);
114
- });
115
- await firstFocusable.focus();
116
- const nextFocusable = _react2.screen.getByRole('textbox');
117
- expect(nextFocusable.tagName).toBe('INPUT');
118
- expect(nextFocusable).toHaveFocus();
119
- await (0, _react2.waitFor)(() => {
120
- const args = renderSpy.mock.lastCall[0];
121
- expect(args.focused).toBe(true);
122
- expect(args.focusable).toBe(nextFocusable);
123
- });
124
- await nextFocusable.blur();
125
- const lastFocusable = _react2.screen.getByText('Click');
126
- expect(lastFocusable.tagName).toBe('A');
127
- expect(lastFocusable).not.toHaveFocus();
128
- await (0, _react2.waitFor)(() => {
129
- const args = renderSpy.mock.lastCall[0];
130
- expect(args.focused).toBe(false);
131
- expect(args.focusable).toBe(lastFocusable);
132
- });
133
- });
134
- it('should maintain focus when the focus element changes', async () => {
135
- const renderSpy = _vitest.vi.fn(() => _a2 || (_a2 = (0, _jsxRuntime.jsx)("a", {
136
- href: "http://focus.net",
137
- children: "Click"
138
- })));
139
- const _render = (0, _react2.render)((0, _jsxRuntime.jsx)(_index.Focusable, {
140
- render: renderSpy
141
- })),
142
- rerender = _render.rerender;
143
- const firstFocusable = _react2.screen.getByText('Click');
144
- await firstFocusable.focus();
145
- await (0, _react2.waitFor)(() => {
146
- const lastCall = renderSpy.mock.lastCall;
147
- const args = lastCall === null || lastCall === void 0 ? void 0 : lastCall[0];
148
- expect(firstFocusable).toHaveFocus();
149
- expect(args === null || args === void 0 ? void 0 : args.focused).toBe(true);
150
- expect(args === null || args === void 0 ? void 0 : args.focusable).toBe(firstFocusable);
151
- });
152
-
153
- // Set prop: render
154
- const nextRenderSpy = _vitest.vi.fn(() => _button3 || (_button3 = (0, _jsxRuntime.jsx)("button", {
155
- children: "Click"
156
- })));
157
- rerender((0, _jsxRuntime.jsx)(_index.Focusable, {
158
- render: nextRenderSpy
159
- }));
160
- const nextFocusable = _react2.screen.getByText('Click');
161
- expect(nextFocusable.tagName).toBe('BUTTON');
162
- expect(nextFocusable).toHaveFocus();
163
- await (0, _react2.waitFor)(() => {
164
- const nextLastCall = nextRenderSpy.mock.lastCall;
165
- const nextArgs = nextLastCall === null || nextLastCall === void 0 ? void 0 : nextLastCall[0];
166
- expect(nextFocusable).toHaveFocus();
167
- expect(nextArgs === null || nextArgs === void 0 ? void 0 : nextArgs.focused).toBe(true);
168
- expect(nextArgs === null || nextArgs === void 0 ? void 0 : nextArgs.focusable).toBe(nextFocusable);
169
- });
170
- });
171
- it('should update the focus element correctly', async () => {
172
- const renderSpy = _vitest.vi.fn();
173
- const _render2 = (0, _react2.render)((0, _jsxRuntime.jsx)(_index.Focusable, {
174
- children: args => {
175
- renderSpy(args);
176
- return _button4 || (_button4 = (0, _jsxRuntime.jsx)("button", {
177
- children: "foo"
178
- }));
179
- }
180
- })),
181
- rerender = _render2.rerender;
182
- const firstButton = _react2.screen.getByText('foo');
183
- await firstButton.focus();
184
- await (0, _react2.waitFor)(() => {
185
- const args = renderSpy.mock.lastCall[0];
186
- expect(args.focused).toBe(true);
187
- });
188
- await firstButton.blur();
189
- await (0, _react2.waitFor)(() => {
190
- const args = renderSpy.mock.lastCall[0];
191
- expect(args.focused).toBe(false);
192
- });
193
-
194
- // Set child
195
- rerender((0, _jsxRuntime.jsx)(_index.Focusable, {
196
- children: args => {
197
- renderSpy(args);
198
- return _span || (_span = (0, _jsxRuntime.jsxs)("span", {
199
- children: ["some content text", (0, _jsxRuntime.jsx)("button", {
200
- children: "bar"
201
- })]
202
- }));
203
- }
204
- }));
205
- const secondButton = _react2.screen.getByText('bar');
206
- await secondButton.focus();
207
- await (0, _react2.waitFor)(() => {
208
- const args = renderSpy.mock.lastCall[0];
209
- expect(args.focused).toBe(true);
210
- });
211
- });
212
- it('should warn when there is more than one focusable descendant', async () => {
213
- (0, _react2.render)((0, _jsxRuntime.jsx)(_index.Focusable, {
214
- children: () => {
215
- return _span2 || (_span2 = (0, _jsxRuntime.jsxs)("span", {
216
- children: [(0, _jsxRuntime.jsx)("button", {
217
- children: "foo"
218
- }), (0, _jsxRuntime.jsx)("span", {
219
- children: (0, _jsxRuntime.jsx)("button", {
220
- children: "bar"
221
- })
222
- })]
223
- }));
224
- }
225
- }));
226
- const expectedWarningMessage = 'Warning: [Focusable] Exactly one focusable child is required (2 found).';
227
- expect(consoleWarningMock).toHaveBeenCalledWith(expect.stringContaining(expectedWarningMessage), expect.any(String));
228
- });
229
- it('should warn when there are no focusable descendants', async () => {
230
- (0, _react2.render)((0, _jsxRuntime.jsx)(_index.Focusable, {
231
- children: () => {
232
- return _span3 || (_span3 = (0, _jsxRuntime.jsx)("span", {
233
- children: "hello!"
234
- }));
235
- }
236
- }));
237
- const expectedWarningMessage = 'Warning: [Focusable] Exactly one focusable child is required (0 found).';
238
- expect(consoleWarningMock).toHaveBeenCalledWith(expect.stringContaining(expectedWarningMessage), expect.any(String));
239
- });
240
- it('should attach event listener correctly even when the focusable element is not the root', async () => {
241
- const renderSpy = _vitest.vi.fn();
242
- (0, _react2.render)((0, _jsxRuntime.jsx)(_index.Focusable, {
243
- children: args => {
244
- renderSpy(args);
245
- return _span4 || (_span4 = (0, _jsxRuntime.jsxs)("span", {
246
- children: [(0, _jsxRuntime.jsx)("h1", {
247
- children: "hello world"
248
- }), (0, _jsxRuntime.jsx)("p", {
249
- children: "some content"
250
- }), (0, _jsxRuntime.jsx)("span", {
251
- children: (0, _jsxRuntime.jsx)("button", {
252
- children: "foo"
253
- })
254
- })]
255
- }));
256
- }
257
- }));
258
- const button = _react2.screen.getByRole('button');
259
- await (0, _react2.waitFor)(() => {
260
- const args = renderSpy.mock.lastCall[0];
261
- expect(args.focused).toBe(false);
262
- });
263
- await button.focus();
264
- await (0, _react2.waitFor)(() => {
265
- const args = renderSpy.mock.lastCall[0];
266
- expect(args.focused).toBe(true);
267
- });
268
- });
269
- it('should provide a focus method', async () => {
270
- let focusable;
271
- (0, _react2.render)((0, _jsxRuntime.jsx)(_index.Focusable, {
272
- ref: el => {
273
- focusable = el;
274
- },
275
- children: () => _button5 || (_button5 = (0, _jsxRuntime.jsx)("button", {
276
- children: "hello world"
277
- }))
278
- }));
279
- await focusable.focus();
280
- const button = _react2.screen.getByText('hello world');
281
- expect(button).toHaveFocus();
282
- });
283
- it('should provide a focused getter', async () => {
284
- var _focusableInstance;
285
- let focusableInstance;
286
- (0, _react2.render)((0, _jsxRuntime.jsx)(_index.Focusable, {
287
- children: args => {
288
- focusableInstance = args;
289
- return _button6 || (_button6 = (0, _jsxRuntime.jsx)("button", {
290
- children: "Click me"
291
- }));
292
- }
293
- }));
294
- const button = _react2.screen.getByText('Click me');
295
- expect((_focusableInstance = focusableInstance) === null || _focusableInstance === void 0 ? void 0 : _focusableInstance.focused).toBe(false);
296
- await button.focus();
297
- await (0, _react2.waitFor)(() => {
298
- var _focusableInstance2;
299
- expect((_focusableInstance2 = focusableInstance) === null || _focusableInstance2 === void 0 ? void 0 : _focusableInstance2.focused).toBe(true);
300
- expect(button).toHaveFocus();
301
- });
302
- });
303
- it('should properly restore the event handlers', async () => {
304
- let inputRef = null;
305
- class TestComponent extends _react.Component {
306
- render() {
307
- const changeValue = this.props.changeValue;
308
- return (0, _jsxRuntime.jsx)(_index.Focusable, {
309
- children: ({
310
- focusVisible
311
- }) => {
312
- return (0, _jsxRuntime.jsx)("input", {
313
- readOnly: true,
314
- ref: el => {
315
- inputRef = el;
316
- },
317
- value: `${focusVisible}_${changeValue}`
318
- });
319
- }
320
- });
321
- }
322
- }
323
- TestComponent.displayName = "TestComponent";
324
- const _render3 = (0, _react2.render)((0, _jsxRuntime.jsx)(TestComponent, {
325
- changeValue: "A"
326
- })),
327
- rerender = _render3.rerender,
328
- container = _render3.container;
329
- const initialInput = container.querySelector('input');
330
- await (0, _react2.waitFor)(() => {
331
- expect(initialInput).toHaveValue('false_A');
332
- });
333
-
334
- // Set Prop: changeValue
335
- rerender((0, _jsxRuntime.jsx)(TestComponent, {
336
- changeValue: "B"
337
- }));
338
- await inputRef.focus();
339
- await (0, _react2.waitFor)(() => {
340
- expect(inputRef).toHaveValue('true_B');
341
- });
342
- });
343
- it('should properly clear the focusable / focused state when focus is unexpectedly lost', async () => {
344
- let buttonRef;
345
- let focusableRef;
346
- let labelRef;
347
- class TestComponent extends _react.Component {
348
- constructor(...args) {
349
- super(...args);
350
- this.state = {
351
- checked: false,
352
- disabled: false
353
- };
354
- }
355
- render() {
356
- const _this$state = this.state,
357
- checked = _this$state.checked,
358
- disabled = _this$state.disabled;
359
- return (0, _jsxRuntime.jsxs)("div", {
360
- children: [(0, _jsxRuntime.jsx)(_index.Focusable, {
361
- ref: el => {
362
- focusableRef = el;
363
- },
364
- children: () => {
365
- return (0, _jsxRuntime.jsx)("label", {
366
- ref: el => {
367
- labelRef = el;
368
- },
369
- "aria-label": 'test-label',
370
- children: (0, _jsxRuntime.jsx)("input", {
371
- checked: checked,
372
- disabled: disabled,
373
- onChange: () => {
374
- this.setState({
375
- checked: true
376
- });
377
- },
378
- type: "checkbox"
379
- })
380
- });
381
- }
382
- }), (0, _jsxRuntime.jsx)("button", {
383
- onClick: () => {
384
- this.setState({
385
- disabled: true
386
- });
387
- },
388
- ref: el => {
389
- buttonRef = el;
390
- },
391
- children: "hello world"
392
- })]
393
- });
394
- }
395
- }
396
- TestComponent.displayName = "TestComponent";
397
- const _render4 = (0, _react2.render)((0, _jsxRuntime.jsx)(TestComponent, {})),
398
- container = _render4.container;
399
- const input = container.querySelector('input');
400
- await (0, _react2.waitFor)(() => {
401
- expect(focusableRef.focused).toBe(false);
402
- });
403
- await _userEvent.default.click(labelRef);
404
- await (0, _react2.waitFor)(() => {
405
- expect(input).toHaveFocus();
406
- });
407
- await _userEvent.default.click(buttonRef);
408
- await (0, _react2.waitFor)(() => {
409
- expect(input).not.toHaveFocus();
410
- });
411
- });
412
- });