Skip to content

Commit

Permalink
Merge 7722664 into 6acd314
Browse files Browse the repository at this point in the history
  • Loading branch information
leofavre committed Oct 15, 2018
2 parents 6acd314 + 7722664 commit 6aee75d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
### Features

* Implement `withWhenConnected` ([cabd37a](https://github.com/leofavre/elmnt/commit/cabd37a))
* Implement withElmnt ([87911d8](https://github.com/leofavre/elmnt/commit/87911d8))
* Implement `withElmnt` ([87911d8](https://github.com/leofavre/elmnt/commit/87911d8))
10 changes: 10 additions & 0 deletions src/decorators/withSetState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import isFunction from '../helpers/isFunction.js';

export default (Base = class {}) => class extends Base {
setState (arg) {
this.state = {
...(this.state || {}),
...(isFunction(arg) ? arg(this.state) : arg)
};
}
};
43 changes: 43 additions & 0 deletions src/decorators/withSetState.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import withSetState from './withSetState.js';

class Dummy extends withSetState() {}

describe('withSetState', () => {
it('Should extend this.state with new properties ' +
'if called with an object.', () => {
const dummy = new Dummy();
dummy.state = { level: 42 };
dummy.setState({ ub: 40 });
expect(dummy.state).to.deep.equal({ level: 42, ub: 40 });
});

it('Should override properties with the same name.', () => {
const dummy = new Dummy();
dummy.state = { level: 42 };
dummy.setState({ level: 202 });
expect(dummy.state).to.deep.equal({ level: 202 });
});

it('Should extend this.state with new properties ' +
'if called with a function that returns an object.', () => {
const dummy = new Dummy();
dummy.state = { level: 42 };
dummy.setState(() => ({ ub: 40 }));
expect(dummy.state).to.deep.equal({ level: 42, ub: 40 });
});

it('Should expose this.state’s previous value ' +
'if called with a function that returns an object.', () => {
const dummy = new Dummy();
dummy.state = { level: 42 };
dummy.setState(prevState => ({ level: prevState.level + 1 }));
expect(dummy.state).to.deep.equal({ level: 43 });
});

it('Should initialize this.state with an empty object ' +
'if not set.', () => {
const dummy = new Dummy();
dummy.setState({ ub: 40 });
expect(dummy.state).to.deep.equal({ ub: 40 });
});
});
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import withElmnt from './decorators/withElmnt.js';
import withSetState from './decorators/withSetState.js';

import coerceToArray from './coercion/coerceToArray.js';
import coerceToArrayOrUndefined from './coercion/coerceToArrayOrUndefined.js';
Expand All @@ -10,6 +11,7 @@ import coerceToStringOrUndefined from './coercion/coerceToStringOrUndefined.js';

export {
withElmnt,
withSetState,
coerceToArray,
coerceToArrayOrUndefined,
coerceToBoolean,
Expand Down

0 comments on commit 6aee75d

Please sign in to comment.