Skip to content

Commit

Permalink
Fix createReducer initialState logic (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy authored Aug 8, 2018
1 parent e7ecee1 commit c1998f0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* The middleware API changed in [#29](https://github.com/Roblox/rodux/pull/29) in a backwards-incompatible way!
* Middleware now run left-to-right instead of right-to-left!
* Errors thrown in `changed` event now have correct stack traces ([#27](https://github.com/Roblox/rodux/pull/27))
* Fixed `createReducer` having incorrect behavior with `nil` state values ([#33](https://github.com/Roblox/rodux/pull/33))

## Public Release (December 13, 2017)
* Initial release!
2 changes: 1 addition & 1 deletion lib/createReducer.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
return function(initialState, handlers)
return function(state, action)
if state == nil then
return initialState
state = initialState
end

local handler = handlers[action.type]
Expand Down
27 changes: 27 additions & 0 deletions lib/createReducer.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ return function()
expect(newState.b).to.equal(0)
end)

it("should still run action handlers if the state is nil", function()
local callCount = 0

local reducer = createReducer(0, {
foo = function(state, action)
callCount = callCount + 1
return nil
end
})

expect(callCount).to.equal(0)

local newState = reducer(nil, {
type = "foo",
})

expect(callCount).to.equal(1)
expect(newState).to.equal(nil)

newState = reducer(newState, {
type = "foo",
})

expect(callCount).to.equal(2)
expect(newState).to.equal(nil)
end)

it("should return the same state if the action is not handled", function()
local initialState = {
a = 0,
Expand Down

0 comments on commit c1998f0

Please sign in to comment.