patch.js
This file
Path
__tests__/patch.js
1,444 lines · 27.6 KiB · JavaScript
Last changed by
Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
History of this fileTake it
The walk stopped after 200 commits: lines older than 1b70ad5 point at it.
fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
line 1why, one step back1"use strict"Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 2–3why, one step back2import {3 produce,fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 4–6why, one step back4 applyPatches,5 produceWithPatches,6 isDraft,fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f
line 7why, one step back7 immerable,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 8–10why, one step back8 nothing,9 enablePatches,10 enableMapSetfix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 11–12why, one step back11} from "../src/immer"12Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 13–14why, one step back13enablePatches()14enableMapSet()fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 15–17why, one step back1516jest.setTimeout(1000)17fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 18–19why, one step back18const isProd = process.env.NODE_ENV === "production"19fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 20why, one step back20function runPatchTest(base, producer, patches, inversePathes, expectedResult) {fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 21–30why, one step back21 let resultProxies, resultEs52223 function runPatchTestHelper() {24 let recordedPatches25 let recordedInversePatches26 const res = produce(base, producer, (p, i) => {27 recordedPatches = p28 recordedInversePatches = i29 })30fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 31–35why, one step back31 if (expectedResult !== undefined)32 test("produced the correct result", () => {33 expect(res).toEqual(expectedResult)34 })35fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 36–209why, one step back36 test("produces the correct patches", () => {37 expect(recordedPatches).toEqual(patches)38 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)39 })4041 test("patches are replayable", () => {42 expect(applyPatches(base, recordedPatches)).toEqual(res)43 })4445 test("patches can be reversed", () => {46 expect(applyPatches(res, recordedInversePatches)).toEqual(base)47 })4849 return res50 }5152 describe(`proxy`, () => {53 resultProxies = runPatchTestHelper()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})209fix: patching maps failed when using number keys (#1025)Marco Schumacherbefore adoptiondd83e2e
lines 210–227why, one step back210describe("simple assignment - 8", () => {211 runPatchTest(212 new Map([[0, new Map([[1, 4]])]]),213 d => {214 d.get(0).set(1, 5)215 d.get(0).set(2, 6)216 },217 [218 {op: "replace", path: [0, 1], value: 5},219 {op: "add", path: [0, 2], value: 6}220 ],221 [222 {op: "replace", path: [0, 1], value: 4},223 {op: "remove", path: [0, 2]}224 ]225 )226})227fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 228–568why, one step back228describe("delete 1", () => {229 runPatchTest(230 {x: {y: 4}},231 d => {232 delete d.x233 },234 [{op: "remove", path: ["x"]}]235 )236})237238describe("delete 2", () => {239 runPatchTest(240 new Map([["x", 1]]),241 d => {242 d.delete("x")243 },244 [{op: "remove", path: ["x"]}],245 [{op: "add", path: ["x"], value: 1}]246 )247})248249describe("delete 3", () => {250 runPatchTest(251 {x: new Map([["y", 1]])},252 d => {253 d.x.delete("y")254 },255 [{op: "remove", path: ["x", "y"]}],256 [{op: "add", path: ["x", "y"], value: 1}]257 )258})259260describe("delete 5", () => {261 const key1 = {prop: "val1"}262 const key2 = {prop: "val2"}263 runPatchTest(264 {265 x: new Map([266 [key1, 1],267 [key2, 2]268 ])269 },270 d => {271 d.x.delete(key1)272 d.x.delete(key2)273 },274 [275 {op: "remove", path: ["x", key1]},276 {op: "remove", path: ["x", key2]}277 ],278 [279 {op: "add", path: ["x", key1], value: 1},280 {op: "add", path: ["x", key2], value: 2}281 ]282 )283})284285describe("delete 6", () => {286 runPatchTest(287 new Set(["x", 1]),288 d => {289 d.delete("x")290 },291 [{op: "remove", path: [0], value: "x"}],292 [{op: "add", path: [0], value: "x"}]293 )294})295296describe("delete 7", () => {297 runPatchTest(298 {x: new Set(["y", 1])},299 d => {300 d.x.delete("y")301 },302 [{op: "remove", path: ["x", 0], value: "y"}],303 [{op: "add", path: ["x", 0], value: "y"}]304 )305})306307describe("renaming properties", () => {308 describe("nested object (no changes)", () => {309 runPatchTest(310 {a: {b: 1}},311 d => {312 d.x = d.a313 delete d.a314 },315 [316 {op: "add", path: ["x"], value: {b: 1}},317 {op: "remove", path: ["a"]}318 ]319 )320 })321322 describe("nested change in object", () => {323 runPatchTest(324 {325 a: {b: 1}326 },327 d => {328 d.a.b++329 },330 [{op: "replace", path: ["a", "b"], value: 2}],331 [{op: "replace", path: ["a", "b"], value: 1}]332 )333 })334335 describe("nested change in map", () => {336 runPatchTest(337 new Map([["a", new Map([["b", 1]])]]),338 d => {339 d.get("a").set("b", 2)340 },341 [{op: "replace", path: ["a", "b"], value: 2}],342 [{op: "replace", path: ["a", "b"], value: 1}]343 )344 })345346 describe("nested change in array", () => {347 runPatchTest(348 [[{b: 1}]],349 d => {350 d[0][0].b++351 },352 [{op: "replace", path: [0, 0, "b"], value: 2}],353 [{op: "replace", path: [0, 0, "b"], value: 1}]354 )355 })356357 describe("nested map (no changes)", () => {358 runPatchTest(359 new Map([["a", new Map([["b", 1]])]]),360 d => {361 d.set("x", d.get("a"))362 d.delete("a")363 },364 [365 {op: "add", path: ["x"], value: new Map([["b", 1]])},366 {op: "remove", path: ["a"]}367 ],368 [369 {op: "remove", path: ["x"]},370 {op: "add", path: ["a"], value: new Map([["b", 1]])}371 ]372 )373 })374375 describe("nested object (with changes)", () => {376 runPatchTest(377 {a: {b: 1, c: 1}},378 d => {379 let a = d.a380 a.b = 2 // change381 delete a.c // delete382 a.y = 2 // add383384 // rename385 d.x = a386 delete d.a387 },388 [389 {op: "add", path: ["x"], value: {b: 2, y: 2}},390 {op: "remove", path: ["a"]}391 ]392 )393 })394395 describe("nested map (with changes)", () => {396 runPatchTest(397 new Map([398 [399 "a",400 new Map([401 ["b", 1],402 ["c", 1]403 ])404 ]405 ]),406 d => {407 let a = d.get("a")408 a.set("b", 2) // change409 a.delete("c") // delete410 a.set("y", 2) // add411412 // rename413 d.set("x", a)414 d.delete("a")415 },416 [417 {418 op: "add",419 path: ["x"],420 value: new Map([421 ["b", 2],422 ["y", 2]423 ])424 },425 {op: "remove", path: ["a"]}426 ],427 [428 {op: "remove", path: ["x"]},429 {430 op: "add",431 path: ["a"],432 value: new Map([433 ["b", 1],434 ["c", 1]435 ])436 }437 ]438 )439 })440441 describe("deeply nested object (with changes)", () => {442 runPatchTest(443 {a: {b: {c: 1, d: 1}}},444 d => {445 let b = d.a.b446 b.c = 2 // change447 delete b.d // delete448 b.y = 2 // add449450 // rename451 d.a.x = b452 delete d.a.b453 },454 [455 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},456 {op: "remove", path: ["a", "b"]}457 ]458 )459 })460461 describe("deeply nested map (with changes)", () => {462 runPatchTest(463 new Map([464 [465 "a",466 new Map([467 [468 "b",469 new Map([470 ["c", 1],471 ["d", 1]472 ])473 ]474 ])475 ]476 ]),477 d => {478 let b = d.get("a").get("b")479 b.set("c", 2) // change480 b.delete("d") // delete481 b.set("y", 2) // add482483 // rename484 d.get("a").set("x", b)485 d.get("a").delete("b")486 },487 [488 {489 op: "add",490 path: ["a", "x"],491 value: new Map([492 ["c", 2],493 ["y", 2]494 ])495 },496 {op: "remove", path: ["a", "b"]}497 ],498 [499 {op: "remove", path: ["a", "x"]},500 {501 op: "add",502 path: ["a", "b"],503 value: new Map([504 ["c", 1],505 ["d", 1]506 ])507 }508 ]509 )510 })511})512513describe("minimum amount of changes", () => {514 runPatchTest(515 {x: 3, y: {a: 4}, z: 3},516 d => {517 d.y.a = 4518 d.y.b = 5519 Object.assign(d, {x: 4, y: {a: 2}})520 },521 [522 {op: "replace", path: ["x"], value: 4},523 {op: "replace", path: ["y"], value: {a: 2}}524 ]525 )526})527528describe("arrays - prepend", () => {529 runPatchTest(530 {x: [1, 2, 3]},531 d => {532 d.x.unshift(4)533 },534 [535 {op: "replace", path: ["x", 0], value: 4},536 {op: "replace", path: ["x", 1], value: 1},537 {op: "replace", path: ["x", 2], value: 2},538 {op: "add", path: ["x", 3], value: 3}539 ]540 )541})542543describe("arrays - multiple prepend", () => {544 runPatchTest(545 {x: [1, 2, 3]},546 d => {547 d.x.unshift(4)548 d.x.unshift(5)549 // 4,5,1,2,3550 },551 [552 {op: "replace", path: ["x", 0], value: 5},553 {op: "replace", path: ["x", 1], value: 4},554 {op: "replace", path: ["x", 2], value: 1},555 {op: "add", path: ["x", 3], value: 2},556 {op: "add", path: ["x", 4], value: 3}557 ]558 )559})560561describe("arrays - splice middle", () => {562 runPatchTest(563 {x: [1, 2, 3]},564 d => {565 d.x.splice(1, 1)566 },567 [568 {op: "replace", path: ["x", 1], value: 3},Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 569why, one step back569 {op: "remove", path: ["x", 2]}fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 570–587why, one step back570 ]571 )572})573574describe("arrays - multiple splice", () => {575 runPatchTest(576 [0, 1, 2, 3, 4, 5, 0],577 d => {578 d.splice(4, 2, 3)579 // [0,1,2,3,3,0]580 d.splice(1, 2, 3)581 // [0,3,3,3,0]582 expect(d.slice()).toEqual([0, 3, 3, 3, 0])583 },584 [585 {op: "replace", path: [1], value: 3},586 {op: "replace", path: [2], value: 3},587 {op: "replace", path: [4], value: 0},Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 588–589why, one step back588 {op: "remove", path: [6]},589 {op: "remove", path: [5]}fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 590–603why, one step back590 ]591 )592})593594describe("arrays - modify and shrink", () => {595 runPatchTest(596 {x: [1, 2, 3]},597 d => {598 d.x[0] = 4599 d.x.length = 2600 // [0, 2]601 },602 [603 {op: "replace", path: ["x", 0], value: 4},Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 604why, one step back604 {op: "remove", path: ["x", 2]}fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 605–649why, one step back605 ],606 [607 {op: "replace", path: ["x", 0], value: 1},608 {op: "add", path: ["x", 2], value: 3}609 ]610 )611})612613describe("arrays - prepend then splice middle", () => {614 runPatchTest(615 {x: [1, 2, 3]},616 d => {617 d.x.unshift(4)618 d.x.splice(2, 1)619 // 4, 1, 3620 },621 [622 {op: "replace", path: ["x", 0], value: 4},623 {op: "replace", path: ["x", 1], value: 1}624 ]625 )626})627628describe("arrays - splice middle then prepend", () => {629 runPatchTest(630 {x: [1, 2, 3]},631 d => {632 d.x.splice(1, 1)633 d.x.unshift(4)634 // [4, 1, 3]635 },636 [637 {op: "replace", path: ["x", 0], value: 4},638 {op: "replace", path: ["x", 1], value: 1}639 ]640 )641})642643describe("arrays - truncate", () => {644 runPatchTest(645 {x: [1, 2, 3]},646 d => {647 d.x.length -= 2648 },649 [Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 650–653why, one step back650 {op: "remove", path: ["x", 2]},651 {op: "remove", path: ["x", 1]}652 ],653 [fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 654–666why, one step back654 {op: "add", path: ["x", 1], value: 2},655 {op: "add", path: ["x", 2], value: 3}656 ]657 )658})659660describe("arrays - pop twice", () => {661 runPatchTest(662 {x: [1, 2, 3]},663 d => {664 d.x.pop()665 d.x.pop()666 },Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 667–670why, one step back667 [668 {op: "remove", path: ["x", 2]},669 {op: "remove", path: ["x", 1]}670 ]fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 671–684why, one step back671 )672})673674describe("arrays - push multiple", () => {675 // These patches were more optimal pre immer 7, but not always correct676 runPatchTest(677 {x: [1, 2, 3]},678 d => {679 d.x.push(4, 5)680 },681 [682 {op: "add", path: ["x", 3], value: 4},683 {op: "add", path: ["x", 4], value: 5}684 ],Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 685–688why, one step back685 [686 {op: "remove", path: ["x", 4]},687 {op: "remove", path: ["x", 3]}688 ]fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 689–707why, one step back689 )690})691692describe("arrays - splice (expand)", () => {693 // These patches were more optimal pre immer 7, but not always correct694 runPatchTest(695 {x: [1, 2, 3]},696 d => {697 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]698 },699 [700 {op: "replace", path: ["x", 1], value: 4},701 {op: "replace", path: ["x", 2], value: 5},702 {op: "add", path: ["x", 3], value: 6},703 {op: "add", path: ["x", 4], value: 3}704 ],705 [706 {op: "replace", path: ["x", 1], value: 2},707 {op: "replace", path: ["x", 2], value: 3},Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 708–709why, one step back708 {op: "remove", path: ["x", 4]},709 {op: "remove", path: ["x", 3]}fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 710–723why, one step back710 ]711 )712})713714describe("arrays - splice (shrink)", () => {715 // These patches were more optimal pre immer 7, but not always correct716 runPatchTest(717 {x: [1, 2, 3, 4, 5]},718 d => {719 d.x.splice(1, 3, 6) // [1, 6, 5]720 },721 [722 {op: "replace", path: ["x", 1], value: 6},723 {op: "replace", path: ["x", 2], value: 5},Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 724–725why, one step back724 {op: "remove", path: ["x", 4]},725 {op: "remove", path: ["x", 3]}fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 726–750why, one step back726 ],727 [728 {op: "replace", path: ["x", 1], value: 2},729 {op: "replace", path: ["x", 2], value: 3},730 {op: "add", path: ["x", 3], value: 4},731 {op: "add", path: ["x", 4], value: 5}732 ]733 )734})735736describe("arrays - delete", () => {737 runPatchTest(738 {739 x: [740 {a: 1, b: 2},741 {c: 3, d: 4}742 ]743 },744 d => {745 delete d.x[1].c746 },747 [{op: "remove", path: ["x", 1, "c"]}]748 )749})750fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b
lines 751–766why, one step back751describe("arrays - append", () => {752 test("appends to array when last part of path is '-'", () => {753 const state = {754 list: [1, 2, 3]755 }756 const patch = {757 op: "add",758 value: 4,759 path: ["list", "-"]760 }761 expect(applyPatches(state, [patch])).toEqual({762 list: [1, 2, 3, 4]763 })764 })765})766fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 767–842why, one step back767describe("sets - add - 1", () => {768 runPatchTest(769 new Set([1]),770 d => {771 d.add(2)772 },773 [{op: "add", path: [1], value: 2}],774 [{op: "remove", path: [1], value: 2}]775 )776})777778describe("sets - add, delete, add - 1", () => {779 runPatchTest(780 new Set([1]),781 d => {782 d.add(2)783 d.delete(2)784 d.add(2)785 },786 [{op: "add", path: [1], value: 2}],787 [{op: "remove", path: [1], value: 2}]788 )789})790791describe("sets - add, delete, add - 2", () => {792 runPatchTest(793 new Set([2, 1]),794 d => {795 d.add(2)796 d.delete(2)797 d.add(2)798 },799 [],800 []801 )802})803804describe("sets - mutate - 1", () => {805 const findById = (set, id) => {806 for (const item of set) {807 if (item.id === id) return item808 }809 }810 runPatchTest(811 new Set([812 {id: 1, val: "We"},813 {id: 2, val: "will"}814 ]),815 d => {816 const obj1 = findById(d, 1)817 const obj2 = findById(d, 2)818 obj1.val = "rock"819 obj2.val = "you"820 },821 [822 {op: "remove", path: [0], value: {id: 1, val: "We"}},823 {op: "remove", path: [1], value: {id: 2, val: "will"}},824 {op: "add", path: [0], value: {id: 1, val: "rock"}},825 {op: "add", path: [1], value: {id: 2, val: "you"}}826 ],827 [828 {op: "remove", path: [1], value: {id: 2, val: "you"}},829 {op: "remove", path: [0], value: {id: 1, val: "rock"}},830 {op: "add", path: [1], value: {id: 2, val: "will"}},831 {op: "add", path: [0], value: {id: 1, val: "We"}}832 ]833 )834})835836describe("arrays - splice should should result in remove op.", () => {837 // These patches were more optimal pre immer 7, but not always correct838 runPatchTest(839 [1, 2],840 d => {841 d.splice(1, 1)842 },Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 843why, one step back843 [{op: "remove", path: [1]}],fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 844–854why, one step back844 [{op: "add", path: [1], value: 2}]845 )846})847848describe("arrays - NESTED splice should should result in remove op.", () => {849 // These patches were more optimal pre immer 7, but not always correct850 runPatchTest(851 {a: {b: {c: [1, 2]}}},852 d => {853 d.a.b.c.splice(1, 1)854 },Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 855why, one step back855 [{op: "remove", path: ["a", "b", "c", 1]}],fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 856–1172why, one step back856 [{op: "add", path: ["a", "b", "c", 1], value: 2}]857 )858})859860describe("simple replacement", () => {861 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])862})863864describe("same value replacement - 1", () => {865 runPatchTest(866 {x: {y: 3}},867 d => {868 const a = d.x869 d.x = a870 },871 []872 )873})874875describe("same value replacement - 2", () => {876 runPatchTest(877 {x: {y: 3}},878 d => {879 const a = d.x880 d.x = 4881 d.x = a882 },883 []884 )885})886887describe("same value replacement - 3", () => {888 runPatchTest(889 {x: 3},890 d => {891 d.x = 3892 },893 []894 )895})896897describe("same value replacement - 4", () => {898 runPatchTest(899 {x: 3},900 d => {901 d.x = 4902 d.x = 3903 },904 []905 )906})907908describe("same value replacement - 5", () => {909 runPatchTest(910 new Map([["x", 3]]),911 d => {912 d.set("x", 4)913 d.set("x", 3)914 },915 [],916 []917 )918})919920describe("same value replacement - 6", () => {921 runPatchTest(922 new Set(["x", 3]),923 d => {924 d.delete("x")925 d.add("x")926 },927 [],928 []929 )930})931932describe("simple delete", () => {933 runPatchTest(934 {x: 2},935 d => {936 delete d.x937 },938 [939 {940 op: "remove",941 path: ["x"]942 }943 ]944 )945})946947describe("patch compressions yields correct results", () => {948 let p1, p2949 runPatchTest(950 {},951 d => {952 d.x = {test: true}953 },954 (p1 = [955 {956 op: "add",957 path: ["x"],958 value: {959 test: true960 }961 }962 ])963 )964 runPatchTest(965 {x: {test: true}},966 d => {967 delete d.x968 },969 (p2 = [970 {971 op: "remove",972 path: ["x"]973 }974 ])975 )976 const res = runPatchTest(977 {},978 d => {979 applyPatches(d, [...p1, ...p2])980 },981 []982 )983984 expect(res).toEqual({})985})986987describe("change then delete property", () => {988 const res = runPatchTest(989 {990 x: 1991 },992 d => {993 d.x = 2994 delete d.x995 },996 [997 {998 op: "remove",999 path: ["x"]1000 }1001 ]1002 )1003 test("valid result", () => {1004 expect(res).toEqual({})1005 })1006})10071008test("replaying patches with interweaved replacements should work correctly", () => {1009 const patches = []1010 const s0 = {x: 1}10111012 const s1 = produce(1013 s0,1014 draft => {1015 draft.x = 21016 },1017 p => {1018 patches.push(...p)1019 }1020 )10211022 const s2 = produce(1023 s1,1024 draft => {1025 return {x: 0}1026 },1027 p => {1028 patches.push(...p)1029 }1030 )10311032 const s3 = produce(1033 s2,1034 draft => {1035 draft.x--1036 },1037 p => {1038 patches.push(...p)1039 }1040 )10411042 expect(s3).toEqual({x: -1}) // correct result1043 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay10441045 // manual replay on a draft should also be correct1046 expect(1047 produce(s0, draft => {1048 return applyPatches(draft, patches)1049 })1050 ).toEqual({x: -1})1051})10521053describe("#468", () => {1054 function run() {1055 const item = {id: 1}1056 const state = [item]1057 const [nextState, patches] = produceWithPatches(state, draft => {1058 draft[0].id = 21059 draft[1] = item1060 })10611062 expect(nextState).toEqual([{id: 2}, {id: 1}])1063 expect(patches).toEqual([1064 {1065 op: "replace",1066 path: [0, "id"],1067 value: 21068 },1069 {1070 op: "add",1071 path: [1],1072 value: {1073 id: 11074 }1075 }1076 ])10771078 const final = applyPatches(state, patches)1079 expect(final).toEqual(nextState)1080 }10811082 test("proxy", () => {1083 run()1084 })1085})10861087test("#521", () => {1088 const state = new Map()10891090 const [nextState, patches] = produceWithPatches(state, draft => {1091 draft.set("hello", new Set(["world"]))1092 })10931094 let patchedState = applyPatches(state, patches)1095 expect(patchedState).toEqual(nextState)10961097 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1098 draft.get("hello").add("immer")1099 })11001101 expect(applyPatches(nextState, patchesV2)).toEqual(1102 new Map([["hello", new Set(["world", "immer"])]])1103 )1104})11051106test("#559 patches works in a nested reducer with proxies", () => {1107 const state = {1108 x: 1,1109 sub: {1110 y: [{a: 0}, {a: 1}]1111 }1112 }11131114 const changes = []1115 const inverseChanges = []11161117 const newState = produce(state, draft => {1118 draft.sub = produce(1119 draft.sub,1120 draft => {1121 draft.y.pop()1122 },1123 (patches, inversePatches) => {1124 expect(isDraft(inversePatches[0].value)).toBeFalsy()1125 expect(inversePatches[0].value).toMatchObject({a: 1})1126 changes.push(...patches)1127 inverseChanges.push(...inversePatches)1128 }1129 )1130 })11311132 const reversedSubState = applyPatches(newState.sub, inverseChanges)11331134 expect(reversedSubState).toMatchObject(state.sub)1135})11361137describe("#588", () => {1138 const reference = {value: {num: 53}}11391140 class Base {1141 [immerable] = true1142 get nested() {1143 return reference.value1144 }1145 set nested(value) {}1146 }11471148 let base = new Base()11491150 runPatchTest(1151 base,1152 vdraft => {1153 reference.value = vdraft1154 produce(base, bdraft => {1155 bdraft.nested.num = 421156 })1157 },1158 [{op: "add", path: ["num"], value: 42}]1159 )1160})11611162test("#676 patching Date objects", () => {1163 class Test {1164 constructor() {1165 this.test = true1166 }1167 perform() {1168 return "tested!"1169 }1170 }11711172 const [nextState, patches] = produceWithPatches({}, function(draft) {chore(tests): use UTC date string in tests to be timezone independent (#705)Grzegorz Rozdzialikbefore adoptione8fd805
line 1173why, one step back1173 draft.date = new Date("2020-11-10T08:08:08.003Z")fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 1174–1186why, one step back1174 draft.test = new Test()1175 })11761177 expect(nextState.date.toJSON()).toMatchInlineSnapshot(1178 `"2020-11-10T08:08:08.003Z"`1179 )1180 expect(nextState.test.perform()).toBe("tested!")11811182 const rebuilt = applyPatches({}, patches)1183 expect(rebuilt.date).toBeInstanceOf(Date)1184 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1185 `"2020-11-10T08:08:08.003Z"`1186 )chore(tests): use UTC date string in tests to be timezone independent (#705)Grzegorz Rozdzialikbefore adoptione8fd805
line 1187why, one step back1187 expect(rebuilt.date).toEqual(new Date("2020-11-10T08:08:08.003Z"))fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 1188–1200why, one step back1188})11891190test("do not allow __proto__ polution - 738", () => {1191 const obj = {}11921193 // @ts-ignore1194 expect(obj.polluted).toBe(undefined)1195 expect(() => {1196 applyPatches({}, [1197 {op: "add", path: ["__proto__", "polluted"], value: "yes"}1198 ])1199 }).toThrow(1200 isProdMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 1201why, one step back1201 ? "19"fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 1202–1222why, one step back1202 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1203 )1204 // @ts-ignore1205 expect(obj.polluted).toBe(undefined)1206})12071208test("do not allow __proto__ polution using arrays - 738", () => {1209 const obj = {}1210 const ar = []12111212 // @ts-ignore1213 expect(obj.polluted).toBe(undefined)1214 // @ts-ignore1215 expect(ar.polluted).toBe(undefined)1216 expect(() => {1217 applyPatches(1218 [],1219 [{op: "add", path: ["__proto__", "polluted"], value: "yes"}]1220 )1221 }).toThrow(1222 isProdMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 1223why, one step back1223 ? "19"fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 1224–1242why, one step back1224 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1225 )1226 // @ts-ignore1227 expect(obj.polluted).toBe(undefined)1228 // @ts-ignore1229 expect(ar.polluted).toBe(undefined)1230})12311232test("do not allow prototype polution - 738", () => {1233 const obj = {}12341235 // @ts-ignore1236 expect(obj.polluted).toBe(undefined)1237 expect(() => {1238 applyPatches(Object, [1239 {op: "add", path: ["prototype", "polluted"], value: "yes"}1240 ])1241 }).toThrow(1242 isProdMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 1243why, one step back1243 ? "19"fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 1244–1272why, one step back1244 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1245 )1246 // @ts-ignore1247 expect(obj.polluted).toBe(undefined)1248})12491250test("do not allow constructor polution - 738", () => {1251 const obj = {}12521253 // @ts-ignore1254 expect(obj.polluted).toBe(undefined)1255 const t = {}1256 applyPatches(t, [{op: "replace", path: ["constructor"], value: "yes"}])1257 expect(typeof t.constructor).toBe("function")1258 // @ts-ignore1259 expect(Object.polluted).toBe(undefined)1260})12611262test("do not allow constructor.prototype polution - 738", () => {1263 const obj = {}12641265 // @ts-ignore1266 expect(obj.polluted).toBe(undefined)1267 expect(() => {1268 applyPatches({}, [1269 {op: "add", path: ["constructor", "prototype", "polluted"], value: "yes"}1270 ])1271 }).toThrow(1272 isProdMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 1273why, one step back1273 ? "19"fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 1274–1294why, one step back1274 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1275 )1276 // @ts-ignore1277 expect(Object.polluted).toBe(undefined)1278})12791280test("maps can store __proto__, prototype and constructor props", () => {1281 const obj = {}1282 const map = new Map()1283 map.set("__proto__", {})1284 map.set("constructor", {})1285 map.set("prototype", {})1286 const newMap = applyPatches(map, [1287 {op: "add", path: ["__proto__", "polluted"], value: "yes"},1288 {op: "add", path: ["constructor", "polluted"], value: "yes"},1289 {op: "add", path: ["prototype", "polluted"], value: "yes"}1290 ])1291 expect(newMap.get("__proto__").polluted).toBe("yes")1292 expect(newMap.get("constructor").polluted).toBe("yes")1293 expect(newMap.get("prototype").polluted).toBe("yes")1294 expect(obj.polluted).toBe(undefined)fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
line 1295why, one step back1295})fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d
line 1296why, one step back1296fix(security): Follow up on CVE-2020-28477 where `path: [["__proto__"], "x"]` could still pollute the prototypeMichel Weststratebefore adoptionfa671e5
lines 1297–1307why, one step back1297test("CVE-2020-28477 (https://snyk.io/vuln/SNYK-JS-IMMER-1019369) follow up", () => {1298 const obj = {}12991300 // @ts-ignore1301 expect(obj.polluted).toBe(undefined)1302 expect(() => {1303 applyPatches({}, [1304 {op: "add", path: [["__proto__"], "polluted"], value: "yes"}1305 ])1306 }).toThrow(1307 isProdMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 1308why, one step back1308 ? "19"fix(security): Follow up on CVE-2020-28477 where `path: [["__proto__"], "x"]` could still pollute the prototypeMichel Weststratebefore adoptionfa671e5
lines 1309–1314why, one step back1309 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1310 )1311 // @ts-ignore1312 expect(obj.polluted).toBe(undefined)1313})1314fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d
lines 1315–1333why, one step back1315test("#648 assigning object to itself should not change patches", () => {1316 const input = {1317 obj: {1318 value: 2001319 }1320 }13211322 const [nextState, patches] = produceWithPatches(input, draft => {1323 draft.obj.value = 11324 draft.obj = draft.obj1325 })13261327 expect(patches).toEqual([1328 {1329 op: "replace",1330 path: ["obj", "value"],1331 value: 11332 }1333 ])fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f
lines 1334–1340why, one step back1334})13351336test("#791 patch for nothing is stored as undefined", () => {1337 const [newState, patches] = produceWithPatches({abc: 123}, draft => nothing)1338 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])13391340 expect(applyPatches({}, patches)).toEqual(undefined)fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d
line 1341why, one step back1341})fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (#878)Julian Scheidbefore adoptione140918
lines 1342–1362why, one step back13421343test("#876 Ensure empty patch set for atomic set+delete on Map", () => {1344 {1345 const [newState, patches] = produceWithPatches(1346 new Map([["foo", "baz"]]),1347 draft => {1348 draft.set("foo", "bar")1349 draft.delete("foo")1350 }1351 )1352 expect(patches).toEqual([{op: "remove", path: ["foo"]}])1353 }13541355 {1356 const [newState, patches] = produceWithPatches(new Map(), draft => {1357 draft.set("foo", "bar")1358 draft.delete("foo")1359 })1360 expect(patches).toEqual([])1361 }1362})fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
lines 1363–1409why, one step back13631364test("#888 patch to a primitive produces the primitive", () => {1365 {1366 const [res, patches] = produceWithPatches({abc: 123}, draft => nothing)1367 expect(res).toEqual(undefined)1368 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1369 }1370 {1371 const [res, patches] = produceWithPatches(null, draft => nothing)1372 expect(res).toEqual(undefined)1373 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1374 }1375 {1376 const [res, patches] = produceWithPatches(0, draft => nothing)1377 expect(res).toEqual(undefined)1378 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1379 }1380 {1381 const [res, patches] = produceWithPatches("foobar", draft => nothing)1382 expect(res).toEqual(undefined)1383 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1384 }1385 {1386 const [res, patches] = produceWithPatches([], draft => nothing)1387 expect(res).toEqual(undefined)1388 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1389 }1390 {1391 const [res, patches] = produceWithPatches(false, draft => nothing)1392 expect(res).toEqual(undefined)1393 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1394 }1395 {1396 const [res, patches] = produceWithPatches(1397 "foobar",1398 draft => "something else"1399 )1400 expect(res).toEqual("something else")1401 expect(patches).toEqual([1402 {op: "replace", path: [], value: "something else"}1403 ])1404 }1405 {1406 const [res, patches] = produceWithPatches(false, draft => true)1407 expect(res).toEqual(true)1408 expect(patches).toEqual([{op: "replace", path: [], value: true}])1409 }fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 1410–1433why, one step back1410})14111412describe("#879 delete item from array", () => {1413 runPatchTest(1414 [1, 2, 3],1415 draft => {1416 delete draft[1]1417 },1418 [{op: "replace", path: [1], value: undefined}],1419 [{op: "replace", path: [1], value: 2}],1420 [1, undefined, 3]1421 )1422})14231424describe("#879 delete item from array - 2", () => {1425 runPatchTest(1426 [1, 2, 3],1427 draft => {1428 delete draft[2]1429 },1430 [{op: "replace", path: [2], value: undefined}],1431 [{op: "replace", path: [2], value: 3}],1432 [1, 2, undefined]1433 )fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
line 1434why, one step back1434})chore: added unit test for #897Michel Weststratebefore adoption607b994
lines 1435–1444why, one step back14351436test("#897 appendPatch", () => {1437 const state0 = {a: []}1438 const state1 = applyPatches(state0, [{op: "add", path: ["a", "-"], value: 1}])1439 const state2 = applyPatches(state1, [{op: "add", path: ["a", "-"], value: 2}])1440 const state3 = applyPatches(state2, [{op: "add", path: ["a", "-"], value: 3}])1441 expect(state3).toEqual({1442 a: [1, 2, 3]1443 })1444})