|
| 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