patch.js
This file
Path
__tests__/patch.js
1,149 lines · 20.5 KiB · JavaScript
Last changed by
fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
History of this fileTake it
1"use strict"2import produce, {3 setUseProxies,4 applyPatches,5 produceWithPatches,6 enableAllPlugins,7 isDraft,8 immerable9} from "../src/immer"1011enableAllPlugins()1213jest.setTimeout(1000)1415function runPatchTest(base, producer, patches, inversePathes) {16 let resultProxies, resultEs51718 function runPatchTestHelper() {19 let recordedPatches20 let recordedInversePatches21 const res = produce(base, producer, (p, i) => {22 recordedPatches = p23 recordedInversePatches = i24 })2526 test("produces the correct patches", () => {27 expect(recordedPatches).toEqual(patches)28 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)29 })3031 test("patches are replayable", () => {32 expect(applyPatches(base, recordedPatches)).toEqual(res)33 })3435 test("patches can be reversed", () => {36 expect(applyPatches(res, recordedInversePatches)).toEqual(base)37 })3839 return res40 }4142 describe(`proxy`, () => {43 setUseProxies(true)44 resultProxies = runPatchTestHelper()45 })4647 describe(`es5`, () => {48 setUseProxies(false)49 resultEs5 = runPatchTestHelper()50 test("ES5 and Proxy implementation yield same result", () => {51 expect(resultEs5).toEqual(resultProxies)52 })53 })5455 return resultProxies56}5758describe("applyPatches", () => {59 it("mutates the base state when it is a draft", () => {60 produce({a: 1}, draft => {61 const result = applyPatches(draft, [62 {op: "replace", path: ["a"], value: 2}63 ])64 expect(result).toBe(draft)65 expect(draft.a).toBe(2)66 })67 })68 it("produces a copy of the base state when not a draft", () => {69 const base = {a: 1}70 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])71 expect(result).not.toBe(base)72 expect(result.a).toBe(2)73 expect(base.a).toBe(1)74 })75 it('throws when `op` is not "add", "replace", nor "remove"', () => {76 expect(() => {77 const patch = {op: "copy", from: [0], path: [1]}78 applyPatches([2], [patch])79 }).toThrowErrorMatchingSnapshot()80 })81 it("throws when `path` cannot be resolved", () => {82 // missing parent83 expect(() => {84 const patch = {op: "add", path: ["a", "b"], value: 1}85 applyPatches({}, [patch])86 }).toThrowErrorMatchingSnapshot()8788 // missing grand-parent89 expect(() => {90 const patch = {op: "add", path: ["a", "b", "c"], value: 1}91 applyPatches({}, [patch])92 }).toThrowErrorMatchingSnapshot()93 })94 it("applied patches cannot be modified", () => {95 // see also: https://github.com/immerjs/immer/issues/41196 const s0 = {97 items: [1]98 }99100 const [s1, p1] = produceWithPatches(s0, draft => {101 draft.items = []102 })103104 const replaceValueBefore = p1[0].value.slice()105106 const [s2, p2] = produceWithPatches(s1, draft => {107 draft.items.push(2)108 })109110 applyPatches(s0, [...p1, ...p2])111112 const replaceValueAfter = p1[0].value.slice()113114 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)115 })116})117118describe("simple assignment - 1", () => {119 runPatchTest(120 {x: 3},121 d => {122 d.x++123 },124 [{op: "replace", path: ["x"], value: 4}]125 )126})127128describe("simple assignment - 2", () => {129 runPatchTest(130 {x: {y: 4}},131 d => {132 d.x.y++133 },134 [{op: "replace", path: ["x", "y"], value: 5}]135 )136})137138describe("simple assignment - 3", () => {139 runPatchTest(140 {x: [{y: 4}]},141 d => {142 d.x[0].y++143 },144 [{op: "replace", path: ["x", 0, "y"], value: 5}]145 )146})147148describe("simple assignment - 4", () => {149 runPatchTest(150 new Map([["x", {y: 4}]]),151 d => {152 d.get("x").y++153 },154 [{op: "replace", path: ["x", "y"], value: 5}],155 [{op: "replace", path: ["x", "y"], value: 4}]156 )157})158159describe("simple assignment - 5", () => {160 runPatchTest(161 {x: new Map([["y", 4]])},162 d => {163 d.x.set("y", 5)164 },165 [{op: "replace", path: ["x", "y"], value: 5}],166 [{op: "replace", path: ["x", "y"], value: 4}]167 )168})169170describe("simple assignment - 6", () => {171 runPatchTest(172 new Map([["x", 1]]),173 d => {174 // Map.prototype.set should return the Map itself175 const res = d.set("x", 2)176 res.set("y", 3)177 },178 [179 {op: "replace", path: ["x"], value: 2},180 {op: "add", path: ["y"], value: 3}181 ],182 [183 {op: "replace", path: ["x"], value: 1},184 {op: "remove", path: ["y"]}185 ]186 )187})188189describe("simple assignment - 7", () => {190 const key1 = {prop: "val1"}191 const key2 = {prop: "val2"}192 runPatchTest(193 {x: new Map([[key1, 4]])},194 d => {195 d.x.set(key1, 5)196 d.x.set(key2, 6)197 },198 [199 {op: "replace", path: ["x", key1], value: 5},200 {op: "add", path: ["x", key2], value: 6}201 ],202 [203 {op: "replace", path: ["x", key1], value: 4},204 {op: "remove", path: ["x", key2]}205 ]206 )207})208209describe("delete 1", () => {210 runPatchTest(211 {x: {y: 4}},212 d => {213 delete d.x214 },215 [{op: "remove", path: ["x"]}]216 )217})218219describe("delete 2", () => {220 runPatchTest(221 new Map([["x", 1]]),222 d => {223 d.delete("x")224 },225 [{op: "remove", path: ["x"]}],226 [{op: "add", path: ["x"], value: 1}]227 )228})229230describe("delete 3", () => {231 runPatchTest(232 {x: new Map([["y", 1]])},233 d => {234 d.x.delete("y")235 },236 [{op: "remove", path: ["x", "y"]}],237 [{op: "add", path: ["x", "y"], value: 1}]238 )239})240241describe("delete 5", () => {242 const key1 = {prop: "val1"}243 const key2 = {prop: "val2"}244 runPatchTest(245 {246 x: new Map([247 [key1, 1],248 [key2, 2]249 ])250 },251 d => {252 d.x.delete(key1)253 d.x.delete(key2)254 },255 [256 {op: "remove", path: ["x", key1]},257 {op: "remove", path: ["x", key2]}258 ],259 [260 {op: "add", path: ["x", key1], value: 1},261 {op: "add", path: ["x", key2], value: 2}262 ]263 )264})265266describe("delete 6", () => {267 runPatchTest(268 new Set(["x", 1]),269 d => {270 d.delete("x")271 },272 [{op: "remove", path: [0], value: "x"}],273 [{op: "add", path: [0], value: "x"}]274 )275})276277describe("delete 7", () => {278 runPatchTest(279 {x: new Set(["y", 1])},280 d => {281 d.x.delete("y")282 },283 [{op: "remove", path: ["x", 0], value: "y"}],284 [{op: "add", path: ["x", 0], value: "y"}]285 )286})287288describe("renaming properties", () => {289 describe("nested object (no changes)", () => {290 runPatchTest(291 {a: {b: 1}},292 d => {293 d.x = d.a294 delete d.a295 },296 [297 {op: "add", path: ["x"], value: {b: 1}},298 {op: "remove", path: ["a"]}299 ]300 )301 })302303 describe("nested change in object", () => {304 runPatchTest(305 {306 a: {b: 1}307 },308 d => {309 d.a.b++310 },311 [{op: "replace", path: ["a", "b"], value: 2}],312 [{op: "replace", path: ["a", "b"], value: 1}]313 )314 })315316 describe("nested change in map", () => {317 runPatchTest(318 new Map([["a", new Map([["b", 1]])]]),319 d => {320 d.get("a").set("b", 2)321 },322 [{op: "replace", path: ["a", "b"], value: 2}],323 [{op: "replace", path: ["a", "b"], value: 1}]324 )325 })326327 describe("nested change in array", () => {328 runPatchTest(329 [[{b: 1}]],330 d => {331 d[0][0].b++332 },333 [{op: "replace", path: [0, 0, "b"], value: 2}],334 [{op: "replace", path: [0, 0, "b"], value: 1}]335 )336 })337338 describe("nested map (no changes)", () => {339 runPatchTest(340 new Map([["a", new Map([["b", 1]])]]),341 d => {342 d.set("x", d.get("a"))343 d.delete("a")344 },345 [346 {op: "add", path: ["x"], value: new Map([["b", 1]])},347 {op: "remove", path: ["a"]}348 ],349 [350 {op: "remove", path: ["x"]},351 {op: "add", path: ["a"], value: new Map([["b", 1]])}352 ]353 )354 })355356 describe("nested object (with changes)", () => {357 runPatchTest(358 {a: {b: 1, c: 1}},359 d => {360 let a = d.a361 a.b = 2 // change362 delete a.c // delete363 a.y = 2 // add364365 // rename366 d.x = a367 delete d.a368 },369 [370 {op: "add", path: ["x"], value: {b: 2, y: 2}},371 {op: "remove", path: ["a"]}372 ]373 )374 })375376 describe("nested map (with changes)", () => {377 runPatchTest(378 new Map([379 [380 "a",381 new Map([382 ["b", 1],383 ["c", 1]384 ])385 ]386 ]),387 d => {388 let a = d.get("a")389 a.set("b", 2) // change390 a.delete("c") // delete391 a.set("y", 2) // add392393 // rename394 d.set("x", a)395 d.delete("a")396 },397 [398 {399 op: "add",400 path: ["x"],401 value: new Map([402 ["b", 2],403 ["y", 2]404 ])405 },406 {op: "remove", path: ["a"]}407 ],408 [409 {op: "remove", path: ["x"]},410 {411 op: "add",412 path: ["a"],413 value: new Map([414 ["b", 1],415 ["c", 1]416 ])417 }418 ]419 )420 })421422 describe("deeply nested object (with changes)", () => {423 runPatchTest(424 {a: {b: {c: 1, d: 1}}},425 d => {426 let b = d.a.b427 b.c = 2 // change428 delete b.d // delete429 b.y = 2 // add430431 // rename432 d.a.x = b433 delete d.a.b434 },435 [436 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},437 {op: "remove", path: ["a", "b"]}438 ]439 )440 })441442 describe("deeply nested map (with changes)", () => {443 runPatchTest(444 new Map([445 [446 "a",447 new Map([448 [449 "b",450 new Map([451 ["c", 1],452 ["d", 1]453 ])454 ]455 ])456 ]457 ]),458 d => {459 let b = d.get("a").get("b")460 b.set("c", 2) // change461 b.delete("d") // delete462 b.set("y", 2) // add463464 // rename465 d.get("a").set("x", b)466 d.get("a").delete("b")467 },468 [469 {470 op: "add",471 path: ["a", "x"],472 value: new Map([473 ["c", 2],474 ["y", 2]475 ])476 },477 {op: "remove", path: ["a", "b"]}478 ],479 [480 {op: "remove", path: ["a", "x"]},481 {482 op: "add",483 path: ["a", "b"],484 value: new Map([485 ["c", 1],486 ["d", 1]487 ])488 }489 ]490 )491 })492})493494describe("minimum amount of changes", () => {495 runPatchTest(496 {x: 3, y: {a: 4}, z: 3},497 d => {498 d.y.a = 4499 d.y.b = 5500 Object.assign(d, {x: 4, y: {a: 2}})501 },502 [503 {op: "replace", path: ["x"], value: 4},504 {op: "replace", path: ["y"], value: {a: 2}}505 ]506 )507})508509describe("arrays - prepend", () => {510 runPatchTest(511 {x: [1, 2, 3]},512 d => {513 d.x.unshift(4)514 },515 [516 {op: "replace", path: ["x", 0], value: 4},517 {op: "replace", path: ["x", 1], value: 1},518 {op: "replace", path: ["x", 2], value: 2},519 {op: "add", path: ["x", 3], value: 3}520 ]521 )522})523524describe("arrays - multiple prepend", () => {525 runPatchTest(526 {x: [1, 2, 3]},527 d => {528 d.x.unshift(4)529 d.x.unshift(5)530 // 4,5,1,2,3531 },532 [533 {op: "replace", path: ["x", 0], value: 5},534 {op: "replace", path: ["x", 1], value: 4},535 {op: "replace", path: ["x", 2], value: 1},536 {op: "add", path: ["x", 3], value: 2},537 {op: "add", path: ["x", 4], value: 3}538 ]539 )540})541542describe("arrays - splice middle", () => {543 runPatchTest(544 {x: [1, 2, 3]},545 d => {546 d.x.splice(1, 1)547 },548 [549 {op: "replace", path: ["x", 1], value: 3},550 {op: "replace", path: ["x", "length"], value: 2}551 ]552 )553})554555describe("arrays - multiple splice", () => {556 runPatchTest(557 [0, 1, 2, 3, 4, 5, 0],558 d => {559 d.splice(4, 2, 3)560 // [0,1,2,3,3,0]561 d.splice(1, 2, 3)562 // [0,3,3,3,0]563 expect(d.slice()).toEqual([0, 3, 3, 3, 0])564 },565 [566 {op: "replace", path: [1], value: 3},567 {op: "replace", path: [2], value: 3},568 {op: "replace", path: [4], value: 0},569 {op: "replace", path: ["length"], value: 5}570 ]571 )572})573574describe("arrays - modify and shrink", () => {575 runPatchTest(576 {x: [1, 2, 3]},577 d => {578 d.x[0] = 4579 d.x.length = 2580 // [0, 2]581 },582 [583 {op: "replace", path: ["x", 0], value: 4},584 {op: "replace", path: ["x", "length"], value: 2}585 ],586 [587 {op: "replace", path: ["x", 0], value: 1},588 {op: "add", path: ["x", 2], value: 3}589 ]590 )591})592593describe("arrays - prepend then splice middle", () => {594 runPatchTest(595 {x: [1, 2, 3]},596 d => {597 d.x.unshift(4)598 d.x.splice(2, 1)599 // 4, 1, 3600 },601 [602 {op: "replace", path: ["x", 0], value: 4},603 {op: "replace", path: ["x", 1], value: 1}604 ]605 )606})607608describe("arrays - splice middle then prepend", () => {609 runPatchTest(610 {x: [1, 2, 3]},611 d => {612 d.x.splice(1, 1)613 d.x.unshift(4)614 // [4, 1, 3]615 },616 [617 {op: "replace", path: ["x", 0], value: 4},618 {op: "replace", path: ["x", 1], value: 1}619 ]620 )621})622623describe("arrays - truncate", () => {624 runPatchTest(625 {x: [1, 2, 3]},626 d => {627 d.x.length -= 2628 },629 [{op: "replace", path: ["x", "length"], value: 1}],630 [631 {op: "add", path: ["x", 1], value: 2},632 {op: "add", path: ["x", 2], value: 3}633 ]634 )635})636637describe("arrays - pop twice", () => {638 runPatchTest(639 {x: [1, 2, 3]},640 d => {641 d.x.pop()642 d.x.pop()643 },644 [{op: "replace", path: ["x", "length"], value: 1}]645 )646})647648describe("arrays - push multiple", () => {649 // These patches were more optimal pre immer 7, but not always correct650 runPatchTest(651 {x: [1, 2, 3]},652 d => {653 d.x.push(4, 5)654 },655 [656 {op: "add", path: ["x", 3], value: 4},657 {op: "add", path: ["x", 4], value: 5}658 ],659 [{op: "replace", path: ["x", "length"], value: 3}]660 )661})662663describe("arrays - splice (expand)", () => {664 // These patches were more optimal pre immer 7, but not always correct665 runPatchTest(666 {x: [1, 2, 3]},667 d => {668 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]669 },670 [671 {op: "replace", path: ["x", 1], value: 4},672 {op: "replace", path: ["x", 2], value: 5},673 {op: "add", path: ["x", 3], value: 6},674 {op: "add", path: ["x", 4], value: 3}675 ],676 [677 {op: "replace", path: ["x", 1], value: 2},678 {op: "replace", path: ["x", 2], value: 3},679 {op: "replace", path: ["x", "length"], value: 3}680 ]681 )682})683684describe("arrays - splice (shrink)", () => {685 // These patches were more optimal pre immer 7, but not always correct686 runPatchTest(687 {x: [1, 2, 3, 4, 5]},688 d => {689 d.x.splice(1, 3, 6) // [1, 6, 5]690 },691 [692 {op: "replace", path: ["x", 1], value: 6},693 {op: "replace", path: ["x", 2], value: 5},694 {op: "replace", path: ["x", "length"], value: 3}695 ],696 [697 {op: "replace", path: ["x", 1], value: 2},698 {op: "replace", path: ["x", 2], value: 3},699 {op: "add", path: ["x", 3], value: 4},700 {op: "add", path: ["x", 4], value: 5}701 ]702 )703})704705describe("arrays - delete", () => {706 runPatchTest(707 {708 x: [709 {a: 1, b: 2},710 {c: 3, d: 4}711 ]712 },713 d => {714 delete d.x[1].c715 },716 [{op: "remove", path: ["x", 1, "c"]}]717 )718})719720describe("sets - add - 1", () => {721 runPatchTest(722 new Set([1]),723 d => {724 d.add(2)725 },726 [{op: "add", path: [1], value: 2}],727 [{op: "remove", path: [1], value: 2}]728 )729})730731describe("sets - add, delete, add - 1", () => {732 runPatchTest(733 new Set([1]),734 d => {735 d.add(2)736 d.delete(2)737 d.add(2)738 },739 [{op: "add", path: [1], value: 2}],740 [{op: "remove", path: [1], value: 2}]741 )742})743744describe("sets - add, delete, add - 2", () => {745 runPatchTest(746 new Set([2, 1]),747 d => {748 d.add(2)749 d.delete(2)750 d.add(2)751 },752 [],753 []754 )755})756757describe("sets - mutate - 1", () => {758 const findById = (set, id) => {759 for (const item of set) {760 if (item.id === id) return item761 }762 }763 runPatchTest(764 new Set([765 {id: 1, val: "We"},766 {id: 2, val: "will"}767 ]),768 d => {769 const obj1 = findById(d, 1)770 const obj2 = findById(d, 2)771 obj1.val = "rock"772 obj2.val = "you"773 },774 [775 {op: "remove", path: [0], value: {id: 1, val: "We"}},776 {op: "remove", path: [1], value: {id: 2, val: "will"}},777 {op: "add", path: [0], value: {id: 1, val: "rock"}},778 {op: "add", path: [1], value: {id: 2, val: "you"}}779 ],780 [781 {op: "remove", path: [1], value: {id: 2, val: "you"}},782 {op: "remove", path: [0], value: {id: 1, val: "rock"}},783 {op: "add", path: [1], value: {id: 2, val: "will"}},784 {op: "add", path: [0], value: {id: 1, val: "We"}}785 ]786 )787})788789describe("arrays - splice should should result in remove op.", () => {790 // These patches were more optimal pre immer 7, but not always correct791 runPatchTest(792 [1, 2],793 d => {794 d.splice(1, 1)795 },796 [{op: "replace", path: ["length"], value: 1}],797 [{op: "add", path: [1], value: 2}]798 )799})800801describe("arrays - NESTED splice should should result in remove op.", () => {802 // These patches were more optimal pre immer 7, but not always correct803 runPatchTest(804 {a: {b: {c: [1, 2]}}},805 d => {806 d.a.b.c.splice(1, 1)807 },808 [{op: "replace", path: ["a", "b", "c", "length"], value: 1}],809 [{op: "add", path: ["a", "b", "c", 1], value: 2}]810 )811})812813describe("simple replacement", () => {814 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])815})816817describe("same value replacement - 1", () => {818 runPatchTest(819 {x: {y: 3}},820 d => {821 const a = d.x822 d.x = a823 },824 []825 )826})827828describe("same value replacement - 2", () => {829 runPatchTest(830 {x: {y: 3}},831 d => {832 const a = d.x833 d.x = 4834 d.x = a835 },836 []837 )838})839840describe("same value replacement - 3", () => {841 runPatchTest(842 {x: 3},843 d => {844 d.x = 3845 },846 []847 )848})849850describe("same value replacement - 4", () => {851 runPatchTest(852 {x: 3},853 d => {854 d.x = 4855 d.x = 3856 },857 []858 )859})860861describe("same value replacement - 5", () => {862 runPatchTest(863 new Map([["x", 3]]),864 d => {865 d.set("x", 4)866 d.set("x", 3)867 },868 [],869 []870 )871})872873describe("same value replacement - 6", () => {874 runPatchTest(875 new Set(["x", 3]),876 d => {877 d.delete("x")878 d.add("x")879 },880 [],881 []882 )883})884885describe("simple delete", () => {886 runPatchTest(887 {x: 2},888 d => {889 delete d.x890 },891 [892 {893 op: "remove",894 path: ["x"]895 }896 ]897 )898})899900describe("patch compressions yields correct results", () => {901 let p1, p2902 runPatchTest(903 {},904 d => {905 d.x = {test: true}906 },907 (p1 = [908 {909 op: "add",910 path: ["x"],911 value: {912 test: true913 }914 }915 ])916 )917 runPatchTest(918 {x: {test: true}},919 d => {920 delete d.x921 },922 (p2 = [923 {924 op: "remove",925 path: ["x"]926 }927 ])928 )929 const res = runPatchTest(930 {},931 d => {932 applyPatches(d, [...p1, ...p2])933 },934 []935 )936937 expect(res).toEqual({})938})939940describe("change then delete property", () => {941 const res = runPatchTest(942 {943 x: 1944 },945 d => {946 d.x = 2947 delete d.x948 },949 [950 {951 op: "remove",952 path: ["x"]953 }954 ]955 )956 test("valid result", () => {957 expect(res).toEqual({})958 })959})960961test("replaying patches with interweaved replacements should work correctly", () => {962 const patches = []963 const s0 = {x: 1}964965 const s1 = produce(966 s0,967 draft => {968 draft.x = 2969 },970 p => {971 patches.push(...p)972 }973 )974975 const s2 = produce(976 s1,977 draft => {978 return {x: 0}979 },980 p => {981 patches.push(...p)982 }983 )984985 const s3 = produce(986 s2,987 draft => {988 draft.x--989 },990 p => {991 patches.push(...p)992 }993 )994995 expect(s3).toEqual({x: -1}) // correct result996 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay997998 // manual replay on a draft should also be correct999 expect(1000 produce(s0, draft => {1001 return applyPatches(draft, patches)1002 })1003 ).toEqual({x: -1})1004})10051006describe("#468", () => {1007 function run() {1008 const item = {id: 1}1009 const state = [item]1010 const [nextState, patches] = produceWithPatches(state, draft => {1011 draft[0].id = 21012 draft[1] = item1013 })10141015 expect(nextState).toEqual([{id: 2}, {id: 1}])1016 expect(patches).toEqual([1017 {1018 op: "replace",1019 path: [0, "id"],1020 value: 21021 },1022 {1023 op: "add",1024 path: [1],1025 value: {1026 id: 11027 }1028 }1029 ])10301031 const final = applyPatches(state, patches)1032 expect(final).toEqual(nextState)1033 }10341035 test("es5", () => {1036 setUseProxies(false)1037 run()1038 })10391040 test("proxy", () => {1041 setUseProxies(true)1042 run()1043 })1044})10451046test("#521", () => {1047 const state = new Map()10481049 const [nextState, patches] = produceWithPatches(state, draft => {1050 draft.set("hello", new Set(["world"]))1051 })10521053 let patchedState = applyPatches(state, patches)1054 expect(patchedState).toEqual(nextState)10551056 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1057 draft.get("hello").add("immer")1058 })10591060 expect(applyPatches(nextState, patchesV2)).toEqual(1061 new Map([["hello", new Set(["world", "immer"])]])1062 )1063})10641065test("#559 patches works in a nested reducer with proxies", () => {1066 setUseProxies(true)10671068 const state = {1069 x: 1,1070 sub: {1071 y: [{a: 0}, {a: 1}]1072 }1073 }10741075 const changes = []1076 const inverseChanges = []10771078 const newState = produce(state, draft => {1079 draft.sub = produce(1080 draft.sub,1081 draft => {1082 draft.y.pop()1083 },1084 (patches, inversePatches) => {1085 expect(isDraft(inversePatches[0].value)).toBeFalsy()1086 expect(inversePatches[0].value).toMatchObject({a: 1})1087 changes.push(...patches)1088 inverseChanges.push(...inversePatches)1089 }1090 )1091 })10921093 const reversedSubState = applyPatches(newState.sub, inverseChanges)10941095 expect(reversedSubState).toMatchObject(state.sub)1096})10971098describe("#588", () => {1099 const reference = {value: {num: 53}}11001101 class Base {1102 [immerable] = true1103 get nested() {1104 return reference.value1105 }1106 set nested(value) {}1107 }11081109 let base = new Base()11101111 runPatchTest(1112 base,1113 vdraft => {1114 reference.value = vdraft1115 produce(base, bdraft => {1116 bdraft.nested.num = 421117 })1118 },1119 [{op: "add", path: ["num"], value: 42}]1120 )1121})11221123test("#676 patching Date objects", () => {1124 class Test {1125 constructor() {1126 this.test = true1127 }1128 perform() {1129 return "tested!"1130 }1131 }11321133 const [nextState, patches] = produceWithPatches({}, function(draft) {1134 draft.date = new Date(2020, 10, 10, 8, 8, 8, 3)1135 draft.test = new Test()1136 })11371138 expect(nextState.date.toJSON()).toMatchInlineSnapshot(1139 `"2020-11-10T08:08:08.003Z"`1140 )1141 expect(nextState.test.perform()).toBe("tested!")11421143 const rebuilt = applyPatches({}, patches)1144 expect(rebuilt.date).toBeInstanceOf(Date)1145 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1146 `"2020-11-10T08:08:08.003Z"`1147 )1148 expect(rebuilt.date).toEqual(new Date(2020, 10, 10, 8, 8, 8, 3))1149})