Skip to content
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

Actions without types throw an error rather than return the default state #140

Closed
JaKXz opened this issue Oct 6, 2016 · 6 comments
Closed

Comments

@JaKXz
Copy link
Contributor

JaKXz commented Oct 6, 2016

In my test setup code I have this line:

test.beforeEach(t => {
  const initialState = reducer();
});

where

const reducer = handleActions({ ... }, { /*initial state*/});

However, the first method throws an error because:
screen shot 2016-10-06 at 11 15 55 am

and in the transpiled source it looks like action.type.toString() is called unguardedly. Now, maybe I'm misunderstanding how FSAs work, but I think it makes sense for a reducer test to be able to check that the initial state is returned when the action type is missing. I can try to make a PR for this as well, if you think I'm correct about the expected behaviour.

If not, could you please fill me in on what I'm missing? Thanks!

@yangmillstheory
Copy link
Contributor

yangmillstheory commented Oct 6, 2016

A reducer returns the default state when the state given is undefined; the action must exist. This isn't FSA thing, just a Redux thing.

Your test is throwing an expected error because you haven't given it an action; reducers respond to actions.

For tests around this, see

For background material, see

@JaKXz
Copy link
Contributor Author

JaKXz commented Oct 6, 2016

Thanks @yangmillstheory - the background material helps.

Follow-up question:

I actually originally tried reducer(undefined, {}) but that failed with the same error. Could that be a valid set of arguments for handleAction?

@yangmillstheory
Copy link
Contributor

See #139 (comment) and the comment below it.

@JaKXz
Copy link
Contributor Author

JaKXz commented Oct 6, 2016

Sorry for the duplication/noise, and 🙏 for a great project! I thought I'd searched the issues properly but I guess not.

@yangmillstheory
Copy link
Contributor

Out of curiosity, when do you encounter actions in your app that don't have a type property?

If you're using this library, createActions makes action creators that will always return actions with type.

@JaKXz
Copy link
Contributor Author

JaKXz commented Oct 6, 2016

Yeah at this point it's just 🚴🏚-ing about test setup code, I can't imagine a real example of having an undefined action or action type being dispatched. And I think I understand the argument that having the "robustness" to handle that is more bad than good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants