diff --git a/__tests__/regressions.js b/__tests__/regressions.js index dfe3367f..618ae286 100644 --- a/__tests__/regressions.js +++ b/__tests__/regressions.js @@ -5,10 +5,12 @@ import { original, isDraft, immerable, - enableMapSet + enableMapSet, + enablePatches } from "../src/immer" enableMapSet() +enablePatches() runBaseTest("proxy (no freeze)", true, false) runBaseTest("proxy (autofreeze)", true, true) @@ -252,6 +254,26 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) { }) }) + test("#1160 assigning undefined to a key only present on the prototype is still stored as an own property", () => { + const proto = {[immerable]: true, name: undefined} + const state = Object.create(proto) + + expect(Object.hasOwnProperty.call(state, "name")).toBe(false) + + const [newState, patches] = produceWithPatches(state, draft => { + draft.name = undefined + }) + + expect(Object.hasOwnProperty.call(newState, "name")).toBe(true) + expect(patches).toEqual([ + { + op: "add", + path: ["name"], + value: undefined + } + ]) + }) + test("Nested and chained produce calls throw 'Cannot perform 'get' on a proxy that has been revoked' error", () => { const state = { foo: { diff --git a/src/core/proxy.ts b/src/core/proxy.ts index cfb84ecc..43fe72ee 100644 --- a/src/core/proxy.ts +++ b/src/core/proxy.ts @@ -241,7 +241,7 @@ export const objectTraps: ProxyHandler = { if ( (state.copy_![prop] === value && // special case: handle new props with value 'undefined' - (value !== undefined || prop in state.copy_)) || + (value !== undefined || has(state.copy_, prop, state.type_))) || // special case: NaN (Number.isNaN(value) && Number.isNaN(state.copy_![prop])) )