patch.js
This file
Path
__tests__/patch.js
1,287 lines · 23.9 KiB · JavaScript
Last changed by
fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f
History of this fileTake it
The walk stopped after 200 commits: lines older than a46b9e7 point at it.
Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
line 1why, one step back1"use strict"fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
lines 2–4why, one step back2import produce, {3 setUseProxies,4 applyPatches,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 5why, one step back5 produceWithPatches,fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
line 6why, one step back6 enableAllPlugins,Immer 7Michel Weststratebefore adoption467ea5d
line 7why, one step back7 isDraft,fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f
lines 8–9why, one step back8 immerable,9 nothingMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 10–12why, one step back10} from "../src/immer"1112enableAllPlugins()Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 13–15why, one step back1314jest.setTimeout(1000)15fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 16–17why, one step back16const isProd = process.env.NODE_ENV === "production"17Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
line 18why, one step back18function runPatchTest(base, producer, patches, inversePathes) {fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 19–20why, one step back19 let resultProxies, resultEs520Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 21–40why, one step back21 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 })fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 41–42why, one step back4142 return resMerge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 43–46why, one step back43 }4445 describe(`proxy`, () => {46 setUseProxies(true)fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
line 47why, one step back47 resultProxies = runPatchTestHelper()Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 48–51why, one step back48 })4950 describe(`es5`, () => {51 setUseProxies(false)fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 52–55why, one step back52 resultEs5 = runPatchTestHelper()53 test("ES5 and Proxy implementation yield same result", () => {54 expect(resultEs5).toEqual(resultProxies)55 })Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
line 56why, one step back56 })fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 57–58why, one step back5758 return resultProxiesMerge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 59–95why, one step back59}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()fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
lines 96–117why, one step back96 })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)Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 118–150why, one step back118 })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})150feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 151–184why, one step back151describe("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 ],chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 185–188why, one step back185 [186 {op: "replace", path: ["x"], value: 1},187 {op: "remove", path: ["y"]}188 ]feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 189–211why, one step back189 )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})211Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 212–221why, one step back212describe("delete 1", () => {213 runPatchTest(214 {x: {y: 4}},215 d => {216 delete d.x217 },218 [{op: "remove", path: ["x"]}]219 )220})221feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 222–247why, one step back222describe("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(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 248–253why, one step back248 {249 x: new Map([250 [key1, 1],251 [key2, 2]252 ])253 },feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 254–258why, one step back254 d => {255 d.x.delete(key1)256 d.x.delete(key2)257 },258 [chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 259–262why, one step back259 {op: "remove", path: ["x", key1]},260 {op: "remove", path: ["x", key2]}261 ],262 [feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 263–290why, one step back263 {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})290Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 291–298why, one step back291describe("renaming properties", () => {292 describe("nested object (no changes)", () => {293 runPatchTest(294 {a: {b: 1}},295 d => {296 d.x = d.a297 delete d.a298 },chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 299–302why, one step back299 [300 {op: "add", path: ["x"], value: {b: 1}},301 {op: "remove", path: ["a"]}302 ]Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 303–305why, one step back303 )304 })305Immer 7Michel Weststratebefore adoption467ea5d
lines 306–340why, one step back306 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 })340feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 341–358why, one step back341 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 })358Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 359–378why, one step back359 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 })378feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 379–380why, one step back379 describe("nested map (with changes)", () => {380 runPatchTest(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 381–389why, one step back381 new Map([382 [383 "a",384 new Map([385 ["b", 1],386 ["c", 1]387 ])388 ]389 ]),feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 390–400why, one step back390 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 [chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 401–408why, one step back401 {402 op: "add",403 path: ["x"],404 value: new Map([405 ["b", 2],406 ["y", 2]407 ])408 },feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 409–412why, one step back409 {op: "remove", path: ["a"]}410 ],411 [412 {op: "remove", path: ["x"]},chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 413–420why, one step back413 {414 op: "add",415 path: ["a"],416 value: new Map([417 ["b", 1],418 ["c", 1]419 ])420 }feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 421–424why, one step back421 ]422 )423 })424Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 425–443why, one step back425 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 })feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 444–446why, one step back444445 describe("deeply nested map (with changes)", () => {446 runPatchTest(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 447–460why, one step back447 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 ]),feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 461–474why, one step back461 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"],chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 475–478why, one step back475 value: new Map([476 ["c", 2],477 ["y", 2]478 ])feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 479–486why, one step back479 },480 {op: "remove", path: ["a", "b"]}481 ],482 [483 {op: "remove", path: ["a", "x"]},484 {485 op: "add",486 path: ["a", "b"],chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 487–490why, one step back487 value: new Map([488 ["c", 1],489 ["d", 1]490 ])feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 491–494why, one step back491 }492 ]493 )494 })Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 495–517why, one step back495})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 },Immer 7Michel Weststratebefore adoption467ea5d
lines 518–523why, one step back518 [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 ]Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 524–532why, one step back524 )525})526527describe("arrays - multiple prepend", () => {528 runPatchTest(529 {x: [1, 2, 3]},530 d => {531 d.x.unshift(4)532 d.x.unshift(5)Immer 7Michel Weststratebefore adoption467ea5d
line 533why, one step back533 // 4,5,1,2,3Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 534–535why, one step back534 },535 [Immer 7Michel Weststratebefore adoption467ea5d
lines 536–540why, one step back536 {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}Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 541–550why, one step back541 ]542 )543})544545describe("arrays - splice middle", () => {546 runPatchTest(547 {x: [1, 2, 3]},548 d => {549 d.x.splice(1, 1)550 },Immer 7Michel Weststratebefore adoption467ea5d
lines 551–554why, one step back551 [552 {op: "replace", path: ["x", 1], value: 3},553 {op: "replace", path: ["x", "length"], value: 2}554 ]Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 555–562why, one step back555 )556})557558describe("arrays - multiple splice", () => {559 runPatchTest(560 [0, 1, 2, 3, 4, 5, 0],561 d => {562 d.splice(4, 2, 3)Immer 7Michel Weststratebefore adoption467ea5d
line 563why, one step back563 // [0,1,2,3,3,0]Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
line 564why, one step back564 d.splice(1, 2, 3)Immer 7Michel Weststratebefore adoption467ea5d
lines 565–566why, one step back565 // [0,3,3,3,0]566 expect(d.slice()).toEqual([0, 3, 3, 3, 0])Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 567–570why, one step back567 },568 [569 {op: "replace", path: [1], value: 3},570 {op: "replace", path: [2], value: 3},Immer 7Michel Weststratebefore adoption467ea5d
lines 571–572why, one step back571 {op: "replace", path: [4], value: 0},572 {op: "replace", path: ["length"], value: 5}Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 573–582why, one step back573 ]574 )575})576577describe("arrays - modify and shrink", () => {578 runPatchTest(579 {x: [1, 2, 3]},580 d => {581 d.x[0] = 4582 d.x.length = 2Immer 7Michel Weststratebefore adoption467ea5d
line 583why, one step back583 // [0, 2]Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 584–585why, one step back584 },585 [chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
line 586why, one step back586 {op: "replace", path: ["x", 0], value: 4},Immer 7Michel Weststratebefore adoption467ea5d
line 587why, one step back587 {op: "replace", path: ["x", "length"], value: 2}chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 588–589why, one step back588 ],589 [Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 590–591why, one step back590 {op: "replace", path: ["x", 0], value: 1},591 {op: "add", path: ["x", 2], value: 3}Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 592–601why, one step back592 ]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)Immer 7Michel Weststratebefore adoption467ea5d
line 602why, one step back602 // 4, 1, 3Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 603–616why, one step back603 },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)Immer 7Michel Weststratebefore adoption467ea5d
line 617why, one step back617 // [4, 1, 3]Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 618–631why, one step back618 },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 },Immer 7Michel Weststratebefore adoption467ea5d
line 632why, one step back632 [{op: "replace", path: ["x", "length"], value: 1}],Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 633–646why, one step back633 [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 },Immer 7Michel Weststratebefore adoption467ea5d
line 647why, one step back647 [{op: "replace", path: ["x", "length"], value: 1}]Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 648–651why, one step back648 )649})650651describe("arrays - push multiple", () => {Immer 7Michel Weststratebefore adoption467ea5d
line 652why, one step back652 // These patches were more optimal pre immer 7, but not always correctMerge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 653–661why, one step back653 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 ],Immer 7Michel Weststratebefore adoption467ea5d
line 662why, one step back662 [{op: "replace", path: ["x", "length"], value: 3}]Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 663–666why, one step back663 )664})665666describe("arrays - splice (expand)", () => {Immer 7Michel Weststratebefore adoption467ea5d
line 667why, one step back667 // These patches were more optimal pre immer 7, but not always correctMerge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 668–670why, one step back668 runPatchTest(669 {x: [1, 2, 3]},670 d => {Immer 7Michel Weststratebefore adoption467ea5d
line 671why, one step back671 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 672–674why, one step back672 },673 [674 {op: "replace", path: ["x", 1], value: 4},Immer 7Michel Weststratebefore adoption467ea5d
lines 675–677why, one step back675 {op: "replace", path: ["x", 2], value: 5},676 {op: "add", path: ["x", 3], value: 6},677 {op: "add", path: ["x", 4], value: 3}Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 678–680why, one step back678 ],679 [680 {op: "replace", path: ["x", 1], value: 2},Immer 7Michel Weststratebefore adoption467ea5d
lines 681–682why, one step back681 {op: "replace", path: ["x", 2], value: 3},682 {op: "replace", path: ["x", "length"], value: 3}Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 683–687why, one step back683 ]684 )685})686687describe("arrays - splice (shrink)", () => {Immer 7Michel Weststratebefore adoption467ea5d
line 688why, one step back688 // These patches were more optimal pre immer 7, but not always correctMerge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 689–691why, one step back689 runPatchTest(690 {x: [1, 2, 3, 4, 5]},691 d => {Immer 7Michel Weststratebefore adoption467ea5d
line 692why, one step back692 d.x.splice(1, 3, 6) // [1, 6, 5]Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 693–695why, one step back693 },694 [695 {op: "replace", path: ["x", 1], value: 6},Immer 7Michel Weststratebefore adoption467ea5d
lines 696–697why, one step back696 {op: "replace", path: ["x", 2], value: 5},697 {op: "replace", path: ["x", "length"], value: 3}Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 698–700why, one step back698 ],699 [700 {op: "replace", path: ["x", 1], value: 2},Immer 7Michel Weststratebefore adoption467ea5d
lines 701–703why, one step back701 {op: "replace", path: ["x", 2], value: 3},702 {op: "add", path: ["x", 3], value: 4},703 {op: "add", path: ["x", 4], value: 5}Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
line 704why, one step back704 ]Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 705–707why, one step back705 )706})707fix: incorrect patches generated for array if property was deleted insideMichel Weststratebefore adoption1e451a3
lines 708–722why, one step back708describe("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})722feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 723–766why, one step back723describe("sets - add - 1", () => {724 runPatchTest(725 new Set([1]),726 d => {727 d.add(2)728 },729 [{op: "add", path: [1], value: 2}],730 [{op: "remove", path: [1], value: 2}]731 )732})733734describe("sets - add, delete, add - 1", () => {735 runPatchTest(736 new Set([1]),737 d => {738 d.add(2)739 d.delete(2)740 d.add(2)741 },742 [{op: "add", path: [1], value: 2}],743 [{op: "remove", path: [1], value: 2}]744 )745})746747describe("sets - add, delete, add - 2", () => {748 runPatchTest(749 new Set([2, 1]),750 d => {751 d.add(2)752 d.delete(2)753 d.add(2)754 },755 [],756 []757 )758})759760describe("sets - mutate - 1", () => {761 const findById = (set, id) => {762 for (const item of set) {763 if (item.id === id) return item764 }765 }766 runPatchTest(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 767–770why, one step back767 new Set([768 {id: 1, val: "We"},769 {id: 2, val: "will"}770 ]),feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 771–791why, one step back771 d => {772 const obj1 = findById(d, 1)773 const obj2 = findById(d, 2)774 obj1.val = "rock"775 obj2.val = "you"776 },777 [778 {op: "remove", path: [0], value: {id: 1, val: "We"}},779 {op: "remove", path: [1], value: {id: 2, val: "will"}},780 {op: "add", path: [0], value: {id: 1, val: "rock"}},781 {op: "add", path: [1], value: {id: 2, val: "you"}}782 ],783 [784 {op: "remove", path: [1], value: {id: 2, val: "you"}},785 {op: "remove", path: [0], value: {id: 1, val: "rock"}},786 {op: "add", path: [1], value: {id: 2, val: "will"}},787 {op: "add", path: [0], value: {id: 1, val: "We"}}788 ]789 )790})791Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
line 792why, one step back792describe("arrays - splice should should result in remove op.", () => {Immer 7Michel Weststratebefore adoption467ea5d
line 793why, one step back793 // These patches were more optimal pre immer 7, but not always correctMerge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 794–798why, one step back794 runPatchTest(795 [1, 2],796 d => {797 d.splice(1, 1)798 },Immer 7Michel Weststratebefore adoption467ea5d
line 799why, one step back799 [{op: "replace", path: ["length"], value: 1}],Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 800–804why, one step back800 [{op: "add", path: [1], value: 2}]801 )802})803804describe("arrays - NESTED splice should should result in remove op.", () => {Immer 7Michel Weststratebefore adoption467ea5d
line 805why, one step back805 // These patches were more optimal pre immer 7, but not always correctMerge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 806–810why, one step back806 runPatchTest(807 {a: {b: {c: [1, 2]}}},808 d => {809 d.a.b.c.splice(1, 1)810 },Immer 7Michel Weststratebefore adoption467ea5d
line 811why, one step back811 [{op: "replace", path: ["a", "b", "c", "length"], value: 1}],Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
line 812why, one step back812 [{op: "add", path: ["a", "b", "c", 1], value: 2}]Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 813–859why, one step back813 )814})815816describe("simple replacement", () => {817 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])818})819820describe("same value replacement - 1", () => {821 runPatchTest(822 {x: {y: 3}},823 d => {824 const a = d.x825 d.x = a826 },827 []828 )829})830831describe("same value replacement - 2", () => {832 runPatchTest(833 {x: {y: 3}},834 d => {835 const a = d.x836 d.x = 4837 d.x = a838 },839 []840 )841})842843describe("same value replacement - 3", () => {844 runPatchTest(845 {x: 3},846 d => {847 d.x = 3848 },849 []850 )851})852853describe("same value replacement - 4", () => {854 runPatchTest(855 {x: 3},856 d => {857 d.x = 4858 d.x = 3859 },feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 860–883why, one step back860 []861 )862})863864describe("same value replacement - 5", () => {865 runPatchTest(866 new Map([["x", 3]]),867 d => {868 d.set("x", 4)869 d.set("x", 3)870 },871 [],872 []873 )874})875876describe("same value replacement - 6", () => {877 runPatchTest(878 new Set(["x", 3]),879 d => {880 d.delete("x")881 d.add("x")882 },883 [],Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
lines 884–900why, one step back884 []885 )886})887888describe("simple delete", () => {889 runPatchTest(890 {x: 2},891 d => {892 delete d.x893 },894 [895 {896 op: "remove",897 path: ["x"]898 }899 ]900 )fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 901–961why, one step back901})902903describe("patch compressions yields correct results", () => {904 let p1, p2905 runPatchTest(906 {},907 d => {908 d.x = {test: true}909 },910 (p1 = [911 {912 op: "add",913 path: ["x"],914 value: {915 test: true916 }917 }918 ])919 )920 runPatchTest(921 {x: {test: true}},922 d => {923 delete d.x924 },925 (p2 = [926 {927 op: "remove",928 path: ["x"]929 }930 ])931 )932 const res = runPatchTest(933 {},934 d => {935 applyPatches(d, [...p1, ...p2])936 },937 []938 )939940 expect(res).toEqual({})941})942943describe("change then delete property", () => {944 const res = runPatchTest(945 {946 x: 1947 },948 d => {949 d.x = 2950 delete d.x951 },952 [953 {954 op: "remove",955 path: ["x"]956 }957 ]958 )959 test("valid result", () => {960 expect(res).toEqual({})961 })fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
lines 962–1006why, one step back962})963964test("replaying patches with interweaved replacements should work correctly", () => {965 const patches = []966 const s0 = {x: 1}967968 const s1 = produce(969 s0,970 draft => {971 draft.x = 2972 },973 p => {974 patches.push(...p)975 }976 )977978 const s2 = produce(979 s1,980 draft => {981 return {x: 0}982 },983 p => {984 patches.push(...p)985 }986 )987988 const s3 = produce(989 s2,990 draft => {991 draft.x--992 },993 p => {994 patches.push(...p)995 }996 )997998 expect(s3).toEqual({x: -1}) // correct result999 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay10001001 // manual replay on a draft should also be correct1002 expect(1003 produce(s0, draft => {1004 return applyPatches(draft, patches)1005 })1006 ).toEqual({x: -1})chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 1007–1008why, one step back1007})1008Immer 7Michel Weststratebefore adoption467ea5d
lines 1009–1016why, one step back1009describe("#468", () => {1010 function run() {1011 const item = {id: 1}1012 const state = [item]1013 const [nextState, patches] = produceWithPatches(state, draft => {1014 draft[0].id = 21015 draft[1] = item1016 })chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
line 1017why, one step back1017Immer 7Michel Weststratebefore adoption467ea5d
lines 1018–1032why, one step back1018 expect(nextState).toEqual([{id: 2}, {id: 1}])1019 expect(patches).toEqual([1020 {1021 op: "replace",1022 path: [0, "id"],1023 value: 21024 },1025 {1026 op: "add",1027 path: [1],1028 value: {1029 id: 11030 }1031 }1032 ])chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
line 1033why, one step back1033Immer 7Michel Weststratebefore adoption467ea5d
lines 1034–1040why, one step back1034 const final = applyPatches(state, patches)1035 expect(final).toEqual(nextState)1036 }10371038 test("es5", () => {1039 setUseProxies(false)1040 run()chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 1041–1042why, one step back1041 })1042Immer 7Michel Weststratebefore adoption467ea5d
lines 1043–1046why, one step back1043 test("proxy", () => {1044 setUseProxies(true)1045 run()1046 })Merge pull request #397 from snapwich/patch-1Michel Weststratebefore adoptiona46b9e7
line 1047why, one step back1047})fix: `Set` inside patches were not correctly cloned. Fixes #521Michel Weststratebefore adoption9d8d995
lines 1048–1065why, one step back10481049test("#521", () => {1050 const state = new Map()10511052 const [nextState, patches] = produceWithPatches(state, draft => {1053 draft.set("hello", new Set(["world"]))1054 })10551056 let patchedState = applyPatches(state, patches)1057 expect(patchedState).toEqual(nextState)10581059 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1060 draft.get("hello").add("immer")1061 })10621063 expect(applyPatches(nextState, patchesV2)).toEqual(1064 new Map([["hello", new Set(["world", "immer"])]])1065 )fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 1066–1098why, one step back1066})10671068test("#559 patches works in a nested reducer with proxies", () => {1069 setUseProxies(true)10701071 const state = {1072 x: 1,1073 sub: {1074 y: [{a: 0}, {a: 1}]1075 }1076 }10771078 const changes = []1079 const inverseChanges = []10801081 const newState = produce(state, draft => {1082 draft.sub = produce(1083 draft.sub,1084 draft => {1085 draft.y.pop()1086 },1087 (patches, inversePatches) => {1088 expect(isDraft(inversePatches[0].value)).toBeFalsy()1089 expect(inversePatches[0].value).toMatchObject({a: 1})1090 changes.push(...patches)1091 inverseChanges.push(...inversePatches)1092 }1093 )1094 })10951096 const reversedSubState = applyPatches(newState.sub, inverseChanges)10971098 expect(reversedSubState).toMatchObject(state.sub)Immer 7Michel Weststratebefore adoption467ea5d
lines 1099–1123why, one step back1099})11001101describe("#588", () => {1102 const reference = {value: {num: 53}}11031104 class Base {1105 [immerable] = true1106 get nested() {1107 return reference.value1108 }1109 set nested(value) {}1110 }11111112 let base = new Base()11131114 runPatchTest(1115 base,1116 vdraft => {1117 reference.value = vdraft1118 produce(base, bdraft => {1119 bdraft.nested.num = 421120 })1121 },1122 [{op: "add", path: ["num"], value: 42}]1123 )fix: `Set` inside patches were not correctly cloned. Fixes #521Michel Weststratebefore adoption9d8d995
line 1124why, one step back1124})fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 1125–1136why, one step back11251126test("#676 patching Date objects", () => {1127 class Test {1128 constructor() {1129 this.test = true1130 }1131 perform() {1132 return "tested!"1133 }1134 }11351136 const [nextState, patches] = produceWithPatches({}, function(draft) {chore(tests): use UTC date string in tests to be timezone independent (#705)Grzegorz Rozdzialikbefore adoptione8fd805
line 1137why, one step back1137 draft.date = new Date("2020-11-10T08:08:08.003Z")fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 1138–1150why, one step back1138 draft.test = new Test()1139 })11401141 expect(nextState.date.toJSON()).toMatchInlineSnapshot(1142 `"2020-11-10T08:08:08.003Z"`1143 )1144 expect(nextState.test.perform()).toBe("tested!")11451146 const rebuilt = applyPatches({}, patches)1147 expect(rebuilt.date).toBeInstanceOf(Date)1148 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1149 `"2020-11-10T08:08:08.003Z"`1150 )chore(tests): use UTC date string in tests to be timezone independent (#705)Grzegorz Rozdzialikbefore adoptione8fd805
line 1151why, one step back1151 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 1152–1258why, one step back1152})11531154test("do not allow __proto__ polution - 738", () => {1155 const obj = {}11561157 // @ts-ignore1158 expect(obj.polluted).toBe(undefined)1159 expect(() => {1160 applyPatches({}, [1161 {op: "add", path: ["__proto__", "polluted"], value: "yes"}1162 ])1163 }).toThrow(1164 isProd1165 ? "24"1166 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1167 )1168 // @ts-ignore1169 expect(obj.polluted).toBe(undefined)1170})11711172test("do not allow __proto__ polution using arrays - 738", () => {1173 const obj = {}1174 const ar = []11751176 // @ts-ignore1177 expect(obj.polluted).toBe(undefined)1178 // @ts-ignore1179 expect(ar.polluted).toBe(undefined)1180 expect(() => {1181 applyPatches(1182 [],1183 [{op: "add", path: ["__proto__", "polluted"], value: "yes"}]1184 )1185 }).toThrow(1186 isProd1187 ? "24"1188 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1189 )1190 // @ts-ignore1191 expect(obj.polluted).toBe(undefined)1192 // @ts-ignore1193 expect(ar.polluted).toBe(undefined)1194})11951196test("do not allow prototype polution - 738", () => {1197 const obj = {}11981199 // @ts-ignore1200 expect(obj.polluted).toBe(undefined)1201 expect(() => {1202 applyPatches(Object, [1203 {op: "add", path: ["prototype", "polluted"], value: "yes"}1204 ])1205 }).toThrow(1206 isProd1207 ? "24"1208 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1209 )1210 // @ts-ignore1211 expect(obj.polluted).toBe(undefined)1212})12131214test("do not allow constructor polution - 738", () => {1215 const obj = {}12161217 // @ts-ignore1218 expect(obj.polluted).toBe(undefined)1219 const t = {}1220 applyPatches(t, [{op: "replace", path: ["constructor"], value: "yes"}])1221 expect(typeof t.constructor).toBe("function")1222 // @ts-ignore1223 expect(Object.polluted).toBe(undefined)1224})12251226test("do not allow constructor.prototype polution - 738", () => {1227 const obj = {}12281229 // @ts-ignore1230 expect(obj.polluted).toBe(undefined)1231 expect(() => {1232 applyPatches({}, [1233 {op: "add", path: ["constructor", "prototype", "polluted"], value: "yes"}1234 ])1235 }).toThrow(1236 isProd1237 ? "24"1238 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1239 )1240 // @ts-ignore1241 expect(Object.polluted).toBe(undefined)1242})12431244test("maps can store __proto__, prototype and constructor props", () => {1245 const obj = {}1246 const map = new Map()1247 map.set("__proto__", {})1248 map.set("constructor", {})1249 map.set("prototype", {})1250 const newMap = applyPatches(map, [1251 {op: "add", path: ["__proto__", "polluted"], value: "yes"},1252 {op: "add", path: ["constructor", "polluted"], value: "yes"},1253 {op: "add", path: ["prototype", "polluted"], value: "yes"}1254 ])1255 expect(newMap.get("__proto__").polluted).toBe("yes")1256 expect(newMap.get("constructor").polluted).toBe("yes")1257 expect(newMap.get("prototype").polluted).toBe("yes")1258 expect(obj.polluted).toBe(undefined)fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
line 1259why, one step back1259})fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d
lines 1260–1279why, one step back12601261test("#648 assigning object to itself should not change patches", () => {1262 const input = {1263 obj: {1264 value: 2001265 }1266 }12671268 const [nextState, patches] = produceWithPatches(input, draft => {1269 draft.obj.value = 11270 draft.obj = draft.obj1271 })12721273 expect(patches).toEqual([1274 {1275 op: "replace",1276 path: ["obj", "value"],1277 value: 11278 }1279 ])fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f
lines 1280–1286why, one step back1280})12811282test("#791 patch for nothing is stored as undefined", () => {1283 const [newState, patches] = produceWithPatches({abc: 123}, draft => nothing)1284 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])12851286 expect(applyPatches({}, patches)).toEqual(undefined)fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d
line 1287why, one step back1287})