| 1 |
import { render, screen } from '@testing-library/react'; |
| 2 |
import { Table } from '../Table'; |
| 3 |
import { Td } from '../Td'; |
| 4 |
import { Th } from '../Th'; |
| 5 |
import { AnimationsProvider, useAnimationsConfig, useHasAnimations } from '@patternfly/react-core/dist/esm/helpers'; |
| 6 |
import styles from '@patternfly/react-styles/css/components/Table/table'; |
| 7 |
|
| 8 |
test('Renders without children', () => { |
| 9 |
render(<Table aria-label="Test table" />); |
| 10 |
|
| 11 |
expect(screen.getByRole('grid', { name: 'Test table' })).toBeInTheDocument(); |
| 12 |
}); |
| 13 |
|
| 14 |
test('Renders with children', () => { |
| 15 |
render( |
| 16 |
<Table aria-label="Test table"> |
| 17 |
<caption>Table caption</caption> |
| 18 |
</Table> |
| 19 |
); |
| 20 |
|
| 21 |
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveTextContent('Table caption'); |
| 22 |
}); |
| 23 |
|
| 24 |
test('Renders with role="treegrid" when isTreeTable is true', () => { |
| 25 |
render(<Table isTreeTable aria-label="Test table" />); |
| 26 |
|
| 27 |
expect(screen.getByRole('treegrid', { name: 'Test table' })).toBeInTheDocument(); |
| 28 |
}); |
| 29 |
|
| 30 |
test(`Renders with class ${styles.table} by default`, () => { |
| 31 |
render(<Table aria-label="Test table" />); |
| 32 |
|
| 33 |
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.table); |
| 34 |
}); |
| 35 |
|
| 36 |
test(`Renders with a pf-m-grid class by default`, () => { |
| 37 |
render(<Table aria-label="Test table" />); |
| 38 |
|
| 39 |
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(/^pf-m-grid/); |
| 40 |
}); |
| 41 |
|
| 42 |
test(`Renders with a pf-m-tree-view-grid class when isTreeTable is true`, () => { |
| 43 |
render(<Table isTreeTable aria-label="Test table" />); |
| 44 |
|
| 45 |
expect(screen.getByRole('treegrid', { name: 'Test table' })).not.toHaveClass(/^pf-m-grid/); |
| 46 |
expect(screen.getByRole('treegrid', { name: 'Test table' })).toHaveClass(/^pf-m-tree-view-grid/); |
| 47 |
}); |
| 48 |
|
| 49 |
test(`Does not render with class ${styles.modifiers.animateExpand} by default`, () => { |
| 50 |
render(<Table aria-label="Test table" />); |
| 51 |
|
| 52 |
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.animateExpand); |
| 53 |
}); |
| 54 |
|
| 55 |
test(`Renders with class ${styles.modifiers.animateExpand} hasAnimations is true`, () => { |
| 56 |
render(<Table hasAnimations aria-label="Test table" />); |
| 57 |
|
| 58 |
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.animateExpand); |
| 59 |
}); |
| 60 |
|
| 61 |
// Animation context tests for expandable tables |
| 62 |
test('respects AnimationsProvider context when no local hasAnimations prop for expandable table', () => { |
| 63 |
render( |
| 64 |
<AnimationsProvider config={{ hasAnimations: true }}> |
| 65 |
<Table isExpandable aria-label="Test table" /> |
| 66 |
</AnimationsProvider> |
| 67 |
); |
| 68 |
|
| 69 |
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.animateExpand); |
| 70 |
}); |
| 71 |
|
| 72 |
test('local hasAnimations prop takes precedence over context for expandable table', () => { |
| 73 |
render( |
| 74 |
<AnimationsProvider config={{ hasAnimations: true }}> |
| 75 |
<Table isExpandable hasAnimations={false} aria-label="Test table" /> |
| 76 |
</AnimationsProvider> |
| 77 |
); |
| 78 |
|
| 79 |
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.animateExpand); |
| 80 |
}); |
| 81 |
|
| 82 |
test('works without AnimationsProvider (backward compatibility) for expandable table', () => { |
| 83 |
render(<Table isExpandable aria-label="Test table" />); |
| 84 |
|
| 85 |
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.animateExpand); |
| 86 |
}); |
| 87 |
|
| 88 |
test('Matches snapshot without children', () => { |
| 89 |
const { asFragment } = render(<Table />); |
| 90 |
|
| 91 |
expect(asFragment()).toMatchSnapshot(); |
| 92 |
}); |
| 93 |
|
| 94 |
test('Renders expandable toggle button with pf-m-small class when variant is compact', () => { |
| 95 |
render( |
| 96 |
<Table variant="compact" isExpandable aria-label="Test table"> |
| 97 |
<tbody> |
| 98 |
<tr> |
| 99 |
<Td |
| 100 |
expand={{ |
| 101 |
rowIndex: 0, |
| 102 |
isExpanded: false, |
| 103 |
onToggle: () => {} |
| 104 |
}} |
| 105 |
/> |
| 106 |
<Td>Test content</Td> |
| 107 |
</tr> |
| 108 |
</tbody> |
| 109 |
</Table> |
| 110 |
); |
| 111 |
|
| 112 |
const toggleButton = screen.getByRole('button', { name: 'Details' }); |
| 113 |
expect(toggleButton).toHaveClass('pf-m-small'); |
| 114 |
}); |
| 115 |
|
| 116 |
test('Renders expandable toggle button in Th with pf-m-small class when variant is compact', () => { |
| 117 |
render( |
| 118 |
<Table variant="compact" isExpandable aria-label="Test table"> |
| 119 |
<thead> |
| 120 |
<tr> |
| 121 |
<Th |
| 122 |
expand={{ |
| 123 |
areAllExpanded: false, |
| 124 |
collapseAllAriaLabel: 'Expand all', |
| 125 |
onToggle: () => {} |
| 126 |
}} |
| 127 |
aria-label="Row expansion" |
| 128 |
/> |
| 129 |
<Th>Name</Th> |
| 130 |
</tr> |
| 131 |
</thead> |
| 132 |
<tbody> |
| 133 |
<tr> |
| 134 |
<Td |
| 135 |
expand={{ |
| 136 |
rowIndex: 0, |
| 137 |
isExpanded: false, |
| 138 |
onToggle: () => {} |
| 139 |
}} |
| 140 |
/> |
| 141 |
<td>Test content</td> |
| 142 |
</tr> |
| 143 |
</tbody> |
| 144 |
</Table> |
| 145 |
); |
| 146 |
|
| 147 |
const toggleButtons = screen.getAllByRole('button'); |
| 148 |
expect(toggleButtons).toHaveLength(2); // One in Th, one in Td |
| 149 |
toggleButtons.forEach((button) => { |
| 150 |
expect(button).toHaveClass('pf-m-small'); |
| 151 |
}); |
| 152 |
}); |
| 153 |
|
| 154 |
test(`Renders with class ${styles.modifiers.plain} when isPlain is true`, () => { |
| 155 |
render(<Table isPlain aria-label="Test table" />); |
| 156 |
|
| 157 |
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.plain); |
| 158 |
}); |
| 159 |
|
| 160 |
test(`Does not render with class ${styles.modifiers.plain} when isPlain is not defined`, () => { |
| 161 |
render(<Table aria-label="Test table" />); |
| 162 |
|
| 163 |
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.plain); |
| 164 |
}); |
| 165 |
|
| 166 |
test(`Does not render with class ${styles.modifiers.plain} when isPlain is false`, () => { |
| 167 |
render(<Table isPlain={false} aria-label="Test table" />); |
| 168 |
|
| 169 |
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.plain); |
| 170 |
}); |
| 171 |
|
| 172 |
test(`Renders with class ${styles.modifiers.noPlainOnGlass} when isNoPlainOnGlass is true`, () => { |
| 173 |
render(<Table isNoPlainOnGlass aria-label="Test table" />); |
| 174 |
|
| 175 |
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.noPlainOnGlass); |
| 176 |
}); |
| 177 |
|
| 178 |
test(`Does not render with class ${styles.modifiers.noPlainOnGlass} when isNoPlainOnGlass is undefined`, () => { |
| 179 |
render(<Table aria-label="Test table" />); |
| 180 |
|
| 181 |
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.noPlainOnGlass); |
| 182 |
}); |
| 183 |
|
| 184 |
test(`Does not render with class ${styles.modifiers.noPlainOnGlass} when isNoPlainOnGlass is false`, () => { |
| 185 |
render(<Table isNoPlainOnGlass={false} aria-label="Test table" />); |
| 186 |
|
| 187 |
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.noPlainOnGlass); |
| 188 |
}); |
| 189 |
|
| 190 |
test(`Renders with class ${styles.modifiers.stickyHeaderBase} when isStickyHeaderBase is true`, () => { |
| 191 |
render(<Table isStickyHeaderBase aria-label="Test table" />); |
| 192 |
|
| 193 |
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.stickyHeaderBase); |
| 194 |
}); |
| 195 |
|
| 196 |
test(`Does not render with class ${styles.modifiers.stickyHeaderBase} when isStickyHeaderBase is false`, () => { |
| 197 |
render(<Table isStickyHeaderBase={false} aria-label="Test table" />); |
| 198 |
|
| 199 |
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderBase); |
| 200 |
}); |
| 201 |
|
| 202 |
test(`Does not render with class ${styles.modifiers.stickyHeaderBase} when isStickyHeaderBase is undefined`, () => { |
| 203 |
render(<Table aria-label="Test table" />); |
| 204 |
|
| 205 |
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderBase); |
| 206 |
}); |
| 207 |
|
| 208 |
test(`Renders with class ${styles.modifiers.stickyHeaderStuck} when isStickyHeaderStuck is true`, () => { |
| 209 |
render(<Table isStickyHeaderStuck aria-label="Test table" />); |
| 210 |
|
| 211 |
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.stickyHeaderStuck); |
| 212 |
}); |
| 213 |
|
| 214 |
test(`Does not render with class ${styles.modifiers.stickyHeaderStuck} when isStickyHeaderStuck is false`, () => { |
| 215 |
render(<Table isStickyHeaderStuck={false} aria-label="Test table" />); |
| 216 |
|
| 217 |
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderStuck); |
| 218 |
}); |
| 219 |
|
| 220 |
test(`Does not render with class ${styles.modifiers.stickyHeaderStuck} when isStickyHeaderStuck is undefined`, () => { |
| 221 |
render(<Table aria-label="Test table" />); |
| 222 |
|
| 223 |
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderStuck); |
| 224 |
}); |