From 5721bb7cface08fe8727d69ead406fc9ff2b8cde Mon Sep 17 00:00:00 2001 From: Ray <48085754+Rayzhangzhang@users.noreply.github.com> Date: Sat, 22 Oct 2022 21:20:13 +0800 Subject: [PATCH] fix: special case NaN in setter (#912) * fix: special case NaN in setter * fix: naN special case Co-authored-by: zhangpeifeng --- __tests__/base.js | 17 +++++++++++++++++ src/core/proxy.ts | 8 ++++---- 2 files changed, 21 insertions(+), 4 deletions(-) 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