@instructure/ui-tabs 10.14.0 → 10.14.1-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,210 @@
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 userEvent from '@testing-library/user-event'
29
+ import '@testing-library/jest-dom'
30
+
31
+ import { Tab } from '../index'
32
+
33
+ describe('<Tabs.Tab />', () => {
34
+ it('should render children', async () => {
35
+ render(
36
+ <Tab id="foo" index={0} controls="foo-panel">
37
+ Tab Label
38
+ </Tab>
39
+ )
40
+ const children = screen.getByText('Tab Label')
41
+
42
+ expect(children).toBeInTheDocument()
43
+ })
44
+
45
+ it('should have appropriate role attribute', async () => {
46
+ render(
47
+ <Tab id="foo" index={0} controls="foo-panel">
48
+ Tab Label
49
+ </Tab>
50
+ )
51
+ const tab = screen.getByRole('tab')
52
+
53
+ expect(tab).toBeInTheDocument()
54
+ })
55
+
56
+ it('should have appropriate aria attributes', async () => {
57
+ render(
58
+ <Tab id="foo" index={0} controls="foo-panel">
59
+ Tab Label
60
+ </Tab>
61
+ )
62
+ const tab = screen.getByRole('tab')
63
+
64
+ expect(tab).not.toHaveAttribute('aria-selected')
65
+ expect(tab).not.toHaveAttribute('aria-disabled')
66
+ })
67
+
68
+ it('should set the aria-selected attribute', async () => {
69
+ render(
70
+ <Tab id="foo" index={0} controls="foo-panel" isSelected>
71
+ Tab Label
72
+ </Tab>
73
+ )
74
+ const tab = screen.getByRole('tab')
75
+
76
+ expect(tab).toHaveAttribute('aria-selected', 'true')
77
+ })
78
+
79
+ it('should set the aria-disabled attribute', async () => {
80
+ render(
81
+ <Tab id="foo" index={0} controls="foo-panel" isDisabled>
82
+ Tab Label
83
+ </Tab>
84
+ )
85
+ const tab = screen.getByRole('tab')
86
+
87
+ expect(tab).toHaveAttribute('aria-disabled', 'true')
88
+ })
89
+
90
+ it('should set the tabindex to 0 when selected', async () => {
91
+ render(
92
+ <Tab id="foo" index={0} controls="foo-panel" isSelected>
93
+ Tab Label
94
+ </Tab>
95
+ )
96
+ const tab = screen.getByRole('tab')
97
+
98
+ expect(tab).toHaveAttribute('tabindex', '0')
99
+ })
100
+
101
+ it('should not set the tabindex when not selected', async () => {
102
+ render(
103
+ <Tab id="foo" index={0} controls="foo-panel">
104
+ Tab Label
105
+ </Tab>
106
+ )
107
+ const tab = screen.getByRole('tab')
108
+
109
+ expect(tab).not.toHaveAttribute('tabindex')
110
+ })
111
+
112
+ it('should remove the tabindex attribute when disabled', async () => {
113
+ render(
114
+ <Tab id="foo" index={0} controls="foo-panel" isDisabled>
115
+ Tab Label
116
+ </Tab>
117
+ )
118
+ const tab = screen.getByRole('tab')
119
+
120
+ expect(tab).not.toHaveAttribute('tabindex')
121
+ })
122
+
123
+ it('should call onClick when clicked', async () => {
124
+ const onClick = vi.fn()
125
+ const index = 2
126
+
127
+ render(
128
+ <Tab id="foo" index={index} controls="foo-panel" onClick={onClick}>
129
+ Tab Label
130
+ </Tab>
131
+ )
132
+ const tab = screen.getByRole('tab')
133
+
134
+ await userEvent.click(tab)
135
+
136
+ await waitFor(() => {
137
+ expect(onClick).toHaveBeenCalled()
138
+
139
+ const args = onClick.mock.calls[0][1]
140
+ expect(args).toHaveProperty('index', index)
141
+ })
142
+ })
143
+
144
+ it('should NOT call onClick when clicked and tab is disabled', async () => {
145
+ const onClick = vi.fn()
146
+
147
+ render(
148
+ <Tab id="foo" index={0} controls="foo-panel" onClick={onClick} isDisabled>
149
+ Tab Label
150
+ </Tab>
151
+ )
152
+ const tab = screen.getByRole('tab')
153
+
154
+ await userEvent.click(tab)
155
+
156
+ await waitFor(() => {
157
+ expect(onClick).not.toHaveBeenCalled()
158
+ })
159
+ })
160
+
161
+ it('should call onKeyDown when keys are pressed and tab is selected', async () => {
162
+ const onKeyDown = vi.fn()
163
+ const index = 2
164
+
165
+ render(
166
+ <Tab
167
+ id="foo"
168
+ isSelected
169
+ index={index}
170
+ controls="foo-panel"
171
+ onKeyDown={onKeyDown}
172
+ >
173
+ Tab Label
174
+ </Tab>
175
+ )
176
+ const tab = screen.getByRole('tab')
177
+
178
+ await userEvent.type(tab, '{enter}')
179
+
180
+ await waitFor(() => {
181
+ expect(onKeyDown).toHaveBeenCalled()
182
+
183
+ const args = onKeyDown.mock.calls[0][1]
184
+ expect(args).toHaveProperty('index', index)
185
+ })
186
+ })
187
+
188
+ it('should NOT call onKeyDown when keys are pressed and tab is disabled', async () => {
189
+ const onKeyDown = vi.fn()
190
+
191
+ render(
192
+ <Tab
193
+ id="foo"
194
+ index={0}
195
+ controls="foo-panel"
196
+ onKeyDown={onKeyDown}
197
+ isDisabled
198
+ >
199
+ Tab Label
200
+ </Tab>
201
+ )
202
+ const tab = screen.getByRole('tab')
203
+
204
+ await userEvent.type(tab, '{enter}')
205
+
206
+ await waitFor(() => {
207
+ expect(onKeyDown).not.toHaveBeenCalled()
208
+ })
209
+ })
210
+ })
@@ -23,14 +23,15 @@
23
23
  */
