Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

1,429 lines · 27.5 KiB · JavaScript

Last changed by

chore: added unit test for #897Michel Weststratebefore adoption607b994

History of this file

Take it

The walk stopped after 200 commits: lines older than b10dfa4 point at it.

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 1–4why, one step back
1"use strict"
2import produce, {3 setUseProxies,4 applyPatches,

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 5why, one step back
5 produceWithPatches,

fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6

line 6why, one step back
6 enableAllPlugins,

Immer 7Michel Weststratebefore adoption467ea5d

line 7why, one step back
7 isDraft,

fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f

lines 8–9why, one step back
8 immerable,
9 nothing

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 10–12why, one step back
10} from "../src/immer"
1112enableAllPlugins()

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 13–15why, one step back
13
14jest.setTimeout(1000)15

fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f

lines 16–17why, one step back
16const isProd = process.env.NODE_ENV === "production"
17

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

line 18why, one step back
18function runPatchTest(base, producer, patches, inversePathes, expectedResult) {

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 19–28why, one step back
19 let resultProxies, resultEs5
2021 function runPatchTestHelper() {22 let recordedPatches23 let recordedInversePatches24 const res = produce(base, producer, (p, i) => {25 recordedPatches = p26 recordedInversePatches = i27 })28

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 29–33why, one step back
29 if (expectedResult !== undefined)
30 test("produced the correct result", () => {31 expect(res).toEqual(expectedResult)32 })33

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 34–310why, one step back
34 test("produces the correct patches", () => {
35 expect(recordedPatches).toEqual(patches)36 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)37 })3839 test("patches are replayable", () => {40 expect(applyPatches(base, recordedPatches)).toEqual(res)41 })4243 test("patches can be reversed", () => {44 expect(applyPatches(res, recordedInversePatches)).toEqual(base)45 })4647 return res48 }4950 describe(`proxy`, () => {51 setUseProxies(true)52 resultProxies = runPatchTestHelper()53 })5455 describe(`es5`, () => {56 setUseProxies(false)57 resultEs5 = runPatchTestHelper()58 test("ES5 and Proxy implementation yield same result", () => {59 expect(resultEs5).toEqual(resultProxies)60 })61 })6263 return resultProxies64}6566describe("applyPatches", () => {67 it("mutates the base state when it is a draft", () => {68 produce({a: 1}, draft => {69 const result = applyPatches(draft, [70 {op: "replace", path: ["a"], value: 2}71 ])72 expect(result).toBe(draft)73 expect(draft.a).toBe(2)74 })75 })76 it("produces a copy of the base state when not a draft", () => {77 const base = {a: 1}78 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])79 expect(result).not.toBe(base)80 expect(result.a).toBe(2)81 expect(base.a).toBe(1)82 })83 it('throws when `op` is not "add", "replace", nor "remove"', () => {84 expect(() => {85 const patch = {op: "copy", from: [0], path: [1]}86 applyPatches([2], [patch])87 }).toThrowErrorMatchingSnapshot()88 })89 it("throws when `path` cannot be resolved", () => {90 // missing parent91 expect(() => {92 const patch = {op: "add", path: ["a", "b"], value: 1}93 applyPatches({}, [patch])94 }).toThrowErrorMatchingSnapshot()9596 // missing grand-parent97 expect(() => {98 const patch = {op: "add", path: ["a", "b", "c"], value: 1}99 applyPatches({}, [patch])100 }).toThrowErrorMatchingSnapshot()101 })102 it("applied patches cannot be modified", () => {103 // see also: https://github.com/immerjs/immer/issues/411104 const s0 = {105 items: [1]106 }107108 const [s1, p1] = produceWithPatches(s0, draft => {109 draft.items = []110 })111112 const replaceValueBefore = p1[0].value.slice()113114 const [s2, p2] = produceWithPatches(s1, draft => {115 draft.items.push(2)116 })117118 applyPatches(s0, [...p1, ...p2])119120 const replaceValueAfter = p1[0].value.slice()121122 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)123 })124})125126describe("simple assignment - 1", () => {127 runPatchTest(128 {x: 3},129 d => {130 d.x++131 },132 [{op: "replace", path: ["x"], value: 4}]133 )134})135136describe("simple assignment - 2", () => {137 runPatchTest(138 {x: {y: 4}},139 d => {140 d.x.y++141 },142 [{op: "replace", path: ["x", "y"], value: 5}]143 )144})145146describe("simple assignment - 3", () => {147 runPatchTest(148 {x: [{y: 4}]},149 d => {150 d.x[0].y++151 },152 [{op: "replace", path: ["x", 0, "y"], value: 5}]153 )154})155156describe("simple assignment - 4", () => {157 runPatchTest(158 new Map([["x", {y: 4}]]),159 d => {160 d.get("x").y++161 },162 [{op: "replace", path: ["x", "y"], value: 5}],163 [{op: "replace", path: ["x", "y"], value: 4}]164 )165})166167describe("simple assignment - 5", () => {168 runPatchTest(169 {x: new Map([["y", 4]])},170 d => {171 d.x.set("y", 5)172 },173 [{op: "replace", path: ["x", "y"], value: 5}],174 [{op: "replace", path: ["x", "y"], value: 4}]175 )176})177178describe("simple assignment - 6", () => {179 runPatchTest(180 new Map([["x", 1]]),181 d => {182 // Map.prototype.set should return the Map itself183 const res = d.set("x", 2)184 res.set("y", 3)185 },186 [187 {op: "replace", path: ["x"], value: 2},188 {op: "add", path: ["y"], value: 3}189 ],190 [191 {op: "replace", path: ["x"], value: 1},192 {op: "remove", path: ["y"]}193 ]194 )195})196197describe("simple assignment - 7", () => {198 const key1 = {prop: "val1"}199 const key2 = {prop: "val2"}200 runPatchTest(201 {x: new Map([[key1, 4]])},202 d => {203 d.x.set(key1, 5)204 d.x.set(key2, 6)205 },206 [207 {op: "replace", path: ["x", key1], value: 5},208 {op: "add", path: ["x", key2], value: 6}209 ],210 [211 {op: "replace", path: ["x", key1], value: 4},212 {op: "remove", path: ["x", key2]}213 ]214 )215})216217describe("delete 1", () => {218 runPatchTest(219 {x: {y: 4}},220 d => {221 delete d.x222 },223 [{op: "remove", path: ["x"]}]224 )225})226227describe("delete 2", () => {228 runPatchTest(229 new Map([["x", 1]]),230 d => {231 d.delete("x")232 },233 [{op: "remove", path: ["x"]}],234 [{op: "add", path: ["x"], value: 1}]235 )236})237238describe("delete 3", () => {239 runPatchTest(240 {x: new Map([["y", 1]])},241 d => {242 d.x.delete("y")243 },244 [{op: "remove", path: ["x", "y"]}],245 [{op: "add", path: ["x", "y"], value: 1}]246 )247})248249describe("delete 5", () => {250 const key1 = {prop: "val1"}251 const key2 = {prop: "val2"}252 runPatchTest(253 {254 x: new Map([255 [key1, 1],256 [key2, 2]257 ])258 },259 d => {260 d.x.delete(key1)261 d.x.delete(key2)262 },263 [264 {op: "remove", path: ["x", key1]},265 {op: "remove", path: ["x", key2]}266 ],267 [268 {op: "add", path: ["x", key1], value: 1},269 {op: "add", path: ["x", key2], value: 2}270 ]271 )272})273274describe("delete 6", () => {275 runPatchTest(276 new Set(["x", 1]),277 d => {278 d.delete("x")279 },280 [{op: "remove", path: [0], value: "x"}],281 [{op: "add", path: [0], value: "x"}]282 )283})284285describe("delete 7", () => {286 runPatchTest(287 {x: new Set(["y", 1])},288 d => {289 d.x.delete("y")290 },291 [{op: "remove", path: ["x", 0], value: "y"}],292 [{op: "add", path: ["x", 0], value: "y"}]293 )294})295296describe("renaming properties", () => {297 describe("nested object (no changes)", () => {298 runPatchTest(299 {a: {b: 1}},300 d => {301 d.x = d.a302 delete d.a303 },304 [305 {op: "add", path: ["x"], value: {b: 1}},306 {op: "remove", path: ["a"]}307 ]308 )309 })310

Immer 7Michel Weststratebefore adoption467ea5d

lines 311–345why, one step back
311 describe("nested change in object", () => {
312 runPatchTest(313 {314 a: {b: 1}315 },316 d => {317 d.a.b++318 },319 [{op: "replace", path: ["a", "b"], value: 2}],320 [{op: "replace", path: ["a", "b"], value: 1}]321 )322 })323324 describe("nested change in map", () => {325 runPatchTest(326 new Map([["a", new Map([["b", 1]])]]),327 d => {328 d.get("a").set("b", 2)329 },330 [{op: "replace", path: ["a", "b"], value: 2}],331 [{op: "replace", path: ["a", "b"], value: 1}]332 )333 })334335 describe("nested change in array", () => {336 runPatchTest(337 [[{b: 1}]],338 d => {339 d[0][0].b++340 },341 [{op: "replace", path: [0, 0, "b"], value: 2}],342 [{op: "replace", path: [0, 0, "b"], value: 1}]343 )344 })345

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 346–522why, one step back
346 describe("nested map (no changes)", () => {
347 runPatchTest(348 new Map([["a", new Map([["b", 1]])]]),349 d => {350 d.set("x", d.get("a"))351 d.delete("a")352 },353 [354 {op: "add", path: ["x"], value: new Map([["b", 1]])},355 {op: "remove", path: ["a"]}356 ],357 [358 {op: "remove", path: ["x"]},359 {op: "add", path: ["a"], value: new Map([["b", 1]])}360 ]361 )362 })363364 describe("nested object (with changes)", () => {365 runPatchTest(366 {a: {b: 1, c: 1}},367 d => {368 let a = d.a369 a.b = 2 // change370 delete a.c // delete371 a.y = 2 // add372373 // rename374 d.x = a375 delete d.a376 },377 [378 {op: "add", path: ["x"], value: {b: 2, y: 2}},379 {op: "remove", path: ["a"]}380 ]381 )382 })383384 describe("nested map (with changes)", () => {385 runPatchTest(386 new Map([387 [388 "a",389 new Map([390 ["b", 1],391 ["c", 1]392 ])393 ]394 ]),395 d => {396 let a = d.get("a")397 a.set("b", 2) // change398 a.delete("c") // delete399 a.set("y", 2) // add400401 // rename402 d.set("x", a)403 d.delete("a")404 },405 [406 {407 op: "add",408 path: ["x"],409 value: new Map([410 ["b", 2],411 ["y", 2]412 ])413 },414 {op: "remove", path: ["a"]}415 ],416 [417 {op: "remove", path: ["x"]},418 {419 op: "add",420 path: ["a"],421 value: new Map([422 ["b", 1],423 ["c", 1]424 ])425 }426 ]427 )428 })429430 describe("deeply nested object (with changes)", () => {431 runPatchTest(432 {a: {b: {c: 1, d: 1}}},433 d => {434 let b = d.a.b435 b.c = 2 // change436 delete b.d // delete437 b.y = 2 // add438439 // rename440 d.a.x = b441 delete d.a.b442 },443 [444 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},445 {op: "remove", path: ["a", "b"]}446 ]447 )448 })449450 describe("deeply nested map (with changes)", () => {451 runPatchTest(452 new Map([453 [454 "a",455 new Map([456 [457 "b",458 new Map([459 ["c", 1],460 ["d", 1]461 ])462 ]463 ])464 ]465 ]),466 d => {467 let b = d.get("a").get("b")468 b.set("c", 2) // change469 b.delete("d") // delete470 b.set("y", 2) // add471472 // rename473 d.get("a").set("x", b)474 d.get("a").delete("b")475 },476 [477 {478 op: "add",479 path: ["a", "x"],480 value: new Map([481 ["c", 2],482 ["y", 2]483 ])484 },485 {op: "remove", path: ["a", "b"]}486 ],487 [488 {op: "remove", path: ["a", "x"]},489 {490 op: "add",491 path: ["a", "b"],492 value: new Map([493 ["c", 1],494 ["d", 1]495 ])496 }497 ]498 )499 })500})501502describe("minimum amount of changes", () => {503 runPatchTest(504 {x: 3, y: {a: 4}, z: 3},505 d => {506 d.y.a = 4507 d.y.b = 5508 Object.assign(d, {x: 4, y: {a: 2}})509 },510 [511 {op: "replace", path: ["x"], value: 4},512 {op: "replace", path: ["y"], value: {a: 2}}513 ]514 )515})516517describe("arrays - prepend", () => {518 runPatchTest(519 {x: [1, 2, 3]},520 d => {521 d.x.unshift(4)522 },

Immer 7Michel Weststratebefore adoption467ea5d

lines 523–528why, one step back
523 [
524 {op: "replace", path: ["x", 0], value: 4},525 {op: "replace", path: ["x", 1], value: 1},526 {op: "replace", path: ["x", 2], value: 2},527 {op: "add", path: ["x", 3], value: 3}528 ]

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 529–537why, one step back
529 )
530})531532describe("arrays - multiple prepend", () => {533 runPatchTest(534 {x: [1, 2, 3]},535 d => {536 d.x.unshift(4)537 d.x.unshift(5)

Immer 7Michel Weststratebefore adoption467ea5d

line 538why, one step back
538 // 4,5,1,2,3

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 539–540why, one step back
539 },
540 [

Immer 7Michel Weststratebefore adoption467ea5d

lines 541–545why, one step back
541 {op: "replace", path: ["x", 0], value: 5},
542 {op: "replace", path: ["x", 1], value: 4},543 {op: "replace", path: ["x", 2], value: 1},544 {op: "add", path: ["x", 3], value: 2},545 {op: "add", path: ["x", 4], value: 3}

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 546–555why, one step back
546 ]
547 )548})549550describe("arrays - splice middle", () => {551 runPatchTest(552 {x: [1, 2, 3]},553 d => {554 d.x.splice(1, 1)555 },

Immer 7Michel Weststratebefore adoption467ea5d

lines 556–559why, one step back
556 [
557 {op: "replace", path: ["x", 1], value: 3},558 {op: "replace", path: ["x", "length"], value: 2}559 ]

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 560–567why, one step back
560 )
561})562563describe("arrays - multiple splice", () => {564 runPatchTest(565 [0, 1, 2, 3, 4, 5, 0],566 d => {567 d.splice(4, 2, 3)

Immer 7Michel Weststratebefore adoption467ea5d

line 568why, one step back
568 // [0,1,2,3,3,0]

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

line 569why, one step back
569 d.splice(1, 2, 3)

Immer 7Michel Weststratebefore adoption467ea5d

lines 570–571why, one step back
570 // [0,3,3,3,0]
571 expect(d.slice()).toEqual([0, 3, 3, 3, 0])

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 572–575why, one step back
572 },
573 [574 {op: "replace", path: [1], value: 3},575 {op: "replace", path: [2], value: 3},

Immer 7Michel Weststratebefore adoption467ea5d

lines 576–577why, one step back
576 {op: "replace", path: [4], value: 0},
577 {op: "replace", path: ["length"], value: 5}

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 578–587why, one step back
578 ]
579 )580})581582describe("arrays - modify and shrink", () => {583 runPatchTest(584 {x: [1, 2, 3]},585 d => {586 d.x[0] = 4587 d.x.length = 2

Immer 7Michel Weststratebefore adoption467ea5d

line 588why, one step back
588 // [0, 2]

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 589–591why, one step back
589 },
590 [591 {op: "replace", path: ["x", 0], value: 4},

Immer 7Michel Weststratebefore adoption467ea5d

line 592why, one step back
592 {op: "replace", path: ["x", "length"], value: 2}

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 593–606why, one step back
593 ],
594 [595 {op: "replace", path: ["x", 0], value: 1},596 {op: "add", path: ["x", 2], value: 3}597 ]598 )599})600601describe("arrays - prepend then splice middle", () => {602 runPatchTest(603 {x: [1, 2, 3]},604 d => {605 d.x.unshift(4)606 d.x.splice(2, 1)

Immer 7Michel Weststratebefore adoption467ea5d

line 607why, one step back
607 // 4, 1, 3

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 608–621why, one step back
608 },
609 [610 {op: "replace", path: ["x", 0], value: 4},611 {op: "replace", path: ["x", 1], value: 1}612 ]613 )614})615616describe("arrays - splice middle then prepend", () => {617 runPatchTest(618 {x: [1, 2, 3]},619 d => {620 d.x.splice(1, 1)621 d.x.unshift(4)

Immer 7Michel Weststratebefore adoption467ea5d

line 622why, one step back
622 // [4, 1, 3]

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 623–636why, one step back
623 },
624 [625 {op: "replace", path: ["x", 0], value: 4},626 {op: "replace", path: ["x", 1], value: 1}627 ]628 )629})630631describe("arrays - truncate", () => {632 runPatchTest(633 {x: [1, 2, 3]},634 d => {635 d.x.length -= 2636 },

Immer 7Michel Weststratebefore adoption467ea5d

line 637why, one step back
637 [{op: "replace", path: ["x", "length"], value: 1}],

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 638–651why, one step back
638 [
639 {op: "add", path: ["x", 1], value: 2},640 {op: "add", path: ["x", 2], value: 3}641 ]642 )643})644645describe("arrays - pop twice", () => {646 runPatchTest(647 {x: [1, 2, 3]},648 d => {649 d.x.pop()650 d.x.pop()651 },

Immer 7Michel Weststratebefore adoption467ea5d

line 652why, one step back
652 [{op: "replace", path: ["x", "length"], value: 1}]

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 653–656why, one step back
653 )
654})655656describe("arrays - push multiple", () => {

Immer 7Michel Weststratebefore adoption467ea5d

line 657why, one step back
657 // These patches were more optimal pre immer 7, but not always correct

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 658–666why, one step back
658 runPatchTest(
659 {x: [1, 2, 3]},660 d => {661 d.x.push(4, 5)662 },663 [664 {op: "add", path: ["x", 3], value: 4},665 {op: "add", path: ["x", 4], value: 5}666 ],

Immer 7Michel Weststratebefore adoption467ea5d

line 667why, one step back
667 [{op: "replace", path: ["x", "length"], value: 3}]

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 668–671why, one step back
668 )
669})670671describe("arrays - splice (expand)", () => {

Immer 7Michel Weststratebefore adoption467ea5d

line 672why, one step back
672 // These patches were more optimal pre immer 7, but not always correct

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 673–675why, one step back
673 runPatchTest(
674 {x: [1, 2, 3]},675 d => {

Immer 7Michel Weststratebefore adoption467ea5d

line 676why, one step back
676 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 677–679why, one step back
677 },
678 [679 {op: "replace", path: ["x", 1], value: 4},

Immer 7Michel Weststratebefore adoption467ea5d

lines 680–682why, one step back
680 {op: "replace", path: ["x", 2], value: 5},
681 {op: "add", path: ["x", 3], value: 6},682 {op: "add", path: ["x", 4], value: 3}

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 683–685why, one step back
683 ],
684 [685 {op: "replace", path: ["x", 1], value: 2},

Immer 7Michel Weststratebefore adoption467ea5d

lines 686–687why, one step back
686 {op: "replace", path: ["x", 2], value: 3},
687 {op: "replace", path: ["x", "length"], value: 3}

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 688–692why, one step back
688 ]
689 )690})691692describe("arrays - splice (shrink)", () => {

Immer 7Michel Weststratebefore adoption467ea5d

line 693why, one step back
693 // These patches were more optimal pre immer 7, but not always correct

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 694–696why, one step back
694 runPatchTest(
695 {x: [1, 2, 3, 4, 5]},696 d => {

Immer 7Michel Weststratebefore adoption467ea5d

line 697why, one step back
697 d.x.splice(1, 3, 6) // [1, 6, 5]

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 698–700why, one step back
698 },
699 [700 {op: "replace", path: ["x", 1], value: 6},

Immer 7Michel Weststratebefore adoption467ea5d

lines 701–702why, one step back
701 {op: "replace", path: ["x", 2], value: 5},
702 {op: "replace", path: ["x", "length"], value: 3}

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 703–705why, one step back
703 ],
704 [705 {op: "replace", path: ["x", 1], value: 2},

Immer 7Michel Weststratebefore adoption467ea5d

lines 706–708why, one step back
706 {op: "replace", path: ["x", 2], value: 3},
707 {op: "add", path: ["x", 3], value: 4},708 {op: "add", path: ["x", 4], value: 5}

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 709–727why, one step back
709 ]
710 )711})712713describe("arrays - delete", () => {714 runPatchTest(715 {716 x: [717 {a: 1, b: 2},718 {c: 3, d: 4}719 ]720 },721 d => {722 delete d.x[1].c723 },724 [{op: "remove", path: ["x", 1, "c"]}]725 )726})727

fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b

lines 728–743why, one step back
728describe("arrays - append", () => {
729 test("appends to array when last part of path is '-'", () => {730 const state = {731 list: [1, 2, 3]732 }733 const patch = {734 op: "add",735 value: 4,736 path: ["list", "-"]737 }738 expect(applyPatches(state, [patch])).toEqual({739 list: [1, 2, 3, 4]740 })741 })742})743

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 744–813why, one step back
744describe("sets - add - 1", () => {
745 runPatchTest(746 new Set([1]),747 d => {748 d.add(2)749 },750 [{op: "add", path: [1], value: 2}],751 [{op: "remove", path: [1], value: 2}]752 )753})754755describe("sets - add, delete, add - 1", () => {756 runPatchTest(757 new Set([1]),758 d => {759 d.add(2)760 d.delete(2)761 d.add(2)762 },763 [{op: "add", path: [1], value: 2}],764 [{op: "remove", path: [1], value: 2}]765 )766})767768describe("sets - add, delete, add - 2", () => {769 runPatchTest(770 new Set([2, 1]),771 d => {772 d.add(2)773 d.delete(2)774 d.add(2)775 },776 [],777 []778 )779})780781describe("sets - mutate - 1", () => {782 const findById = (set, id) => {783 for (const item of set) {784 if (item.id === id) return item785 }786 }787 runPatchTest(788 new Set([789 {id: 1, val: "We"},790 {id: 2, val: "will"}791 ]),792 d => {793 const obj1 = findById(d, 1)794 const obj2 = findById(d, 2)795 obj1.val = "rock"796 obj2.val = "you"797 },798 [799 {op: "remove", path: [0], value: {id: 1, val: "We"}},800 {op: "remove", path: [1], value: {id: 2, val: "will"}},801 {op: "add", path: [0], value: {id: 1, val: "rock"}},802 {op: "add", path: [1], value: {id: 2, val: "you"}}803 ],804 [805 {op: "remove", path: [1], value: {id: 2, val: "you"}},806 {op: "remove", path: [0], value: {id: 1, val: "rock"}},807 {op: "add", path: [1], value: {id: 2, val: "will"}},808 {op: "add", path: [0], value: {id: 1, val: "We"}}809 ]810 )811})812813describe("arrays - splice should should result in remove op.", () => {

Immer 7Michel Weststratebefore adoption467ea5d

line 814why, one step back
814 // These patches were more optimal pre immer 7, but not always correct

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 815–819why, one step back
815 runPatchTest(
816 [1, 2],817 d => {818 d.splice(1, 1)819 },

Immer 7Michel Weststratebefore adoption467ea5d

line 820why, one step back
820 [{op: "replace", path: ["length"], value: 1}],

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 821–825why, one step back
821 [{op: "add", path: [1], value: 2}]
822 )823})824825describe("arrays - NESTED splice should should result in remove op.", () => {

Immer 7Michel Weststratebefore adoption467ea5d

line 826why, one step back
826 // These patches were more optimal pre immer 7, but not always correct

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 827–831why, one step back
827 runPatchTest(
828 {a: {b: {c: [1, 2]}}},829 d => {830 d.a.b.c.splice(1, 1)831 },

Immer 7Michel Weststratebefore adoption467ea5d

line 832why, one step back
832 [{op: "replace", path: ["a", "b", "c", "length"], value: 1}],

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 833–1029why, one step back
833 [{op: "add", path: ["a", "b", "c", 1], value: 2}]
834 )835})836837describe("simple replacement", () => {838 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])839})840841describe("same value replacement - 1", () => {842 runPatchTest(843 {x: {y: 3}},844 d => {845 const a = d.x846 d.x = a847 },848 []849 )850})851852describe("same value replacement - 2", () => {853 runPatchTest(854 {x: {y: 3}},855 d => {856 const a = d.x857 d.x = 4858 d.x = a859 },860 []861 )862})863864describe("same value replacement - 3", () => {865 runPatchTest(866 {x: 3},867 d => {868 d.x = 3869 },870 []871 )872})873874describe("same value replacement - 4", () => {875 runPatchTest(876 {x: 3},877 d => {878 d.x = 4879 d.x = 3880 },881 []882 )883})884885describe("same value replacement - 5", () => {886 runPatchTest(887 new Map([["x", 3]]),888 d => {889 d.set("x", 4)890 d.set("x", 3)891 },892 [],893 []894 )895})896897describe("same value replacement - 6", () => {898 runPatchTest(899 new Set(["x", 3]),900 d => {901 d.delete("x")902 d.add("x")903 },904 [],905 []906 )907})908909describe("simple delete", () => {910 runPatchTest(911 {x: 2},912 d => {913 delete d.x914 },915 [916 {917 op: "remove",918 path: ["x"]919 }920 ]921 )922})923924describe("patch compressions yields correct results", () => {925 let p1, p2926 runPatchTest(927 {},928 d => {929 d.x = {test: true}930 },931 (p1 = [932 {933 op: "add",934 path: ["x"],935 value: {936 test: true937 }938 }939 ])940 )941 runPatchTest(942 {x: {test: true}},943 d => {944 delete d.x945 },946 (p2 = [947 {948 op: "remove",949 path: ["x"]950 }951 ])952 )953 const res = runPatchTest(954 {},955 d => {956 applyPatches(d, [...p1, ...p2])957 },958 []959 )960961 expect(res).toEqual({})962})963964describe("change then delete property", () => {965 const res = runPatchTest(966 {967 x: 1968 },969 d => {970 d.x = 2971 delete d.x972 },973 [974 {975 op: "remove",976 path: ["x"]977 }978 ]979 )980 test("valid result", () => {981 expect(res).toEqual({})982 })983})984985test("replaying patches with interweaved replacements should work correctly", () => {986 const patches = []987 const s0 = {x: 1}988989 const s1 = produce(990 s0,991 draft => {992 draft.x = 2993 },994 p => {995 patches.push(...p)996 }997 )998999 const s2 = produce(1000 s1,1001 draft => {1002 return {x: 0}1003 },1004 p => {1005 patches.push(...p)1006 }1007 )10081009 const s3 = produce(1010 s2,1011 draft => {1012 draft.x--1013 },1014 p => {1015 patches.push(...p)1016 }1017 )10181019 expect(s3).toEqual({x: -1}) // correct result1020 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay10211022 // manual replay on a draft should also be correct1023 expect(1024 produce(s0, draft => {1025 return applyPatches(draft, patches)1026 })1027 ).toEqual({x: -1})1028})1029

Immer 7Michel Weststratebefore adoption467ea5d

lines 1030–1037why, one step back
1030describe("#468", () => {
1031 function run() {1032 const item = {id: 1}1033 const state = [item]1034 const [nextState, patches] = produceWithPatches(state, draft => {1035 draft[0].id = 21036 draft[1] = item1037 })

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

line 1038why, one step back
1038

Immer 7Michel Weststratebefore adoption467ea5d

lines 1039–1053why, one step back
1039 expect(nextState).toEqual([{id: 2}, {id: 1}])
1040 expect(patches).toEqual([1041 {1042 op: "replace",1043 path: [0, "id"],1044 value: 21045 },1046 {1047 op: "add",1048 path: [1],1049 value: {1050 id: 11051 }1052 }1053 ])

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

line 1054why, one step back
1054

Immer 7Michel Weststratebefore adoption467ea5d

lines 1055–1061why, one step back
1055 const final = applyPatches(state, patches)
1056 expect(final).toEqual(nextState)1057 }10581059 test("es5", () => {1060 setUseProxies(false)1061 run()

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 1062–1063why, one step back
1062 })
1063

Immer 7Michel Weststratebefore adoption467ea5d

lines 1064–1067why, one step back
1064 test("proxy", () => {
1065 setUseProxies(true)1066 run()1067 })

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

lines 1068–1086why, one step back
1068})
10691070test("#521", () => {1071 const state = new Map()10721073 const [nextState, patches] = produceWithPatches(state, draft => {1074 draft.set("hello", new Set(["world"]))1075 })10761077 let patchedState = applyPatches(state, patches)1078 expect(patchedState).toEqual(nextState)10791080 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1081 draft.get("hello").add("immer")1082 })10831084 expect(applyPatches(nextState, patchesV2)).toEqual(1085 new Map([["hello", new Set(["world", "immer"])]])1086 )

fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6

lines 1087–1119why, one step back
1087})
10881089test("#559 patches works in a nested reducer with proxies", () => {1090 setUseProxies(true)10911092 const state = {1093 x: 1,1094 sub: {1095 y: [{a: 0}, {a: 1}]1096 }1097 }10981099 const changes = []1100 const inverseChanges = []11011102 const newState = produce(state, draft => {1103 draft.sub = produce(1104 draft.sub,1105 draft => {1106 draft.y.pop()1107 },1108 (patches, inversePatches) => {1109 expect(isDraft(inversePatches[0].value)).toBeFalsy()1110 expect(inversePatches[0].value).toMatchObject({a: 1})1111 changes.push(...patches)1112 inverseChanges.push(...inversePatches)1113 }1114 )1115 })11161117 const reversedSubState = applyPatches(newState.sub, inverseChanges)11181119 expect(reversedSubState).toMatchObject(state.sub)

Immer 7Michel Weststratebefore adoption467ea5d

lines 1120–1144why, one step back
1120})
11211122describe("#588", () => {1123 const reference = {value: {num: 53}}11241125 class Base {1126 [immerable] = true1127 get nested() {1128 return reference.value1129 }1130 set nested(value) {}1131 }11321133 let base = new Base()11341135 runPatchTest(1136 base,1137 vdraft => {1138 reference.value = vdraft1139 produce(base, bdraft => {1140 bdraft.nested.num = 421141 })1142 },1143 [{op: "add", path: ["num"], value: 42}]1144 )

chore: fixed Immer version numberMichel Weststratebefore adoptionb10dfa4

line 1145why, one step back
1145})

fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5

lines 1146–1157why, one step back
1146
1147test("#676 patching Date objects", () => {1148 class Test {1149 constructor() {1150 this.test = true1151 }1152 perform() {1153 return "tested!"1154 }1155 }11561157 const [nextState, patches] = produceWithPatches({}, function(draft) {

chore(tests): use UTC date string in tests to be timezone independent (#705)Grzegorz Rozdzialikbefore adoptione8fd805

line 1158why, one step back
1158 draft.date = new Date("2020-11-10T08:08:08.003Z")

fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5

lines 1159–1171why, one step back
1159 draft.test = new Test()
1160 })11611162 expect(nextState.date.toJSON()).toMatchInlineSnapshot(1163 `"2020-11-10T08:08:08.003Z"`1164 )1165 expect(nextState.test.perform()).toBe("tested!")11661167 const rebuilt = applyPatches({}, patches)1168 expect(rebuilt.date).toBeInstanceOf(Date)1169 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1170 `"2020-11-10T08:08:08.003Z"`1171 )

chore(tests): use UTC date string in tests to be timezone independent (#705)Grzegorz Rozdzialikbefore adoptione8fd805

line 1172why, one step back
1172 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 1173–1279why, one step back
1173})
11741175test("do not allow __proto__ polution - 738", () => {1176 const obj = {}11771178 // @ts-ignore1179 expect(obj.polluted).toBe(undefined)1180 expect(() => {1181 applyPatches({}, [1182 {op: "add", path: ["__proto__", "polluted"], value: "yes"}1183 ])1184 }).toThrow(1185 isProd1186 ? "24"1187 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1188 )1189 // @ts-ignore1190 expect(obj.polluted).toBe(undefined)1191})11921193test("do not allow __proto__ polution using arrays - 738", () => {1194 const obj = {}1195 const ar = []11961197 // @ts-ignore1198 expect(obj.polluted).toBe(undefined)1199 // @ts-ignore1200 expect(ar.polluted).toBe(undefined)1201 expect(() => {1202 applyPatches(1203 [],1204 [{op: "add", path: ["__proto__", "polluted"], value: "yes"}]1205 )1206 }).toThrow(1207 isProd1208 ? "24"1209 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1210 )1211 // @ts-ignore1212 expect(obj.polluted).toBe(undefined)1213 // @ts-ignore1214 expect(ar.polluted).toBe(undefined)1215})12161217test("do not allow prototype polution - 738", () => {1218 const obj = {}12191220 // @ts-ignore1221 expect(obj.polluted).toBe(undefined)1222 expect(() => {1223 applyPatches(Object, [1224 {op: "add", path: ["prototype", "polluted"], value: "yes"}1225 ])1226 }).toThrow(1227 isProd1228 ? "24"1229 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1230 )1231 // @ts-ignore1232 expect(obj.polluted).toBe(undefined)1233})12341235test("do not allow constructor polution - 738", () => {1236 const obj = {}12371238 // @ts-ignore1239 expect(obj.polluted).toBe(undefined)1240 const t = {}1241 applyPatches(t, [{op: "replace", path: ["constructor"], value: "yes"}])1242 expect(typeof t.constructor).toBe("function")1243 // @ts-ignore1244 expect(Object.polluted).toBe(undefined)1245})12461247test("do not allow constructor.prototype polution - 738", () => {1248 const obj = {}12491250 // @ts-ignore1251 expect(obj.polluted).toBe(undefined)1252 expect(() => {1253 applyPatches({}, [1254 {op: "add", path: ["constructor", "prototype", "polluted"], value: "yes"}1255 ])1256 }).toThrow(1257 isProd1258 ? "24"1259 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1260 )1261 // @ts-ignore1262 expect(Object.polluted).toBe(undefined)1263})12641265test("maps can store __proto__, prototype and constructor props", () => {1266 const obj = {}1267 const map = new Map()1268 map.set("__proto__", {})1269 map.set("constructor", {})1270 map.set("prototype", {})1271 const newMap = applyPatches(map, [1272 {op: "add", path: ["__proto__", "polluted"], value: "yes"},1273 {op: "add", path: ["constructor", "polluted"], value: "yes"},1274 {op: "add", path: ["prototype", "polluted"], value: "yes"}1275 ])1276 expect(newMap.get("__proto__").polluted).toBe("yes")1277 expect(newMap.get("constructor").polluted).toBe("yes")1278 expect(newMap.get("prototype").polluted).toBe("yes")1279 expect(obj.polluted).toBe(undefined)

fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5

line 1280why, one step back
1280})

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1281why, one step back
1281

fix(security): Follow up on CVE-2020-28477 where `path: [["__proto__"], "x"]` could still pollute the prototypeMichel Weststratebefore adoptionfa671e5

lines 1282–1299why, one step back
1282test("CVE-2020-28477 (https://snyk.io/vuln/SNYK-JS-IMMER-1019369) follow up", () => {
1283 const obj = {}12841285 // @ts-ignore1286 expect(obj.polluted).toBe(undefined)1287 expect(() => {1288 applyPatches({}, [1289 {op: "add", path: [["__proto__"], "polluted"], value: "yes"}1290 ])1291 }).toThrow(1292 isProd1293 ? "24"1294 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1295 )1296 // @ts-ignore1297 expect(obj.polluted).toBe(undefined)1298})1299

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1300–1318why, one step back
1300test("#648 assigning object to itself should not change patches", () => {
1301 const input = {1302 obj: {1303 value: 2001304 }1305 }13061307 const [nextState, patches] = produceWithPatches(input, draft => {1308 draft.obj.value = 11309 draft.obj = draft.obj1310 })13111312 expect(patches).toEqual([1313 {1314 op: "replace",1315 path: ["obj", "value"],1316 value: 11317 }1318 ])

fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f

lines 1319–1325why, one step back
1319})
13201321test("#791 patch for nothing is stored as undefined", () => {1322 const [newState, patches] = produceWithPatches({abc: 123}, draft => nothing)1323 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])13241325 expect(applyPatches({}, patches)).toEqual(undefined)

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1326why, one step back
1326})

fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (#878)Julian Scheidbefore adoptione140918

lines 1327–1347why, one step back
1327
1328test("#876 Ensure empty patch set for atomic set+delete on Map", () => {1329 {1330 const [newState, patches] = produceWithPatches(1331 new Map([["foo", "baz"]]),1332 draft => {1333 draft.set("foo", "bar")1334 draft.delete("foo")1335 }1336 )1337 expect(patches).toEqual([{op: "remove", path: ["foo"]}])1338 }13391340 {1341 const [newState, patches] = produceWithPatches(new Map(), draft => {1342 draft.set("foo", "bar")1343 draft.delete("foo")1344 })1345 expect(patches).toEqual([])1346 }1347})

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

lines 1348–1394why, one step back
1348
1349test("#888 patch to a primitive produces the primitive", () => {1350 {1351 const [res, patches] = produceWithPatches({abc: 123}, draft => nothing)1352 expect(res).toEqual(undefined)1353 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1354 }1355 {1356 const [res, patches] = produceWithPatches(null, draft => nothing)1357 expect(res).toEqual(undefined)1358 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1359 }1360 {1361 const [res, patches] = produceWithPatches(0, draft => nothing)1362 expect(res).toEqual(undefined)1363 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1364 }1365 {1366 const [res, patches] = produceWithPatches("foobar", draft => nothing)1367 expect(res).toEqual(undefined)1368 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1369 }1370 {1371 const [res, patches] = produceWithPatches([], draft => nothing)1372 expect(res).toEqual(undefined)1373 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1374 }1375 {1376 const [res, patches] = produceWithPatches(false, draft => nothing)1377 expect(res).toEqual(undefined)1378 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1379 }1380 {1381 const [res, patches] = produceWithPatches(1382 "foobar",1383 draft => "something else"1384 )1385 expect(res).toEqual("something else")1386 expect(patches).toEqual([1387 {op: "replace", path: [], value: "something else"}1388 ])1389 }1390 {1391 const [res, patches] = produceWithPatches(false, draft => true)1392 expect(res).toEqual(true)1393 expect(patches).toEqual([{op: "replace", path: [], value: true}])1394 }

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 1395–1418why, one step back
1395})
13961397describe("#879 delete item from array", () => {1398 runPatchTest(1399 [1, 2, 3],1400 draft => {1401 delete draft[1]1402 },1403 [{op: "replace", path: [1], value: undefined}],1404 [{op: "replace", path: [1], value: 2}],1405 [1, undefined, 3]1406 )1407})14081409describe("#879 delete item from array - 2", () => {1410 runPatchTest(1411 [1, 2, 3],1412 draft => {1413 delete draft[2]1414 },1415 [{op: "replace", path: [2], value: undefined}],1416 [{op: "replace", path: [2], value: 3}],1417 [1, 2, undefined]1418 )

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 1419why, one step back
1419})

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1420–1429why, one step back
1420
1421test("#897 appendPatch", () => {1422 const state0 = {a: []}1423 const state1 = applyPatches(state0, [{op: "add", path: ["a", "-"], value: 1}])1424 const state2 = applyPatches(state1, [{op: "add", path: ["a", "-"], value: 2}])1425 const state3 = applyPatches(state2, [{op: "add", path: ["a", "-"], value: 3}])1426 expect(state3).toEqual({1427 a: [1, 2, 3]1428 })1429})