diff --git a/__tests__/base.js b/__tests__/base.js index 105bdc32..e37dba2a 100644 --- a/__tests__/base.js +++ b/__tests__/base.js @@ -1282,6 +1282,23 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) { expect(nextState).not.toEqual(baseState) }) + it("should handle equality correctly - 3", () => { + const baseState = { + x: "s1", + y: 1, + z: NaN + } + const nextState = produce(baseState, draft => { + draft.x = "s2" + draft.y = 1 + draft.z = NaN + expect(draft[DRAFT_STATE].assigned_.x).toBe(true) + expect(draft[DRAFT_STATE].assigned_.y).toBe(undefined) + expect(draft[DRAFT_STATE].assigned_.z).toBe(undefined) + }) + expect(nextState.x).toBe("s2") + }) + // AKA: recursive produce calls describe("nested producers", () => { describe("when base state is not a draft", () => { diff --git a/src/core/proxy.ts b/src/core/proxy.ts index 7e6503a9..1d9f61da 100644 --- a/src/core/proxy.ts +++ b/src/core/proxy.ts @@ -158,11 +158,11 @@ export const objectTraps: ProxyHandler = { } if ( - state.copy_![prop] === value && + (state.copy_![prop] === value && + // special case: handle new props with value 'undefined' + (value !== undefined || prop in state.copy_)) || // special case: NaN - typeof value !== "number" && - // special case: handle new props with value 'undefined' - (value !== undefined || prop in state.copy_) + (Number.isNaN(value) && Number.isNaN(state.copy_![prop])) ) return true