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

Fix createReducer initialState logic #33

Merged
merged 2 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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