-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters