Skip to content

Commit c1998f0

Browse files
authored
Fix createReducer initialState logic (#33)
1 parent e7ecee1 commit c1998f0

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* The middleware API changed in [#29](https://github.com/Roblox/rodux/pull/29) in a backwards-incompatible way!
1111
* Middleware now run left-to-right instead of right-to-left!
1212
* Errors thrown in `changed` event now have correct stack traces ([#27](https://github.com/Roblox/rodux/pull/27))
13+
* Fixed `createReducer` having incorrect behavior with `nil` state values ([#33](https://github.com/Roblox/rodux/pull/33))
1314

1415
## Public Release (December 13, 2017)
1516
* Initial release!

lib/createReducer.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
return function(initialState, handlers)
22
return function(state, action)
33
if state == nil then
4-
return initialState
4+
state = initialState
55
end
66

77
local handler = handlers[action.type]

lib/createReducer.spec.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,33 @@ return function()
4949
expect(newState.b).to.equal(0)
5050
end)
5151

52+
it("should still run action handlers if the state is nil", function()
53+
local callCount = 0
54+
55+
local reducer = createReducer(0, {
56+
foo = function(state, action)
57+
callCount = callCount + 1
58+
return nil
59+
end
60+
})
61+
62+
expect(callCount).to.equal(0)
63+
64+
local newState = reducer(nil, {
65+
type = "foo",
66+
})
67+
68+
expect(callCount).to.equal(1)
69+
expect(newState).to.equal(nil)
70+
71+
newState = reducer(newState, {
72+
type = "foo",
73+
})
74+
75+
expect(callCount).to.equal(2)
76+
expect(newState).to.equal(nil)
77+
end)
78+
5279
it("should return the same state if the action is not handled", function()
5380
local initialState = {
5481
a = 0,

0 commit comments

Comments
 (0)