Skip to content

Commit

Permalink
fix: make sure changing an undefined value to undefined is not picked…
Browse files Browse the repository at this point in the history
… up as change. Fixes #646
  • Loading branch information
mweststrate committed Jul 24, 2020
1 parent 2697430 commit 5521527
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
20 changes: 20 additions & 0 deletions __tests__/regressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,25 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
x: 1
})
})

test("#646 setting undefined field to undefined should not create new result", () => {
const foo = {
bar: undefined
}
const foo2 = produce(foo, draft => {
draft.bar = undefined
})
expect(foo2).toBe(foo)
})

test("#646 -2 setting undefined field to undefined should not create new result", () => {
const foo = {}
const foo2 = produce(foo, draft => {
draft.bar = undefined
})
expect(foo2).not.toBe(foo)
expect(foo).toEqual({})
expect(foo2).toEqual({bar: undefined})
})
})
}
7 changes: 6 additions & 1 deletion src/core/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ export const objectTraps: ProxyHandler<ProxyState> = {
}
state.assigned_[prop] = true
if (!state.modified_) {
if (is(value, peek(latest(state), prop)) && value !== undefined)
// the last check is because we need to be able to distinguish setting a non-existig to undefined (which is a change)
// from setting an existing property with value undefined to undefined (which is not a change)
if (
is(value, peek(latest(state), prop)) &&
(value !== undefined || has(state.base_, prop))
)
return true
prepareCopy(state)
markChanged(state)
Expand Down

0 comments on commit 5521527

Please # to comment.