-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathindex.test.tsx
More file actions
200 lines (166 loc) · 6.16 KB
/
index.test.tsx
File metadata and controls
200 lines (166 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import {render, screen} from '@testing-library/react'
import {ChakraProvider} from '@chakra-ui/react'
import {SkipNavLink, SkipNavContent} from './index'
import system from '../../theme'
const TestWrapper = ({children}: {children: React.ReactNode}) => (
<ChakraProvider value={system}>{children}</ChakraProvider>
)
describe('SkipNavLink', () => {
test('renders with default props', () => {
render(
<TestWrapper>
<SkipNavLink>Skip to Content</SkipNavLink>
</TestWrapper>
)
const link = screen.getByRole('link', {name: 'Skip to Content'})
expect(link).toBeInTheDocument()
expect(link).toHaveAttribute('href', '#skip-to-content')
})
test('renders with custom href', () => {
render(
<TestWrapper>
<SkipNavLink href="#main-content">Skip to Main</SkipNavLink>
</TestWrapper>
)
const link = screen.getByRole('link', {name: 'Skip to Main'})
expect(link).toHaveAttribute('href', '#main-content')
})
test('has correct accessibility attributes', () => {
render(
<TestWrapper>
<SkipNavLink>Skip to Content</SkipNavLink>
</TestWrapper>
)
const link = screen.getByRole('link', {name: 'Skip to Content'})
expect(link).toHaveAttribute('href', '#skip-to-content')
})
test('has visually hidden styles by default', () => {
render(
<TestWrapper>
<SkipNavLink>Skip to Content</SkipNavLink>
</TestWrapper>
)
const link = screen.getByRole('link', {name: 'Skip to Content'})
// The link should be focusable (no aria-hidden) but positioned off-screen
expect(link).not.toHaveAttribute('aria-hidden')
expect(link).toHaveAttribute('href', '#skip-to-content')
})
test('accepts custom zIndex prop', () => {
render(
<TestWrapper>
<SkipNavLink zIndex={9999}>Skip to Content</SkipNavLink>
</TestWrapper>
)
const link = screen.getByRole('link', {name: 'Skip to Content'})
expect(link).toBeInTheDocument()
// Note: Testing the actual zIndex value may require more complex setup
// as it depends on the Chakra theme system
})
})
describe('SkipNavContent', () => {
test('renders with default props', () => {
render(
<TestWrapper>
<SkipNavContent>
<div>Main content</div>
</SkipNavContent>
</TestWrapper>
)
const content = screen.getByText('Main content')
const container = content.parentElement
expect(container).toHaveAttribute('id', 'skip-to-content')
expect(container).toHaveAttribute('tabIndex', '-1')
})
test('renders with custom id', () => {
render(
<TestWrapper>
<SkipNavContent id="main-content">
<div>Main content</div>
</SkipNavContent>
</TestWrapper>
)
const content = screen.getByText('Main content')
const container = content.parentElement
expect(container).toHaveAttribute('id', 'main-content')
})
test('applies custom styles', () => {
const customStyles = {
backgroundColor: 'red',
padding: '10px'
}
render(
<TestWrapper>
<SkipNavContent style={customStyles}>
<div>Main content</div>
</SkipNavContent>
</TestWrapper>
)
const content = screen.getByText('Main content')
const container = content.parentElement
expect(container).toHaveStyle('background-color: red')
expect(container).toHaveStyle('padding: 10px')
})
test('renders children correctly', () => {
render(
<TestWrapper>
<SkipNavContent>
<h1>Main Heading</h1>
<p>Some content</p>
</SkipNavContent>
</TestWrapper>
)
expect(screen.getByText('Main Heading')).toBeInTheDocument()
expect(screen.getByText('Some content')).toBeInTheDocument()
})
test('has correct tabIndex for focus management', () => {
render(
<TestWrapper>
<SkipNavContent>
<div>Main content</div>
</SkipNavContent>
</TestWrapper>
)
const content = screen.getByText('Main content')
const container = content.parentElement
expect(container).toHaveAttribute('tabIndex', '-1')
})
})
describe('SkipNav Integration', () => {
test('link and content work together', () => {
render(
<TestWrapper>
<SkipNavLink href="#main-content">Skip to Content</SkipNavLink>
<SkipNavContent id="main-content">
<div>Main content</div>
</SkipNavContent>
</TestWrapper>
)
const link = screen.getByRole('link', {name: 'Skip to Content'})
const content = screen.getByText('Main content')
const container = content.parentElement
expect(link).toHaveAttribute('href', '#main-content')
expect(container).toHaveAttribute('id', 'main-content')
})
test('uses default ids when not specified', () => {
render(
<TestWrapper>
<SkipNavLink>Skip to Content</SkipNavLink>
<SkipNavContent>
<div>Main content</div>
</SkipNavContent>
</TestWrapper>
)
const link = screen.getByRole('link', {name: 'Skip to Content'})
const content = screen.getByText('Main content')
const container = content.parentElement
expect(link).toHaveAttribute('href', '#skip-to-content')
expect(container).toHaveAttribute('id', 'skip-to-content')
})
})