Skip to content

Commit 9a68b63

Browse files
holmanszehoiwinghkodiakhq[bot]
authored
fix(DataTable): guard npe when cells being sorted do not exist (#9714)
* 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>
1 parent 49e5a26 commit 9a68b63

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

packages/react/src/components/DataTable/tools/__tests__/sorting-test.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,56 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { defaultSortRow } from '../sorting';
8+
import { sortRows, defaultSortRow } from '../sorting';
99
import { sortStates } from '../../state/sorting';
1010

11+
describe('sortRow', () => {
12+
const rowIds = ['row2', 'row1'];
13+
const cellsById = {
14+
'row1:header1': { value: 'cell11' },
15+
'row2:header1': { value: 'cell21' },
16+
};
17+
18+
it('should sort data in ascending order', () => {
19+
expect(
20+
sortRows({
21+
rowIds,
22+
cellsById,
23+
sortDirection: sortStates.ASC,
24+
key: 'header1',
25+
locale: 'en',
26+
sortRow: defaultSortRow,
27+
})
28+
).toEqual(['row1', 'row2']);
29+
});
30+
31+
it('should sort data in descending order', () => {
32+
expect(
33+
sortRows({
34+
rowIds,
35+
cellsById,
36+
sortDirection: sortStates.DESC,
37+
key: 'header1',
38+
locale: 'en',
39+
sortRow: defaultSortRow,
40+
})
41+
).toEqual(['row2', 'row1']);
42+
});
43+
44+
it('should return unsorted data if cells not found', () => {
45+
expect(
46+
sortRows({
47+
rowIds,
48+
cellsById,
49+
sortDirection: sortStates.ASC,
50+
key: 'header2',
51+
locale: 'en',
52+
sortRow: defaultSortRow,
53+
})
54+
).toEqual(['row2', 'row1']);
55+
});
56+
});
57+
1158
describe('defaultSortRow', () => {
1259
it('should sort data in ascending order', () => {
1360
const sortProps = {

packages/react/src/components/DataTable/tools/sorting.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const sortRows = ({
8080
rowIds.slice().sort((a, b) => {
8181
const cellA = cellsById[getCellId(a, key)];
8282
const cellB = cellsById[getCellId(b, key)];
83-
return sortRow(cellA.value, cellB.value, {
83+
return sortRow(cellA && cellA.value, cellB && cellB.value, {
8484
key,
8585
sortDirection,
8686
locale,

0 commit comments

Comments
 (0)