Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 97807ad

Browse files
committed
test(accordion): add shift+tab tests for accordion
1 parent 5bcc4d5 commit 97807ad

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

packages/chakra-ui-core/src/CAccordion/tests/CAccordion.test.js

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ it('has the proper aria attributes', () => {
144144
expect(panel).toHaveAttribute('aria-labelledby')
145145
})
146146

147-
// test that tab moves focus to the next focusable element
148147
it('tab moves focus to the next focusable element', async () => {
149148
renderComponent({
150149
template: `
@@ -178,7 +177,43 @@ it('tab moves focus to the next focusable element', async () => {
178177
expect(last).toHaveFocus()
179178
})
180179

181-
// test that aria-contols for button is same as id for panel
180+
181+
it('shift+tab moves focus to the previous focusable element', async () => {
182+
renderComponent({
183+
template: `
184+
<CAccordion>
185+
<CAccordionItem>
186+
<CAccordionHeader>First section</CAccordionHeader>
187+
<CAccordionPanel>Panel 1</CAccordionPanel>
188+
</CAccordionItem>
189+
<CAccordionItem>
190+
<CAccordionHeader>Second section</CAccordionHeader>
191+
<CAccordionPanel>Panel 2</CAccordionPanel>
192+
</CAccordionItem>
193+
<CAccordionItem>
194+
<CAccordionHeader>Last section</CAccordionHeader>
195+
<CAccordionPanel>Panel 3</CAccordionPanel>
196+
</CAccordionItem>
197+
</CAccordion>`
198+
})
199+
200+
const first = screen.getByText('First section')
201+
const second = screen.getByText('Second section')
202+
const last = screen.getByText('Last section')
203+
204+
await userEvent.tab()
205+
expect(first).toHaveFocus()
206+
207+
await userEvent.tab()
208+
expect(second).toHaveFocus()
209+
210+
await userEvent.tab()
211+
expect(last).toHaveFocus()
212+
213+
await userEvent.tab({shift: true}) // shift+tab
214+
expect(second).toHaveFocus()
215+
})
216+
182217
it('aria-contols for button is same as id for panel', () => {
183218
renderComponent({
184219
template: `
@@ -195,7 +230,6 @@ it('aria-contols for button is same as id for panel', () => {
195230
expect(button.getAttribute('aria-controls')).toEqual(panel.getAttribute('id'))
196231
})
197232

198-
// test that aria-expanded is true/false when accordion is open/closed
199233
it('aria-expanded is true/false when accordion is open/closed', () => {
200234
renderComponent({
201235
template: `
@@ -215,7 +249,6 @@ it('aria-expanded is true/false when accordion is open/closed', () => {
215249
expect(button).toHaveAttribute('aria-expanded', 'true')
216250
})
217251

218-
// test that panel has role=region and aria-labelledby
219252
it('panel has role=region and aria-labelledby', () => {
220253
renderComponent({
221254
template: `
@@ -230,30 +263,4 @@ it('panel has role=region and aria-labelledby', () => {
230263

231264
expect(panel).toHaveAttribute('aria-labelledby')
232265
expect(panel).toHaveAttribute('role', 'region')
233-
})
234-
235-
// eslint-disable-next-line no-undef
236-
xit('arrow up & down moves focus to next/previous accordion', async () => {
237-
renderComponent({
238-
template: `
239-
<CAccordion>
240-
<CAccordionItem>
241-
<CAccordionHeader>Section 1 title</CAccordionHeader>
242-
<CAccordionPanel>Panel 1</CAccordionPanel>
243-
</CAccordionItem>
244-
<CAccordionItem>
245-
<CAccordionHeader>Section 2 title</CAccordionHeader>
246-
<CAccordionPanel>Panel 2</CAccordionPanel>
247-
</CAccordionItem>
248-
</CAccordion>`
249-
})
250-
251-
const first = screen.getByText('Section 1 title')
252-
const second = screen.getByText('Section 2 title')
253-
254-
await fireEvent.keyDown(first, { key: 'ArrowDown', keyCode: 40 })
255-
expect(second).toHaveFocus()
256-
257-
await fireEvent.keyDown(second, { key: 'ArrowUp', keyCode: 38 })
258-
expect(first).toHaveFocus()
259-
})
266+
})

0 commit comments

Comments
 (0)