Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SNOOP-388] Update UncontrolledTable to use case insensitive sorting #1203

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/Table/UncontrolledTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ export default class UncontrolledTable extends React.Component {
};
}

sortedData = (rows, column, ascending) => orderBy(rows, [column], [ascending ? 'asc' : 'desc']);
sortedData = (rows, column, ascending) =>
orderBy(
rows,
(row) => (typeof row[column] === 'string' ? row[column].toLowerCase() : row[column]),
[ascending ? 'asc' : 'desc']
);

sortBy = (column, ascending) => {
let sort;
Expand Down
65 changes: 49 additions & 16 deletions src/components/Table/UncontrolledTable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,42 +366,75 @@ describe('<UncontrolledTable />', () => {
});

it('should show correct rows on sort change', () => {
const columns = [{ header: 'Name', key: 'name', cell: (row) => row }];
const rows = [{ name: 'Alpha' }, { name: 'Bravo' }, { name: 'Charlie' }];
const columns = [
{ header: 'Name', key: 'name', cell: (row) => row },
{ header: 'Age', key: 'age', cell: (row) => row },
];
const rows = [
{ name: 'Alpha', age: 35 },
{ name: 'Bravo', age: 30 },
{ name: 'Charlie', age: 25 },
{ name: 'amanda', age: 50 },
{ name: '2 Chainz', age: 40 },
];
const wrapper = shallow(
<UncontrolledTable
columns={columns}
rows={rows}
sort={{ column: 'name', ascending: false }}
paginated
pageSize={4}
/>
);

assert.deepStrictEqual(wrapper.find(SortableTable).prop('rows'), [
{ name: 'Charlie' },
{ name: 'Bravo' },
{ name: 'Alpha' },
{ name: 'Charlie', age: 25 },
{ name: 'Bravo', age: 30 },
{ name: 'amanda', age: 50 },
{ name: 'Alpha', age: 35 },
{ name: '2 Chainz', age: 40 },
]);

wrapper.find(SortableTable).prop('columns')[0].onSort(true); // Simulate sort by ascending order
wrapper.find(SortableTable).prop('columns')[0].onSort(true); // Simulate sort by ascending order by name (string)
wrapper.update();
assert.deepStrictEqual(wrapper.find(SortableTable).prop('rows'), [
{ name: 'Alpha' },
{ name: 'Bravo' },
{ name: 'Charlie' },
{ name: '2 Chainz', age: 40 },
{ name: 'Alpha', age: 35 },
{ name: 'amanda', age: 50 },
{ name: 'Bravo', age: 30 },
{ name: 'Charlie', age: 25 },
]);

wrapper.find(SortableTable).prop('columns')[0].onSort(false); // Simulate sort by descending order
wrapper.find(SortableTable).prop('columns')[0].onSort(false); // Simulate sort by descending order by name (string)
wrapper.update();
assert.deepStrictEqual(wrapper.find(SortableTable).prop('rows'), [
{ name: 'Charlie' },
{ name: 'Bravo' },
{ name: 'Alpha' },
{ name: 'Charlie', age: 25 },
{ name: 'Bravo', age: 30 },
{ name: 'amanda', age: 50 },
{ name: 'Alpha', age: 35 },
{ name: '2 Chainz', age: 40 },
]);

wrapper.find(SortableTable).prop('columns')[1].onSort(true); // Simulate sort by ascending order by age (integer)
wrapper.update();
assert.deepStrictEqual(wrapper.find(SortableTable).prop('rows'), [
{ name: 'Charlie', age: 25 },
{ name: 'Bravo', age: 30 },
{ name: 'Alpha', age: 35 },
{ name: '2 Chainz', age: 40 },
{ name: 'amanda', age: 50 },
]);

wrapper.find(SortableTable).prop('columns')[1].onSort(false); // Simulate sort by descending order by age (integer)
wrapper.update();
assert.deepStrictEqual(wrapper.find(SortableTable).prop('rows'), [
{ name: 'amanda', age: 50 },
{ name: '2 Chainz', age: 40 },
{ name: 'Alpha', age: 35 },
{ name: 'Bravo', age: 30 },
{ name: 'Charlie', age: 25 },
]);
});

it('should show correct rows on sort change', () => {
it('should call onSort with correct props on sort change', () => {
const columns = [
{ header: 'Name', key: 'name', cell: (row) => row },
{ header: 'Age', key: 'age', cell: (row) => row },
Expand Down
Loading