Skip to content

Commit 0c45f89

Browse files
tdeekensmontezume
authored andcommitted
fix(base-table): onSortChange being called without being passed (#827)
1 parent f140d4d commit 0c45f89

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

src/components/table/base-table/base-table.js

+2
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ export default class BaseTable extends React.Component {
196196
// from the row index before passing it to the parent
197197
getBodyRowIndex = rowIndex => rowIndex - 1;
198198
handleChangeSortDirection = columnKey => {
199+
if (!this.props.onSortChange) return;
200+
199201
if (columnKey !== this.props.sortBy) {
200202
this.props.onSortChange(columnKey, 'ASC');
201203
} else {

src/components/table/base-table/base-table.spec.js

+45-26
Original file line numberDiff line numberDiff line change
@@ -717,44 +717,63 @@ describe('BaseTable', () => {
717717
let props;
718718
let wrapper;
719719
describe('handleChangeSortDirection', () => {
720-
beforeEach(() => {
721-
props = createTestProps({
722-
onSortChange: jest.fn(),
723-
sortBy: 'id',
724-
sortDirection: 'ASC',
725-
});
726-
wrapper = shallow(<BaseTable {...props} />);
727-
});
728-
describe('when previous direction was ASC', () => {
729-
beforeEach(() => {
730-
wrapper.instance().handleChangeSortDirection('id');
731-
});
732-
it('should call onSortChange with direction DESC', () => {
733-
expect(props.onSortChange).toHaveBeenCalledWith('id', 'DESC');
734-
});
735-
});
736-
describe('when previous direction was DESC', () => {
720+
describe('with `onSortChange`', () => {
737721
beforeEach(() => {
738722
props = createTestProps({
739723
onSortChange: jest.fn(),
740724
sortBy: 'id',
741-
sortDirection: 'DESC',
725+
sortDirection: 'ASC',
742726
});
743727
wrapper = shallow(<BaseTable {...props} />);
744728
});
745-
beforeEach(() => {
746-
wrapper.instance().handleChangeSortDirection('id');
729+
describe('when previous direction was ASC', () => {
730+
beforeEach(() => {
731+
wrapper.instance().handleChangeSortDirection('id');
732+
});
733+
it('should call onSortChange with direction DESC', () => {
734+
expect(props.onSortChange).toHaveBeenCalledWith('id', 'DESC');
735+
});
736+
});
737+
describe('when previous direction was DESC', () => {
738+
beforeEach(() => {
739+
props = createTestProps({
740+
onSortChange: jest.fn(),
741+
sortBy: 'id',
742+
sortDirection: 'DESC',
743+
});
744+
wrapper = shallow(<BaseTable {...props} />);
745+
});
746+
beforeEach(() => {
747+
wrapper.instance().handleChangeSortDirection('id');
748+
});
749+
it('should call onSortChange with direction ASC', () => {
750+
expect(props.onSortChange).toHaveBeenCalledWith('id', 'ASC');
751+
});
747752
});
748-
it('should call onSortChange with direction ASC', () => {
749-
expect(props.onSortChange).toHaveBeenCalledWith('id', 'ASC');
753+
describe('when the sorted column changes', () => {
754+
beforeEach(() => {
755+
wrapper.instance().handleChangeSortDirection('foo');
756+
});
757+
it('should call onSortChange with direction ASC', () => {
758+
expect(props.onSortChange).toHaveBeenCalledWith('foo', 'ASC');
759+
});
750760
});
751761
});
752-
describe('when the sorted column changes', () => {
762+
763+
describe('without `onSortChange`', () => {
753764
beforeEach(() => {
754-
wrapper.instance().handleChangeSortDirection('foo');
765+
props = createTestProps({
766+
onSortChange: null,
767+
sortBy: 'id',
768+
sortDirection: 'ASC',
769+
});
770+
wrapper = shallow(<BaseTable {...props} />);
771+
772+
wrapper.instance().handleChangeSortDirection('id');
755773
});
756-
it('should call onSortChange with direction ASC', () => {
757-
expect(props.onSortChange).toHaveBeenCalledWith('foo', 'ASC');
774+
775+
it('should not call onSortChange', () => {
776+
expect(props.onSortChange).toBe(null);
758777
});
759778
});
760779
});

0 commit comments

Comments
 (0)