| 1 |
import { render, screen } from '@testing-library/react'; |
| 2 |
import { Th } from '../Th'; |
| 3 |
|
| 4 |
test('Does not render with aria-label by default', () => { |
| 5 |
render(<Th />); |
| 6 |
expect(screen.getByRole('columnheader')).not.toHaveAccessibleName(); |
| 7 |
}); |
| 8 |
|
| 9 |
test('Renders with aria-label when passed in', () => { |
| 10 |
render(<Th aria-label="Test" />); |
| 11 |
expect(screen.getByRole('columnheader')).toHaveAccessibleName('Test'); |
| 12 |
}); |
| 13 |
|
| 14 |
test('Does not render with screen reader text by default', () => { |
| 15 |
render(<Th />); |
| 16 |
|
| 17 |
expect(screen.getByRole('columnheader')).toBeEmptyDOMElement(); |
| 18 |
}); |
| 19 |
|
| 20 |
test('Does not render with screen reader text when children are passed in', () => { |
| 21 |
render(<Th screenReaderText="Test">Heading label</Th>); |
| 22 |
|
| 23 |
expect(screen.getByRole('columnheader')).not.toHaveTextContent('Test'); |
| 24 |
}); |
| 25 |
|
| 26 |
test('Renders with screen reader text when screenReaderText is passed in', () => { |
| 27 |
render(<Th screenReaderText="Test" />); |
| 28 |
|
| 29 |
expect(screen.getByRole('columnheader')).toHaveTextContent('Test'); |
| 30 |
}); |
| 31 |
|
| 32 |
test('Does not render with additional content by default', () => { |
| 33 |
render(<Th />); |
| 34 |
|
| 35 |
expect(screen.getByRole('columnheader')).toBeEmptyDOMElement(); |
| 36 |
}); |
| 37 |
|
| 38 |
test('Render with additional content when additionalContent is passed in', () => { |
| 39 |
render(<Th additionalContent={<div>Extra</div>}>Test</Th>); |
| 40 |
|
| 41 |
expect(screen.getByRole('columnheader')).toHaveTextContent('Extra'); |
| 42 |
}); |
| 43 |
|
| 44 |
test('Additional content renders after children when additionalContent is passed in', () => { |
| 45 |
render( |
| 46 |
<Th additionalContent={<div>Extra</div>}> |
| 47 |
<div>Test</div> |
| 48 |
</Th> |
| 49 |
); |
| 50 |
|
| 51 |
const th = screen.getByRole('columnheader'); |
| 52 |
const thChildren = th.children; |
| 53 |
|
| 54 |
expect(thChildren.item(0)?.textContent).toEqual('Test'); |
| 55 |
expect(thChildren.item(1)?.textContent).toEqual('Extra'); |
| 56 |
}); |
| 57 |
|
| 58 |
test('Renders checkbox without indeterminate state by default', () => { |
| 59 |
render(<Th select={{ onSelect: jest.fn(), isSelected: false }} aria-label="Select all" />); |
| 60 |
|
| 61 |
const checkbox = screen.getByRole('checkbox') as HTMLInputElement; |
| 62 |
expect(checkbox).not.toBeChecked(); |
| 63 |
expect(checkbox.indeterminate).toBe(false); |
| 64 |
}); |
| 65 |
|
| 66 |
test('Renders checkbox with indeterminate state when isIndeterminate is true', () => { |
| 67 |
render(<Th select={{ onSelect: jest.fn(), isSelected: false, isIndeterminate: true }} aria-label="Select all" />); |
| 68 |
|
| 69 |
const checkbox = screen.getByRole('checkbox') as HTMLInputElement; |
| 70 |
expect(checkbox.indeterminate).toBe(true); |
| 71 |
}); |
| 72 |
|
| 73 |
test('Renders checked checkbox when isSelected is true and isIndeterminate is false', () => { |
| 74 |
render(<Th select={{ onSelect: jest.fn(), isSelected: true, isIndeterminate: false }} aria-label="Select all" />); |
| 75 |
|
| 76 |
const checkbox = screen.getByRole('checkbox') as HTMLInputElement; |
| 77 |
expect(checkbox).toBeChecked(); |
| 78 |
expect(checkbox.indeterminate).toBe(false); |
| 79 |
}); |