|
1 | | -import { screen } from '@testing-library/react'; |
| 1 | +import { fireEvent, screen } from '@testing-library/react'; |
2 | 2 | import userEvent from '@testing-library/user-event'; |
3 | 3 | import { CardHorizontal, CardHorizontalProps } from '@/components/CardHorizontal'; |
4 | 4 | import { renderCUI } from '@/utils/test-utils'; |
@@ -290,4 +290,212 @@ describe('CardHorizontal Component', () => { |
290 | 290 | expect(warnSpy).toHaveBeenCalledTimes(1); |
291 | 291 | expect(warnSpy.mock.calls[0][0]).toContain('javascript:alert(1)'); |
292 | 292 | }); |
| 293 | + |
| 294 | + describe('keyboard', () => { |
| 295 | + it('should call onClick when Enter is pressed on the focused card', async () => { |
| 296 | + const onClick = vitest.fn(); |
| 297 | + const { container } = renderCard({ title: 'Test Card', onClick }); |
| 298 | + |
| 299 | + (container.firstChild as HTMLElement).focus(); |
| 300 | + await userEvent.keyboard('{Enter}'); |
| 301 | + |
| 302 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 303 | + }); |
| 304 | + |
| 305 | + it('should call onClick when Space is pressed on the focused card', async () => { |
| 306 | + const onClick = vitest.fn(); |
| 307 | + const { container } = renderCard({ title: 'Test Card', onClick }); |
| 308 | + |
| 309 | + (container.firstChild as HTMLElement).focus(); |
| 310 | + await userEvent.keyboard(' '); |
| 311 | + |
| 312 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 313 | + }); |
| 314 | + |
| 315 | + it('should prevent the default Space action so the page does not scroll', () => { |
| 316 | + const { container } = renderCard({ title: 'Test Card', onClick: vitest.fn() }); |
| 317 | + |
| 318 | + const notPrevented = fireEvent.keyDown(container.firstChild as HTMLElement, { |
| 319 | + key: ' ', |
| 320 | + }); |
| 321 | + |
| 322 | + expect(notPrevented).toBe(false); |
| 323 | + }); |
| 324 | + |
| 325 | + it.each([ |
| 326 | + ['Enter', 'Enter'], |
| 327 | + ['Space', ' '], |
| 328 | + ])('should activate only once while %s is held down', (_name, key) => { |
| 329 | + const onClick = vitest.fn(); |
| 330 | + const { container } = renderCard({ title: 'Test Card', onClick }); |
| 331 | + |
| 332 | + const wrapper = container.firstChild as HTMLElement; |
| 333 | + fireEvent.keyDown(wrapper, { key }); |
| 334 | + fireEvent.keyDown(wrapper, { key, repeat: true }); |
| 335 | + fireEvent.keyDown(wrapper, { key, repeat: true }); |
| 336 | + |
| 337 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 338 | + }); |
| 339 | + |
| 340 | + it('should keep preventing the default Space action while Space is held down', () => { |
| 341 | + const { container } = renderCard({ title: 'Test Card', onClick: vitest.fn() }); |
| 342 | + |
| 343 | + const notPrevented = fireEvent.keyDown(container.firstChild as HTMLElement, { |
| 344 | + key: ' ', |
| 345 | + repeat: true, |
| 346 | + }); |
| 347 | + |
| 348 | + expect(notPrevented).toBe(false); |
| 349 | + }); |
| 350 | + |
| 351 | + it('should keep the default Space action on a display-only card', () => { |
| 352 | + const { container } = renderCard({ title: 'Summary', isSelectable: false }); |
| 353 | + |
| 354 | + const notPrevented = fireEvent.keyDown(container.firstChild as HTMLElement, { |
| 355 | + key: ' ', |
| 356 | + }); |
| 357 | + |
| 358 | + expect(notPrevented).toBe(true); |
| 359 | + }); |
| 360 | + |
| 361 | + it('should leave Enter to the inner button when the card has one', () => { |
| 362 | + const onButtonClick = vitest.fn(); |
| 363 | + const { container } = renderCard({ |
| 364 | + title: 'Test Card', |
| 365 | + infoText: 'Continue', |
| 366 | + onButtonClick, |
| 367 | + }); |
| 368 | + |
| 369 | + fireEvent.keyDown(container.firstChild as HTMLElement, { key: 'Enter' }); |
| 370 | + |
| 371 | + expect(onButtonClick).not.toHaveBeenCalled(); |
| 372 | + }); |
| 373 | + |
| 374 | + it('should keep the default Space action when the card has an inner button', () => { |
| 375 | + const { container } = renderCard({ |
| 376 | + title: 'Test Card', |
| 377 | + infoText: 'Continue', |
| 378 | + onButtonClick: vitest.fn(), |
| 379 | + }); |
| 380 | + |
| 381 | + const notPrevented = fireEvent.keyDown(container.firstChild as HTMLElement, { |
| 382 | + key: ' ', |
| 383 | + }); |
| 384 | + |
| 385 | + expect(notPrevented).toBe(true); |
| 386 | + }); |
| 387 | + |
| 388 | + it('should call onButtonClick when Enter is pressed on the focused card', async () => { |
| 389 | + const onButtonClick = vitest.fn(); |
| 390 | + const { container } = renderCard({ title: 'Test Card', onButtonClick }); |
| 391 | + |
| 392 | + (container.firstChild as HTMLElement).focus(); |
| 393 | + await userEvent.keyboard('{Enter}'); |
| 394 | + |
| 395 | + expect(onButtonClick).toHaveBeenCalledTimes(1); |
| 396 | + }); |
| 397 | + |
| 398 | + it('should open infoUrl when Enter is pressed on the focused card', async () => { |
| 399 | + const windowOpenSpy = vitest.spyOn(window, 'open').mockImplementation(() => null); |
| 400 | + const { container } = renderCard({ |
| 401 | + title: 'Test Card', |
| 402 | + infoUrl: 'https://example.com', |
| 403 | + }); |
| 404 | + |
| 405 | + (container.firstChild as HTMLElement).focus(); |
| 406 | + await userEvent.keyboard('{Enter}'); |
| 407 | + |
| 408 | + expect(windowOpenSpy).toHaveBeenCalledTimes(1); |
| 409 | + expect(windowOpenSpy).toHaveBeenCalledWith( |
| 410 | + 'https://example.com', |
| 411 | + '_blank', |
| 412 | + 'noopener' |
| 413 | + ); |
| 414 | + }); |
| 415 | + |
| 416 | + it('should not activate a disabled card', () => { |
| 417 | + const onButtonClick = vitest.fn(); |
| 418 | + const windowOpenSpy = vitest.spyOn(window, 'open').mockImplementation(() => null); |
| 419 | + const { container } = renderCard({ |
| 420 | + title: 'Test Card', |
| 421 | + disabled: true, |
| 422 | + infoUrl: 'https://example.com', |
| 423 | + onButtonClick, |
| 424 | + }); |
| 425 | + |
| 426 | + const wrapper = container.firstChild as HTMLElement; |
| 427 | + fireEvent.keyDown(wrapper, { key: 'Enter' }); |
| 428 | + fireEvent.keyDown(wrapper, { key: ' ' }); |
| 429 | + |
| 430 | + expect(onButtonClick).not.toHaveBeenCalled(); |
| 431 | + expect(windowOpenSpy).not.toHaveBeenCalled(); |
| 432 | + }); |
| 433 | + |
| 434 | + it('should activate only once when Enter is pressed on the inner button', async () => { |
| 435 | + const onButtonClick = vitest.fn(); |
| 436 | + const windowOpenSpy = vitest.spyOn(window, 'open').mockImplementation(() => null); |
| 437 | + const { getByRole } = renderCard({ |
| 438 | + title: 'Test Card', |
| 439 | + infoText: 'Click me', |
| 440 | + infoUrl: 'https://example.com', |
| 441 | + onButtonClick, |
| 442 | + }); |
| 443 | + |
| 444 | + getByRole('button').focus(); |
| 445 | + await userEvent.keyboard('{Enter}'); |
| 446 | + |
| 447 | + expect(onButtonClick).toHaveBeenCalledTimes(1); |
| 448 | + expect(windowOpenSpy).toHaveBeenCalledTimes(1); |
| 449 | + }); |
| 450 | + |
| 451 | + it("should call the consumer's onKeyDown", async () => { |
| 452 | + const onKeyDown = vitest.fn(); |
| 453 | + const { container } = renderCard({ title: 'Test Card', onKeyDown }); |
| 454 | + |
| 455 | + (container.firstChild as HTMLElement).focus(); |
| 456 | + await userEvent.keyboard('{Enter}'); |
| 457 | + |
| 458 | + expect(onKeyDown).toHaveBeenCalledTimes(1); |
| 459 | + }); |
| 460 | + |
| 461 | + it("should not activate when the consumer's onKeyDown prevents the default", async () => { |
| 462 | + const onClick = vitest.fn(); |
| 463 | + const { container } = renderCard({ |
| 464 | + title: 'Test Card', |
| 465 | + onClick, |
| 466 | + onKeyDown: e => e.preventDefault(), |
| 467 | + }); |
| 468 | + |
| 469 | + (container.firstChild as HTMLElement).focus(); |
| 470 | + await userEvent.keyboard('{Enter}'); |
| 471 | + |
| 472 | + expect(onClick).not.toHaveBeenCalled(); |
| 473 | + }); |
| 474 | + }); |
| 475 | + |
| 476 | + describe('role', () => { |
| 477 | + it('should expose an actionable card as a button', () => { |
| 478 | + renderCard({ title: 'Test Card', onClick: vitest.fn() }); |
| 479 | + |
| 480 | + expect(screen.getByRole('button')).toHaveTextContent('Test Card'); |
| 481 | + }); |
| 482 | + |
| 483 | + it('should not expose the card as a button when it renders an inner button', () => { |
| 484 | + renderCard({ |
| 485 | + title: 'Test Card', |
| 486 | + infoText: 'Click me', |
| 487 | + infoUrl: 'https://example.com', |
| 488 | + }); |
| 489 | + |
| 490 | + const buttons = screen.getAllByRole('button'); |
| 491 | + expect(buttons).toHaveLength(1); |
| 492 | + expect(buttons[0]).toHaveTextContent('Click me'); |
| 493 | + }); |
| 494 | + |
| 495 | + it('should not expose a display-only card as a button', () => { |
| 496 | + renderCard({ title: 'Summary' }); |
| 497 | + |
| 498 | + expect(screen.queryByRole('button')).not.toBeInTheDocument(); |
| 499 | + }); |
| 500 | + }); |
293 | 501 | }); |
0 commit comments