Skip to content

Commit e09d8df

Browse files
committed
Add tests for own PureComponent implementation
1 parent 092bbd0 commit e09d8df

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

packages/enzyme-test-suite/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"enzyme": "^3.3.0",
3434
"enzyme-adapter-utils": "^1.5.0",
3535
"jsdom": "^6.5.1",
36+
"lodash.isequal": "^4.5.0",
3637
"mocha-wrap": "^2.1.2",
3738
"object.assign": "^4.1.0",
3839
"object-inspect": "^1.6.0",

packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
44
import { expect } from 'chai';
55
import sinon from 'sinon';
66
import wrap from 'mocha-wrap';
7+
import isEqual from 'lodash.isequal';
78
import {
89
mount,
910
render,
@@ -27,6 +28,7 @@ import {
2728
createRef,
2829
Fragment,
2930
forwardRef,
31+
PureComponent,
3032
} from './_helpers/react-compat';
3133
import {
3234
describeWithDOM,
@@ -41,6 +43,7 @@ import {
4143
} from './_helpers/version';
4244
import realArrowFunction from './_helpers/realArrowFunction';
4345
import sloppyReturnThis from './_helpers/untranspiledSloppyReturnThis';
46+
import { isDeepStrictEqual } from 'util';
4447

4548
const getElementPropSelector = prop => x => x.props[prop];
4649
const getWrapperPropSelector = prop => x => x.prop(prop);
@@ -5130,7 +5133,7 @@ describeWithDOM('mount', () => {
51305133

51315134
describeIf(is('>= 15.3'), 'PureComponent', () => {
51325135
it('should not update when state and props did not change', () => {
5133-
class Foo extends React.PureComponent {
5136+
class Foo extends PureComponent {
51345137
constructor(props) {
51355138
super(props);
51365139
this.state = {
@@ -5162,6 +5165,44 @@ describeWithDOM('mount', () => {
51625165
});
51635166
});
51645167

5168+
describe('Own PureComponent implementation', () => {
5169+
it('should not update when state and props did not change', () => {
5170+
class Foo extends React.Component {
5171+
constructor(props) {
5172+
super(props);
5173+
this.state = {
5174+
foo: 'init',
5175+
};
5176+
}
5177+
5178+
shouldComponentUpdate(nextProps, nextState) {
5179+
return !isEqual(this.props, nextProps) || !isEqual(this.state, nextState);
5180+
}
5181+
5182+
componentDidUpdate() {}
5183+
5184+
render() {
5185+
return (
5186+
<div>
5187+
{this.state.foo}
5188+
</div>
5189+
);
5190+
}
5191+
}
5192+
const spy = sinon.spy(Foo.prototype, 'componentDidUpdate');
5193+
const wrapper = mount(<Foo id={1} />);
5194+
wrapper.setState({ foo: 'update' });
5195+
expect(spy).to.have.property('callCount', 1);
5196+
wrapper.setState({ foo: 'update' });
5197+
expect(spy).to.have.property('callCount', 1);
5198+
5199+
wrapper.setProps({ id: 2 });
5200+
expect(spy).to.have.property('callCount', 2);
5201+
wrapper.setProps({ id: 2 });
5202+
expect(spy).to.have.property('callCount', 2);
5203+
});
5204+
});
5205+
51655206
describeIf(is('>= 16.3'), 'support getSnapshotBeforeUpdate', () => {
51665207
it('should call getSnapshotBeforeUpdate and pass snapshot to componentDidUpdate', () => {
51675208
const spy = sinon.spy();

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33
import { expect } from 'chai';
44
import sinon from 'sinon';
55
import wrap from 'mocha-wrap';
6+
import isEqual from 'lodash.isequal';
67
import {
78
shallow,
89
render,
@@ -27,6 +28,7 @@ import {
2728
createRef,
2829
Fragment,
2930
forwardRef,
31+
PureComponent,
3032
} from './_helpers/react-compat';
3133
import {
3234
describeIf,
@@ -5501,7 +5503,7 @@ describe('shallow', () => {
55015503

55025504
describeIf(is('>= 15.3'), 'PureComponent', () => {
55035505
it('should not update when state and props did not change', () => {
5504-
class Foo extends React.PureComponent {
5506+
class Foo extends PureComponent {
55055507
constructor(props) {
55065508
super(props);
55075509
this.state = {
@@ -5533,6 +5535,44 @@ describe('shallow', () => {
55335535
});
55345536
});
55355537

5538+
describe('Own PureComponent implementation', () => {
5539+
it('should not update when state and props did not change', () => {
5540+
class Foo extends React.Component {
5541+
constructor(props) {
5542+
super(props);
5543+
this.state = {
5544+
foo: 'init',
5545+
};
5546+
}
5547+
5548+
shouldComponentUpdate(nextProps, nextState) {
5549+
return !isEqual(this.props, nextProps) || !isEqual(this.state, nextState);
5550+
}
5551+
5552+
componentDidUpdate() {}
5553+
5554+
render() {
5555+
return (
5556+
<div>
5557+
{this.state.foo}
5558+
</div>
5559+
);
5560+
}
5561+
}
5562+
const spy = sinon.spy(Foo.prototype, 'componentDidUpdate');
5563+
const wrapper = mount(<Foo id={1} />);
5564+
wrapper.setState({ foo: 'update' });
5565+
expect(spy).to.have.property('callCount', 1);
5566+
wrapper.setState({ foo: 'update' });
5567+
expect(spy).to.have.property('callCount', 1);
5568+
5569+
wrapper.setProps({ id: 2 });
5570+
expect(spy).to.have.property('callCount', 2);
5571+
wrapper.setProps({ id: 2 });
5572+
expect(spy).to.have.property('callCount', 2);
5573+
});
5574+
});
5575+
55365576
describeIf(is('>= 16.3'), 'support getSnapshotBeforeUpdate', () => {
55375577
it('should call getSnapshotBeforeUpdate and pass snapshot to componentDidUpdate', () => {
55385578
const spy = sinon.spy();

packages/enzyme-test-suite/test/_helpers/react-compat.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ let Fragment;
1616
let StrictMode;
1717
let AsyncMode;
1818
let Profiler;
19+
let PureComponent;
1920

2021
if (is('>=15.5 || ^16.0.0-alpha || ^16.3.0-alpha')) {
2122
// eslint-disable-next-line import/no-extraneous-dependencies
@@ -37,6 +38,12 @@ if (is('^16.0.0-0 || ^16.3.0-0')) {
3738
createPortal = null;
3839
}
3940

41+
if (is('>=15.3')) {
42+
({ PureComponent } = require('react'));
43+
} else {
44+
PureComponent = null;
45+
}
46+
4047
if (is('^16.2.0-0')) {
4148
({ Fragment } = require('react'));
4249
} else {
@@ -78,4 +85,5 @@ export {
7885
StrictMode,
7986
AsyncMode,
8087
Profiler,
88+
PureComponent,
8189
};

0 commit comments

Comments
 (0)