Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compat/src/PureComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { shallowDiffers } from './util';
/**
* Component class with a predefined `shouldComponentUpdate` implementation
*/
export function PureComponent(p) {
export function PureComponent(p, c) {
this.props = p;
this.context = c;
}
PureComponent.prototype = new Component();
// Some third-party libraries check if this property is present
Expand Down
32 changes: 32 additions & 0 deletions compat/test/browser/PureComponent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,38 @@ describe('PureComponent', () => {
expect(spy).to.be.calledWithMatch(expected, expected);
});

it('should pass context in constructor', () => {
let instance;
// Not initializing state matches React behavior: https://codesandbox.io/s/rml19v8o2q
class Foo extends React.PureComponent {
constructor(props, context) {
super(props, context);
expect(this.props).to.equal(props);
expect(this.state).to.deep.equal(undefined);
expect(this.context).to.equal(context);

instance = this;
}
render(props) {
return <div {...props}>Hello</div>;
}
}

sinon.spy(Foo.prototype, 'render');

const PROPS = { foo: 'bar' };
React.render(<Foo {...PROPS} />, scratch);

expect(Foo.prototype.render)
.to.have.been.calledOnce.and.to.have.been.calledWithMatch(PROPS, {}, {})
.and.to.have.returned(sinon.match({ type: 'div', props: PROPS }));
expect(instance.props).to.deep.equal(PROPS);
expect(instance.state).to.deep.equal({});
expect(instance.context).to.deep.equal({});

expect(scratch.innerHTML).to.equal('<div foo="bar">Hello</div>');
});

it('should ignore the __source variable', () => {
const pureSpy = sinon.spy();
const appSpy = sinon.spy();
Expand Down