Skip to content

Commit

Permalink
fix: new map keys were not added if value is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jun 22, 2020
2 parents 4057f79 + 90978bc commit 4a1bd65
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions __tests__/map-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,14 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
})
expect(Array.from(set).sort()).toEqual([1, 2, 3])
})

test("#627 - new map key with value=undefined", () => {
const map = new Map()
const map1 = produce(map, draft => {
draft.set("key", undefined)
})
expect(map1.has("key")).toBe(true)
expect(map1.get("key")).toBe(undefined)
})
})
}
2 changes: 1 addition & 1 deletion src/plugins/mapset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function enableMapSet() {
p.set = function(key: any, value: any) {
const state: MapState = this[DRAFT_STATE]
assertUnrevoked(state)
if (latest(state).get(key) !== value) {
if (!latest(state).has(key) || latest(state).get(key) !== value) {
prepareMapCopy(state)
markChanged(state)
state.assigned_!.set(key, true)
Expand Down

0 comments on commit 4a1bd65

Please sign in to comment.