Skip to content

Commit

Permalink
fix: #807 new undefined properties should end up in result object
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jun 24, 2021
1 parent 5412c9f commit dc3f66c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 12 additions & 0 deletions __tests__/regressions.js
Expand Up @@ -239,5 +239,17 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
bar: {x: 3}
})
})

test("#807 new undefined member not stored", () => {
const state = {}
const newState = produce(state, draft => {
draft.baz = undefined
})
expect(state).not.toBe(newState)
expect(Object.hasOwnProperty.call(newState, "baz")).toBe(true)
expect(newState).toEqual({
baz: undefined
})
})
})
}
9 changes: 8 additions & 1 deletion src/core/proxy.ts
Expand Up @@ -157,7 +157,14 @@ export const objectTraps: ProxyHandler<ProxyState> = {
markChanged(state)
}

if (state.copy_![prop] === value && typeof value !== "number") return true
if (
state.copy_![prop] === value &&
// special case: NaN
typeof value !== "number" &&
// special case: handle new props with value 'undefined'
(value !== undefined || prop in state.copy_)
)
return true

// @ts-ignore
state.copy_![prop] = value
Expand Down

0 comments on commit dc3f66c

Please sign in to comment.