diff --git a/__tests__/patch.js b/__tests__/patch.js index f74dd5de..2a30f994 100644 --- a/__tests__/patch.js +++ b/__tests__/patch.js @@ -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"])]]) + ) +}) diff --git a/src/patches.ts b/src/patches.ts index 74135fd5..e2ba2752 100644 --- a/src/patches.ts +++ b/src/patches.ts @@ -14,7 +14,8 @@ import { ES5ObjectState, ProxyObjectState, Archtype, - isMap + isMap, + isSet } from "./internal" export type PatchPath = (string | number)[] @@ -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