24
24
 
25
25
  import React from 'react'
26
-
27
- import { Tabs } from '../index'
28
- import { fireEvent, render, screen } from '@testing-library/react'
26
+ import { render, screen, waitFor } from '@testing-library/react'
29
27
  import { vi } from 'vitest'
30
28
  import type { MockInstance } from 'vitest'
31
-
29
+ import userEvent from '@testing-library/user-event'
32
30
  import '@testing-library/jest-dom'
33
31
 
32
+ import { runAxeCheck } from '@instructure/ui-axe-check'
33
+ import { Tabs } from '../index'
34
+
34
35
  const TabExample = (props: { onIndexChange: (arg: number) => void }) => {
35
36
  const [selectedIndex, setSelectedIndex] = React.useState(0)
36
37
  return (
@@ -65,17 +66,27 @@ const TabExample = (props: { onIndexChange: (arg: number) => void }) => {
65
66
  }
66
67
 
67
68
  describe('<Tabs />', () => {
69
+ const tab1Content = 'Tab 1 content'
70
+ const tab2Content = 'Tab 2 content'
71
+ const tab3Content = 'Tab 3 content'
72
+
68
73
  let consoleErrorMock: ReturnType<typeof vi.spyOn>
74
+ let consoleWarningMock: ReturnType<typeof vi.spyOn>
69
75
 
70
76
  beforeEach(() => {
71
77
  // Mocking console to prevent test output pollution and expect for messages
72
78
  consoleErrorMock = vi
73
79
  .spyOn(console, 'error')
74
80
  .mockImplementation(() => {}) as MockInstance
81
+
82
+ consoleWarningMock = vi
83
+ .spyOn(console, 'warn')
84
+ .mockImplementation(() => {}) as MockInstance
75
85
  })
76
86
 
77
87
  afterEach(() => {
78
88
  consoleErrorMock.mockRestore()
89
+ consoleWarningMock.mockRestore()
79
90
  })
80
91
 
81
92
  it('should render the correct number of panels', () => {
@@ -88,8 +99,46 @@ describe('<Tabs />', () => {
88
99
  </Tabs.Panel>
89
100
  </Tabs>
90
101
  )
102
+ const panels = container.querySelectorAll('[role="tabpanel"]')
91
103
 
92
- expect(container.firstChild).toBeInTheDocument()
104
+ expect(panels.length).toBe(3)
105
+ })
106
+
107
+ it('should render with null children', async () => {
108
+ const { container } = render(
109
+ <Tabs>
110
+ <Tabs.Panel renderTitle="First Tab">Tab 1 content</Tabs.Panel>
111
+ <Tabs.Panel renderTitle="Second Tab">Tab 2 content</Tabs.Panel>
112
+ <Tabs.Panel renderTitle="Third Tab" isDisabled>
113
+ Tab 3 content
114
+ </Tabs.Panel>
115
+ {null}
116
+ </Tabs>
117
+ )
118
+ const panels = container.querySelectorAll('[role="tabpanel"]')
119
+
120
+ expect(panels.length).toBe(3)
121
+ })
122
+
123
+ it('should be okay with rendering without any children', async () => {
124
+ render(<Tabs></Tabs>)
125
+
126
+ expect(consoleErrorMock).not.toHaveBeenCalled()
127
+ })
128
+
129
+ it('should render correct number of tabs', async () => {
130
+ render(
131
+ <Tabs>
132
+ <Tabs.Panel renderTitle="First Tab">Tab 1 content</Tabs.Panel>
133
+ <Tabs.Panel renderTitle="Second Tab">Tab 2 content</Tabs.Panel>
134
+ <Tabs.Panel renderTitle="Third Tab" isDisabled>
135
+ Tab 3 content
136
+ </Tabs.Panel>
137
+ </Tabs>
138
+ )
139
+ const tabs = screen.getAllByRole('tab')
140
+
141
+ expect(tabs.length).toBe(3)
93
142
  })
94
143
 
95
144
  it('should render same content for other tabs as for the active one', () => {
@@ -115,7 +164,7 @@ describe('<Tabs />', () => {
115
164
  expect(childContent).toBeNull()
116
165
  })
117
166
 
118
- it('should render the same content in second tab when selected', () => {
167
+ it('should render the same content in second tab when selected', async () => {
119
168
  const onIndexChange = vi.fn()
120
169
 
121
170
  const { container } = render(<TabExample onIndexChange={onIndexChange} />)
@@ -123,9 +172,11 @@ describe('<Tabs />', () => {
123
172
 
124
173
  const secondTab = screen.getAllByRole('tab')[1]
125
174
 
126
- fireEvent.click(secondTab)
175
+ await userEvent.click(secondTab)
127
176
 
128
- expect(onIndexChange).toHaveBeenCalledWith(1)
177
+ await waitFor(() => {
178
+ expect(onIndexChange).toHaveBeenCalledWith(1)
179
+ })
129
180
 
130
181
  const panelContent = screen.queryByText('CONTENT')
131
182
 
@@ -153,4 +204,227 @@ describe('<Tabs />', () => {
153
204
  'Warning: [Tabs] Only one Panel can be marked as active.'
154
205
  )
155
206
  })
207
+
208
+ it('should default to selecting the first tab', async () => {
209
+ const { container } = render(
210
+ <Tabs>
211
+ <Tabs.Panel renderTitle="First Tab">{tab1Content}</Tabs.Panel>
212
+ <Tabs.Panel renderTitle="Second Tab">{tab2Content}</Tabs.Panel>
213
+ <Tabs.Panel renderTitle="Third Tab" isDisabled>
214
+ {tab3Content}
215
+ </Tabs.Panel>
216
+ </Tabs>
217
+ )
218
+ const panelsContainer = container.querySelector(
219
+ '[class$="panelsContainer"]'
220
+ )
221
+
222
+ expect(panelsContainer).toHaveTextContent(tab1Content)
223
+ expect(screen.getByText(tab1Content)).toBeVisible()
224
+
225
+ expect(panelsContainer).not.toHaveTextContent(tab2Content)
226
+ expect(panelsContainer).not.toHaveTextContent(tab3Content)
227
+ })
228
+
229
+ it('should honor the isSelected prop', async () => {
230
+ const { container } = render(
231
+ <Tabs>
232
+ <Tabs.Panel renderTitle="First Tab">{tab1Content}</Tabs.Panel>
233
+ <Tabs.Panel renderTitle="Second Tab" isSelected>
234
+ {tab2Content}
235
+ </Tabs.Panel>
236
+ <Tabs.Panel renderTitle="Third Tab" isDisabled>
237
+ {tab3Content}
238
+ </Tabs.Panel>
239
+ </Tabs>
240
+ )
241
+ const panelsContainer = container.querySelector(
242
+ '[class$="panelsContainer"]'
243
+ )
244
+
245
+ expect(panelsContainer).toHaveTextContent(tab2Content)
246
+ expect(screen.getByText(tab2Content)).toBeVisible()
247
+
248
+ expect(panelsContainer).not.toHaveTextContent(tab1Content)
249
+ expect(panelsContainer).not.toHaveTextContent(tab3Content)
250
+ })
251
+
252
+ it('should not allow selecting a disabled tab', async () => {
253
+ const { container } = render(
254
+ <Tabs>
255
+ <Tabs.Panel renderTitle="First Tab">Tab 1 content</Tabs.Panel>
256
+ <Tabs.Panel renderTitle="Second Tab">Tab 2 content</Tabs.Panel>
257
+ <Tabs.Panel renderTitle="Third Tab" isDisabled isSelected>
258
+ Tab 3 content
259
+ </Tabs.Panel>
260
+ </Tabs>
261
+ )
262
+ const panelsContainer = container.querySelector(
263
+ '[class$="panelsContainer"]'
264
+ )
265
+ const panels = container.querySelectorAll('[role="tabpanel"]')
266
+
267
+ expect(panelsContainer).toHaveTextContent(tab1Content)
268
+ expect(screen.getByText(tab1Content)).toBeVisible()
269
+
270
+ expect(panelsContainer).not.toHaveTextContent(tab2Content)
271
+ expect(panelsContainer).not.toHaveTextContent(tab3Content)
272
+
273
+ expect(panels.length).toBe(3)
274
+ expect(panels[0]).not.toHaveAttribute('aria-hidden', 'true')
275
+ expect(panels[1]).toHaveAttribute('aria-hidden', 'true')
276
+ expect(panels[2]).toHaveAttribute('aria-hidden', 'true')
277
+ })
278
+
279
+ it('should call onRequestTabChange when selection changes via click', async () => {
280
+ const onChange = vi.fn()
281
+
282
+ render(
283
+ <Tabs onRequestTabChange={onChange}>
284
+ <Tabs.Panel renderTitle="First Tab" isSelected id="one">
285
+ Tab 1 content
286
+ </Tabs.Panel>
287
+ <Tabs.Panel renderTitle="Second Tab" id="two">
288
+ Tab 2 content
289
+ </Tabs.Panel>
290
+ <Tabs.Panel renderTitle="Third Tab" isDisabled id="three">
291
+ Tab 3 content
292
+ </Tabs.Panel>
293
+ </Tabs>
294
+ )
295
+ const secondTab = screen.getByText('Second Tab')
296
+
297
+ await userEvent.click(secondTab)
298
+
299
+ await waitFor(() => {
300
+ expect(onChange).toHaveBeenCalled()
301
+
302
+ const args = onChange.mock.calls[0][1]
303
+
304
+ expect(args).toHaveProperty('index', 1)
305
+ expect(args).toHaveProperty('id', 'two')
306
+ })
307
+ })
308
+
309
+ it('should focus the selected tab when shouldFocusOnRender is set', async () => {
310
+ render(
311
+ <Tabs shouldFocusOnRender>
312
+ <Tabs.Panel renderTitle="First Tab">Tab 1 content</Tabs.Panel>
313
+ <Tabs.Panel renderTitle="Second Tab" isSelected>
314
+ Tab 2 content
315
+ </Tabs.Panel>
316
+ <Tabs.Panel renderTitle="Third Tab" isDisabled>
317
+ Tab 3 content
318
+ </Tabs.Panel>
319
+ </Tabs>
320
+ )
321
+ const secondTab = screen.getByText('Second Tab')
322
+
323
+ await waitFor(() => {
324
+ expect(document.activeElement).toBe(secondTab)
325
+ })
326
+ })
327
+
328
+ it('should not call onRequestTabChange when clicking a disabled tab', async () => {
329
+ const onChange = vi.fn()
330
+ render(
331
+ <Tabs onRequestTabChange={onChange}>
332
+ <Tabs.Panel renderTitle="First Tab">Tab 1 content</Tabs.Panel>
333
+ <Tabs.Panel renderTitle="Second Tab">Tab 2 content</Tabs.Panel>
334
+ <Tabs.Panel renderTitle="Third Tab" isDisabled>
335
+ Tab 3 content
336
+ </Tabs.Panel>
337
+ </Tabs>
338
+ )
339
+ const thirdTab = screen.getByText('Third Tab')
340
+
341
+ await userEvent.click(thirdTab)
342
+
343
+ await waitFor(() => {
344
+ expect(onChange).not.toHaveBeenCalled()
345
+ })
346
+ })
347
+
348
+ it('should meet a11y standards when set to the secondary variant', async () => {
349
+ const { container } = render(
350
+ <Tabs variant="secondary">
351
+ <Tabs.Panel renderTitle="First Tab">Tab 1 content</Tabs.Panel>
352
+ <Tabs.Panel renderTitle="Second Tab">Tab 2 content</Tabs.Panel>
353
+ <Tabs.Panel renderTitle="Third Tab" isDisabled>
354
+ Tab 3 content
355
+ </Tabs.Panel>
356
+ </Tabs>
357
+ )
358
+ const axeCheck = await runAxeCheck(container)
359
+
360
+ expect(axeCheck).toBe(true)
361
+ })
362
+
363
+ it('should meet a11y standards when set to the default variant', async () => {
364
+ const { container } = render(
365
+ <Tabs variant="default">
366
+ <Tabs.Panel renderTitle="First Tab">Tab 1 content</Tabs.Panel>
367
+ <Tabs.Panel renderTitle="Second Tab">Tab 2 content</Tabs.Panel>
368
+ <Tabs.Panel renderTitle="Third Tab" isDisabled>
369
+ Tab 3 content
370
+ </Tabs.Panel>
371
+ </Tabs>
372
+ )
373
+ const axeCheck = await runAxeCheck(container)
374
+
375
+ expect(axeCheck).toBe(true)
376
+ })
377
+
378
+ it('should link tabs with the corresponding panels via ids', async () => {
379
+ const { container } = render(
380
+ <Tabs>
381
+ <Tabs.Panel renderTitle="First Tab">Tab 1 content</Tabs.Panel>
382
+ <Tabs.Panel renderTitle="Second Tab">Tab 2 content</Tabs.Panel>
383
+ <Tabs.Panel renderTitle="Third Tab" isDisabled>
384
+ Tab 3 content
385
+ </Tabs.Panel>
386
+ </Tabs>
387
+ )
388
+ const firstTab = screen.getByText('First Tab')
389
+ const firstPanel = container.querySelectorAll('[role="tabpanel"]')[0]
390
+
391
+ expect(firstTab).toHaveAttribute('aria-controls', firstPanel.id)
392
+ expect(firstPanel).toHaveAttribute('aria-labelledby', firstTab.id)
393
+ })
394
+
395
+ describe('with duplicate-named tabs', () => {
396
+ it('should still render the correct number of panels', async () => {
397
+ const { container } = render(
398
+ <Tabs>
399
+ <Tabs.Panel renderTitle="A Tab">Contents of first tab.</Tabs.Panel>
400
+ <Tabs.Panel renderTitle="A Tab">Contents of second tab.</Tabs.Panel>
401
+ <Tabs.Panel renderTitle="A Tab" isDisabled>
402
+ Contents of third tab.
403
+ </Tabs.Panel>
404
+ </Tabs>
405
+ )
406
+ const tabPanels = container.querySelectorAll('[role="tabpanel"]')
407
+
408
+ expect(tabPanels.length).toBe(3)
409
+ })
410
+ })
411
+
412
+ describe('with nodes as tab titles', () => {
413
+ it('should still render the correct number of panels', async () => {
414
+ const { container } = render(
415
+ <Tabs>
416
+ <Tabs.Panel renderTitle={<div />}>Contents of first tab.</Tabs.Panel>
417
+ <Tabs.Panel renderTitle={<span />}>
418
+ Contents of second tab.
419
+ </Tabs.Panel>
420
+ <Tabs.Panel renderTitle={<img alt="example" />} isDisabled>
421
+ Contents of third tab.
422
+ </Tabs.Panel>
423
+ </Tabs>
424
+ )
425
+ const tabPanels = container.querySelectorAll('[role="tabpanel"]')
426
+
427
+ expect(tabPanels.length).toBe(3)
428
+ })
429
+ })
156
430
  })
@@ -9,13 +9,13 @@
9
9
  "references": [
10
10
  { "path": "../ui-babel-preset/tsconfig.build.json" },
11
11
  { "path": "../ui-color-utils/tsconfig.build.json" },
12
- { "path": "../ui-test-locator/tsconfig.build.json" },
13
12
  { "path": "../ui-test-utils/tsconfig.build.json" },
14
13
  { "path": "../ui-themes/tsconfig.build.json" },
15
14
  { "path": "../console/tsconfig.build.json" },
16
15
  { "path": "../debounce/tsconfig.build.json" },
17
16
  { "path": "../emotion/tsconfig.build.json" },
18
17
  { "path": "../shared-types/tsconfig.build.json" },
18
+ { "path": "../ui-axe-check/tsconfig.build.json" },
19
19
  { "path": "../ui-dom-utils/tsconfig.build.json" },
20
20
  { "path": "../ui-focusable/tsconfig.build.json" },
21
21
  { "path": "../ui-i18n/tsconfig.build.json" },