Skip to content

Commit

Permalink
make combineReducers not throw when initialState is nil (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaranthineCodices authored and LPGhatguy committed Mar 12, 2018
1 parent 0a2479b commit c02815b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/combineReducers.lua
Expand Up @@ -3,6 +3,11 @@
]]
local function combineReducers(map)
return function(state, action)
-- If state is nil, substitute it with a blank table.
if state == nil then
state = {}
end

local newState = {}

for key, reducer in pairs(map) do
Expand Down
15 changes: 15 additions & 0 deletions lib/combineReducers.spec.lua
Expand Up @@ -34,4 +34,19 @@ return function()
expect(newState.a).to.equal(1)
expect(newState.b).to.equal(3)
end)

it("should not throw when state is nil", function()
local reducer = combineReducers({
a = function(state, action)
return (state or 0) + 1
end,
b = function(state, action)
return (state or 0) + 3
end,
})

expect(function()
reducer(nil, {})
end).to.never.throw()
end)
end

0 comments on commit c02815b

Please sign in to comment.