|
10 | 10 | * Red Hat, Inc. - initial API and implementation |
11 | 11 | */ |
12 | 12 |
|
| 13 | +import { fireEvent } from '@testing-library/react'; |
13 | 14 | import userEvent from '@testing-library/user-event'; |
14 | 15 | import React from 'react'; |
15 | 16 | import { Provider } from 'react-redux'; |
@@ -141,4 +142,122 @@ describe('UserPreferences', () => { |
141 | 142 | expect(screen.queryByRole('tabpanel', { name: 'SSH Keys' })).toBeTruthy(); |
142 | 143 | }); |
143 | 144 | }); |
| 145 | + |
| 146 | + describe('Keyboard navigation', () => { |
| 147 | + it('should move to the next tab on ArrowRight', () => { |
| 148 | + const location = buildUserPreferencesLocation(UserPreferencesTab.CONTAINER_REGISTRIES); |
| 149 | + renderComponent(location); |
| 150 | + |
| 151 | + const tab = screen.getByRole('tab', { name: 'Container Registries' }); |
| 152 | + fireEvent.keyDown(tab, { key: 'ArrowRight' }); |
| 153 | + |
| 154 | + expect(mockNavigate).toHaveBeenCalledWith( |
| 155 | + expect.stringContaining(`tab=${UserPreferencesTab.GIT_SERVICES}`), |
| 156 | + ); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should move to the next tab on ArrowDown', () => { |
| 160 | + const location = buildUserPreferencesLocation(UserPreferencesTab.CONTAINER_REGISTRIES); |
| 161 | + renderComponent(location); |
| 162 | + |
| 163 | + const tab = screen.getByRole('tab', { name: 'Container Registries' }); |
| 164 | + fireEvent.keyDown(tab, { key: 'ArrowDown' }); |
| 165 | + |
| 166 | + expect(mockNavigate).toHaveBeenCalledWith( |
| 167 | + expect.stringContaining(`tab=${UserPreferencesTab.GIT_SERVICES}`), |
| 168 | + ); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should move to the previous tab on ArrowLeft', () => { |
| 172 | + const location = buildUserPreferencesLocation(UserPreferencesTab.GIT_SERVICES); |
| 173 | + renderComponent(location); |
| 174 | + |
| 175 | + const tab = screen.getByRole('tab', { name: 'Git Services' }); |
| 176 | + fireEvent.keyDown(tab, { key: 'ArrowLeft' }); |
| 177 | + |
| 178 | + expect(mockNavigate).toHaveBeenCalledWith( |
| 179 | + expect.stringContaining(`tab=${UserPreferencesTab.CONTAINER_REGISTRIES}`), |
| 180 | + ); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should move to the previous tab on ArrowUp', () => { |
| 184 | + const location = buildUserPreferencesLocation(UserPreferencesTab.GIT_SERVICES); |
| 185 | + renderComponent(location); |
| 186 | + |
| 187 | + const tab = screen.getByRole('tab', { name: 'Git Services' }); |
| 188 | + fireEvent.keyDown(tab, { key: 'ArrowUp' }); |
| 189 | + |
| 190 | + expect(mockNavigate).toHaveBeenCalledWith( |
| 191 | + expect.stringContaining(`tab=${UserPreferencesTab.CONTAINER_REGISTRIES}`), |
| 192 | + ); |
| 193 | + }); |
| 194 | + |
| 195 | + it('should wrap around to the first tab on ArrowRight from the last tab', () => { |
| 196 | + const location = buildUserPreferencesLocation(UserPreferencesTab.SSH_KEYS); |
| 197 | + renderComponent(location); |
| 198 | + |
| 199 | + const tab = screen.getByRole('tab', { name: 'SSH Keys' }); |
| 200 | + fireEvent.keyDown(tab, { key: 'ArrowRight' }); |
| 201 | + |
| 202 | + expect(mockNavigate).toHaveBeenCalledWith( |
| 203 | + expect.stringContaining(`tab=${UserPreferencesTab.CONTAINER_REGISTRIES}`), |
| 204 | + ); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should wrap around to the last tab on ArrowLeft from the first tab', () => { |
| 208 | + const location = buildUserPreferencesLocation(UserPreferencesTab.CONTAINER_REGISTRIES); |
| 209 | + renderComponent(location); |
| 210 | + |
| 211 | + const tab = screen.getByRole('tab', { name: 'Container Registries' }); |
| 212 | + fireEvent.keyDown(tab, { key: 'ArrowLeft' }); |
| 213 | + |
| 214 | + expect(mockNavigate).toHaveBeenCalledWith( |
| 215 | + expect.stringContaining(`tab=${UserPreferencesTab.SSH_KEYS}`), |
| 216 | + ); |
| 217 | + }); |
| 218 | + |
| 219 | + it('should move to the first tab on Home', () => { |
| 220 | + const location = buildUserPreferencesLocation(UserPreferencesTab.GITCONFIG); |
| 221 | + renderComponent(location); |
| 222 | + |
| 223 | + const tab = screen.getByRole('tab', { name: 'Gitconfig' }); |
| 224 | + fireEvent.keyDown(tab, { key: 'Home' }); |
| 225 | + |
| 226 | + expect(mockNavigate).toHaveBeenCalledWith( |
| 227 | + expect.stringContaining(`tab=${UserPreferencesTab.CONTAINER_REGISTRIES}`), |
| 228 | + ); |
| 229 | + }); |
| 230 | + |
| 231 | + it('should move to the last tab on End', () => { |
| 232 | + const location = buildUserPreferencesLocation(UserPreferencesTab.CONTAINER_REGISTRIES); |
| 233 | + renderComponent(location); |
| 234 | + |
| 235 | + const tab = screen.getByRole('tab', { name: 'Container Registries' }); |
| 236 | + fireEvent.keyDown(tab, { key: 'End' }); |
| 237 | + |
| 238 | + expect(mockNavigate).toHaveBeenCalledWith( |
| 239 | + expect.stringContaining(`tab=${UserPreferencesTab.SSH_KEYS}`), |
| 240 | + ); |
| 241 | + }); |
| 242 | + |
| 243 | + it('should not navigate on unhandled keys', () => { |
| 244 | + const location = buildUserPreferencesLocation(UserPreferencesTab.CONTAINER_REGISTRIES); |
| 245 | + renderComponent(location); |
| 246 | + |
| 247 | + const tab = screen.getByRole('tab', { name: 'Container Registries' }); |
| 248 | + fireEvent.keyDown(tab, { key: 'Enter' }); |
| 249 | + |
| 250 | + expect(mockNavigate).not.toHaveBeenCalled(); |
| 251 | + }); |
| 252 | + |
| 253 | + it('should not navigate when keydown target is not a tab', () => { |
| 254 | + const location = buildUserPreferencesLocation(UserPreferencesTab.CONTAINER_REGISTRIES); |
| 255 | + renderComponent(location); |
| 256 | + |
| 257 | + const tabPanel = screen.getByRole('tabpanel', { name: 'Container Registries' }); |
| 258 | + fireEvent.keyDown(tabPanel, { key: 'ArrowRight' }); |
| 259 | + |
| 260 | + expect(mockNavigate).not.toHaveBeenCalled(); |
| 261 | + }); |
| 262 | + }); |
144 | 263 | }); |
0 commit comments