@instructure/ui-focusable 10.14.1-snapshot-9 → 10.14.1-snapshot-12

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