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
32 changes: 32 additions & 0 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -1605,3 +1605,35 @@ test("applyPatches throws proper immer error when intermediate path value is nul
applyPatches({a: null}, [{op: "add", path: ["a", "b"], value: 1}])
}).toThrow(isProd ? "18" : "Cannot apply patch, path doesn't resolve: a/b")
})

describe("assigning a base value back over its own draft", () => {
// Reading a property drafts it; writing the base value back leaves the
// property untouched, so it must not show up in the patches at all.
const rootChild = {id: 1}
runPatchTests(
"root level",
{child: rootChild, other: 0},
d => {
d.child
d.child = rootChild
d.other = 1
},
[{op: "replace", path: ["other"], value: 1}],
[{op: "replace", path: ["other"], value: 0}],
{child: {id: 1}, other: 1}
)

const nestedChild = {id: 1}
runPatchTests(
"nested",
{a: {child: nestedChild, other: 0}},
d => {
d.a.child
d.a.child = nestedChild
d.a.other = 1
},
[{op: "replace", path: ["a", "other"], value: 1}],
[{op: "replace", path: ["a", "other"], value: 0}],
{a: {child: {id: 1}, other: 1}}
)
})
4 changes: 3 additions & 1 deletion src/core/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ export const objectTraps: ProxyHandler<ProxyState> = {
const currentState: ProxyObjectState = current?.[DRAFT_STATE]
if (currentState && currentState.base_ === value) {
state.copy_![prop] = value
state.assigned_!.set(prop, false)
// The property still holds its base value, so nothing was assigned
// and nothing was deleted. `false` here reads as a deletion later.
state.assigned_!.delete(prop)
return true
}
if (
Expand Down