From dc3f66cdea53fd5a8c814924bfafa9f6b53c9c62 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Thu, 24 Jun 2021 10:06:42 +0100 Subject: [PATCH] fix: #807 new undefined properties should end up in result object --- __tests__/regressions.js | 12 ++++++++++++ src/core/proxy.ts | 9 ++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/__tests__/regressions.js b/__tests__/regressions.js index d3e63cac..329b0a09 100644 --- a/__tests__/regressions.js +++ b/__tests__/regressions.js @@ -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 + }) + }) }) } diff --git a/src/core/proxy.ts b/src/core/proxy.ts index 00254366..7aaaf4cd 100644 --- a/src/core/proxy.ts +++ b/src/core/proxy.ts @@ -157,7 +157,14 @@ export const objectTraps: ProxyHandler = { 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