Skip to content

Commit

Permalink
Ingore non-string action values
Browse files Browse the repository at this point in the history
  • Loading branch information
kenkunz committed Nov 8, 2021
1 parent 174bcab commit 5b3077d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export default function (state, states = {}) {
}

function invoke(event, ...args) {
const newState = dispatch(event, ...args);
if (newState !== undefined && newState !== state) {
const newState = dispatch(event, ...args)?.valueOf();
if (typeof newState === 'string' && newState !== state) {
transition(newState, event, args);
}
return state;
Expand Down
13 changes: 12 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ describe('a finite state machine', () => {
surge: 'blown',
kick: sinon.stub(),
subscribe: sinon.fake(),
dateAction: new Date(),
objectAction: {},
numericAction: 1,
async asyncAction() {},
arrowFunction: () => {
this.shouldExplode();
}
Expand Down Expand Up @@ -106,12 +110,19 @@ describe('a finite state machine', () => {
assert.notCalled(callback);
});

it('should transition to invoked action return value', () => {
it('should transition to invoked action return value (string)', () => {
states.off.kick.returns('on');
assert.equal('on', machine.kick());
assert.calledWithExactly(callback, 'on');
});

it('should ignore non-string action return values', () => {
assert.equal('off', machine.dateAction());
assert.equal('off', machine.objectAction());
assert.equal('off', machine.numericAction());
assert.equal('off', machine.asyncAction());
});

it('should invoke action with correct `this` binding and arguments', () => {
machine.kick('hard');
assert.calledOn(states.off.kick, machine);
Expand Down

0 comments on commit 5b3077d

Please sign in to comment.