Skip to content

Commit

Permalink
fix: Set inside patches were not correctly cloned. Fixes #521
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Feb 4, 2020
1 parent fb0f3a0 commit 9d8d995
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 19 additions & 0 deletions __tests__/patch.js
Expand Up @@ -977,3 +977,22 @@ test.skip("#468", () => {
const final = applyPatches(state, patches)
expect(final).toEqual(nextState)
})

test("#521", () => {
const state = new Map()

const [nextState, patches] = produceWithPatches(state, draft => {
draft.set("hello", new Set(["world"]))
})

let patchedState = applyPatches(state, patches)
expect(patchedState).toEqual(nextState)

const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {
draft.get("hello").add("immer")
})

expect(applyPatches(nextState, patchesV2)).toEqual(
new Map([["hello", new Set(["world", "immer"])]])
)
})
5 changes: 3 additions & 2 deletions src/patches.ts
Expand Up @@ -14,7 +14,8 @@ import {
ES5ObjectState,
ProxyObjectState,
Archtype,
isMap
isMap,
isSet
} from "./internal"

export type PatchPath = (string | number)[]
Expand Down Expand Up @@ -255,7 +256,7 @@ function deepClonePatchValue(obj: any) {
return new Map(
Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])
)
// Not needed: if (isSet(obj)) return new Set(Array.from(obj.values()).map(deepClone))
if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))
const cloned = Object.create(Object.getPrototypeOf(obj))
for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])
return cloned
Expand Down

0 comments on commit 9d8d995

Please sign in to comment.