Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 8849e85

Browse files
author
Marc-André Rivet
committed
unit test table shouldComponentUpdate condition
1 parent a30121e commit 8849e85

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/dash-table/components/Table/index.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ import 'react-select/dist/react-select.css';
2020
import './Table.less';
2121
import './style';
2222
import './Dropdown.css';
23-
import { isEqual } from 'core/comparer';
2423
import { SingleColumnSyntaxTree } from 'dash-table/syntax-tree';
2524
import derivedFilterMap from 'dash-table/derived/filter/map';
2625

2726
import controlledPropsHelper from './controlledPropsHelper';
2827
import derivedPropsHelper from './derivedPropsHelper';
2928
import DOM from 'core/browser/DOM';
30-
31-
const DERIVED_REGEX = /^derived_/;
29+
import shouldComponentUpdate from './shouldComponentUpdate';
3230

3331
export default class Table extends Component<SanitizedAndDerivedProps, StandaloneState> {
3432
constructor(props: SanitizedAndDerivedProps) {
@@ -94,10 +92,7 @@ export default class Table extends Component<SanitizedAndDerivedProps, Standalon
9492
const props: any = this.props;
9593
const state: any = this.state;
9694

97-
return R.any(key =>
98-
!DERIVED_REGEX.test(key) && props[key] !== nextProps[key],
99-
R.keysIn({ ...props, ...nextProps })
100-
) || !isEqual(state, nextState);
95+
return shouldComponentUpdate(props, nextProps, state, nextState);
10196
}
10297

10398
render() {
@@ -159,3 +154,4 @@ export default class Table extends Component<SanitizedAndDerivedProps, Standalon
159154
private readonly controlledPropsHelper = controlledPropsHelper();
160155
private readonly updateDerivedProps = derivedPropsHelper();
161156
}
157+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as R from 'ramda';
2+
import { isEqual } from 'core/comparer';
3+
4+
const DERIVED_REGEX = /^derived_/;
5+
6+
export default (
7+
props: any,
8+
nextProps: any,
9+
state: any,
10+
nextState: any
11+
) => R.any(
12+
key => !DERIVED_REGEX.test(key) && props[key] !== nextProps[key],
13+
R.keysIn({ ...props, ...nextProps })
14+
) || !isEqual(state, nextState);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import shouldComponentUpdate from 'dash-table/components/Table/shouldComponentUpdate';
2+
3+
describe('shouldComponentUpdate', () => {
4+
it('should update on undefined -> defined props', () => {
5+
assert(shouldComponentUpdate({}, { a: 0 }, {}, {}));
6+
});
7+
8+
it('should update on undefined -> defined state', () => {
9+
assert(shouldComponentUpdate({}, {}, {}, { a: 0 }));
10+
});
11+
12+
it('should update on defined -> undefined props', () => {
13+
assert(shouldComponentUpdate({ a: 0 }, {}, {}, {}));
14+
});
15+
16+
it('should update on defined -> undefined state', () => {
17+
assert(shouldComponentUpdate({}, {}, { a: 0 }, {}));
18+
});
19+
20+
it('should not update on derived props', () => {
21+
assert(!shouldComponentUpdate({ derived_test: 0 }, { derived_test: 1 }, {}, {}));
22+
});
23+
24+
it('should update on derived state', () => {
25+
assert(shouldComponentUpdate({}, {}, { derived_test: 0 }, { derived_test: 1 }));
26+
});
27+
});

0 commit comments

Comments
 (0)