@instructure/ui-a11y-utils 9.2.1-snapshot-4 → 9.2.1-snapshot-6

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.
@@ -0,0 +1,240 @@
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 React from 'react'
26
+ import { render, screen, waitFor } from '@testing-library/react'
27
+ import '@testing-library/jest-dom'
28
+
29
+ import { ScreenReaderFocusRegion } from '../ScreenReaderFocusRegion'
30
+
31
+
32
+ describe('ScreenReaderFocusRegion', () => {
33
+ const element = (
34
+ <div
35
+ data-testid="parent"
36
+ role="main"
37
+ aria-label="test app"
38
+ id="test-parent3"
39
+ >
40
+ <div data-testid="ignore" role="alert">
41
+ <span>test alert</span>
42
+ </div>
43
+ <div data-testid="child">
44
+ <div data-testid="descendant">foo</div>
45
+ <div data-testid="descendant">
46
+ <div data-testid="descendant">bar</div>
47
+ </div>
48
+ </div>
49
+ <div data-testid="parent" aria-hidden="true" id="test-parent2">
50
+ <div data-testid="child"></div>
51
+ <div
52
+ role="dialog"
53
+ aria-label="some content"
54
+ data-testid="parent"
55
+ id="test-parent1"
56
+ >
57
+ <div data-testid="content">
58
+ <div>Hello world</div>
59
+ <button>click me</button>
60
+ <button>or click me</button>
61
+ </div>
62
+ <span data-testid="child">
63
+ <ul data-testid="descendant">
64
+ <li data-testid="descendant">item 1</li>
65
+ <li data-testid="descendant">item 2</li>
66
+ <li data-testid="descendant">item 3</li>
67
+ </ul>
68
+ </span>
69
+ </div>
70
+ </div>
71
+ <div data-testid="child-initial-hidden" aria-hidden="true">
72
+ <div data-testid="descendant">foo</div>
73
+ <div data-testid="descendant">bar</div>
74
+ </div>
75
+ </div>
76
+ )
77
+
78
+ it('should accept a function for liveRegion', async () => {
79
+ render(element)
80
+
81
+ const ignore = screen.getByTestId('ignore')
82
+ const content = screen.getByTestId('content')
83
+
84
+ const screenReaderFocusRegion = new ScreenReaderFocusRegion(content, {
85
+ liveRegion: () => ignore,
86
+ shouldContainFocus: true
87
+ })
88
+
89
+ screenReaderFocusRegion.activate()
90
+
91
+ await waitFor(() => {
92
+ expect(ignore).not.toHaveAttribute('aria-hidden')
93
+ })
94
+ })
95
+
96
+ it("should apply aria-hidden to all children of content's parent nodes unless they are live regions", async () => {
97
+ render(element)
98
+
99
+ const ignore = screen.getByTestId('ignore')
100
+ const content = screen.getByTestId('content')
101
+ const children = screen.getAllByTestId('child')
102
+
103
+ const screenReaderFocusRegion = new ScreenReaderFocusRegion(content, {
104
+ liveRegion: ignore,
105
+ shouldContainFocus: true
106
+ })
107
+
108
+ screenReaderFocusRegion.activate()
109
+
110
+ children.forEach((node) => {
111
+ expect(node).toHaveAttribute('aria-hidden')
112
+ })
113
+
114
+ expect(ignore).not.toHaveAttribute('aria-hidden')
115
+ })
116
+
117
+ it("should mute designated attributes for content's parent nodes", async () => {
118
+ render(element)
119
+
120
+ const content = screen.getByTestId('content')
121
+ const parents = screen.getAllByTestId('parent')
122
+
123
+ const screenReaderFocusRegion = new ScreenReaderFocusRegion(content)
124
+ screenReaderFocusRegion.activate()
125
+
126
+ parents.forEach((node) => {
127
+ expect(node).not.toHaveAttribute('aria-hidden')
128
+ expect(node).not.toHaveAttribute('aria-label')
129
+ expect(node).not.toHaveAttribute('role')
130
+ })
131
+ })
132
+
133
+ it('should not apply aria-hidden to descendants', async () => {
134
+ render(element)
135
+
136
+ const content = screen.getByTestId('content')
137
+ const descendants = screen.getAllByTestId('descendant')
138
+
139
+ const screenReaderFocusRegion = new ScreenReaderFocusRegion(content)
140
+ screenReaderFocusRegion.activate()
141
+
142
+ descendants.forEach((node) => {
143
+ expect(node).not.toHaveAttribute('aria-hidden')
144
+ })
145
+ })
146
+
147
+ it('should not apply aria-hidden to dynamically added descendants of content', async () => {
148
+ render(element)
149
+
150
+ const content = screen.getByTestId('content')
151
+ const screenReaderFocusRegion = new ScreenReaderFocusRegion(content)
152
+
153
+ screenReaderFocusRegion.activate()
154
+
155
+ const desc = document.createElement('div')
156
+ content.appendChild(desc)
157
+
158
+ screenReaderFocusRegion.handleDOMMutation([
159
+ { addedNodes: [desc], removedNodes: [] } as unknown as MutationRecord
160
+ ])
161
+
162
+ Array.from(content.childNodes).forEach((node) => {
163
+ expect(node).not.toHaveAttribute('aria-hidden')
164
+ })
165
+ })
166
+
167
+ it('should remove aria-hidden from children unless they had aria-hidden before', async () => {
168
+ render(element)
169
+
170
+ const content = screen.getByTestId('content')
171
+ const screenReaderFocusRegion = new ScreenReaderFocusRegion(content)
172
+
173
+ const childNodes = screen.getAllByTestId('child')
174
+ const exception = screen.getByTestId('child-initial-hidden')
175
+
176
+ screenReaderFocusRegion.activate()
177
+ screenReaderFocusRegion.deactivate()
178
+
179
+ childNodes.forEach((node) => {
180
+ expect(node).not.toHaveAttribute('aria-hidden')
181
+ })
182
+
183
+ expect(exception).toHaveAttribute('aria-hidden')
184
+ })
185
+
186
+ it('should properly restore and unmute parent attributes', async () => {
187
+ render(element)
188
+
189
+ const content = screen.getByTestId('content')
190
+ const screenReaderFocusRegion = new ScreenReaderFocusRegion(content)
191
+
192
+ const parentNodes = screen.getAllByTestId('parent')
193
+ const attrsMap: Record<string, Attr[]> = {}
194
+
195
+ parentNodes.forEach((node) => {
196
+ attrsMap[node.getAttribute('id')!] = [...node.attributes]
197
+ })
198
+
199
+ screenReaderFocusRegion.activate()
200
+ screenReaderFocusRegion.deactivate()
201
+
202
+ parentNodes.forEach((node) => {
203
+ const preNodeAttrs = attrsMap[node.getAttribute('id')!]
204
+ const postNodeAttrs = [...node.attributes]
205
+
206
+ // both should have same number of attributes
207
+ expect(preNodeAttrs.length).toEqual(postNodeAttrs.length)
208
+
209
+ preNodeAttrs.forEach((preNodeAttribute) => {
210
+ const matchingAttribute = postNodeAttrs.filter(
211
+ (postNodeAttribute) =>
212
+ preNodeAttribute.name === postNodeAttribute.name
213
+ )[0]
214
+
215
+ expect(matchingAttribute.value).toEqual(preNodeAttribute.value)
216
+ })
217
+ })
218
+ })
219
+
220
+ it('should not apply aria-hidden to elements that have aria-live attributes', async () => {
221
+ render(
222
+ <div data-testid="main" role="main" aria-label="test app">
223
+ <div data-testid="live" aria-live="assertive"></div>
224
+ <div data-testid="regular"></div>
225
+ <div data-testid="content"></div>
226
+ </div>
227
+ )
228
+
229
+ const content = screen.getByTestId('content')
230
+ const screenReaderFocusRegion = new ScreenReaderFocusRegion(content)
231
+
232
+ screenReaderFocusRegion.activate()
233
+
234
+ const liveRegion = screen.getByTestId('live')
235
+ const regularRegion = screen.getByTestId('regular')
236
+
237
+ expect(liveRegion).not.toHaveAttribute('aria-hidden')
238
+ expect(regularRegion).toHaveAttribute('aria-hidden')
239
+ })
240
+ })
@@ -0,0 +1,40 @@
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 React from 'react'
26
+ import '@testing-library/jest-dom'
27
+
28
+ import { ScreenReaderContent } from '@instructure/ui-a11y-content'
29
+ import { hasVisibleChildren } from '../hasVisibleChildren'
30
+
31
+ describe('hasVisibleChildren', () => {
32
+ it('should not count ScreenReaderContent as visible content', async () => {
33
+ expect(
34
+ hasVisibleChildren(<ScreenReaderContent>Foo</ScreenReaderContent>)
35
+ ).toBe(false)
36
+ })
37
+ it('should count everything else as visible', async () => {
38
+ expect(hasVisibleChildren(<div>Foo</div>)).toBe(true)
39
+ })
40
+ })
@@ -0,0 +1,106 @@
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 React from 'react'
26
+ import { render, screen, waitFor } from '@testing-library/react'
27
+ import { vi } from 'vitest'
28
+ import '@testing-library/jest-dom'
29
+
30
+ import { scopeTab } from '../scopeTab'
31
+
32
+
33
+ const MOCK_EVENT = new KeyboardEvent('mockEvent', { shiftKey: false })
34
+ MOCK_EVENT.preventDefault = () => {}
35
+
36
+ describe('scopeTab', () => {
37
+ it('should scope tab within container', async () => {
38
+ render(
39
+ <div>
40
+ <div data-testid="container">
41
+ <input data-testid="first" />
42
+ <input data-testid="second" />
43
+ </div>
44
+ </div>
45
+ )
46
+
47
+ const container = screen.getByTestId('container')
48
+ const first = screen.getByTestId('first')
49
+ const second = screen.getByTestId('second')
50
+
51
+ second.focus()
52
+
53
+ await waitFor(() => {
54
+ expect(document.activeElement).toBe(second)
55
+ })
56
+
57
+ scopeTab(container, MOCK_EVENT as any)
58
+
59
+ await waitFor(() => {
60
+ expect(document.activeElement).toBe(first)
61
+ })
62
+ })
63
+
64
+ it('should not attempt scoping when no tabbable children', async () => {
65
+ render(
66
+ <div>
67
+ <div data-testid="container">Hello</div>
68
+ <input />
69
+ </div>
70
+ )
71
+
72
+ const input = screen.getByRole('textbox')
73
+ const container = screen.getByTestId('container')
74
+
75
+ input.focus()
76
+
77
+ scopeTab(container, MOCK_EVENT as any)
78
+
79
+ await waitFor(() => {
80
+ expect(document.activeElement).toBe(input)
81
+ })
82
+ })
83
+
84
+ it('should execute callback when provided instead of default behavior', async () => {
85
+ const cb = vi.fn()
86
+
87
+ render(
88
+ <div>
89
+ <div data-testid="container">
90
+ <input />
91
+ </div>
92
+ </div>
93
+ )
94
+
95
+ const input = screen.getByRole('textbox')
96
+ const container = screen.getByTestId('container')
97
+
98
+ input.focus()
99
+
100
+ scopeTab(container, MOCK_EVENT as any, cb)
101
+
102
+ await waitFor(() => {
103
+ expect(cb).toHaveBeenCalled()
104
+ })
105
+ })
106
+ })
@@ -15,7 +15,6 @@
15
15
  { "path": "../uid/tsconfig.build.json" },
16
16
  { "path": "../ui-babel-preset/tsconfig.build.json" },
17
17
  { "path": "../ui-color-utils/tsconfig.build.json" },
18
- { "path": "../shared-types/tsconfig.build.json" },
19
- { "path": "../ui-test-utils/tsconfig.build.json" }
18
+ { "path": "../shared-types/tsconfig.build.json" }
20
19
  ]
21
20
  }