Skip to content

Commit

Permalink
support passing context into pure component
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jan 29, 2024
1 parent 79be156 commit 230c852
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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

0 comments on commit 230c852

Please sign in to comment.