Skip to content

Commit 5412c9f

Browse files
committed
fix: #791 return 'nothing' should produce undefined patch
1 parent 58b74a6 commit 5412c9f

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

__tests__/patch.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import produce, {
55
produceWithPatches,
66
enableAllPlugins,
77
isDraft,
8-
immerable
8+
immerable,
9+
nothing
910
} from "../src/immer"
1011

1112
enableAllPlugins()
@@ -1277,3 +1278,10 @@ test("#648 assigning object to itself should not change patches", () => {
12771278
}
12781279
])
12791280
})
1281+
1282+
test("#791 patch for nothing is stored as undefined", () => {
1283+
const [newState, patches] = produceWithPatches({abc: 123}, draft => nothing)
1284+
expect(patches).toEqual([{op: "replace", path: [], value: undefined}])
1285+
1286+
expect(applyPatches({}, patches)).toEqual(undefined)
1287+
})

src/plugins/patches.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import {
2020
Archtype,
2121
die,
2222
isDraft,
23-
isDraftable
23+
isDraftable,
24+
NOTHING
2425
} from "../internal"
2526

2627
export function enablePatches() {
@@ -190,7 +191,7 @@ export function enablePatches() {
190191
patches.push({
191192
op: REPLACE,
192193
path: [],
193-
value: replacement
194+
value: replacement === NOTHING ? undefined : replacement
194195
})
195196
inversePatches.push({
196197
op: REPLACE,

0 commit comments

Comments
 (0)