|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import React from 'react'; |
| 5 | +import { render, screen } from '@testing-library/react'; |
| 6 | + |
| 7 | +import DragHandle from '../../../../../lib/components/internal/components/drag-handle'; |
| 8 | + |
| 9 | +import styles from '../../../../../lib/components/internal/components/drag-handle/styles.css.js'; |
| 10 | + |
| 11 | +const allVariants = ['drag-indicator', 'resize-area', 'resize-horizontal', 'resize-vertical'] as const; |
| 12 | +const allSizes = ['small', 'normal'] as const; |
| 13 | + |
| 14 | +test('renders default variant and size', () => { |
| 15 | + render(<DragHandle ariaLabel="drag handle" />); |
| 16 | + |
| 17 | + expect(document.querySelector(`.${styles.handle}`)).toBeInTheDocument(); |
| 18 | + expect(document.querySelector(`.${styles['handle-drag-indicator']}`)).toBeInTheDocument(); |
| 19 | + expect(document.querySelector(`.${styles['handle-size-normal']}`)).toBeInTheDocument(); |
| 20 | +}); |
| 21 | + |
| 22 | +test('renders all variants and sizes', () => { |
| 23 | + for (const variant of allVariants) { |
| 24 | + for (const size of allSizes) { |
| 25 | + render(<DragHandle ariaLabel="drag handle" variant={variant} size={size} />); |
| 26 | + |
| 27 | + expect(document.querySelector(`.${styles.handle}`)).toBeInTheDocument(); |
| 28 | + expect(document.querySelector(`.${styles[`handle-${variant}`]}`)).toBeInTheDocument(); |
| 29 | + expect(document.querySelector(`.${styles[`handle-size-${size}`]}`)).toBeInTheDocument(); |
| 30 | + } |
| 31 | + } |
| 32 | +}); |
| 33 | + |
| 34 | +test('assigns aria label and aria description', () => { |
| 35 | + render( |
| 36 | + <div> |
| 37 | + <DragHandle ariaLabel="drag" ariaDescribedby="description" /> |
| 38 | + <div id="description">handle</div> |
| 39 | + </div> |
| 40 | + ); |
| 41 | + expect(document.querySelector(`.${styles.handle}`)).toHaveAccessibleName('drag'); |
| 42 | + expect(document.querySelector(`.${styles.handle}`)).toHaveAccessibleDescription('handle'); |
| 43 | +}); |
| 44 | + |
| 45 | +test('has role="button" by default', () => { |
| 46 | + render(<DragHandle ariaLabel="drag handle" />); |
| 47 | + |
| 48 | + expect(screen.getByRole('button')).toHaveAccessibleName('drag handle'); |
| 49 | +}); |
| 50 | + |
| 51 | +test('has role="slider" and aria-value attributes when ariaValue is set', () => { |
| 52 | + render(<DragHandle ariaLabel="drag handle" ariaValue={{ valueMin: -1, valueMax: 1, valueNow: 0 }} />); |
| 53 | + |
| 54 | + const handle = screen.getByRole('slider'); |
| 55 | + expect(handle).toHaveAccessibleName('drag handle'); |
| 56 | + expect(handle).toHaveAttribute('aria-valuemin', '-1'); |
| 57 | + expect(handle).toHaveAttribute('aria-valuemax', '1'); |
| 58 | + expect(handle).toHaveAttribute('aria-valuenow', '0'); |
| 59 | +}); |
| 60 | + |
| 61 | +test('sets aria-disabled when disabled', () => { |
| 62 | + const { rerender } = render(<DragHandle ariaLabel="drag handle" disabled={false} />); |
| 63 | + |
| 64 | + expect(document.querySelector(`.${styles.handle}`)).toHaveAttribute('aria-disabled', 'false'); |
| 65 | + expect(document.querySelector(`.${styles['handle-disabled']}`)).not.toBeInTheDocument(); |
| 66 | + |
| 67 | + rerender(<DragHandle ariaLabel="drag handle" disabled={true} />); |
| 68 | + |
| 69 | + expect(document.querySelector(`.${styles.handle}`)).toHaveAttribute('aria-disabled'); |
| 70 | + expect(document.querySelector(`.${styles['handle-disabled']}`)).toBeInTheDocument(); |
| 71 | +}); |
0 commit comments