@instructure/ui-focusable 10.19.2-snapshot-3 → 10.19.2-snapshot-5

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