Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions __tests__/regressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,32 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
])
})

for (const modified of [false, true]) {
test.each(["default", NaN])(
`assigning an inherited value creates an own property (modified: ${modified}, value: %s)`,
value => {
const proto = {[immerable]: true, name: value}
const state = Object.create(proto)

const [newState, patches, inversePatches] = produceWithPatches(
state,
draft => {
if (modified) draft.changed = true
draft.name = value
}
)

expect(newState).not.toBe(state)
expect(Object.hasOwnProperty.call(state, "name")).toBe(false)
expect(Object.hasOwnProperty.call(newState, "name")).toBe(true)
expect(newState.name).toBe(value)
expect(Object.getPrototypeOf(newState)).toBe(proto)
expect(patches).toContainEqual({op: "add", path: ["name"], value})
expect(inversePatches).toContainEqual({op: "remove", path: ["name"]})
}
)
}

test("Nested and chained produce calls throw 'Cannot perform 'get' on a proxy that has been revoked' error", () => {
const state = {
foo: {
Expand Down
18 changes: 6 additions & 12 deletions src/core/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ export const objectTraps: ProxyHandler<ProxyState> = {
return true
}
if (!state.modified_) {
// the last check is because we need to be able to distinguish setting a non-existing to undefined (which is a change)
// from setting an existing property with value undefined to undefined (which is not a change)
// Assigning an inherited value creates an own property, even when the value is unchanged.
const current = peek(latest(state), prop)
// special case, if we assigning the original value to a draft, we can ignore the assignment
const currentState: ProxyObjectState = current?.[DRAFT_STATE]
Expand All @@ -189,21 +188,16 @@ export const objectTraps: ProxyHandler<ProxyState> = {
state.assigned_!.set(prop, false)
return true
}
if (
is(value, current) &&
(value !== undefined || has(state.base_, prop, state.type_))
)
return true
if (is(value, current) && has(state.base_, prop, state.type_)) return true
prepareCopy(state)
markChanged(state)
}

if (
(state.copy_![prop] === value &&
// special case: handle new props with value 'undefined'
(value !== undefined || has(state.copy_, prop, state.type_))) ||
// special case: NaN
(Number.isNaN(value) && Number.isNaN(state.copy_![prop]))
(state.copy_![prop] === value ||
// special case: NaN
(Number.isNaN(value) && Number.isNaN(state.copy_![prop]))) &&
has(state.copy_, prop, state.type_)
)
return true

Expand Down