@instructure/ui-alerts 10.2.1 → 10.2.2-snapshot-1

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,275 @@
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 { runAxeCheck } from '@instructure/ui-axe-check'
29
+ import '@testing-library/jest-dom'
30
+ import userEvent from '@testing-library/user-event'
31
+
32
+ import { Alert } from '../index'
33
+ import type { AlertProps } from '../props'
34
+ // eslint-disable-next-line no-restricted-imports
35
+ import { generateA11yTests } from '@instructure/ui-scripts/lib/test/generateA11yTests'
36
+ import AlertExamples from '../__examples__/Alert.examples'
37
+
38
+ describe('<Alert />', () => {
39
+ let srdiv: HTMLDivElement | null
40
+ let consoleWarningMock: ReturnType<typeof vi.spyOn>
41
+ let consoleErrorMock: ReturnType<typeof vi.spyOn>
42
+
43
+ beforeEach(async () => {
44
+ // Mocking console to prevent test output pollution and expect
45
+ consoleWarningMock = vi
46
+ .spyOn(console, 'warn')
47
+ .mockImplementation(() => {}) as any
48
+ consoleErrorMock = vi
49
+ .spyOn(console, 'error')
50
+ .mockImplementation(() => {}) as any
51
+ srdiv = document.createElement('div')
52
+ srdiv.id = '_alertLiveRegion'
53
+ srdiv.setAttribute('role', 'alert')
54
+ srdiv.setAttribute('aria-live', 'assertive')
55
+ srdiv.setAttribute('aria-relevant', 'additions text')
56
+ srdiv.setAttribute('aria-atomic', 'false')
57
+ document.body.appendChild(srdiv)
58
+ })
59
+
60
+ afterEach(async () => {
61
+ srdiv?.parentNode?.removeChild(srdiv)
62
+ srdiv = null
63
+ consoleWarningMock.mockRestore()
64
+ consoleErrorMock.mockRestore()
65
+ })
66
+
67
+ it('should render', async () => {
68
+ render(<Alert variant="success">Success: Sample alert text.</Alert>)
69
+ const text = screen.getByText('Success: Sample alert text.')
70
+ expect(text).toBeInTheDocument()
71
+ })
72
+
73
+ describe('with generated examples', () => {
74
+ const generatedComponents = generateA11yTests(Alert, AlertExamples)
75
+
76
+ for (const component of generatedComponents) {
77
+ it(component.description, async () => {
78
+ const { container } = render(component.content)
79
+ const axeCheck = await runAxeCheck(container)
80
+ expect(axeCheck).toBe(true)
81
+ })
82
+ }
83
+ })
84
+
85
+ it('should not render the Close button when `renderCloseButtonLabel` is not provided', async () => {
86
+ render(<Alert variant="success">Success: Sample alert text.</Alert>)
87
+ const closeButton = screen.queryByRole('button')
88
+
89
+ expect(closeButton).not.toBeInTheDocument()
90
+ })
91
+
92
+ it('should call `onDismiss` when the close button is clicked with renderCloseButtonLabel', async () => {
93
+ const onDismiss = vi.fn()
94
+ render(
95
+ <Alert
96
+ variant="success"
97
+ renderCloseButtonLabel={<div>Close</div>}
98
+ onDismiss={onDismiss}
99
+ >
100
+ Success: Sample alert text.
101
+ </Alert>
102
+ )
103
+ const closeButton = screen.getByRole('button')
104
+
105
+ userEvent.click(closeButton)
106
+
107
+ await waitFor(() => {
108
+ expect(onDismiss).toHaveBeenCalled()
109
+ })
110
+ })
111
+
112
+ const iconComponentsVariants: Record<
113
+ NonNullable<AlertProps['variant']>,
114
+ string
115
+ > = {
116
+ error: 'IconNo',
117
+ info: 'IconInfoBorderless',
118
+ success: 'IconCheckMark',
119
+ warning: 'IconWarningBorderless'
120
+ }
121
+
122
+ ;(
123
+ Object.entries(iconComponentsVariants) as [
124
+ NonNullable<AlertProps['variant']>,
125
+ string
126
+ ][]
127
+ ).forEach(([variant, iconComponent]) => {
128
+ it(`"${variant}" variant should have icon "${iconComponent}".`, async () => {
129
+ const { container } = render(
130
+ <Alert variant={variant} transition="none">
131
+ Success: Sample alert text.
132
+ </Alert>
133
+ )
134
+ const icon = container.querySelector('svg[class$="-svgIcon"]')
135
+
136
+ expect(icon).toHaveAttribute('name', iconComponent)
137
+ })
138
+ })
139
+
140
+ it('should meet a11y standards', async () => {
141
+ const { container } = render(
142
+ <Alert variant="success" transition="none">
143
+ Success: Sample alert text.
144
+ </Alert>
145
+ )
146
+ const axeCheck = await runAxeCheck(container)
147
+ expect(axeCheck).toBe(true)
148
+ })
149
+
150
+ it('should add alert text to aria live region, when present', async () => {
151
+ const liveRegion = document.getElementById('_alertLiveRegion')!
152
+ render(
153
+ <Alert
154
+ variant="success"
155
+ transition="none"
156
+ liveRegion={() => liveRegion}
157
+ liveRegionPoliteness="polite"
158
+ >
159
+ Success: Sample alert text.
160
+ </Alert>
161
+ )
162
+
163
+ expect(liveRegion).toHaveTextContent('Success: Sample alert text.')
164
+ expect(liveRegion).toHaveAttribute('aria-live', 'polite')
165
+ })
166
+
167
+ describe('with `screenReaderOnly', () => {
168
+ it('should not render anything when using `liveRegion`', async () => {
169
+ const liveRegion = document.getElementById('_alertLiveRegion')!
170
+ const { container } = render(
171
+ <Alert
172
+ variant="success"
173
+ liveRegion={() => liveRegion}
174
+ screenReaderOnly={true}
175
+ >
176
+ Success: Sample alert text. asdsfds
177
+ </Alert>
178
+ )
179
+
180
+ expect(container.children.length).toBe(0)
181
+ expect(liveRegion.children.length).toBe(1)
182
+ })
183
+
184
+ it('should warn if `liveRegion` is not defined', async () => {
185
+ const consoleWarningSpy = vi
186
+ .spyOn(console, 'error')
187
+ .mockImplementation(() => {})
188
+ const warning =
189
+ "Warning: [Alert] The 'screenReaderOnly' prop must be used in conjunction with 'liveRegion'."
190
+ render(
191
+ <Alert variant="success" screenReaderOnly={true}>
192
+ Success: Sample alert text.
193
+ </Alert>
194
+ )
195
+
196
+ await waitFor(() => {
197
+ expect(consoleWarningSpy.mock.calls[0][0]).toEqual(
198
+ expect.stringContaining(warning)
199
+ )
200
+ })
201
+ })
202
+
203
+ it('should set aria-atomic to the aria live region when isLiveRegionAtomic is present', async () => {
204
+ const liveRegion = document.getElementById('_alertLiveRegion')!
205
+ render(
206
+ <Alert
207
+ variant="success"
208
+ transition="none"
209
+ liveRegion={() => liveRegion}
210
+ liveRegionPoliteness="polite"
211
+ isLiveRegionAtomic
212
+ >
213
+ Success: Sample alert text.
214
+ </Alert>
215
+ )
216
+
217
+ expect(liveRegion).toHaveTextContent('Success: Sample alert text.')
218
+ expect(liveRegion).toHaveAttribute('aria-atomic', 'true')
219
+ })
220
+
221
+ it('should close when told to, with transition', async () => {
222
+ const liveRegion = document.getElementById('_alertLiveRegion')!
223
+ const { rerender } = render(
224
+ <Alert variant="success" liveRegion={() => liveRegion}>
225
+ Success: Sample alert text.
226
+ </Alert>
227
+ )
228
+
229
+ expect(liveRegion.children.length).toBe(1)
230
+
231
+ //set open to false
232
+ rerender(
233
+ <Alert variant="success" open={false} liveRegion={() => liveRegion}>
234
+ Success: Sample alert text.
235
+ </Alert>
236
+ )
237
+
238
+ await waitFor(() => {
239
+ expect(liveRegion.children.length).toBe(0)
240
+ })
241
+ })
242
+
243
+ it('should close when told to, without transition', async () => {
244
+ const liveRegion = document.getElementById('_alertLiveRegion')!
245
+ const { rerender, container } = render(
246
+ <Alert
247
+ variant="success"
248
+ transition="none"
249
+ liveRegion={() => liveRegion}
250
+ >
251
+ Success: Sample alert text.
252
+ </Alert>
253
+ )
254
+
255
+ expect(liveRegion.children.length).toBe(1)
256
+
257
+ //set open to false
258
+ rerender(
259
+ <Alert
260
+ open={false}
261
+ variant="success"
262
+ transition="none"
263
+ liveRegion={() => liveRegion}
264
+ >
265
+ Success: Sample alert text.
266
+ </Alert>
267
+ )
268
+
269
+ await waitFor(() => {
270
+ expect(container).not.toHaveTextContent('Success: Sample alert text.')
271
+ expect(liveRegion.children.length).toBe(0)
272
+ })
273
+ })
274
+ })
275
+ })
@@ -14,10 +14,12 @@
14
14
  { "path": "../emotion/tsconfig.build.json" },
15
15
  { "path": "../shared-types/tsconfig.build.json" },
16
16
  { "path": "../ui-a11y-content/tsconfig.build.json" },
17
+ { "path": "../ui-axe-check/tsconfig.build.json" },
17
18
  { "path": "../ui-buttons/tsconfig.build.json" },
18
19
  { "path": "../ui-icons/tsconfig.build.json" },
19
20
  { "path": "../ui-motion/tsconfig.build.json" },
20
21
  { "path": "../ui-react-utils/tsconfig.build.json" },
22
+ { "path": "../ui-scripts/tsconfig.build.json" },
21
23
  { "path": "../ui-themes/tsconfig.build.json" },
22
24
  { "path": "../ui-view/tsconfig.build.json" }
23
25
  ]