Skip to content

Commit e774d8e

Browse files
committed
Clarify the behavior: only undefined is forbidden as action type
1 parent d7926b5 commit e774d8e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

docs/recipes/ReducingBoilerplate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Why is this beneficial? **It is often claimed that constants are unnecessary, an
3131
* It helps keep the naming consistent because all action types are gathered in a single place.
3232
* Sometimes you want to see all existing actions before working on a new feature. It may be that the action you need was already added by somebody on the team, but you didn’t know.
3333
* The list of action types that were added, removed, and changed in a Pull Request helps everyone on the team keep track of scope and implementation of new features.
34-
* If you make a typo when importing an action constant, you will get `undefined`. This is much easier to notice than a typo when you wonder why nothing happens when the action is dispatched.
34+
* If you make a typo when importing an action constant, you will get `undefined`. Redux will immediately throw when dispatching such an action, and you’ll find the mistake sooner.
3535

3636
It is up to you to choose the conventions for your project. You may start by using inline strings, and later transition to constants, and maybe later group them into a single file. Redux does not have any opinion here, so use your best judgment.
3737

test/createStore.spec.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,33 @@ describe('createStore', () => {
292292
).toNotThrow();
293293
});
294294

295-
it('should only accept actions with a `type`', () => {
295+
it('should throw if action type is missing', () => {
296296
const store = createStore(reducers.todos);
297297
expect(() =>
298298
store.dispatch({})
299299
).toThrow(/Actions may not have an undefined "type" property/);
300300
});
301+
302+
it('should throw if action type is undefined', () => {
303+
const store = createStore(reducers.todos);
304+
expect(() =>
305+
store.dispatch({ type: undefined })
306+
).toThrow(/Actions may not have an undefined "type" property/);
307+
});
308+
309+
it('should not throw if action type is falsy', () => {
310+
const store = createStore(reducers.todos);
311+
expect(() =>
312+
store.dispatch({ type: false })
313+
).toNotThrow();
314+
expect(() =>
315+
store.dispatch({ type: 0 })
316+
).toNotThrow();
317+
expect(() =>
318+
store.dispatch({ type: null })
319+
).toNotThrow();
320+
expect(() =>
321+
store.dispatch({ type: '' })
322+
).toNotThrow();
323+
});
301324
});

0 commit comments

Comments
 (0)