Skip to content

Commit

Permalink
Fix for onChange (#10)
Browse files Browse the repository at this point in the history
* proppy: tests failing onChange

* more tests…

* proppy: fix for onChange
  • Loading branch information
fahad19 authored Jun 9, 2018
1 parent 1372872 commit 71b93ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/proppy/src/onChange.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,17 @@ describe('proppy :: onChange', () => {
expect(p.props.counter).toEqual(0);

p.props.setCounter(5);
expect(p.props.counter).toEqual(5);
expect(p.props.foo).toEqual('changed foo with counter 5');

// should still be same since no change
p.props.setCounter(5);
expect(p.props.counter).toEqual(5);
expect(p.props.foo).toEqual('changed foo with counter 5');

p.props.setCounter(10);
expect(p.props.counter).toEqual(10);
expect(p.props.foo).toEqual('changed foo with counter 10');
});

test('changes by string, returning async props', () => {
Expand Down
13 changes: 11 additions & 2 deletions packages/proppy/src/onChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export function onChange(propName, fn): ProppyFactory {
return create({
initialize() {
this._prevProps = this.props;
this._own = {};

this._i = false;
},
Expand All @@ -20,14 +21,22 @@ export function onChange(propName, fn): ProppyFactory {
? (p, n) => p[propName] !== n[propName]
: propName;

this.set(Object.assign({}, parentProps, this._own));

const cb = newProps => {
this._own = newProps;
this.set(newProps);
};

if (detector(this._prevProps, parentProps)) {
const cb = newProps => this.set(newProps);
const result = fn(parentProps, this.providers, cb);

if (result) {
this.set(result);
cb(result);
}
}

this._prevProps = this.props;
},
});
}

0 comments on commit 71b93ba

Please sign in to comment.