patch.js
This file
Path
__tests__/patch.js
1,122 lines · 19.9 KiB · JavaScript
Last changed by
fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5
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"10import {run} from "jest"1112enableAllPlugins()1314jest.setTimeout(1000)1516function runPatchTest(base, producer, patches, inversePathes) {17 let resultProxies, resultEs51819 function runPatchTestHelper() {20 let recordedPatches21 let recordedInversePatches22 const res = produce(base, producer, (p, i) => {23 recordedPatches = p24 recordedInversePatches = i25 })2627 test("produces the correct patches", () => {28 expect(recordedPatches).toEqual(patches)29 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)30 })3132 test("patches are replayable", () => {33 expect(applyPatches(base, recordedPatches)).toEqual(res)34 })3536 test("patches can be reversed", () => {37 expect(applyPatches(res, recordedInversePatches)).toEqual(base)38 })3940 return res41 }4243 describe(`proxy`, () => {44 setUseProxies(true)45 resultProxies = runPatchTestHelper()46 })4748 describe(`es5`, () => {49 setUseProxies(false)50 resultEs5 = runPatchTestHelper()51 test("ES5 and Proxy implementation yield same result", () => {52 expect(resultEs5).toEqual(resultProxies)53 })54 })5556 return resultProxies57}5859describe("applyPatches", () => {60 it("mutates the base state when it is a draft", () => {61 produce({a: 1}, draft => {62 const result = applyPatches(draft, [63 {op: "replace", path: ["a"], value: 2}64 ])65 expect(result).toBe(draft)66 expect(draft.a).toBe(2)67 })68 })69 it("produces a copy of the base state when not a draft", () => {70 const base = {a: 1}71 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])72 expect(result).not.toBe(base)73 expect(result.a).toBe(2)74 expect(base.a).toBe(1)75 })76 it('throws when `op` is not "add", "replace", nor "remove"', () => {77 expect(() => {78 const patch = {op: "copy", from: [0], path: [1]}79 applyPatches([2], [patch])80 }).toThrowErrorMatchingSnapshot()81 })82 it("throws when `path` cannot be resolved", () => {83 // missing parent84 expect(() => {85 const patch = {op: "add", path: ["a", "b"], value: 1}86 applyPatches({}, [patch])87 }).toThrowErrorMatchingSnapshot()8889 // missing grand-parent90 expect(() => {91 const patch = {op: "add", path: ["a", "b", "c"], value: 1}92 applyPatches({}, [patch])93 }).toThrowErrorMatchingSnapshot()94 })95 it("applied patches cannot be modified", () => {96 // see also: https://github.com/immerjs/immer/issues/41197 const s0 = {98 items: [1]99 }100101 const [s1, p1] = produceWithPatches(s0, draft => {102 draft.items = []103 })104105 const replaceValueBefore = p1[0].value.slice()106107 const [s2, p2] = produceWithPatches(s1, draft => {108 draft.items.push(2)109 })110111 applyPatches(s0, [...p1, ...p2])112113 const replaceValueAfter = p1[0].value.slice()114115 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)116 })117})118119describe("simple assignment - 1", () => {120 runPatchTest(121 {x: 3},122 d => {123 d.x++124 },125 [{op: "replace", path: ["x"], value: 4}]126 )127})128129describe("simple assignment - 2", () => {130 runPatchTest(131 {x: {y: 4}},132 d => {133 d.x.y++134 },135 [{op: "replace", path: ["x", "y"], value: 5}]136 )137})138139describe("simple assignment - 3", () => {140 runPatchTest(141 {x: [{y: 4}]},142 d => {143 d.x[0].y++144 },145 [{op: "replace", path: ["x", 0, "y"], value: 5}]146 )147})148149describe("simple assignment - 4", () => {150 runPatchTest(151 new Map([["x", {y: 4}]]),152 d => {153 d.get("x").y++154 },155 [{op: "replace", path: ["x", "y"], value: 5}],156 [{op: "replace", path: ["x", "y"], value: 4}]157 )158})159160describe("simple assignment - 5", () => {161 runPatchTest(162 {x: new Map([["y", 4]])},163 d => {164 d.x.set("y", 5)165 },166 [{op: "replace", path: ["x", "y"], value: 5}],167 [{op: "replace", path: ["x", "y"], value: 4}]168 )169})170171describe("simple assignment - 6", () => {172 runPatchTest(173 new Map([["x", 1]]),174 d => {175 // Map.prototype.set should return the Map itself176 const res = d.set("x", 2)177 res.set("y", 3)178 },179 [180 {op: "replace", path: ["x"], value: 2},181 {op: "add", path: ["y"], value: 3}182 ],183 [184 {op: "replace", path: ["x"], value: 1},185 {op: "remove", path: ["y"]}186 ]187 )188})189190describe("simple assignment - 7", () => {191 const key1 = {prop: "val1"}192 const key2 = {prop: "val2"}193 runPatchTest(194 {x: new Map([[key1, 4]])},195 d => {196 d.x.set(key1, 5)197 d.x.set(key2, 6)198 },199 [200 {op: "replace", path: ["x", key1], value: 5},201 {op: "add", path: ["x", key2], value: 6}202 ],203 [204 {op: "replace", path: ["x", key1], value: 4},205 {op: "remove", path: ["x", key2]}206 ]207 )208})209210describe("delete 1", () => {211 runPatchTest(212 {x: {y: 4}},213 d => {214 delete d.x215 },216 [{op: "remove", path: ["x"]}]217 )218})219220describe("delete 2", () => {221 runPatchTest(222 new Map([["x", 1]]),223 d => {224 d.delete("x")225 },226 [{op: "remove", path: ["x"]}],227 [{op: "add", path: ["x"], value: 1}]228 )229})230231describe("delete 3", () => {232 runPatchTest(233 {x: new Map([["y", 1]])},234 d => {235 d.x.delete("y")236 },237 [{op: "remove", path: ["x", "y"]}],238 [{op: "add", path: ["x", "y"], value: 1}]239 )240})241242describe("delete 5", () => {243 const key1 = {prop: "val1"}244 const key2 = {prop: "val2"}245 runPatchTest(246 {247 x: new Map([248 [key1, 1],249 [key2, 2]250 ])251 },252 d => {253 d.x.delete(key1)254 d.x.delete(key2)255 },256 [257 {op: "remove", path: ["x", key1]},258 {op: "remove", path: ["x", key2]}259 ],260 [261 {op: "add", path: ["x", key1], value: 1},262 {op: "add", path: ["x", key2], value: 2}263 ]264 )265})266267describe("delete 6", () => {268 runPatchTest(269 new Set(["x", 1]),270 d => {271 d.delete("x")272 },273 [{op: "remove", path: [0], value: "x"}],274 [{op: "add", path: [0], value: "x"}]275 )276})277278describe("delete 7", () => {279 runPatchTest(280 {x: new Set(["y", 1])},281 d => {282 d.x.delete("y")283 },284 [{op: "remove", path: ["x", 0], value: "y"}],285 [{op: "add", path: ["x", 0], value: "y"}]286 )287})288289describe("renaming properties", () => {290 describe("nested object (no changes)", () => {291 runPatchTest(292 {a: {b: 1}},293 d => {294 d.x = d.a295 delete d.a296 },297 [298 {op: "add", path: ["x"], value: {b: 1}},299 {op: "remove", path: ["a"]}300 ]301 )302 })303304 describe("nested change in object", () => {305 runPatchTest(306 {307 a: {b: 1}308 },309 d => {310 d.a.b++311 },312 [{op: "replace", path: ["a", "b"], value: 2}],313 [{op: "replace", path: ["a", "b"], value: 1}]314 )315 })316317 describe("nested change in map", () => {318 runPatchTest(319 new Map([["a", new Map([["b", 1]])]]),320 d => {321 d.get("a").set("b", 2)322 },323 [{op: "replace", path: ["a", "b"], value: 2}],324 [{op: "replace", path: ["a", "b"], value: 1}]325 )326 })327328 describe("nested change in array", () => {329 runPatchTest(330 [[{b: 1}]],331 d => {332 d[0][0].b++333 },334 [{op: "replace", path: [0, 0, "b"], value: 2}],335 [{op: "replace", path: [0, 0, "b"], value: 1}]336 )337 })338339 describe("nested map (no changes)", () => {340 runPatchTest(341 new Map([["a", new Map([["b", 1]])]]),342 d => {343 d.set("x", d.get("a"))344 d.delete("a")345 },346 [347 {op: "add", path: ["x"], value: new Map([["b", 1]])},348 {op: "remove", path: ["a"]}349 ],350 [351 {op: "remove", path: ["x"]},352 {op: "add", path: ["a"], value: new Map([["b", 1]])}353 ]354 )355 })356357 describe("nested object (with changes)", () => {358 runPatchTest(359 {a: {b: 1, c: 1}},360 d => {361 let a = d.a362 a.b = 2 // change363 delete a.c // delete364 a.y = 2 // add365366 // rename367 d.x = a368 delete d.a369 },370 [371 {op: "add", path: ["x"], value: {b: 2, y: 2}},372 {op: "remove", path: ["a"]}373 ]374 )375 })376377 describe("nested map (with changes)", () => {378 runPatchTest(379 new Map([380 [381 "a",382 new Map([383 ["b", 1],384 ["c", 1]385 ])386 ]387 ]),388 d => {389 let a = d.get("a")390 a.set("b", 2) // change391 a.delete("c") // delete392 a.set("y", 2) // add393394 // rename395 d.set("x", a)396 d.delete("a")397 },398 [399 {400 op: "add",401 path: ["x"],402 value: new Map([403 ["b", 2],404 ["y", 2]405 ])406 },407 {op: "remove", path: ["a"]}408 ],409 [410 {op: "remove", path: ["x"]},411 {412 op: "add",413 path: ["a"],414 value: new Map([415 ["b", 1],416 ["c", 1]417 ])418 }419 ]420 )421 })422423 describe("deeply nested object (with changes)", () => {424 runPatchTest(425 {a: {b: {c: 1, d: 1}}},426 d => {427 let b = d.a.b428 b.c = 2 // change429 delete b.d // delete430 b.y = 2 // add431432 // rename433 d.a.x = b434 delete d.a.b435 },436 [437 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},438 {op: "remove", path: ["a", "b"]}439 ]440 )441 })442443 describe("deeply nested map (with changes)", () => {444 runPatchTest(445 new Map([446 [447 "a",448 new Map([449 [450 "b",451 new Map([452 ["c", 1],453 ["d", 1]454 ])455 ]456 ])457 ]458 ]),459 d => {460 let b = d.get("a").get("b")461 b.set("c", 2) // change462 b.delete("d") // delete463 b.set("y", 2) // add464465 // rename466 d.get("a").set("x", b)467 d.get("a").delete("b")468 },469 [470 {471 op: "add",472 path: ["a", "x"],473 value: new Map([474 ["c", 2],475 ["y", 2]476 ])477 },478 {op: "remove", path: ["a", "b"]}479 ],480 [481 {op: "remove", path: ["a", "x"]},482 {483 op: "add",484 path: ["a", "b"],485 value: new Map([486 ["c", 1],487 ["d", 1]488 ])489 }490 ]491 )492 })493})494495describe("minimum amount of changes", () => {496 runPatchTest(497 {x: 3, y: {a: 4}, z: 3},498 d => {499 d.y.a = 4500 d.y.b = 5501 Object.assign(d, {x: 4, y: {a: 2}})502 },503 [504 {op: "replace", path: ["x"], value: 4},505 {op: "replace", path: ["y"], value: {a: 2}}506 ]507 )508})509510describe("arrays - prepend", () => {511 runPatchTest(512 {x: [1, 2, 3]},513 d => {514 d.x.unshift(4)515 },516 [517 {op: "replace", path: ["x", 0], value: 4},518 {op: "replace", path: ["x", 1], value: 1},519 {op: "replace", path: ["x", 2], value: 2},520 {op: "add", path: ["x", 3], value: 3}521 ]522 )523})524525describe("arrays - multiple prepend", () => {526 runPatchTest(527 {x: [1, 2, 3]},528 d => {529 d.x.unshift(4)530 d.x.unshift(5)531 // 4,5,1,2,3532 },533 [534 {op: "replace", path: ["x", 0], value: 5},535 {op: "replace", path: ["x", 1], value: 4},536 {op: "replace", path: ["x", 2], value: 1},537 {op: "add", path: ["x", 3], value: 2},538 {op: "add", path: ["x", 4], value: 3}539 ]540 )541})542543describe("arrays - splice middle", () => {544 runPatchTest(545 {x: [1, 2, 3]},546 d => {547 d.x.splice(1, 1)548 },549 [550 {op: "replace", path: ["x", 1], value: 3},551 {op: "replace", path: ["x", "length"], value: 2}552 ]553 )554})555556describe("arrays - multiple splice", () => {557 runPatchTest(558 [0, 1, 2, 3, 4, 5, 0],559 d => {560 d.splice(4, 2, 3)561 // [0,1,2,3,3,0]562 d.splice(1, 2, 3)563 // [0,3,3,3,0]564 expect(d.slice()).toEqual([0, 3, 3, 3, 0])565 },566 [567 {op: "replace", path: [1], value: 3},568 {op: "replace", path: [2], value: 3},569 {op: "replace", path: [4], value: 0},570 {op: "replace", path: ["length"], value: 5}571 ]572 )573})574575describe("arrays - modify and shrink", () => {576 runPatchTest(577 {x: [1, 2, 3]},578 d => {579 d.x[0] = 4580 d.x.length = 2581 // [0, 2]582 },583 [584 {op: "replace", path: ["x", 0], value: 4},585 {op: "replace", path: ["x", "length"], value: 2}586 ],587 [588 {op: "replace", path: ["x", 0], value: 1},589 {op: "add", path: ["x", 2], value: 3}590 ]591 )592})593594describe("arrays - prepend then splice middle", () => {595 runPatchTest(596 {x: [1, 2, 3]},597 d => {598 d.x.unshift(4)599 d.x.splice(2, 1)600 // 4, 1, 3601 },602 [603 {op: "replace", path: ["x", 0], value: 4},604 {op: "replace", path: ["x", 1], value: 1}605 ]606 )607})608609describe("arrays - splice middle then prepend", () => {610 runPatchTest(611 {x: [1, 2, 3]},612 d => {613 d.x.splice(1, 1)614 d.x.unshift(4)615 // [4, 1, 3]616 },617 [618 {op: "replace", path: ["x", 0], value: 4},619 {op: "replace", path: ["x", 1], value: 1}620 ]621 )622})623624describe("arrays - truncate", () => {625 runPatchTest(626 {x: [1, 2, 3]},627 d => {628 d.x.length -= 2629 },630 [{op: "replace", path: ["x", "length"], value: 1}],631 [632 {op: "add", path: ["x", 1], value: 2},633 {op: "add", path: ["x", 2], value: 3}634 ]635 )636})637638describe("arrays - pop twice", () => {639 runPatchTest(640 {x: [1, 2, 3]},641 d => {642 d.x.pop()643 d.x.pop()644 },645 [{op: "replace", path: ["x", "length"], value: 1}]646 )647})648649describe("arrays - push multiple", () => {650 // These patches were more optimal pre immer 7, but not always correct651 runPatchTest(652 {x: [1, 2, 3]},653 d => {654 d.x.push(4, 5)655 },656 [657 {op: "add", path: ["x", 3], value: 4},658 {op: "add", path: ["x", 4], value: 5}659 ],660 [{op: "replace", path: ["x", "length"], value: 3}]661 )662})663664describe("arrays - splice (expand)", () => {665 // These patches were more optimal pre immer 7, but not always correct666 runPatchTest(667 {x: [1, 2, 3]},668 d => {669 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]670 },671 [672 {op: "replace", path: ["x", 1], value: 4},673 {op: "replace", path: ["x", 2], value: 5},674 {op: "add", path: ["x", 3], value: 6},675 {op: "add", path: ["x", 4], value: 3}676 ],677 [678 {op: "replace", path: ["x", 1], value: 2},679 {op: "replace", path: ["x", 2], value: 3},680 {op: "replace", path: ["x", "length"], value: 3}681 ]682 )683})684685describe("arrays - splice (shrink)", () => {686 // These patches were more optimal pre immer 7, but not always correct687 runPatchTest(688 {x: [1, 2, 3, 4, 5]},689 d => {690 d.x.splice(1, 3, 6) // [1, 6, 5]691 },692 [693 {op: "replace", path: ["x", 1], value: 6},694 {op: "replace", path: ["x", 2], value: 5},695 {op: "replace", path: ["x", "length"], value: 3}696 ],697 [698 {op: "replace", path: ["x", 1], value: 2},699 {op: "replace", path: ["x", 2], value: 3},700 {op: "add", path: ["x", 3], value: 4},701 {op: "add", path: ["x", 4], value: 5}702 ]703 )704})705706describe("arrays - delete", () => {707 runPatchTest(708 {709 x: [710 {a: 1, b: 2},711 {c: 3, d: 4}712 ]713 },714 d => {715 delete d.x[1].c716 },717 [{op: "remove", path: ["x", 1, "c"]}]718 )719})720721describe("sets - add - 1", () => {722 runPatchTest(723 new Set([1]),724 d => {725 d.add(2)726 },727 [{op: "add", path: [1], value: 2}],728 [{op: "remove", path: [1], value: 2}]729 )730})731732describe("sets - add, delete, add - 1", () => {733 runPatchTest(734 new Set([1]),735 d => {736 d.add(2)737 d.delete(2)738 d.add(2)739 },740 [{op: "add", path: [1], value: 2}],741 [{op: "remove", path: [1], value: 2}]742 )743})744745describe("sets - add, delete, add - 2", () => {746 runPatchTest(747 new Set([2, 1]),748 d => {749 d.add(2)750 d.delete(2)751 d.add(2)752 },753 [],754 []755 )756})757758describe("sets - mutate - 1", () => {759 const findById = (set, id) => {760 for (const item of set) {761 if (item.id === id) return item762 }763 }764 runPatchTest(765 new Set([766 {id: 1, val: "We"},767 {id: 2, val: "will"}768 ]),769 d => {770 const obj1 = findById(d, 1)771 const obj2 = findById(d, 2)772 obj1.val = "rock"773 obj2.val = "you"774 },775 [776 {op: "remove", path: [0], value: {id: 1, val: "We"}},777 {op: "remove", path: [1], value: {id: 2, val: "will"}},778 {op: "add", path: [0], value: {id: 1, val: "rock"}},779 {op: "add", path: [1], value: {id: 2, val: "you"}}780 ],781 [782 {op: "remove", path: [1], value: {id: 2, val: "you"}},783 {op: "remove", path: [0], value: {id: 1, val: "rock"}},784 {op: "add", path: [1], value: {id: 2, val: "will"}},785 {op: "add", path: [0], value: {id: 1, val: "We"}}786 ]787 )788})789790describe("arrays - splice should should result in remove op.", () => {791 // These patches were more optimal pre immer 7, but not always correct792 runPatchTest(793 [1, 2],794 d => {795 d.splice(1, 1)796 },797 [{op: "replace", path: ["length"], value: 1}],798 [{op: "add", path: [1], value: 2}]799 )800})801802describe("arrays - NESTED splice should should result in remove op.", () => {803 // These patches were more optimal pre immer 7, but not always correct804 runPatchTest(805 {a: {b: {c: [1, 2]}}},806 d => {807 d.a.b.c.splice(1, 1)808 },809 [{op: "replace", path: ["a", "b", "c", "length"], value: 1}],810 [{op: "add", path: ["a", "b", "c", 1], value: 2}]811 )812})813814describe("simple replacement", () => {815 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])816})817818describe("same value replacement - 1", () => {819 runPatchTest(820 {x: {y: 3}},821 d => {822 const a = d.x823 d.x = a824 },825 []826 )827})828829describe("same value replacement - 2", () => {830 runPatchTest(831 {x: {y: 3}},832 d => {833 const a = d.x834 d.x = 4835 d.x = a836 },837 []838 )839})840841describe("same value replacement - 3", () => {842 runPatchTest(843 {x: 3},844 d => {845 d.x = 3846 },847 []848 )849})850851describe("same value replacement - 4", () => {852 runPatchTest(853 {x: 3},854 d => {855 d.x = 4856 d.x = 3857 },858 []859 )860})861862describe("same value replacement - 5", () => {863 runPatchTest(864 new Map([["x", 3]]),865 d => {866 d.set("x", 4)867 d.set("x", 3)868 },869 [],870 []871 )872})873874describe("same value replacement - 6", () => {875 runPatchTest(876 new Set(["x", 3]),877 d => {878 d.delete("x")879 d.add("x")880 },881 [],882 []883 )884})885886describe("simple delete", () => {887 runPatchTest(888 {x: 2},889 d => {890 delete d.x891 },892 [893 {894 op: "remove",895 path: ["x"]896 }897 ]898 )899})900901describe("patch compressions yields correct results", () => {902 let p1, p2903 runPatchTest(904 {},905 d => {906 d.x = {test: true}907 },908 (p1 = [909 {910 op: "add",911 path: ["x"],912 value: {913 test: true914 }915 }916 ])917 )918 runPatchTest(919 {x: {test: true}},920 d => {921 delete d.x922 },923 (p2 = [924 {925 op: "remove",926 path: ["x"]927 }928 ])929 )930 const res = runPatchTest(931 {},932 d => {933 applyPatches(d, [...p1, ...p2])934 },935 []936 )937938 expect(res).toEqual({})939})940941describe("change then delete property", () => {942 const res = runPatchTest(943 {944 x: 1945 },946 d => {947 d.x = 2948 delete d.x949 },950 [951 {952 op: "remove",953 path: ["x"]954 }955 ]956 )957 test("valid result", () => {958 expect(res).toEqual({})959 })960})961962test("replaying patches with interweaved replacements should work correctly", () => {963 const patches = []964 const s0 = {x: 1}965966 const s1 = produce(967 s0,968 draft => {969 draft.x = 2970 },971 p => {972 patches.push(...p)973 }974 )975976 const s2 = produce(977 s1,978 draft => {979 return {x: 0}980 },981 p => {982 patches.push(...p)983 }984 )985986 const s3 = produce(987 s2,988 draft => {989 draft.x--990 },991 p => {992 patches.push(...p)993 }994 )995996 expect(s3).toEqual({x: -1}) // correct result997 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay998999 // manual replay on a draft should also be correct1000 expect(1001 produce(s0, draft => {1002 return applyPatches(draft, patches)1003 })1004 ).toEqual({x: -1})1005})10061007describe("#468", () => {1008 function run() {1009 const item = {id: 1}1010 const state = [item]1011 const [nextState, patches] = produceWithPatches(state, draft => {1012 draft[0].id = 21013 draft[1] = item1014 })10151016 expect(nextState).toEqual([{id: 2}, {id: 1}])1017 expect(patches).toEqual([1018 {1019 op: "replace",1020 path: [0, "id"],1021 value: 21022 },1023 {1024 op: "add",1025 path: [1],1026 value: {1027 id: 11028 }1029 }1030 ])10311032 const final = applyPatches(state, patches)1033 expect(final).toEqual(nextState)1034 }10351036 test("es5", () => {1037 setUseProxies(false)1038 run()1039 })10401041 test("proxy", () => {1042 setUseProxies(true)1043 run()1044 })1045})10461047test("#521", () => {1048 const state = new Map()10491050 const [nextState, patches] = produceWithPatches(state, draft => {1051 draft.set("hello", new Set(["world"]))1052 })10531054 let patchedState = applyPatches(state, patches)1055 expect(patchedState).toEqual(nextState)10561057 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1058 draft.get("hello").add("immer")1059 })10601061 expect(applyPatches(nextState, patchesV2)).toEqual(1062 new Map([["hello", new Set(["world", "immer"])]])1063 )1064})10651066test("#559 patches works in a nested reducer with proxies", () => {1067 setUseProxies(true)10681069 const state = {1070 x: 1,1071 sub: {1072 y: [{a: 0}, {a: 1}]1073 }1074 }10751076 const changes = []1077 const inverseChanges = []10781079 const newState = produce(state, draft => {1080 draft.sub = produce(1081 draft.sub,1082 draft => {1083 draft.y.pop()1084 },1085 (patches, inversePatches) => {1086 expect(isDraft(inversePatches[0].value)).toBeFalsy()1087 expect(inversePatches[0].value).toMatchObject({a: 1})1088 changes.push(...patches)1089 inverseChanges.push(...inversePatches)1090 }1091 )1092 })10931094 const reversedSubState = applyPatches(newState.sub, inverseChanges)10951096 expect(reversedSubState).toMatchObject(state.sub)1097})10981099describe("#588", () => {1100 const reference = {value: {num: 53}}11011102 class Base {1103 [immerable] = true1104 get nested() {1105 return reference.value1106 }1107 set nested(value) {}1108 }11091110 let base = new Base()11111112 runPatchTest(1113 base,1114 vdraft => {1115 reference.value = vdraft1116 produce(base, bdraft => {1117 bdraft.nested.num = 421118 })1119 },1120 [{op: "add", path: ["num"], value: 42}]1121 )1122})