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

Commit 1a19a07

Browse files
test(skip-nav): add keyboard testing
1 parent eb30f0f commit 1a19a07

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

packages/chakra-ui-core/src/CSkipNav/CSkipNav.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ const CSkipNavContent = {
111111
tabIndex: '-1',
112112
style: {
113113
outline: 0
114-
}
114+
},
115+
'data-testid': 'chakra-skip-nav'
115116
}
116117
},
117118
this.$slots.default
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { CSkipNavLink, CSkipNavContent } from '../CSkipNav'
2+
import { fireEvent, render, userEvent, screen, wait } from '@/tests/test-utils'
3+
4+
const renderComponent = (props) => {
5+
const base = {
6+
components: { CSkipNavLink, CSkipNavContent },
7+
template: `
8+
<div>
9+
<CSkipNavLink>Skip to Content</CSkipNavLink>
10+
<CSkipNavContent>
11+
<main>
12+
<form>
13+
<input type="text" placeholder="Search" />
14+
</form>
15+
</main>
16+
</CSkipNavContent>
17+
</div>
18+
`,
19+
...props
20+
}
21+
return render(base)
22+
}
23+
24+
const getSkipLink = () => screen.getByText('Skip to Content')
25+
26+
const getContentWrapper = () => screen.getByTestId('chakra-skip-nav')
27+
28+
const triggerSkipLink = async () => {
29+
const link = getSkipLink()
30+
await fireEvent.keyDown(link, {
31+
key: 'Enter',
32+
code: 'Enter'
33+
})
34+
}
35+
36+
describe('CSkipNav', () => {
37+
beforeEach(async () => {
38+
renderComponent()
39+
await userEvent.tab()
40+
})
41+
42+
it('should be tabbed to link after initial render', () => {
43+
const link = getSkipLink()
44+
expect(link).toHaveAttribute('href', '#chakra-skip-nav')
45+
})
46+
47+
it('should navigate to content wrapper on selecting skip link', async () => {
48+
await triggerSkipLink()
49+
const contentWrapper = getContentWrapper()
50+
51+
wait(() => {
52+
expect(contentWrapper).toHaveFocus()
53+
})
54+
})
55+
56+
it('should tab to input after wrapper focus', async () => {
57+
await triggerSkipLink()
58+
await userEvent.tab()
59+
60+
const input = screen.getByPlaceholderText('Search')
61+
62+
expect(input).toHaveFocus()
63+
})
64+
})

0 commit comments

Comments
 (0)