Skip to content

Commit

Permalink
fix(DataTable): guard npe when cells being sorted do not exist (#9714)
Browse files Browse the repository at this point in the history
* fix(DataTable): guard npe when cells being sorted do not exist

* fix(DataTable): add unit tests

Co-authored-by: Holman Sze <hoiwingh@ca.ibm.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 22, 2021
1 parent 49e5a26 commit 9a68b63
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,56 @@
* LICENSE file in the root directory of this source tree.
*/

import { defaultSortRow } from '../sorting';
import { sortRows, defaultSortRow } from '../sorting';
import { sortStates } from '../../state/sorting';

describe('sortRow', () => {
const rowIds = ['row2', 'row1'];
const cellsById = {
'row1:header1': { value: 'cell11' },
'row2:header1': { value: 'cell21' },
};

it('should sort data in ascending order', () => {
expect(
sortRows({
rowIds,
cellsById,
sortDirection: sortStates.ASC,
key: 'header1',
locale: 'en',
sortRow: defaultSortRow,
})
).toEqual(['row1', 'row2']);
});

it('should sort data in descending order', () => {
expect(
sortRows({
rowIds,
cellsById,
sortDirection: sortStates.DESC,
key: 'header1',
locale: 'en',
sortRow: defaultSortRow,
})
).toEqual(['row2', 'row1']);
});

it('should return unsorted data if cells not found', () => {
expect(
sortRows({
rowIds,
cellsById,
sortDirection: sortStates.ASC,
key: 'header2',
locale: 'en',
sortRow: defaultSortRow,
})
).toEqual(['row2', 'row1']);
});
});

describe('defaultSortRow', () => {
it('should sort data in ascending order', () => {
const sortProps = {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/DataTable/tools/sorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const sortRows = ({
rowIds.slice().sort((a, b) => {
const cellA = cellsById[getCellId(a, key)];
const cellB = cellsById[getCellId(b, key)];
return sortRow(cellA.value, cellB.value, {
return sortRow(cellA && cellA.value, cellB && cellB.value, {
key,
sortDirection,
locale,
Expand Down

0 comments on commit 9a68b63

Please sign in to comment.