Skip to content

Commit

Permalink
Fix table selection (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeandroTorresSicilia authored Apr 24, 2019
1 parent 9b9ceae commit babf4c8
Show file tree
Hide file tree
Showing 16 changed files with 249 additions and 67 deletions.
7 changes: 7 additions & 0 deletions integration/specs/Table/table-5.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('Table with selection', () => {
});
it('should select and then deselect a row', () => {
const table = new PageTable(TABLE);
table.waitUntilDataIsLoaded();
const row5 = table.getRow(4);
expect(row5.isRowSelected()).toBe(false);
row5.selectRow();
Expand All @@ -23,6 +24,7 @@ describe('Table with selection', () => {
});
it('should select all rows', () => {
const table = new PageTable(TABLE);
table.waitUntilDataIsLoaded();
const row1 = table.getRow(0);
const row2 = table.getRow(1);
const row3 = table.getRow(2);
Expand All @@ -48,6 +50,7 @@ describe('Table with selection', () => {
});
it('should deselect all rows', () => {
const table = new PageTable(TABLE);
table.waitUntilDataIsLoaded();
const row1 = table.getRow(0);
const row2 = table.getRow(1);
const row3 = table.getRow(2);
Expand All @@ -67,6 +70,7 @@ describe('Table with selection', () => {
});
it('should select the second and fifth row', () => {
const table = new PageTable(TABLE);
table.waitUntilDataIsLoaded();
const row1 = table.getRow(0);
const row2 = table.getRow(1);
const row3 = table.getRow(2);
Expand All @@ -86,6 +90,7 @@ describe('Table with selection', () => {
});
it('should deselect all rows when the second and fifth row are selected and then deselect all rows', () => {
const table = new PageTable(TABLE);
table.waitUntilDataIsLoaded();
const row1 = table.getRow(0);
const row2 = table.getRow(1);
const row3 = table.getRow(2);
Expand All @@ -106,6 +111,7 @@ describe('Table with selection', () => {
});
it('should select all rows between selected rows while press shift key', () => {
const table = new PageTable(TABLE);
table.waitUntilDataIsLoaded();
const row1 = table.getRow(0);
const row2 = table.getRow(1);
const row3 = table.getRow(2);
Expand All @@ -127,6 +133,7 @@ describe('Table with selection', () => {
});
it('should deselect all rows between the two rows deselected while press shift key', () => {
const table = new PageTable(TABLE);
table.waitUntilDataIsLoaded();
const row1 = table.getRow(0);
const row2 = table.getRow(1);
const row3 = table.getRow(2);
Expand Down
4 changes: 2 additions & 2 deletions src/components/Modal/pageObject/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ class PageModal {
}

/**
* Waiting until the open modal transition has finished.
* Wait until the open modal transition has finished.
* @method
*/
waitUntilOpen() {
browser.waitUntil(() => this.isOpen());
}

/**
* Waiting until the close modal transition has finished.
* Wait until the close modal transition has finished.
* @method
*/
waitUntilClose() {
Expand Down
62 changes: 62 additions & 0 deletions src/components/Table/__test__/table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,4 +853,66 @@ describe('<Table />', () => {
}]);
expect(component.instance().lastSelectedRowKey).toBe('1234qwerty');
});
it('should set the right indexes when data prop changes', () => {
const component = mount(
<Table
data={[]}
showCheckboxColumn
keyField="id">

<Column field="name" header="Name" />
</Table>,
);
component.setProps({
data: tableData,
});
expect(component.instance().indexes).toEqual({
'1234qwerty': { rowIndex: 0 },
'5678asdfgh': { rowIndex: 1 },
'9012zxcvbn': { rowIndex: 2 },
});
});
it('should set the right state when data prop changes', () => {
const component = mount(
<Table
data={[]}
showCheckboxColumn
keyField="id">

<Column field="name" header="Name" />
</Table>,
);
component.setProps({
data: tableData,
});
const { state } = component.instance();
expect(state.rows).toEqual([
{ inputType: 'checkbox', isDisabled: false, isSelected: false, key: '1234qwerty' },
{ inputType: 'checkbox', isDisabled: false, isSelected: false, key: '5678asdfgh' },
{ inputType: 'checkbox', isDisabled: false, isSelected: false, key: '9012zxcvbn' },
]);
expect(state.bulkSelection).toBe('none');
});
it('should set the right state when data prop changes and have selected rows', () => {
const component = mount(
<Table
data={[]}
showCheckboxColumn
selectedRows={['5678asdfgh']}
keyField="id">

<Column field="name" header="Name" />
</Table>,
);
component.setProps({
data: tableData,
});
const { state } = component.instance();
expect(state.rows).toEqual([
{ inputType: 'checkbox', isDisabled: false, isSelected: false, key: '1234qwerty' },
{ inputType: 'checkbox', isDisabled: false, isSelected: true, key: '5678asdfgh' },
{ inputType: 'checkbox', isDisabled: false, isSelected: false, key: '9012zxcvbn' },
]);
expect(state.bulkSelection).toBe('some');
});
});
14 changes: 11 additions & 3 deletions src/components/Table/body/__test__/body.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@ const rows = [
];

describe('<Body />', () => {
it('should return an empty component if data and columns are not passed and is not loading', () => {
it('should return an empty component when data and columns are not passed and is not loading', () => {
const component = mount(<Body isLoading={false} />);
expect(component.find('Empty').exists()).toBe(true);
});
it('should return an empty component if data or columns are not arrays and is not loading', () => {
const component = mount(<Body columns={{}} data={[]} isLoading={false} />);
it('should return an empty component when there is not data and is not loading', () => {
const component = mount(<Body columns={[{}]} data={[]} isLoading={false} />);
expect(component.find('Empty').exists()).toBe(true);
});
it('should return a loading component when there is not data and is loading', () => {
const component = mount(<Body columns={[{}]} data={[]} isLoading />);
expect(component.find('Loading').exists()).toBe(true);
});
it('should return one row more when there is data and is loading', () => {
const component = mount(<Body columns={columns} data={data} isLoading />);
expect(component.children().length).toBe(3);
});
it('should return an array of Row components', () => {
const component = mount(<Body data={data} columns={columns} rows={rows} />);
const rowElements = component.find('Row');
Expand Down
18 changes: 18 additions & 0 deletions src/components/Table/body/__test__/loadingCells.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import { mount } from 'enzyme';
import LoadingCells from '../loadingCells';

describe('<LoadingCells />', () => {
it('should not render anything when value is 0', () => {
const component = mount(
<LoadingCells value={0} />,
);
expect(component.children().length).toBe(0);
});
it('should render the amount of children that match with the value passed', () => {
const component = mount(
<LoadingCells value={3} />,
);
expect(component.children().length).toBe(3);
});
});
7 changes: 7 additions & 0 deletions src/components/Table/body/__test__/row.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,11 @@ describe('<Row />', () => {
expect(cell.at(1).prop('isFirst')).toBe(true);
expect(cell.at(2).prop('isFirst')).toBe(false);
});
it('should render LoadingCells component when data type is LOADING', () => {
const rowData = { type: 'LOADING' };
const component = mount(
<Row data={rowData} columns={columns} />,
);
expect(component.find('LoadingCells').exists()).toBe(true);
});
});
36 changes: 16 additions & 20 deletions src/components/Table/body/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export default class Body extends PureComponent {
emptyTitle,
emptyDescription,
} = this.props;
const hasData = Array.isArray(data) && data.length > 0;
const isEmpty = data.length === 0;
const columnsLength = columns.length;

Expand All @@ -50,34 +49,31 @@ export default class Body extends PureComponent {
);
}

if (hasData && Array.isArray(columns)) {
const newData = getData(data, isLoading);
return newData.map((item, index) => {
const row = rows[index];
const rowKeyValue = rows[index] && rows[index].key;
const key = rowKeyValue || `row-${index + 1}`;
const newData = getData(data, isLoading);
return newData.map((item, index) => {
const row = rows[index];
const rowKeyValue = rows[index] && rows[index].key;
const key = rowKeyValue || `row-${index + 1}`;

return (
<Row
{...row}
key={key}
data={item}
columns={columns}
tableId={tableId}
onSelectRow={(event, isMultiple) => onSelectRow(
return (
<Row
{...row}
key={key}
data={item}
columns={columns}
tableId={tableId}
onSelectRow={(event, isMultiple) => onSelectRow(
event,
isMultiple,
rowKeyValue,
)}
onDeselectRow={(event, isMultiple) => onDeselectRow(
onDeselectRow={(event, isMultiple) => onDeselectRow(
event,
isMultiple,
rowKeyValue,
)} />
);
});
}
return null;
);
});
}
}

Expand Down
39 changes: 26 additions & 13 deletions src/components/Table/body/loadingCells.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import PropTypes from 'prop-types';

const widths = ['40%', '60%', '70%', '85%', '95%'];

Expand All @@ -7,18 +8,30 @@ function getRandomWidth() {
}

export default function LoadingCells({ value }) {
return Array(value).fill().map((item, index) => {
const key = `loading-cell-${index}`;
const styles = {
width: getRandomWidth(),
};
if (value > 0) {
return Array(value).fill().map((item, index) => {
const key = `loading-cell-${index}`;
const styles = {
width: getRandomWidth(),
};

return (
<td key={key}>
<div className="rainbow-table_body--loading">
<div className="rainbow-table_body-element--loading" style={styles} />
</div>
</td>
);
});
return (
<td key={key}>
<div className="rainbow-table_body--loading">
<div className="rainbow-table_body-element--loading" style={styles} />
</div>
</td>
);
});
}
return null;
}

LoadingCells.propTypes = {
value: PropTypes.number,
};

LoadingCells.defaultProps = {
value: 0,
};

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ describe('getColumns', () => {
const showCheckboxColumn = false;
expect(getColumns(columns, showCheckboxColumn)).toBeNull();
});
it('should return an empty array when columns is not passed', () => {
const columns = undefined;
const showCheckboxColumn = false;
expect(getColumns(columns, showCheckboxColumn)).toEqual([]);
});
it('should return an empty array when columns is an array with falsy values and showCheckboxColumn is false', () => {
const columns = [null, undefined];
const showCheckboxColumn = false;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Table/helpers/columns/getColumns.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { SELECTABLE_CHECKBOX } from './';

export default function getColumns(columns, showCheckboxColumn) {
export default function getColumns(columns = [], showCheckboxColumn) {
const columnsData = React.Children.map(columns, (column) => {
if (column) {
return column.props;
Expand Down
14 changes: 14 additions & 0 deletions src/components/Table/helpers/data/__test__/normalizeData.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { normalizeData } from '../';

describe('normalizeData', () => {
it('should return the same data passed when it is an empty array', () => {
expect(normalizeData([])).toEqual([]);
});
it('should return the same data passed when it is an array', () => {
expect(normalizeData(['data-1', 'data-2'])).toEqual(['data-1', 'data-2']);
});
it('should return an empty array when data passed is not an array', () => {
const values = [null, undefined, {}, 'asd', 123, () => {}];
values.forEach(value => expect(normalizeData(value)).toEqual([]));
});
});
8 changes: 8 additions & 0 deletions src/components/Table/helpers/data/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable import/prefer-default-export */

export function normalizeData(data) {
if (Array.isArray(data)) {
return data;
}
return [];
}
Loading

0 comments on commit babf4c8

Please sign in to comment.