|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import React from 'react' |
| 9 | +import {screen} from '@testing-library/react' |
| 10 | +import {SkipNavLink, SkipNavContent} from './index' |
| 11 | +import {renderWithProviders} from '../../utils/test-utils' |
| 12 | + |
| 13 | +describe('SkipNavLink', () => { |
| 14 | + test('renders with default props', () => { |
| 15 | + renderWithProviders(<SkipNavLink>Skip to Content</SkipNavLink>, {}) |
| 16 | + |
| 17 | + const link = screen.getByRole('link', {name: 'Skip to Content'}) |
| 18 | + expect(link).toBeInTheDocument() |
| 19 | + expect(link).toHaveAttribute('href', '#skip-to-content') |
| 20 | + }) |
| 21 | + |
| 22 | + test('renders with custom href', () => { |
| 23 | + renderWithProviders(<SkipNavLink href="#main-content">Skip to Main</SkipNavLink>, {}) |
| 24 | + |
| 25 | + const link = screen.getByRole('link', {name: 'Skip to Main'}) |
| 26 | + expect(link).toHaveAttribute('href', '#main-content') |
| 27 | + }) |
| 28 | + |
| 29 | + test('has correct accessibility attributes', () => { |
| 30 | + renderWithProviders(<SkipNavLink>Skip to Content</SkipNavLink>, {}) |
| 31 | + |
| 32 | + const link = screen.getByRole('link', {name: 'Skip to Content'}) |
| 33 | + expect(link).toHaveAttribute('href', '#skip-to-content') |
| 34 | + }) |
| 35 | + |
| 36 | + test('has visually hidden styles by default', () => { |
| 37 | + renderWithProviders(<SkipNavLink>Skip to Content</SkipNavLink>, {}) |
| 38 | + |
| 39 | + const link = screen.getByRole('link', {name: 'Skip to Content'}) |
| 40 | + |
| 41 | + // The link should be focusable (no aria-hidden) but positioned off-screen |
| 42 | + expect(link).not.toHaveAttribute('aria-hidden') |
| 43 | + expect(link).toHaveAttribute('href', '#skip-to-content') |
| 44 | + }) |
| 45 | + |
| 46 | + test('uses theme styles', () => { |
| 47 | + renderWithProviders(<SkipNavLink>Skip to Content</SkipNavLink>, {}) |
| 48 | + |
| 49 | + const link = screen.getByRole('link', {name: 'Skip to Content'}) |
| 50 | + expect(link).toBeInTheDocument() |
| 51 | + // The component should use theme styles from the skipNav slot recipe |
| 52 | + expect(link).toHaveAttribute('href', '#skip-to-content') |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +describe('SkipNavContent', () => { |
| 57 | + test('renders with default props', () => { |
| 58 | + renderWithProviders( |
| 59 | + <SkipNavContent> |
| 60 | + <div>Main content</div> |
| 61 | + </SkipNavContent>, |
| 62 | + {} |
| 63 | + ) |
| 64 | + |
| 65 | + const content = screen.getByText('Main content') |
| 66 | + const container = content.parentElement |
| 67 | + |
| 68 | + expect(container).toHaveAttribute('id', 'skip-to-content') |
| 69 | + expect(container).toHaveAttribute('tabIndex', '-1') |
| 70 | + }) |
| 71 | + |
| 72 | + test('renders with custom id', () => { |
| 73 | + renderWithProviders( |
| 74 | + <SkipNavContent id="main-content"> |
| 75 | + <div>Main content</div> |
| 76 | + </SkipNavContent>, |
| 77 | + {} |
| 78 | + ) |
| 79 | + |
| 80 | + const content = screen.getByText('Main content') |
| 81 | + const container = content.parentElement |
| 82 | + |
| 83 | + expect(container).toHaveAttribute('id', 'main-content') |
| 84 | + expect(container).toHaveAttribute('tabIndex', '-1') |
| 85 | + }) |
| 86 | + |
| 87 | + test('has correct accessibility attributes', () => { |
| 88 | + renderWithProviders( |
| 89 | + <SkipNavContent> |
| 90 | + <div>Main content</div> |
| 91 | + </SkipNavContent>, |
| 92 | + {} |
| 93 | + ) |
| 94 | + |
| 95 | + const content = screen.getByText('Main content') |
| 96 | + const container = content.parentElement |
| 97 | + |
| 98 | + expect(container).toHaveAttribute('id', 'skip-to-content') |
| 99 | + expect(container).toHaveAttribute('tabIndex', '-1') |
| 100 | + }) |
| 101 | + |
| 102 | + test('uses theme styles', () => { |
| 103 | + renderWithProviders( |
| 104 | + <SkipNavContent> |
| 105 | + <div>Content with theme</div> |
| 106 | + </SkipNavContent>, |
| 107 | + {} |
| 108 | + ) |
| 109 | + |
| 110 | + const content = screen.getByText('Content with theme') |
| 111 | + const container = content.parentElement |
| 112 | + |
| 113 | + expect(container).toBeInTheDocument() |
| 114 | + // The component should use theme styles from the skipNav slot recipe |
| 115 | + expect(container).toHaveAttribute('id', 'skip-to-content') |
| 116 | + }) |
| 117 | +}) |
| 118 | + |
| 119 | +describe('SkipNavLink and SkipNavContent integration', () => { |
| 120 | + test('link href matches content id', () => { |
| 121 | + renderWithProviders( |
| 122 | + <div> |
| 123 | + <SkipNavLink>Skip to Content</SkipNavLink> |
| 124 | + <SkipNavContent> |
| 125 | + <div>Main content</div> |
| 126 | + </SkipNavContent> |
| 127 | + </div>, |
| 128 | + {} |
| 129 | + ) |
| 130 | + |
| 131 | + const link = screen.getByRole('link', {name: 'Skip to Content'}) |
| 132 | + const content = screen.getByText('Main content') |
| 133 | + const container = content.parentElement |
| 134 | + |
| 135 | + expect(link).toHaveAttribute('href', '#skip-to-content') |
| 136 | + expect(container).toHaveAttribute('id', 'skip-to-content') |
| 137 | + }) |
| 138 | + |
| 139 | + test('custom href and id work together', () => { |
| 140 | + renderWithProviders( |
| 141 | + <div> |
| 142 | + <SkipNavLink href="#main">Skip to Main</SkipNavLink> |
| 143 | + <SkipNavContent id="main"> |
| 144 | + <div>Main content</div> |
| 145 | + </SkipNavContent> |
| 146 | + </div>, |
| 147 | + {} |
| 148 | + ) |
| 149 | + |
| 150 | + const link = screen.getByRole('link', {name: 'Skip to Main'}) |
| 151 | + const content = screen.getByText('Main content') |
| 152 | + const container = content.parentElement |
| 153 | + |
| 154 | + expect(link).toHaveAttribute('href', '#main') |
| 155 | + expect(container).toHaveAttribute('id', 'main') |
| 156 | + }) |
| 157 | + |
| 158 | + test('both components use skip-nav theme styles', () => { |
| 159 | + renderWithProviders( |
| 160 | + <div> |
| 161 | + <SkipNavLink>Skip to Content</SkipNavLink> |
| 162 | + <SkipNavContent> |
| 163 | + <div>Main content</div> |
| 164 | + </SkipNavContent> |
| 165 | + </div>, |
| 166 | + {} |
| 167 | + ) |
| 168 | + |
| 169 | + const link = screen.getByRole('link', {name: 'Skip to Content'}) |
| 170 | + const content = screen.getByText('Main content') |
| 171 | + const container = content.parentElement |
| 172 | + |
| 173 | + // Both components should be rendered and use theme styles |
| 174 | + expect(link).toBeInTheDocument() |
| 175 | + expect(container).toBeInTheDocument() |
| 176 | + expect(link).toHaveAttribute('href', '#skip-to-content') |
| 177 | + expect(container).toHaveAttribute('id', 'skip-to-content') |
| 178 | + }) |
| 179 | +}) |
0 commit comments