| 1 |
import { render, screen, waitFor } from '@testing-library/react'; |
| 2 |
import userEvent from '@testing-library/user-event'; |
| 3 |
|
| 4 |
import { |
| 5 |
Visibility, |
| 6 |
selectable, |
| 7 |
sortable, |
| 8 |
cellWidth, |
| 9 |
collapsible, |
| 10 |
scopeColTransformer, |
| 11 |
headerCol, |
| 12 |
editable, |
| 13 |
emptyCol, |
| 14 |
mapProps, |
| 15 |
expandable, |
| 16 |
expandedRow, |
| 17 |
wrappable, |
| 18 |
textCenter, |
| 19 |
cellActions |
| 20 |
} from './'; |
| 21 |
import { |
| 22 |
IAction, |
| 23 |
IActions, |
| 24 |
IActionsResolver, |
| 25 |
IAreActionsDisabled, |
| 26 |
IExtra, |
| 27 |
IExtraData, |
| 28 |
IRowData, |
| 29 |
ISeparator |
| 30 |
} from '../TableTypes'; |
| 31 |
import { css } from '@patternfly/react-styles'; |
| 32 |
import tableStyles from '@patternfly/react-styles/css/components/Table/table'; |
| 33 |
import menuStyles from '@patternfly/react-styles/css/components/Menu/menu'; |
| 34 |
|
| 35 |
const testCellActions = async ({ |
| 36 |
actions, |
| 37 |
actionResolver, |
| 38 |
areActionsDisabled, |
| 39 |
rowData, |
| 40 |
extraData, |
| 41 |
expectDisabled |
| 42 |
}: { |
| 43 |
actions?: IActions; |
| 44 |
actionResolver?: IActionsResolver; |
| 45 |
areActionsDisabled?: IAreActionsDisabled; |
| 46 |
rowData?: IRowData; |
| 47 |
extraData?: IExtraData; |
| 48 |
expectDisabled?: boolean; |
| 49 |
}) => { |
| 50 |
const user = userEvent.setup(); |
| 51 |
const returnedData = cellActions( |
| 52 |
actions, |
| 53 |
actionResolver, |
| 54 |
areActionsDisabled |
| 55 |
)('', { |
| 56 |
rowIndex: 0, |
| 57 |
rowData, |
| 58 |
column: { |
| 59 |
extraParams: { |
| 60 |
dropdownPosition: 'right', |
| 61 |
dropdownDirection: 'down' |
| 62 |
} |
| 63 |
} |
| 64 |
}); |
| 65 |
|
| 66 |
if (actionResolver) { |
| 67 |
actions = actionResolver(rowData, extraData); |
| 68 |
} |
| 69 |
|
| 70 |
expect(returnedData).toMatchObject({ className: tableStyles.tableAction }); |
| 71 |
|
| 72 |
if (!actions || actions.length === 0) { |
| 73 |
expect(returnedData.children).toBeUndefined(); |
| 74 |
} else { |
| 75 |
const { container } = render(returnedData.children as React.ReactElement<any>); |
| 76 |
await user.click(screen.getAllByRole('button')[0]); |
| 77 |
await waitFor(() => |
| 78 |
// eslint-disable-next-line testing-library/no-container |
| 79 |
expect(container.querySelectorAll(`.${menuStyles.menu} li button`)).toHaveLength(expectDisabled ? 0 : 1) |
| 80 |
); |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
describe('Transformer functions', () => { |
| 85 |
describe('selectable', () => { |
| 86 |
test('main select (header) - should toggle from unchecked to checked', async () => { |
| 87 |
const onSelect = jest.fn((_event, selected, rowId) => ({ selected, rowId })); |
| 88 |
const column = { |
| 89 |
extraParams: { onSelect } |
| 90 |
}; |
| 91 |
const returnedData = selectable('', { column, rowData: {} } as IExtra); |
| 92 |
expect(returnedData).toMatchObject({ className: tableStyles.tableCheck }); |
| 93 |
|
| 94 |
const user = userEvent.setup(); |
| 95 |
render(returnedData.children as React.ReactElement<any>); |
| 96 |
|
| 97 |
// Click the header radio button (should be unchecked initially) |
| 98 |
await user.click(screen.getByRole('radio')); |
| 99 |
|
| 100 |
expect(onSelect).toHaveBeenCalledTimes(1); |
| 101 |
expect(onSelect.mock.results[0].value).toMatchObject({ rowId: -1, selected: true }); |
| 102 |
}); |
| 103 |
|
| 104 |
test('row select (checkbox) - should toggle from selected to unselected', async () => { |
| 105 |
const onSelect = jest.fn((_event, selected, rowId) => ({ selected, rowId })); |
| 106 |
const column = { |
| 107 |
extraParams: { onSelect, selectVariant: 'checkbox' } |
| 108 |
}; |
| 109 |
const returnedData = selectable('', { column, rowIndex: 0, rowData: { selected: true } } as IExtra); |
| 110 |
expect(returnedData).toMatchObject({ className: tableStyles.tableCheck }); |
| 111 |
|
| 112 |
const user = userEvent.setup(); |
| 113 |
render(returnedData.children as React.ReactElement<any>); |
| 114 |
|
| 115 |
// Click the row checkbox (should be checked initially, clicking should uncheck) |
| 116 |
await user.click(screen.getByRole('checkbox')); |
| 117 |
expect(onSelect).toHaveBeenCalledTimes(1); |
| 118 |
expect(onSelect.mock.results[0].value).toMatchObject({ rowId: 0, selected: false }); |
| 119 |
}); |
| 120 |
|
| 121 |
test('row select (checkbox) - should toggle from unselected to selected', async () => { |
| 122 |
const onSelect = jest.fn((_event, selected, rowId) => ({ selected, rowId })); |
| 123 |
const column = { |
| 124 |
extraParams: { onSelect, selectVariant: 'checkbox' } |
| 125 |
}; |
| 126 |
const returnedData = selectable('', { column, rowIndex: 0, rowData: { selected: false } } as IExtra); |
| 127 |
expect(returnedData).toMatchObject({ className: tableStyles.tableCheck }); |
| 128 |
|
| 129 |
const user = userEvent.setup(); |
| 130 |
render(returnedData.children as React.ReactElement<any>); |
| 131 |
|
| 132 |
// Click the row checkbox (should be unchecked initially, clicking should check) |
| 133 |
await user.click(screen.getByRole('checkbox')); |
| 134 |
expect(onSelect).toHaveBeenCalledTimes(1); |
| 135 |
expect(onSelect.mock.results[0].value).toMatchObject({ rowId: 0, selected: true }); |
| 136 |
}); |
| 137 |
|
| 138 |
test('row select (radio) - clicking already selected radio should not trigger onSelect', async () => { |
| 139 |
const onSelect = jest.fn((_event, selected, rowId) => ({ selected, rowId })); |
| 140 |
const column = { |
| 141 |
extraParams: { onSelect, selectVariant: 'radio' } |
| 142 |
}; |
| 143 |
const returnedData = selectable('', { column, rowIndex: 0, rowData: { selected: true } } as IExtra); |
| 144 |
expect(returnedData).toMatchObject({ className: tableStyles.tableCheck }); |
| 145 |
|
| 146 |
const user = userEvent.setup(); |
| 147 |
render(returnedData.children as React.ReactElement<any>); |
| 148 |
|
| 149 |
// Click the row radio button (should be checked initially, clicking should not trigger change) |
| 150 |
await user.click(screen.getByRole('radio')); |
| 151 |
expect(onSelect).toHaveBeenCalledTimes(0); |
| 152 |
}); |
| 153 |
|
| 154 |
test('row select (radio) - should toggle from unselected to selected', async () => { |
| 155 |
const onSelect = jest.fn((_event, selected, rowId) => ({ selected, rowId })); |
| 156 |
const column = { |
| 157 |
extraParams: { onSelect, selectVariant: 'radio' } |
| 158 |
}; |
| 159 |
const returnedData = selectable('', { column, rowIndex: 0, rowData: { selected: false } } as IExtra); |
| 160 |
expect(returnedData).toMatchObject({ className: tableStyles.tableCheck }); |
| 161 |
|
| 162 |
const user = userEvent.setup(); |
| 163 |
render(returnedData.children as React.ReactElement<any>); |
| 164 |
|
| 165 |
// Click the row radio button (should be unchecked initially, clicking should check) |
| 166 |
await user.click(screen.getByRole('radio')); |
| 167 |
expect(onSelect).toHaveBeenCalledTimes(1); |
| 168 |
expect(onSelect.mock.results[0].value).toMatchObject({ rowId: 0, selected: true }); |
| 169 |
}); |
| 170 |
}); |
| 171 |
|
| 172 |
describe('sortable', () => { |
| 173 |
test('unsorted', async () => { |
| 174 |
const onSort = jest.fn(); |
| 175 |
const column = { extraParams: { sortBy: {}, onSort } }; |
| 176 |
const returnedData = sortable('', { column, columnIndex: 0 }); |
| 177 |
expect(returnedData).toMatchObject({ className: tableStyles.tableSort }); |
| 178 |
|
| 179 |
const user = userEvent.setup(); |
| 180 |
|
| 181 |
render(returnedData.children as React.ReactElement<any>); |
| 182 |
await user.click(screen.getByRole('button')); |
| 183 |
expect(onSort.mock.calls).toHaveLength(1); |
| 184 |
}); |
| 185 |
|
| 186 |
test('asc', async () => { |
| 187 |
const onSort = jest.fn(); |
| 188 |
const column = { extraParams: { sortBy: { index: 0, direction: 'asc' }, onSort } }; |
| 189 |
const returnedData = sortable('', { column, columnIndex: 0 } as IExtra); |
| 190 |
expect(returnedData).toMatchSnapshot(); |
| 191 |
|
| 192 |
const user = userEvent.setup(); |
| 193 |
|
| 194 |
render(returnedData.children as React.ReactElement<any>); |
| 195 |
await user.click(screen.getByRole('button')); |
| 196 |
expect(onSort.mock.calls).toHaveLength(1); |
| 197 |
}); |
| 198 |
|
| 199 |
test('desc', async () => { |
| 200 |
const onSort = jest.fn(); |
| 201 |
const column = { extraParams: { sortBy: { index: 0, direction: 'desc' }, onSort } }; |
| 202 |
const returnedData = sortable('', { column, columnIndex: 0 } as IExtra); |
| 203 |
expect(returnedData).toMatchObject({ className: css(tableStyles.tableSort, tableStyles.modifiers.selected) }); |
| 204 |
|
| 205 |
const user = userEvent.setup(); |
| 206 |
|
| 207 |
render(returnedData.children as React.ReactElement<any>); |
| 208 |
await user.click(screen.getByRole('button')); |
| 209 |
expect(onSort.mock.calls).toHaveLength(1); |
| 210 |
}); |
| 211 |
}); |
| 212 |
|
| 213 |
test('simpleCellActions', async () => { |
| 214 |
const actions: IActions = [ |
| 215 |
{ |
| 216 |
title: 'Some', |
| 217 |
onClick: jest.fn() |
| 218 |
} |
| 219 |
]; |
| 220 |
|
| 221 |
const actionConfigs = [ |
| 222 |
{ |
| 223 |
actions: [] as IActions |
| 224 |
}, |
| 225 |
{ |
| 226 |
actions |
| 227 |
}, |
| 228 |
{ |
| 229 |
actionResolver: () => null as (IAction | ISeparator)[] |
| 230 |
}, |
| 231 |
{ |
| 232 |
actionResolver: () => actions as (IAction | ISeparator)[] |
| 233 |
}, |
| 234 |
{ |
| 235 |
actionResolver: () => actions as (IAction | ISeparator)[], |
| 236 |
areActionsDisabled: () => false |
| 237 |
}, |
| 238 |
{ |
| 239 |
actions, |
| 240 |
rowData: { |
| 241 |
disableActions: true |
| 242 |
} as IRowData, |
| 243 |
expectDisabled: true |
| 244 |
}, |
| 245 |
{ |
| 246 |
actionResolver: () => actions, |
| 247 |
areActionsDisabled: () => true, |
| 248 |
expectDisabled: true |
| 249 |
} |
| 250 |
]; |
| 251 |
|
| 252 |
actionConfigs.forEach(testCellActions); |
| 253 |
}); |
| 254 |
|
| 255 |
type widthType = 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 60 | 70 | 80 | 90 | 100; |
| 256 |
|
| 257 |
describe('cellWidth', () => { |
| 258 |
const widths = [10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100]; |
| 259 |
widths.forEach((width) => |
| 260 |
test(`${width}`, () => { |
| 261 |
expect(cellWidth(width as widthType)()).toEqual({ className: `pf-m-width-${width}` }); |
| 262 |
}) |
| 263 |
); |
| 264 |
}); |
| 265 |
|
| 266 |
test('collapsible', async () => { |
| 267 |
const onCollapse = jest.fn(); |
| 268 |
const rowData = { |
| 269 |
isOpen: true |
| 270 |
}; |
| 271 |
const column = { |
| 272 |
extraParams: { onCollapse } |
| 273 |
}; |
| 274 |
const returnedData = collapsible('', { rowIndex: 0, rowData, column }); |
| 275 |
expect(returnedData).toMatchObject({ className: tableStyles.tableToggle }); |
| 276 |
|
| 277 |
const user = userEvent.setup(); |
| 278 |
|
| 279 |
render(returnedData.children as React.ReactElement<any>); |
| 280 |
await user.click(screen.getByRole('button')); |
| 281 |
expect(onCollapse.mock.calls).toHaveLength(1); |
| 282 |
}); |
| 283 |
|
| 284 |
test('collapsible full width', () => { |
| 285 |
const onCollapse = jest.fn(); |
| 286 |
const rowData = { |
| 287 |
fullWidth: true |
| 288 |
}; |
| 289 |
const column = { |
| 290 |
extraParams: { onCollapse } |
| 291 |
}; |
| 292 |
const returnedData = collapsible('', { rowIndex: 0, rowData, column }); |
| 293 |
expect(returnedData).toMatchObject({ className: false, isVisible: false }); |
| 294 |
}); |
| 295 |
|
| 296 |
describe('expandable', () => { |
| 297 |
test('with parent', () => { |
| 298 |
const returned = expandable('test', { |
| 299 |
rowIndex: 2, |
| 300 |
rowData: { parent: 1 }, |
| 301 |
column: { extraParams: {} } |
| 302 |
} as IExtra); |
| 303 |
render(returned as React.ReactElement<any>); |
| 304 |
expect(screen.getByText('test').parentElement).toBeInTheDocument(); |
| 305 |
}); |
| 306 |
|
| 307 |
test('no parent', () => { |
| 308 |
expect(expandable('test', { rowData: {}, column: { extraParams: {} } })).toBe('test'); |
| 309 |
}); |
| 310 |
}); |
| 311 |
|
| 312 |
describe('expandedRow', () => { |
| 313 |
test('with parent', () => { |
| 314 |
const returned = expandedRow(5)( |
| 315 |
{ title: 'test' }, |
| 316 |
{ rowIndex: 2, rowData: { parent: 1 }, column: { extraParams: {} } } |
| 317 |
); |
| 318 |
expect(returned).toMatchObject({ colSpan: 5, id: 'expanded-content2' }); |
| 319 |
}); |
| 320 |
|
| 321 |
test('no parent', () => { |
| 322 |
expect(expandedRow(5)({ title: 'test' }, { rowData: {}, column: { extraParams: {} } })).toBe(false); |
| 323 |
}); |
| 324 |
|
| 325 |
test('full width', () => { |
| 326 |
const returned = expandedRow(5)( |
| 327 |
{ title: 'test' }, |
| 328 |
{ rowIndex: 2, rowData: { parent: 1, fullWidth: true }, column: { extraParams: {} } } |
| 329 |
); |
| 330 |
expect(returned).toMatchObject({ colSpan: 6, id: 'expanded-content2' }); |
| 331 |
}); |
| 332 |
|
| 333 |
test('no padding', () => { |
| 334 |
const returned = expandedRow(5)( |
| 335 |
{ title: 'test' }, |
| 336 |
{ rowIndex: 2, rowData: { parent: 1, noPadding: true }, column: { extraParams: {} } } |
| 337 |
); |
| 338 |
expect(returned).toMatchObject({ colSpan: 5, id: 'expanded-content2', className: 'pf-m-no-padding' }); |
| 339 |
}); |
| 340 |
}); |
| 341 |
|
| 342 |
test('scopeColTransformer', () => { |
| 343 |
expect(scopeColTransformer()).toEqual({ scope: 'col' }); |
| 344 |
}); |
| 345 |
|
| 346 |
test('headerCol', () => { |
| 347 |
const returned = headerCol('some-id')('value', { rowIndex: 0 }); |
| 348 |
expect(returned).toMatchObject({ component: 'th' }); |
| 349 |
|
| 350 |
render(returned.children as React.ReactElement<any>); |
| 351 |
expect(screen.getByText('value')).toBeInTheDocument(); |
| 352 |
}); |
| 353 |
|
| 354 |
test('emptyCol', () => { |
| 355 |
expect(emptyCol('')).toEqual({ scope: '' }); |
| 356 |
expect(emptyCol('some')).toEqual({}); |
| 357 |
}); |
| 358 |
|
| 359 |
test('mapProps', () => { |
| 360 |
const rowData = { |
| 361 |
some: { props: { one: 1 } } |
| 362 |
}; |
| 363 |
expect(mapProps(undefined, { property: 'some', rowData })).toEqual({ one: 1 }); |
| 364 |
expect(mapProps(undefined, { property: 'wrong', rowData })).toEqual({}); |
| 365 |
}); |
| 366 |
|
| 367 |
test('textCenter', () => { |
| 368 |
expect(textCenter()).toEqual({ textCenter: true }); |
| 369 |
}); |
| 370 |
|
| 371 |
test('wrappable', () => { |
| 372 |
expect(wrappable()).toEqual({ className: 'pf-m-wrap' }); |
| 373 |
}); |
| 374 |
|
| 375 |
test('editable', () => { |
| 376 |
const onRowEdit = jest.fn(); |
| 377 |
const column = { |
| 378 |
extraParams: { onRowEdit } |
| 379 |
}; |
| 380 |
const returned = editable('test', { rowIndex: 0, column }); |
| 381 |
expect(returned).toMatchObject({ className: tableStyles.tableInlineEditAction }); |
| 382 |
}); |
| 383 |
|
| 384 |
describe('visibility classNames', () => { |
| 385 |
Object.keys(Visibility).forEach((className) => { |
| 386 |
test(`${className} is defined`, () => { |
| 387 |
expect(Visibility[className]).not.toBe(undefined); |
| 388 |
}); |
| 389 |
}); |
| 390 |
}); |
| 391 |
}); |