New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple 'from' and 'to' states for a single event gives error in node cmd with jasmine. #88

Closed
NimishChandola opened this Issue Apr 14, 2016 · 5 comments

Comments

Projects
None yet
4 participants
@NimishChandola

NimishChandola commented Apr 14, 2016

Initializing the state machine like below works properly in browser but not in node console when writing jasmine unit test cases.
var fsm = StateMachine.create({
initial: 'hungry',
events: [
{ name: 'rest', from: ['hungry', 'satisfied', 'full', 'sick'], to: 'hungry' },
]});

When i am creating a jasmine unit test and run it in node console then it throws error because
in console it generate "map" json object like {"hungry,satisfied,'full','sick'" : "hungry"}
and map[event].hasOwnProperty(this.current) is failing at line 79

If we check it in browser "map" object will be like
{'hungry':'hungry', 'satisfied':'hungry', 'full':'sick', 'sick':'hungry'} which is correct.

@Arunkumar-MS

This comment has been minimized.

Show comment
Hide comment
@Arunkumar-MS

Arunkumar-MS Apr 14, 2016

@jakesgordon Even i am also facing same problem while writing an unit for it using jasmine :(

Arunkumar-MS commented Apr 14, 2016

@jakesgordon Even i am also facing same problem while writing an unit for it using jasmine :(

@rakesh-nayak

This comment has been minimized.

Show comment
Hide comment
@rakesh-nayak

rakesh-nayak Apr 14, 2016

@NimishChandola & @Arunkumar-MS

I also faced the same problem.

Then I changed the code as below

var fsm = StateMachine.create({
initial: 'hungry',
events: [
{name: 'rest', from: 'hungry', to: 'hungry' },
{name: 'rest', from: 'satisfied', to: 'hungry' },
{name: 'rest', from: 'full', to: 'hungry' },
{name: 'rest', from: 'sick', to: 'hungry' }
]});

And unit test started working. But I am not sure is this a good idea to do that or not.

@jakesgordon : I believe for writing the unit test case we should not modify the actual implementation .. but I could not find any other way. Is this correct way to do this?

rakesh-nayak commented Apr 14, 2016

@NimishChandola & @Arunkumar-MS

I also faced the same problem.

Then I changed the code as below

var fsm = StateMachine.create({
initial: 'hungry',
events: [
{name: 'rest', from: 'hungry', to: 'hungry' },
{name: 'rest', from: 'satisfied', to: 'hungry' },
{name: 'rest', from: 'full', to: 'hungry' },
{name: 'rest', from: 'sick', to: 'hungry' }
]});

And unit test started working. But I am not sure is this a good idea to do that or not.

@jakesgordon : I believe for writing the unit test case we should not modify the actual implementation .. but I could not find any other way. Is this correct way to do this?

@NimishChandola

This comment has been minimized.

Show comment
Hide comment
@NimishChandola

NimishChandola Apr 15, 2016

I debug code and get to know that when we use injectr module for unit testing then "instance of Array" is not returning true though there is an array and due to that for multiple 'to' , array generate like
[ ['hungry', 'satisfied', 'full', 'sick']]
check the below code at line 49
var from = (e.from instanceof Array) ? e.from : (e.from ? [e.from] : [StateMachine.WILDCARD]);

NimishChandola commented Apr 15, 2016

I debug code and get to know that when we use injectr module for unit testing then "instance of Array" is not returning true though there is an array and due to that for multiple 'to' , array generate like
[ ['hungry', 'satisfied', 'full', 'sick']]
check the below code at line 49
var from = (e.from instanceof Array) ? e.from : (e.from ? [e.from] : [StateMachine.WILDCARD]);

NimishChandola added a commit to NimishChandola/javascript-state-machine that referenced this issue Apr 18, 2016

Update state-machine.js
To fix the issue for injectr as explained in jakesgordon#88 i have updated the  state-machine.js and my code is running fine
@NimishChandola

This comment has been minimized.

Show comment
Hide comment
@NimishChandola

NimishChandola Apr 24, 2016

The issue is coming due to injector module, but as part of defensive programming i have updated the condition so that due to some other library issue , our code should not break.

NimishChandola commented Apr 24, 2016

The issue is coming due to injector module, but as part of defensive programming i have updated the condition so that due to some other library issue , our code should not break.

jakesgordon added a commit that referenced this issue Nov 18, 2016

Prefer es5 Array.isArray(obj) rather than problematic (obj instanceof…
… Array)

Fixes Issue #88 "Multiple 'from' and 'to' states for a single event gives error in node cmd with jasmine"
@jakesgordon

This comment has been minimized.

Show comment
Hide comment
@jakesgordon

jakesgordon Nov 18, 2016

Owner

This is fixed in (upcoming) v2.4

Owner

jakesgordon commented Nov 18, 2016

This is fixed in (upcoming) v2.4

@jakesgordon jakesgordon added v2.4 and removed v3.0 labels Nov 18, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment