Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Migrate Component.getInitialState() here from preact core (as of prea…
Browse files Browse the repository at this point in the history
…ct 6.2.0)
  • Loading branch information
developit committed Oct 3, 2016
1 parent f40948f commit 916bb4e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ function afterRender() {

function Component(props, context, opts) {
PreactComponent.call(this, props, context);
if (this.getInitialState) this.state = this.getInitialState();
this.refs = {};
this._refProxies = {};
if (opts!==BYPASS_HOOK) {
Expand Down
20 changes: 20 additions & 0 deletions test/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ describe('components', () => {
});
});

describe('getInitialState', () => {
it('should be invoked for new components', () => {
class Foo extends React.Component {
getInitialState() {
return { foo: 'bar' };
}
render() {
return <div />;
}
}

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

let a = React.render(<Foo />, scratch);

expect(Foo.prototype.getInitialState).to.have.been.calledOnce;
expect(a.state).to.eql({ foo: 'bar' });
});
});

describe('defaultProps', () => {
it('should support defaultProps for components', () => {
let render = sinon.stub().returns(<div />);
Expand Down
2 changes: 1 addition & 1 deletion test/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ describe('jsx', () => {

let html = renderToString(jsx);

expect(html).to.equal('<div data-foo="bar" class="foo bar"><span id="some_id">inner!</span>ab</div>');
expect(html).to.equal('<div class="foo bar" data-foo="bar"><span id="some_id">inner!</span>ab</div>');
});
});

0 comments on commit 916bb4e

Please sign in to comment.