Skip to content

Commit

Permalink
fix: undraftable values should not be cloned for patches, fixes #676
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Oct 20, 2020
1 parent 12f4cf8 commit 1b70ad5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
28 changes: 28 additions & 0 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,3 +1119,31 @@ describe("#588", () => {
[{op: "add", path: ["num"], value: 42}]
)
})

test("#676 patching Date objects", () => {
class Test {
constructor() {
this.test = true
}
perform() {
return "tested!"
}
}

const [nextState, patches] = produceWithPatches({}, function(draft) {
draft.date = new Date(2020, 10, 10, 8, 8, 8, 3)
draft.test = new Test()
})

expect(nextState.date.toJSON()).toMatchInlineSnapshot(
`"2020-11-10T08:08:08.003Z"`
)
expect(nextState.test.perform()).toBe("tested!")

const rebuilt = applyPatches({}, patches)
expect(rebuilt.date).toBeInstanceOf(Date)
expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(
`"2020-11-10T08:08:08.003Z"`
)
expect(rebuilt.date).toEqual(new Date(2020, 10, 10, 8, 8, 8, 3))
})
5 changes: 3 additions & 2 deletions src/plugins/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {
ArchtypeSet,
ArchtypeArray,
die,
isDraft
isDraft,
isDraftable
} from "../internal"

export function enablePatches() {
Expand Down Expand Up @@ -267,7 +268,7 @@ export function enablePatches() {
// (See failing test when deepClone just returns obj)
function deepClonePatchValue<T>(obj: T): T
function deepClonePatchValue(obj: any) {
if (!obj || typeof obj !== "object") return obj
if (!isDraftable(obj)) return obj
if (Array.isArray(obj)) return obj.map(deepClonePatchValue)
if (isMap(obj))
return new Map(
Expand Down

0 comments on commit 1b70ad5

Please sign in to comment.