diff --git a/__tests__/patch.js b/__tests__/patch.js index eee94a45..1885094a 100644 --- a/__tests__/patch.js +++ b/__tests__/patch.js @@ -5,7 +5,8 @@ import produce, { produceWithPatches, enableAllPlugins, isDraft, - immerable + immerable, + nothing } from "../src/immer" enableAllPlugins() @@ -1277,3 +1278,10 @@ test("#648 assigning object to itself should not change patches", () => { } ]) }) + +test("#791 patch for nothing is stored as undefined", () => { + const [newState, patches] = produceWithPatches({abc: 123}, draft => nothing) + expect(patches).toEqual([{op: "replace", path: [], value: undefined}]) + + expect(applyPatches({}, patches)).toEqual(undefined) +}) diff --git a/src/plugins/patches.ts b/src/plugins/patches.ts index baa1516c..e0cdc091 100644 --- a/src/plugins/patches.ts +++ b/src/plugins/patches.ts @@ -20,7 +20,8 @@ import { Archtype, die, isDraft, - isDraftable + isDraftable, + NOTHING } from "../internal" export function enablePatches() { @@ -190,7 +191,7 @@ export function enablePatches() { patches.push({ op: REPLACE, path: [], - value: replacement + value: replacement === NOTHING ? undefined : replacement }) inversePatches.push({ op: REPLACE,