Skip to content

Commit c096ff5

Browse files
Add tests for fixedFirstColumns prop in DataTable
1 parent 3b38fff commit c096ff5

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

polaris-react/src/components/DataTable/DataTable.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,15 @@ class DataTableInner extends PureComponent<CombinedProps, DataTableState> {
405405
}
406406

407407
private fixedFirstColumns() {
408-
const {hasFixedFirstColumn, fixedFirstColumns = 0} = this.props;
409-
return hasFixedFirstColumn && !fixedFirstColumns ? 1 : fixedFirstColumns;
408+
const {hasFixedFirstColumn, fixedFirstColumns = 0, headings} = this.props;
409+
const numberOfFixedFirstColumns =
410+
hasFixedFirstColumn && !fixedFirstColumns ? 1 : fixedFirstColumns;
411+
412+
if (numberOfFixedFirstColumns >= headings.length) {
413+
return 0;
414+
}
415+
416+
return numberOfFixedFirstColumns;
410417
}
411418

412419
private setCellRef = ({

polaris-react/src/components/DataTable/tests/DataTable.test.tsx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,77 @@ describe('<DataTable />', () => {
565565
expect(threeSpanCells).toHaveLength(1);
566566
});
567567
});
568+
569+
describe('fixedFirstColumns', () => {
570+
const headings = ['Product', 'Price', 'Order Number'];
571+
const rows = [
572+
[
573+
'Navy Merino Wool Blazer with khaki chinos and yellow belt',
574+
'$875.00',
575+
124518,
576+
83,
577+
'$122,500.00',
578+
],
579+
];
580+
581+
const columnContentTypes: DataTableProps['columnContentTypes'] = [
582+
'text',
583+
'numeric',
584+
'numeric',
585+
];
586+
587+
const fixedFirstColumnsProps = {
588+
headings,
589+
rows,
590+
columnContentTypes,
591+
};
592+
593+
it('sets fixed first columns', async () => {
594+
const dataTable = mountWithApp(
595+
<DataTable {...fixedFirstColumnsProps} fixedFirstColumns={2} />,
596+
);
597+
598+
const table = dataTable.find('table')!.domNode;
599+
600+
Object.defineProperty(table, 'scrollWidth', {
601+
value: 100,
602+
writable: true,
603+
configurable: true,
604+
});
605+
606+
window.dispatchEvent(new Event('resize'));
607+
timer.runAllTimers();
608+
609+
dataTable.forceUpdate();
610+
611+
const separateTable = dataTable.findAll('table')[0];
612+
const separateTableHeadingGroup = separateTable.find('thead');
613+
const separateTableHeaders = separateTableHeadingGroup?.findAll('th');
614+
615+
expect(separateTableHeaders).toHaveLength(2);
616+
});
617+
618+
it('sets "fixedFirstColumns" to 0 if it exceeds or is equal to columns amount', () => {
619+
const dataTable = mountWithApp(
620+
<DataTable {...fixedFirstColumnsProps} fixedFirstColumns={3} />,
621+
);
622+
623+
const table = dataTable.find('table')!.domNode;
624+
625+
Object.defineProperty(table, 'scrollWidth', {
626+
value: 100,
627+
writable: true,
628+
configurable: true,
629+
});
630+
631+
window.dispatchEvent(new Event('resize'));
632+
timer.runAllTimers();
633+
634+
dataTable.forceUpdate();
635+
636+
const tables = dataTable.findAll('table');
637+
638+
expect(tables).toHaveLength(1);
639+
});
640+
});
568641
});

0 commit comments

Comments
 (0)