patch.js
This file
Path
__tests__/patch.js
1,321 lines · 24.6 KiB · JavaScript
Last changed by
fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b
History of this fileTake it
1"use strict"2import produce, {3 setUseProxies,4 applyPatches,5 produceWithPatches,6 enableAllPlugins,7 isDraft,8 immerable,9 nothing10} from "../src/immer"1112enableAllPlugins()1314jest.setTimeout(1000)1516const isProd = process.env.NODE_ENV === "production"1718function runPatchTest(base, producer, patches, inversePathes) {19 let resultProxies, resultEs52021 function runPatchTestHelper() {22 let recordedPatches23 let recordedInversePatches24 const res = produce(base, producer, (p, i) => {25 recordedPatches = p26 recordedInversePatches = i27 })2829 test("produces the correct patches", () => {30 expect(recordedPatches).toEqual(patches)31 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)32 })3334 test("patches are replayable", () => {35 expect(applyPatches(base, recordedPatches)).toEqual(res)36 })3738 test("patches can be reversed", () => {39 expect(applyPatches(res, recordedInversePatches)).toEqual(base)40 })4142 return res43 }4445 describe(`proxy`, () => {46 setUseProxies(true)47 resultProxies = runPatchTestHelper()48 })4950 describe(`es5`, () => {51 setUseProxies(false)52 resultEs5 = runPatchTestHelper()53 test("ES5 and Proxy implementation yield same result", () => {54 expect(resultEs5).toEqual(resultProxies)55 })56 })5758 return resultProxies59}6061describe("applyPatches", () => {62 it("mutates the base state when it is a draft", () => {63 produce({a: 1}, draft => {64 const result = applyPatches(draft, [65 {op: "replace", path: ["a"], value: 2}66 ])67 expect(result).toBe(draft)68 expect(draft.a).toBe(2)69 })70 })71 it("produces a copy of the base state when not a draft", () => {72 const base = {a: 1}73 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])74 expect(result).not.toBe(base)75 expect(result.a).toBe(2)76 expect(base.a).toBe(1)77 })78 it('throws when `op` is not "add", "replace", nor "remove"', () => {79 expect(() => {80 const patch = {op: "copy", from: [0], path: [1]}81 applyPatches([2], [patch])82 }).toThrowErrorMatchingSnapshot()83 })84 it("throws when `path` cannot be resolved", () => {85 // missing parent86 expect(() => {87 const patch = {op: "add", path: ["a", "b"], value: 1}88 applyPatches({}, [patch])89 }).toThrowErrorMatchingSnapshot()9091 // missing grand-parent92 expect(() => {93 const patch = {op: "add", path: ["a", "b", "c"], value: 1}94 applyPatches({}, [patch])95 }).toThrowErrorMatchingSnapshot()96 })97 it("applied patches cannot be modified", () => {98 // see also: https://github.com/immerjs/immer/issues/41199 const s0 = {100 items: [1]101 }102103 const [s1, p1] = produceWithPatches(s0, draft => {104 draft.items = []105 })106107 const replaceValueBefore = p1[0].value.slice()108109 const [s2, p2] = produceWithPatches(s1, draft => {110 draft.items.push(2)111 })112113 applyPatches(s0, [...p1, ...p2])114115 const replaceValueAfter = p1[0].value.slice()116117 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)118 })119})120121describe("simple assignment - 1", () => {122 runPatchTest(123 {x: 3},124 d => {125 d.x++126 },127 [{op: "replace", path: ["x"], value: 4}]128 )129})130131describe("simple assignment - 2", () => {132 runPatchTest(133 {x: {y: 4}},134 d => {135 d.x.y++136 },137 [{op: "replace", path: ["x", "y"], value: 5}]138 )139})140141describe("simple assignment - 3", () => {142 runPatchTest(143 {x: [{y: 4}]},144 d => {145 d.x[0].y++146 },147 [{op: "replace", path: ["x", 0, "y"], value: 5}]148 )149})150151describe("simple assignment - 4", () => {152 runPatchTest(153 new Map([["x", {y: 4}]]),154 d => {155 d.get("x").y++156 },157 [{op: "replace", path: ["x", "y"], value: 5}],158 [{op: "replace", path: ["x", "y"], value: 4}]159 )160})161162describe("simple assignment - 5", () => {163 runPatchTest(164 {x: new Map([["y", 4]])},165 d => {166 d.x.set("y", 5)167 },168 [{op: "replace", path: ["x", "y"], value: 5}],169 [{op: "replace", path: ["x", "y"], value: 4}]170 )171})172173describe("simple assignment - 6", () => {174 runPatchTest(175 new Map([["x", 1]]),176 d => {177 // Map.prototype.set should return the Map itself178 const res = d.set("x", 2)179 res.set("y", 3)180 },181 [182 {op: "replace", path: ["x"], value: 2},183 {op: "add", path: ["y"], value: 3}184 ],185 [186 {op: "replace", path: ["x"], value: 1},187 {op: "remove", path: ["y"]}188 ]189 )190})191192describe("simple assignment - 7", () => {193 const key1 = {prop: "val1"}194 const key2 = {prop: "val2"}195 runPatchTest(196 {x: new Map([[key1, 4]])},197 d => {198 d.x.set(key1, 5)199 d.x.set(key2, 6)200 },201 [202 {op: "replace", path: ["x", key1], value: 5},203 {op: "add", path: ["x", key2], value: 6}204 ],205 [206 {op: "replace", path: ["x", key1], value: 4},207 {op: "remove", path: ["x", key2]}208 ]209 )210})211212describe("delete 1", () => {213 runPatchTest(214 {x: {y: 4}},215 d => {216 delete d.x217 },218 [{op: "remove", path: ["x"]}]219 )220})221222describe("delete 2", () => {223 runPatchTest(224 new Map([["x", 1]]),225 d => {226 d.delete("x")227 },228 [{op: "remove", path: ["x"]}],229 [{op: "add", path: ["x"], value: 1}]230 )231})232233describe("delete 3", () => {234 runPatchTest(235 {x: new Map([["y", 1]])},236 d => {237 d.x.delete("y")238 },239 [{op: "remove", path: ["x", "y"]}],240 [{op: "add", path: ["x", "y"], value: 1}]241 )242})243244describe("delete 5", () => {245 const key1 = {prop: "val1"}246 const key2 = {prop: "val2"}247 runPatchTest(248 {249 x: new Map([250 [key1, 1],251 [key2, 2]252 ])253 },254 d => {255 d.x.delete(key1)256 d.x.delete(key2)257 },258 [259 {op: "remove", path: ["x", key1]},260 {op: "remove", path: ["x", key2]}261 ],262 [263 {op: "add", path: ["x", key1], value: 1},264 {op: "add", path: ["x", key2], value: 2}265 ]266 )267})268269describe("delete 6", () => {270 runPatchTest(271 new Set(["x", 1]),272 d => {273 d.delete("x")274 },275 [{op: "remove", path: [0], value: "x"}],276 [{op: "add", path: [0], value: "x"}]277 )278})279280describe("delete 7", () => {281 runPatchTest(282 {x: new Set(["y", 1])},283 d => {284 d.x.delete("y")285 },286 [{op: "remove", path: ["x", 0], value: "y"}],287 [{op: "add", path: ["x", 0], value: "y"}]288 )289})290291describe("renaming properties", () => {292 describe("nested object (no changes)", () => {293 runPatchTest(294 {a: {b: 1}},295 d => {296 d.x = d.a297 delete d.a298 },299 [300 {op: "add", path: ["x"], value: {b: 1}},301 {op: "remove", path: ["a"]}302 ]303 )304 })305306 describe("nested change in object", () => {307 runPatchTest(308 {309 a: {b: 1}310 },311 d => {312 d.a.b++313 },314 [{op: "replace", path: ["a", "b"], value: 2}],315 [{op: "replace", path: ["a", "b"], value: 1}]316 )317 })318319 describe("nested change in map", () => {320 runPatchTest(321 new Map([["a", new Map([["b", 1]])]]),322 d => {323 d.get("a").set("b", 2)324 },325 [{op: "replace", path: ["a", "b"], value: 2}],326 [{op: "replace", path: ["a", "b"], value: 1}]327 )328 })329330 describe("nested change in array", () => {331 runPatchTest(332 [[{b: 1}]],333 d => {334 d[0][0].b++335 },336 [{op: "replace", path: [0, 0, "b"], value: 2}],337 [{op: "replace", path: [0, 0, "b"], value: 1}]338 )339 })340341 describe("nested map (no changes)", () => {342 runPatchTest(343 new Map([["a", new Map([["b", 1]])]]),344 d => {345 d.set("x", d.get("a"))346 d.delete("a")347 },348 [349 {op: "add", path: ["x"], value: new Map([["b", 1]])},350 {op: "remove", path: ["a"]}351 ],352 [353 {op: "remove", path: ["x"]},354 {op: "add", path: ["a"], value: new Map([["b", 1]])}355 ]356 )357 })358359 describe("nested object (with changes)", () => {360 runPatchTest(361 {a: {b: 1, c: 1}},362 d => {363 let a = d.a364 a.b = 2 // change365 delete a.c // delete366 a.y = 2 // add367368 // rename369 d.x = a370 delete d.a371 },372 [373 {op: "add", path: ["x"], value: {b: 2, y: 2}},374 {op: "remove", path: ["a"]}375 ]376 )377 })378379 describe("nested map (with changes)", () => {380 runPatchTest(381 new Map([382 [383 "a",384 new Map([385 ["b", 1],386 ["c", 1]387 ])388 ]389 ]),390 d => {391 let a = d.get("a")392 a.set("b", 2) // change393 a.delete("c") // delete394 a.set("y", 2) // add395396 // rename397 d.set("x", a)398 d.delete("a")399 },400 [401 {402 op: "add",403 path: ["x"],404 value: new Map([405 ["b", 2],406 ["y", 2]407 ])408 },409 {op: "remove", path: ["a"]}410 ],411 [412 {op: "remove", path: ["x"]},413 {414 op: "add",415 path: ["a"],416 value: new Map([417 ["b", 1],418 ["c", 1]419 ])420 }421 ]422 )423 })424425 describe("deeply nested object (with changes)", () => {426 runPatchTest(427 {a: {b: {c: 1, d: 1}}},428 d => {429 let b = d.a.b430 b.c = 2 // change431 delete b.d // delete432 b.y = 2 // add433434 // rename435 d.a.x = b436 delete d.a.b437 },438 [439 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},440 {op: "remove", path: ["a", "b"]}441 ]442 )443 })444445 describe("deeply nested map (with changes)", () => {446 runPatchTest(447 new Map([448 [449 "a",450 new Map([451 [452 "b",453 new Map([454 ["c", 1],455 ["d", 1]456 ])457 ]458 ])459 ]460 ]),461 d => {462 let b = d.get("a").get("b")463 b.set("c", 2) // change464 b.delete("d") // delete465 b.set("y", 2) // add466467 // rename468 d.get("a").set("x", b)469 d.get("a").delete("b")470 },471 [472 {473 op: "add",474 path: ["a", "x"],475 value: new Map([476 ["c", 2],477 ["y", 2]478 ])479 },480 {op: "remove", path: ["a", "b"]}481 ],482 [483 {op: "remove", path: ["a", "x"]},484 {485 op: "add",486 path: ["a", "b"],487 value: new Map([488 ["c", 1],489 ["d", 1]490 ])491 }492 ]493 )494 })495})496497describe("minimum amount of changes", () => {498 runPatchTest(499 {x: 3, y: {a: 4}, z: 3},500 d => {501 d.y.a = 4502 d.y.b = 5503 Object.assign(d, {x: 4, y: {a: 2}})504 },505 [506 {op: "replace", path: ["x"], value: 4},507 {op: "replace", path: ["y"], value: {a: 2}}508 ]509 )510})511512describe("arrays - prepend", () => {513 runPatchTest(514 {x: [1, 2, 3]},515 d => {516 d.x.unshift(4)517 },518 [519 {op: "replace", path: ["x", 0], value: 4},520 {op: "replace", path: ["x", 1], value: 1},521 {op: "replace", path: ["x", 2], value: 2},522 {op: "add", path: ["x", 3], value: 3}523 ]524 )525})526527describe("arrays - multiple prepend", () => {528 runPatchTest(529 {x: [1, 2, 3]},530 d => {531 d.x.unshift(4)532 d.x.unshift(5)533 // 4,5,1,2,3534 },535 [536 {op: "replace", path: ["x", 0], value: 5},537 {op: "replace", path: ["x", 1], value: 4},538 {op: "replace", path: ["x", 2], value: 1},539 {op: "add", path: ["x", 3], value: 2},540 {op: "add", path: ["x", 4], value: 3}541 ]542 )543})544545describe("arrays - splice middle", () => {546 runPatchTest(547 {x: [1, 2, 3]},548 d => {549 d.x.splice(1, 1)550 },551 [552 {op: "replace", path: ["x", 1], value: 3},553 {op: "replace", path: ["x", "length"], value: 2}554 ]555 )556})557558describe("arrays - multiple splice", () => {559 runPatchTest(560 [0, 1, 2, 3, 4, 5, 0],561 d => {562 d.splice(4, 2, 3)563 // [0,1,2,3,3,0]564 d.splice(1, 2, 3)565 // [0,3,3,3,0]566 expect(d.slice()).toEqual([0, 3, 3, 3, 0])567 },568 [569 {op: "replace", path: [1], value: 3},570 {op: "replace", path: [2], value: 3},571 {op: "replace", path: [4], value: 0},572 {op: "replace", path: ["length"], value: 5}573 ]574 )575})576577describe("arrays - modify and shrink", () => {578 runPatchTest(579 {x: [1, 2, 3]},580 d => {581 d.x[0] = 4582 d.x.length = 2583 // [0, 2]584 },585 [586 {op: "replace", path: ["x", 0], value: 4},587 {op: "replace", path: ["x", "length"], value: 2}588 ],589 [590 {op: "replace", path: ["x", 0], value: 1},591 {op: "add", path: ["x", 2], value: 3}592 ]593 )594})595596describe("arrays - prepend then splice middle", () => {597 runPatchTest(598 {x: [1, 2, 3]},599 d => {600 d.x.unshift(4)601 d.x.splice(2, 1)602 // 4, 1, 3603 },604 [605 {op: "replace", path: ["x", 0], value: 4},606 {op: "replace", path: ["x", 1], value: 1}607 ]608 )609})610611describe("arrays - splice middle then prepend", () => {612 runPatchTest(613 {x: [1, 2, 3]},614 d => {615 d.x.splice(1, 1)616 d.x.unshift(4)617 // [4, 1, 3]618 },619 [620 {op: "replace", path: ["x", 0], value: 4},621 {op: "replace", path: ["x", 1], value: 1}622 ]623 )624})625626describe("arrays - truncate", () => {627 runPatchTest(628 {x: [1, 2, 3]},629 d => {630 d.x.length -= 2631 },632 [{op: "replace", path: ["x", "length"], value: 1}],633 [634 {op: "add", path: ["x", 1], value: 2},635 {op: "add", path: ["x", 2], value: 3}636 ]637 )638})639640describe("arrays - pop twice", () => {641 runPatchTest(642 {x: [1, 2, 3]},643 d => {644 d.x.pop()645 d.x.pop()646 },647 [{op: "replace", path: ["x", "length"], value: 1}]648 )649})650651describe("arrays - push multiple", () => {652 // These patches were more optimal pre immer 7, but not always correct653 runPatchTest(654 {x: [1, 2, 3]},655 d => {656 d.x.push(4, 5)657 },658 [659 {op: "add", path: ["x", 3], value: 4},660 {op: "add", path: ["x", 4], value: 5}661 ],662 [{op: "replace", path: ["x", "length"], value: 3}]663 )664})665666describe("arrays - splice (expand)", () => {667 // These patches were more optimal pre immer 7, but not always correct668 runPatchTest(669 {x: [1, 2, 3]},670 d => {671 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]672 },673 [674 {op: "replace", path: ["x", 1], value: 4},675 {op: "replace", path: ["x", 2], value: 5},676 {op: "add", path: ["x", 3], value: 6},677 {op: "add", path: ["x", 4], value: 3}678 ],679 [680 {op: "replace", path: ["x", 1], value: 2},681 {op: "replace", path: ["x", 2], value: 3},682 {op: "replace", path: ["x", "length"], value: 3}683 ]684 )685})686687describe("arrays - splice (shrink)", () => {688 // These patches were more optimal pre immer 7, but not always correct689 runPatchTest(690 {x: [1, 2, 3, 4, 5]},691 d => {692 d.x.splice(1, 3, 6) // [1, 6, 5]693 },694 [695 {op: "replace", path: ["x", 1], value: 6},696 {op: "replace", path: ["x", 2], value: 5},697 {op: "replace", path: ["x", "length"], value: 3}698 ],699 [700 {op: "replace", path: ["x", 1], value: 2},701 {op: "replace", path: ["x", 2], value: 3},702 {op: "add", path: ["x", 3], value: 4},703 {op: "add", path: ["x", 4], value: 5}704 ]705 )706})707708describe("arrays - delete", () => {709 runPatchTest(710 {711 x: [712 {a: 1, b: 2},713 {c: 3, d: 4}714 ]715 },716 d => {717 delete d.x[1].c718 },719 [{op: "remove", path: ["x", 1, "c"]}]720 )721})722723describe("arrays - append", () => {724 test("appends to array when last part of path is '-'", () => {725 const state = {726 list: [1, 2, 3]727 }728 const patch = {729 op: "add",730 value: 4,731 path: ["list", "-"]732 }733 expect(applyPatches(state, [patch])).toEqual({734 list: [1, 2, 3, 4]735 })736 })737})738739describe("sets - add - 1", () => {740 runPatchTest(741 new Set([1]),742 d => {743 d.add(2)744 },745 [{op: "add", path: [1], value: 2}],746 [{op: "remove", path: [1], value: 2}]747 )748})749750describe("sets - add, delete, add - 1", () => {751 runPatchTest(752 new Set([1]),753 d => {754 d.add(2)755 d.delete(2)756 d.add(2)757 },758 [{op: "add", path: [1], value: 2}],759 [{op: "remove", path: [1], value: 2}]760 )761})762763describe("sets - add, delete, add - 2", () => {764 runPatchTest(765 new Set([2, 1]),766 d => {767 d.add(2)768 d.delete(2)769 d.add(2)770 },771 [],772 []773 )774})775776describe("sets - mutate - 1", () => {777 const findById = (set, id) => {778 for (const item of set) {779 if (item.id === id) return item780 }781 }782 runPatchTest(783 new Set([784 {id: 1, val: "We"},785 {id: 2, val: "will"}786 ]),787 d => {788 const obj1 = findById(d, 1)789 const obj2 = findById(d, 2)790 obj1.val = "rock"791 obj2.val = "you"792 },793 [794 {op: "remove", path: [0], value: {id: 1, val: "We"}},795 {op: "remove", path: [1], value: {id: 2, val: "will"}},796 {op: "add", path: [0], value: {id: 1, val: "rock"}},797 {op: "add", path: [1], value: {id: 2, val: "you"}}798 ],799 [800 {op: "remove", path: [1], value: {id: 2, val: "you"}},801 {op: "remove", path: [0], value: {id: 1, val: "rock"}},802 {op: "add", path: [1], value: {id: 2, val: "will"}},803 {op: "add", path: [0], value: {id: 1, val: "We"}}804 ]805 )806})807808describe("arrays - splice should should result in remove op.", () => {809 // These patches were more optimal pre immer 7, but not always correct810 runPatchTest(811 [1, 2],812 d => {813 d.splice(1, 1)814 },815 [{op: "replace", path: ["length"], value: 1}],816 [{op: "add", path: [1], value: 2}]817 )818})819820describe("arrays - NESTED splice should should result in remove op.", () => {821 // These patches were more optimal pre immer 7, but not always correct822 runPatchTest(823 {a: {b: {c: [1, 2]}}},824 d => {825 d.a.b.c.splice(1, 1)826 },827 [{op: "replace", path: ["a", "b", "c", "length"], value: 1}],828 [{op: "add", path: ["a", "b", "c", 1], value: 2}]829 )830})831832describe("simple replacement", () => {833 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])834})835836describe("same value replacement - 1", () => {837 runPatchTest(838 {x: {y: 3}},839 d => {840 const a = d.x841 d.x = a842 },843 []844 )845})846847describe("same value replacement - 2", () => {848 runPatchTest(849 {x: {y: 3}},850 d => {851 const a = d.x852 d.x = 4853 d.x = a854 },855 []856 )857})858859describe("same value replacement - 3", () => {860 runPatchTest(861 {x: 3},862 d => {863 d.x = 3864 },865 []866 )867})868869describe("same value replacement - 4", () => {870 runPatchTest(871 {x: 3},872 d => {873 d.x = 4874 d.x = 3875 },876 []877 )878})879880describe("same value replacement - 5", () => {881 runPatchTest(882 new Map([["x", 3]]),883 d => {884 d.set("x", 4)885 d.set("x", 3)886 },887 [],888 []889 )890})891892describe("same value replacement - 6", () => {893 runPatchTest(894 new Set(["x", 3]),895 d => {896 d.delete("x")897 d.add("x")898 },899 [],900 []901 )902})903904describe("simple delete", () => {905 runPatchTest(906 {x: 2},907 d => {908 delete d.x909 },910 [911 {912 op: "remove",913 path: ["x"]914 }915 ]916 )917})918919describe("patch compressions yields correct results", () => {920 let p1, p2921 runPatchTest(922 {},923 d => {924 d.x = {test: true}925 },926 (p1 = [927 {928 op: "add",929 path: ["x"],930 value: {931 test: true932 }933 }934 ])935 )936 runPatchTest(937 {x: {test: true}},938 d => {939 delete d.x940 },941 (p2 = [942 {943 op: "remove",944 path: ["x"]945 }946 ])947 )948 const res = runPatchTest(949 {},950 d => {951 applyPatches(d, [...p1, ...p2])952 },953 []954 )955956 expect(res).toEqual({})957})958959describe("change then delete property", () => {960 const res = runPatchTest(961 {962 x: 1963 },964 d => {965 d.x = 2966 delete d.x967 },968 [969 {970 op: "remove",971 path: ["x"]972 }973 ]974 )975 test("valid result", () => {976 expect(res).toEqual({})977 })978})979980test("replaying patches with interweaved replacements should work correctly", () => {981 const patches = []982 const s0 = {x: 1}983984 const s1 = produce(985 s0,986 draft => {987 draft.x = 2988 },989 p => {990 patches.push(...p)991 }992 )993994 const s2 = produce(995 s1,996 draft => {997 return {x: 0}998 },999 p => {1000 patches.push(...p)1001 }1002 )10031004 const s3 = produce(1005 s2,1006 draft => {1007 draft.x--1008 },1009 p => {1010 patches.push(...p)1011 }1012 )10131014 expect(s3).toEqual({x: -1}) // correct result1015 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay10161017 // manual replay on a draft should also be correct1018 expect(1019 produce(s0, draft => {1020 return applyPatches(draft, patches)1021 })1022 ).toEqual({x: -1})1023})10241025describe("#468", () => {1026 function run() {1027 const item = {id: 1}1028 const state = [item]1029 const [nextState, patches] = produceWithPatches(state, draft => {1030 draft[0].id = 21031 draft[1] = item1032 })10331034 expect(nextState).toEqual([{id: 2}, {id: 1}])1035 expect(patches).toEqual([1036 {1037 op: "replace",1038 path: [0, "id"],1039 value: 21040 },1041 {1042 op: "add",1043 path: [1],1044 value: {1045 id: 11046 }1047 }1048 ])10491050 const final = applyPatches(state, patches)1051 expect(final).toEqual(nextState)1052 }10531054 test("es5", () => {1055 setUseProxies(false)1056 run()1057 })10581059 test("proxy", () => {1060 setUseProxies(true)1061 run()1062 })1063})10641065test("#521", () => {1066 const state = new Map()10671068 const [nextState, patches] = produceWithPatches(state, draft => {1069 draft.set("hello", new Set(["world"]))1070 })10711072 let patchedState = applyPatches(state, patches)1073 expect(patchedState).toEqual(nextState)10741075 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1076 draft.get("hello").add("immer")1077 })10781079 expect(applyPatches(nextState, patchesV2)).toEqual(1080 new Map([["hello", new Set(["world", "immer"])]])1081 )1082})10831084test("#559 patches works in a nested reducer with proxies", () => {1085 setUseProxies(true)10861087 const state = {1088 x: 1,1089 sub: {1090 y: [{a: 0}, {a: 1}]1091 }1092 }10931094 const changes = []1095 const inverseChanges = []10961097 const newState = produce(state, draft => {1098 draft.sub = produce(1099 draft.sub,1100 draft => {1101 draft.y.pop()1102 },1103 (patches, inversePatches) => {1104 expect(isDraft(inversePatches[0].value)).toBeFalsy()1105 expect(inversePatches[0].value).toMatchObject({a: 1})1106 changes.push(...patches)1107 inverseChanges.push(...inversePatches)1108 }1109 )1110 })11111112 const reversedSubState = applyPatches(newState.sub, inverseChanges)11131114 expect(reversedSubState).toMatchObject(state.sub)1115})11161117describe("#588", () => {1118 const reference = {value: {num: 53}}11191120 class Base {1121 [immerable] = true1122 get nested() {1123 return reference.value1124 }1125 set nested(value) {}1126 }11271128 let base = new Base()11291130 runPatchTest(1131 base,1132 vdraft => {1133 reference.value = vdraft1134 produce(base, bdraft => {1135 bdraft.nested.num = 421136 })1137 },1138 [{op: "add", path: ["num"], value: 42}]1139 )1140})11411142test("#676 patching Date objects", () => {1143 class Test {1144 constructor() {1145 this.test = true1146 }1147 perform() {1148 return "tested!"1149 }1150 }11511152 const [nextState, patches] = produceWithPatches({}, function(draft) {1153 draft.date = new Date("2020-11-10T08:08:08.003Z")1154 draft.test = new Test()1155 })11561157 expect(nextState.date.toJSON()).toMatchInlineSnapshot(1158 `"2020-11-10T08:08:08.003Z"`1159 )1160 expect(nextState.test.perform()).toBe("tested!")11611162 const rebuilt = applyPatches({}, patches)1163 expect(rebuilt.date).toBeInstanceOf(Date)1164 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1165 `"2020-11-10T08:08:08.003Z"`1166 )1167 expect(rebuilt.date).toEqual(new Date("2020-11-10T08:08:08.003Z"))1168})11691170test("do not allow __proto__ polution - 738", () => {1171 const obj = {}11721173 // @ts-ignore1174 expect(obj.polluted).toBe(undefined)1175 expect(() => {1176 applyPatches({}, [1177 {op: "add", path: ["__proto__", "polluted"], value: "yes"}1178 ])1179 }).toThrow(1180 isProd1181 ? "24"1182 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1183 )1184 // @ts-ignore1185 expect(obj.polluted).toBe(undefined)1186})11871188test("do not allow __proto__ polution using arrays - 738", () => {1189 const obj = {}1190 const ar = []11911192 // @ts-ignore1193 expect(obj.polluted).toBe(undefined)1194 // @ts-ignore1195 expect(ar.polluted).toBe(undefined)1196 expect(() => {1197 applyPatches(1198 [],1199 [{op: "add", path: ["__proto__", "polluted"], value: "yes"}]1200 )1201 }).toThrow(1202 isProd1203 ? "24"1204 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1205 )1206 // @ts-ignore1207 expect(obj.polluted).toBe(undefined)1208 // @ts-ignore1209 expect(ar.polluted).toBe(undefined)1210})12111212test("do not allow prototype polution - 738", () => {1213 const obj = {}12141215 // @ts-ignore1216 expect(obj.polluted).toBe(undefined)1217 expect(() => {1218 applyPatches(Object, [1219 {op: "add", path: ["prototype", "polluted"], value: "yes"}1220 ])1221 }).toThrow(1222 isProd1223 ? "24"1224 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1225 )1226 // @ts-ignore1227 expect(obj.polluted).toBe(undefined)1228})12291230test("do not allow constructor polution - 738", () => {1231 const obj = {}12321233 // @ts-ignore1234 expect(obj.polluted).toBe(undefined)1235 const t = {}1236 applyPatches(t, [{op: "replace", path: ["constructor"], value: "yes"}])1237 expect(typeof t.constructor).toBe("function")1238 // @ts-ignore1239 expect(Object.polluted).toBe(undefined)1240})12411242test("do not allow constructor.prototype polution - 738", () => {1243 const obj = {}12441245 // @ts-ignore1246 expect(obj.polluted).toBe(undefined)1247 expect(() => {1248 applyPatches({}, [1249 {op: "add", path: ["constructor", "prototype", "polluted"], value: "yes"}1250 ])1251 }).toThrow(1252 isProd1253 ? "24"1254 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1255 )1256 // @ts-ignore1257 expect(Object.polluted).toBe(undefined)1258})12591260test("maps can store __proto__, prototype and constructor props", () => {1261 const obj = {}1262 const map = new Map()1263 map.set("__proto__", {})1264 map.set("constructor", {})1265 map.set("prototype", {})1266 const newMap = applyPatches(map, [1267 {op: "add", path: ["__proto__", "polluted"], value: "yes"},1268 {op: "add", path: ["constructor", "polluted"], value: "yes"},1269 {op: "add", path: ["prototype", "polluted"], value: "yes"}1270 ])1271 expect(newMap.get("__proto__").polluted).toBe("yes")1272 expect(newMap.get("constructor").polluted).toBe("yes")1273 expect(newMap.get("prototype").polluted).toBe("yes")1274 expect(obj.polluted).toBe(undefined)1275})12761277test("CVE-2020-28477 (https://snyk.io/vuln/SNYK-JS-IMMER-1019369) follow up", () => {1278 const obj = {}12791280 // @ts-ignore1281 expect(obj.polluted).toBe(undefined)1282 expect(() => {1283 applyPatches({}, [1284 {op: "add", path: [["__proto__"], "polluted"], value: "yes"}1285 ])1286 }).toThrow(1287 isProd1288 ? "24"1289 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1290 )1291 // @ts-ignore1292 expect(obj.polluted).toBe(undefined)1293})12941295test("#648 assigning object to itself should not change patches", () => {1296 const input = {1297 obj: {1298 value: 2001299 }1300 }13011302 const [nextState, patches] = produceWithPatches(input, draft => {1303 draft.obj.value = 11304 draft.obj = draft.obj1305 })13061307 expect(patches).toEqual([1308 {1309 op: "replace",1310 path: ["obj", "value"],1311 value: 11312 }1313 ])1314})13151316test("#791 patch for nothing is stored as undefined", () => {1317 const [newState, patches] = produceWithPatches({abc: 123}, draft => nothing)1318 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])13191320 expect(applyPatches({}, patches)).toEqual(undefined)1321})