Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,34 @@ describe("applyPatches", () => {
expect(result.a).toBe(2)
expect(base.a).toBe(1)
})
it.each(["add", "replace"])(
"preserves and deep clones symbol properties in %s patch values",
op => {
const key = Symbol("key")
const nestedKey = Symbol("nested")
const base = op === "add" ? {} : {item: null}
const [next, patches] = produceWithPatches(base, draft => {
draft.item = {[key]: {[nestedKey]: {value: 1}}}
})
const result = applyPatches(base, patches)
expect(patches[0].op).toBe(op)
expect(result).toEqual(next)
expect(result.item[key]).not.toBe(patches[0].value[key])
expect(result.item[key][nestedKey]).not.toBe(
patches[0].value[key][nestedKey]
)
}
)
it("restores symbol properties from inverse patch values", () => {
const key = Symbol("key")
const base = {item: {[key]: {value: 1}}}
const [next, , inversePatches] = produceWithPatches(base, draft => {
draft.item = null
})
const restored = applyPatches(next, inversePatches)
expect(restored).toEqual(base)
expect(restored.item[key]).not.toBe(base.item[key])
})
it('throws when `op` is not "add", "replace", nor "remove"', () => {
expect(() => {
const patch = {op: "copy", from: [0], path: [1]}
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ export function enablePatches() {
if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))
const cloned = Object.create(getPrototypeOf(obj))
for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])
for (const key of Object.getOwnPropertySymbols(obj)) {
if (Object.prototype.propertyIsEnumerable.call(obj, key)) {
cloned[key] = deepClonePatchValue(obj[key])
}
}
if (has(obj, immerable)) cloned[immerable] = obj[immerable]
return cloned
}
Expand Down