| 1 |
import { render, screen } from '@testing-library/react'; |
| 2 |
|
| 3 |
import { |
| 4 |
TableGridBreakpoint, |
| 5 |
TableVariant, |
| 6 |
RowSelectVariant, |
| 7 |
cellWidth, |
| 8 |
headerCol, |
| 9 |
sortable, |
| 10 |
expandable, |
| 11 |
compoundExpand, |
| 12 |
IRow, |
| 13 |
OnCollapse, |
| 14 |
OnExpand, |
| 15 |
OnSelect, |
| 16 |
OnRowEdit, |
| 17 |
OnSort, |
| 18 |
wrappable, |
| 19 |
nowrap, |
| 20 |
truncate, |
| 21 |
breakWord, |
| 22 |
fitContent, |
| 23 |
ICell |
| 24 |
} from '../../../../components'; |
| 25 |
import { Table, TableHeader, TableBody } from '../index'; |
| 26 |
import { rows, columns, editableRows, editableColumns, actions } from '../../../../test-helpers/data-sets'; |
| 27 |
import { ColumnsType } from '../../../../components/Table/base/types'; |
| 28 |
|
| 29 |
describe('Table', () => { |
| 30 |
describe('simple', () => { |
| 31 |
test('caption', () => { |
| 32 |
const { asFragment } = render( |
| 33 |
<Table caption="Simple Table" cells={columns} rows={rows}> |
| 34 |
<TableHeader /> |
| 35 |
<TableBody /> |
| 36 |
</Table> |
| 37 |
); |
| 38 |
expect(asFragment()).toMatchSnapshot(); |
| 39 |
}); |
| 40 |
|
| 41 |
test('ouiaSafe can be set to false', () => { |
| 42 |
render( |
| 43 |
<Table aria-label="Table with ouiaSafe" ouiaSafe={false} cells={columns} rows={rows}> |
| 44 |
<TableHeader /> |
| 45 |
<TableBody /> |
| 46 |
</Table> |
| 47 |
); |
| 48 |
const table = screen.getByLabelText('Table with ouiaSafe'); |
| 49 |
expect(table.getAttribute('data-ouia-safe')).toEqual('false'); |
| 50 |
}); |
| 51 |
|
| 52 |
test('header', () => { |
| 53 |
const { asFragment } = render( |
| 54 |
<Table header={<h4>Header title</h4>} cells={columns} rows={rows}> |
| 55 |
<TableHeader /> |
| 56 |
<TableBody /> |
| 57 |
</Table> |
| 58 |
); |
| 59 |
expect(asFragment()).toMatchSnapshot(); |
| 60 |
}); |
| 61 |
|
| 62 |
test('aria-label', () => { |
| 63 |
const { asFragment } = render( |
| 64 |
<Table aria-label="Aria labeled" cells={columns} rows={rows}> |
| 65 |
<TableHeader /> |
| 66 |
<TableBody /> |
| 67 |
</Table> |
| 68 |
); |
| 69 |
expect(asFragment()).toMatchSnapshot(); |
| 70 |
}); |
| 71 |
|
| 72 |
test('Editable table', () => { |
| 73 |
const onRowEdit: OnRowEdit = () => undefined; |
| 74 |
const { asFragment } = render( |
| 75 |
<Table caption="Editable Table" cells={editableColumns} rows={editableRows} onRowEdit={onRowEdit}> |
| 76 |
<TableHeader /> |
| 77 |
<TableBody /> |
| 78 |
</Table> |
| 79 |
); |
| 80 |
|
| 81 |
expect(asFragment()).toMatchSnapshot(); |
| 82 |
}); |
| 83 |
|
| 84 |
test('Sortable table', () => { |
| 85 |
columns[0] = { ...(columns[0] as object), transforms: [sortable] }; |
| 86 |
|
| 87 |
const onSortCall: OnSort = () => undefined; |
| 88 |
const { asFragment } = render( |
| 89 |
<Table aria-label="Aria labeled" onSort={onSortCall} sortBy={{}} cells={columns} rows={rows}> |
| 90 |
<TableHeader /> |
| 91 |
<TableBody /> |
| 92 |
</Table> |
| 93 |
); |
| 94 |
|
| 95 |
expect(asFragment()).toMatchSnapshot(); |
| 96 |
}); |
| 97 |
|
| 98 |
test('Row click table', () => { |
| 99 |
const rowClickHandler = jest.fn(); |
| 100 |
const { asFragment } = render( |
| 101 |
<Table aria-label="Row click table" cells={columns} rows={rows}> |
| 102 |
<TableHeader /> |
| 103 |
<TableBody onRowClick={rowClickHandler} /> |
| 104 |
</Table> |
| 105 |
); |
| 106 |
|
| 107 |
expect(asFragment()).toMatchSnapshot(); |
| 108 |
}); |
| 109 |
}); |
| 110 |
|
| 111 |
describe('Table variants', () => { |
| 112 |
Object.values(TableGridBreakpoint).forEach((oneBreakpoint) => { |
| 113 |
test(`Breakpoint - ${oneBreakpoint}`, () => { |
| 114 |
const { asFragment } = render( |
| 115 |
<Table aria-label="Aria labeled" gridBreakPoint={oneBreakpoint} cells={columns} rows={rows}> |
| 116 |
<TableHeader /> |
| 117 |
<TableBody /> |
| 118 |
</Table> |
| 119 |
); |
| 120 |
expect(asFragment()).toMatchSnapshot(); |
| 121 |
}); |
| 122 |
}); |
| 123 |
|
| 124 |
Object.values(TableVariant).forEach((onevariant) => { |
| 125 |
test(`Size - ${onevariant}`, () => { |
| 126 |
const { asFragment } = render( |
| 127 |
<Table aria-label="Aria labeled" variant={onevariant} cells={columns} rows={rows}> |
| 128 |
<TableHeader /> |
| 129 |
<TableBody /> |
| 130 |
</Table> |
| 131 |
); |
| 132 |
expect(asFragment()).toMatchSnapshot(); |
| 133 |
}); |
| 134 |
}); |
| 135 |
}); |
| 136 |
|
| 137 |
test('Simple Actions table', () => { |
| 138 |
const rowsWithDisabledAction = [ |
| 139 |
...rows, |
| 140 |
{ |
| 141 |
cells: ['one', 'two', 'three', 'four', 'five'], |
| 142 |
disableActions: true |
| 143 |
} |
| 144 |
]; |
| 145 |
const { asFragment } = render( |
| 146 |
<Table aria-label="Aria labeled" actions={actions} cells={columns} rows={rowsWithDisabledAction}> |
| 147 |
<TableHeader /> |
| 148 |
<TableBody /> |
| 149 |
</Table> |
| 150 |
); |
| 151 |
|
| 152 |
expect(asFragment()).toMatchSnapshot(); |
| 153 |
}); |
| 154 |
|
| 155 |
test('Actions table', () => { |
| 156 |
const { asFragment } = render( |
| 157 |
<Table |
| 158 |
aria-label="Aria labeled" |
| 159 |
actionResolver={() => actions} |
| 160 |
areActionsDisabled={() => false} |
| 161 |
cells={columns} |
| 162 |
rows={rows} |
| 163 |
> |
| 164 |
<TableHeader /> |
| 165 |
<TableBody /> |
| 166 |
</Table> |
| 167 |
); |
| 168 |
expect(asFragment()).toMatchSnapshot(); |
| 169 |
}); |
| 170 |
|
| 171 |
test('Cell header table', () => { |
| 172 |
columns[0] = { ...(columns[0] as object), cellTransforms: [headerCol('test-headercol-')] }; |
| 173 |
const { asFragment } = render( |
| 174 |
<Table aria-label="Aria labeled" cells={columns} rows={rows}> |
| 175 |
<TableHeader /> |
| 176 |
<TableBody /> |
| 177 |
</Table> |
| 178 |
); |
| 179 |
|
| 180 |
expect(asFragment()).toMatchSnapshot(); |
| 181 |
}); |
| 182 |
|
| 183 |
test('Collapsible table', () => { |
| 184 |
rows[0] = { ...rows[0], isOpen: true }; |
| 185 |
rows[1] = { ...rows[1], parent: 0 }; |
| 186 |
rows[3] = { ...rows[3], isOpen: false }; |
| 187 |
rows[4] = { ...rows[4], parent: 3 }; |
| 188 |
columns[0] = { ...(columns[0] as object), cellFormatters: [expandable] }; |
| 189 |
|
| 190 |
const onCollapse: OnCollapse = () => undefined; |
| 191 |
const { asFragment } = render( |
| 192 |
<Table aria-label="Aria labeled" onCollapse={onCollapse} cells={columns} rows={rows}> |
| 193 |
<TableHeader /> |
| 194 |
<TableBody /> |
| 195 |
</Table> |
| 196 |
); |
| 197 |
|
| 198 |
expect(asFragment()).toMatchSnapshot(); |
| 199 |
}); |
| 200 |
|
| 201 |
test('Compound Expandable table', () => { |
| 202 |
const compoundColumns: ColumnsType = [ |
| 203 |
{ title: 'col1', cellTransforms: [compoundExpand] }, |
| 204 |
{ title: 'col2', cellTransforms: [compoundExpand] } |
| 205 |
]; |
| 206 |
const compoundRows: IRow[] = [ |
| 207 |
{ |
| 208 |
isOpen: true, |
| 209 |
cells: [ |
| 210 |
{ title: '1', props: { isOpen: true } }, |
| 211 |
{ title: '2', props: { isOpen: false } } |
| 212 |
] |
| 213 |
}, |
| 214 |
{ parent: 0, compoundParent: 0, cells: [{ title: 'expanded', props: { colSpan: 2 } }] }, |
| 215 |
{ |
| 216 |
isOpen: false, |
| 217 |
cells: [ |
| 218 |
{ title: '3', props: { isOpen: false } }, |
| 219 |
{ title: '4', props: { isOpen: false } } |
| 220 |
] |
| 221 |
}, |
| 222 |
{ parent: 2, compoundParent: 0, cells: [{ title: 'expanded', props: { colSpan: 2 } }] } |
| 223 |
]; |
| 224 |
|
| 225 |
const onExpand: OnExpand = () => undefined; |
| 226 |
const { asFragment } = render( |
| 227 |
<Table aria-label="Aria labeled" onExpand={onExpand} cells={compoundColumns} rows={compoundRows}> |
| 228 |
<TableHeader /> |
| 229 |
<TableBody /> |
| 230 |
</Table> |
| 231 |
); |
| 232 |
|
| 233 |
expect(asFragment()).toMatchSnapshot(); |
| 234 |
}); |
| 235 |
|
| 236 |
test('Collapsible nested table', () => { |
| 237 |
rows[0] = { ...rows[0], isOpen: false }; |
| 238 |
rows[1] = { ...rows[1], parent: 0, isOpen: true }; |
| 239 |
rows[2] = { ...rows[2], parent: 1 }; |
| 240 |
|
| 241 |
const onCollapse: OnCollapse = () => undefined; |
| 242 |
const { asFragment } = render( |
| 243 |
<Table aria-label="Aria labeled" onCollapse={onCollapse} cells={columns} rows={rows}> |
| 244 |
<TableHeader /> |
| 245 |
<TableBody /> |
| 246 |
</Table> |
| 247 |
); |
| 248 |
|
| 249 |
expect(asFragment()).toMatchSnapshot(); |
| 250 |
}); |
| 251 |
|
| 252 |
test('Selectable table', () => { |
| 253 |
const onSelect: OnSelect = () => undefined; |
| 254 |
const { asFragment } = render( |
| 255 |
<Table aria-label="Aria labeled" onSelect={onSelect} cells={columns} rows={rows}> |
| 256 |
<TableHeader /> |
| 257 |
<TableBody /> |
| 258 |
</Table> |
| 259 |
); |
| 260 |
|
| 261 |
expect(asFragment()).toMatchSnapshot(); |
| 262 |
}); |
| 263 |
|
| 264 |
test('Selectable table with Radio', () => { |
| 265 |
const onSelect: OnSelect = () => undefined; |
| 266 |
const { asFragment } = render( |
| 267 |
<Table |
| 268 |
aria-label="Aria labeled" |
| 269 |
selectVariant={RowSelectVariant.radio} |
| 270 |
onSelect={onSelect} |
| 271 |
cells={columns} |
| 272 |
rows={rows} |
| 273 |
> |
| 274 |
<TableHeader /> |
| 275 |
<TableBody /> |
| 276 |
</Table> |
| 277 |
); |
| 278 |
|
| 279 |
expect(asFragment()).toMatchSnapshot(); |
| 280 |
}); |
| 281 |
|
| 282 |
test('Control text table', () => { |
| 283 |
const controlTextColumns: ICell[] = [ |
| 284 |
{ ...(columns[0] as object), transforms: [nowrap] }, |
| 285 |
{ title: 'new object column', transforms: [wrappable] }, |
| 286 |
{ ...(columns[2] as object), transforms: [breakWord] }, |
| 287 |
{ title: 'new object column', transforms: [truncate] }, |
| 288 |
{ ...(columns[4] as object), transforms: [fitContent] } |
| 289 |
]; |
| 290 |
|
| 291 |
const { asFragment } = render( |
| 292 |
<Table aria-label="Aria labeled" cells={controlTextColumns} rows={rows}> |
| 293 |
<TableHeader /> |
| 294 |
<TableBody /> |
| 295 |
</Table> |
| 296 |
); |
| 297 |
expect(asFragment()).toMatchSnapshot(); |
| 298 |
}); |
| 299 |
|
| 300 |
test('Header width table', () => { |
| 301 |
columns[0] = { ...(columns[0] as object), transforms: [cellWidth(10)] }; |
| 302 |
columns[2] = { ...(columns[2] as object), transforms: [cellWidth(30)] }; |
| 303 |
columns[4] = { ...(columns[4] as object), transforms: [cellWidth(100)] }; |
| 304 |
const { asFragment } = render( |
| 305 |
<Table aria-label="Aria labeled" cells={columns} rows={rows}> |
| 306 |
<TableHeader /> |
| 307 |
<TableBody /> |
| 308 |
</Table> |
| 309 |
); |
| 310 |
expect(asFragment()).toMatchSnapshot(); |
| 311 |
}); |
| 312 |
|
| 313 |
test('Selectable table with selected expandable row', () => { |
| 314 |
const data = { |
| 315 |
cells: ['column'], |
| 316 |
rows: [ |
| 317 |
{ |
| 318 |
cells: ['one'], |
| 319 |
selected: true |
| 320 |
}, |
| 321 |
{ |
| 322 |
cells: ['one'], |
| 323 |
parent: 0 |
| 324 |
} |
| 325 |
], |
| 326 |
onSelect: (e: React.FormEvent<HTMLInputElement>) => e |
| 327 |
}; |
| 328 |
|
| 329 |
const { asFragment } = render( |
| 330 |
<Table aria-label="Aria labeled" {...data}> |
| 331 |
<TableHeader /> |
| 332 |
<TableBody /> |
| 333 |
</Table> |
| 334 |
); |
| 335 |
|
| 336 |
expect(asFragment()).toMatchSnapshot(); |
| 337 |
}); |
| 338 |
|
| 339 |
test('Empty state table', () => { |
| 340 |
const data = { |
| 341 |
cells: ['Hostname', 'IP address', 'Role', 'Team'], |
| 342 |
rows: [ |
| 343 |
{ |
| 344 |
heightAuto: true, |
| 345 |
cells: [ |
| 346 |
{ |
| 347 |
title: <div>Empty State Component</div>, |
| 348 |
props: { colSpan: 8 } |
| 349 |
} |
| 350 |
] |
| 351 |
} |
| 352 |
] |
| 353 |
}; |
| 354 |
|
| 355 |
render( |
| 356 |
<Table aria-label="Aria labeled" {...data}> |
| 357 |
<TableHeader /> |
| 358 |
<TableBody /> |
| 359 |
</Table> |
| 360 |
); |
| 361 |
|
| 362 |
expect(screen.getByText('Empty State Component').parentElement).toHaveAttribute('colspan', '8'); |
| 363 |
}); |
| 364 |
}); |