diff --git a/CHANGELOG.md b/CHANGELOG.md index bdbdae1..effb45c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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! \ No newline at end of file diff --git a/lib/createReducer.lua b/lib/createReducer.lua index ffb2683..5560b59 100644 --- a/lib/createReducer.lua +++ b/lib/createReducer.lua @@ -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] diff --git a/lib/createReducer.spec.lua b/lib/createReducer.spec.lua index 7d5ff23..a704944 100644 --- a/lib/createReducer.spec.lua +++ b/lib/createReducer.spec.lua @@ -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,