Skip to content

Commit

Permalink
(fix) - minor corrections for diffing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jul 12, 2019
1 parent e304341 commit 0ef3573
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 14 deletions.
11 changes: 0 additions & 11 deletions debug/test/browser/debug.test.js
Expand Up @@ -189,17 +189,6 @@ describe('debug', () => {
expect(fn).to.throw(/createElement/);
});

it('should throw errors when accessing certain attributes', () => {
const vnode = h('div', null);
expect(() => vnode).to.not.throw();
expect(() => vnode.attributes).to.throw(/use vnode.props/);
expect(() => vnode.nodeName).to.throw(/use vnode.type/);
expect(() => vnode.children).to.throw(/use vnode.props.children/);
expect(() => vnode.attributes = {}).to.throw(/use vnode.props/);
expect(() => vnode.nodeName = 'test').to.throw(/use vnode.type/);
expect(() => vnode.children = [<div />]).to.throw(/use vnode.props.children/);
});

it('should print an error when component is an array', () => {
let fn = () => render(h([<div />]), scratch);
expect(fn).to.throw(/createElement/);
Expand Down
4 changes: 2 additions & 2 deletions src/component.js
Expand Up @@ -13,6 +13,7 @@ import { Fragment } from './create-element';
export function Component(props, context) {
this.props = props;
this.context = context;
if (this.state==null) this.state = {};
// this.constructor // When component is functional component, this is reset to functional component
// if (this.state==null) this.state = {};
// this.state = {};
Expand All @@ -38,9 +39,9 @@ export function Component(props, context) {
* updated
*/
Component.prototype.setState = function(update, callback) {
if (this.state) this._prevState = this.state;
// only clone state when copying to nextState the first time.
let s = (this._nextState!==this.state && this._nextState) || (this._nextState = assign({}, this.state));

// if update() mutates state in-place, skip the copy:
if (typeof update!=='function' || (update = update(s, this.props))) {
assign(s, update);
Expand All @@ -51,7 +52,6 @@ Component.prototype.setState = function(update, callback) {

if (this._vnode) {
if (callback) this._renderCallbacks.push(callback);
if (this.state) this._prevState = this.state;
enqueueRender(this);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/diff/index.js
Expand Up @@ -71,6 +71,7 @@ export function diff(parentDom, newVNode, oldVNode, context, isSvg, excessDomChi
c._nextState = c.state;
}
if (newType.getDerivedStateFromProps!=null) {
c._prevState = c.state;
assign(c._nextState==c.state ? (c._nextState = assign({}, c._nextState)) : c._nextState, newType.getDerivedStateFromProps(newProps, c._nextState));
}

Expand Down Expand Up @@ -100,7 +101,7 @@ export function diff(parentDom, newVNode, oldVNode, context, isSvg, excessDomChi
}

oldProps = c.props;
oldState = c._prevState;
oldState = c._prevState || {};

c.context = cctx;
c.props = newProps;
Expand Down

0 comments on commit 0ef3573

Please sign in to comment.