patch.js
This file
Path
__tests__/patch.js
1,279 lines · 23.6 KiB · JavaScript
Last changed by
fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d
History of this fileTake it
The walk stopped after 200 commits: lines older than fa1f5e6 point at it.
fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
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
lines 7–8why, one step back7 isDraft,8 immerableMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 9–11why, one step back9} from "../src/immer"1011enableAllPlugins()fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 12–14why, one step back1213jest.setTimeout(1000)14fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 15–16why, one step back15const isProd = process.env.NODE_ENV === "production"16fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 17why, one step back17function runPatchTest(base, producer, patches, inversePathes) {fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 18–19why, one step back18 let resultProxies, resultEs519style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 20–26why, one step back20 function runPatchTestHelper() {21 let recordedPatches22 let recordedInversePatches23 const res = produce(base, producer, (p, i) => {24 recordedPatches = p25 recordedInversePatches = i26 })fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 27why, one step back27style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 28–31why, one step back28 test("produces the correct patches", () => {29 expect(recordedPatches).toEqual(patches)30 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)31 })fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 32why, one step back32style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 33–35why, one step back33 test("patches are replayable", () => {34 expect(applyPatches(base, recordedPatches)).toEqual(res)35 })fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 36why, one step back36style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 37–39why, one step back37 test("patches can be reversed", () => {38 expect(applyPatches(res, recordedInversePatches)).toEqual(base)39 })fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 40–41why, one step back4041 return resstyle: use tabsuse-tabsbefore adoptiona9c0fd0
line 42why, one step back42 }fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 43why, one step back43style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 44–45why, one step back44 describe(`proxy`, () => {45 setUseProxies(true)fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
line 46why, one step back46 resultProxies = runPatchTestHelper()style: use tabsuse-tabsbefore adoptiona9c0fd0
line 47why, one step back47 })fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 48why, one step back48style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 49–50why, one step back49 describe(`es5`, () => {50 setUseProxies(false)fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 51–54why, one step back51 resultEs5 = runPatchTestHelper()52 test("ES5 and Proxy implementation yield same result", () => {53 expect(resultEs5).toEqual(resultProxies)54 })style: use tabsuse-tabsbefore adoptiona9c0fd0
line 55why, one step back55 })fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 56–57why, one step back5657 return resultProxiesfix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 58–60why, one step back58}5960describe("applyPatches", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 61–88why, one step back61 it("mutates the base state when it is a draft", () => {62 produce({a: 1}, draft => {63 const result = applyPatches(draft, [64 {op: "replace", path: ["a"], value: 2}65 ])66 expect(result).toBe(draft)67 expect(draft.a).toBe(2)68 })69 })70 it("produces a copy of the base state when not a draft", () => {71 const base = {a: 1}72 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])73 expect(result).not.toBe(base)74 expect(result.a).toBe(2)75 expect(base.a).toBe(1)76 })77 it('throws when `op` is not "add", "replace", nor "remove"', () => {78 expect(() => {79 const patch = {op: "copy", from: [0], path: [1]}80 applyPatches([2], [patch])81 }).toThrowErrorMatchingSnapshot()82 })83 it("throws when `path` cannot be resolved", () => {84 // missing parent85 expect(() => {86 const patch = {op: "add", path: ["a", "b"], value: 1}87 applyPatches({}, [patch])88 }).toThrowErrorMatchingSnapshot()fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 89why, one step back89style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 90–94why, one step back90 // missing grand-parent91 expect(() => {92 const patch = {op: "add", path: ["a", "b", "c"], value: 1}93 applyPatches({}, [patch])94 }).toThrowErrorMatchingSnapshot()fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
lines 95–116why, one step back95 })96 it("applied patches cannot be modified", () => {97 // see also: https://github.com/immerjs/immer/issues/41198 const s0 = {99 items: [1]100 }101102 const [s1, p1] = produceWithPatches(s0, draft => {103 draft.items = []104 })105106 const replaceValueBefore = p1[0].value.slice()107108 const [s2, p2] = produceWithPatches(s1, draft => {109 draft.items.push(2)110 })111112 applyPatches(s0, [...p1, ...p2])113114 const replaceValueAfter = p1[0].value.slice()115116 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 117why, one step back117 })fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 118–120why, one step back118})119120describe("simple assignment - 1", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 121–127why, one step back121 runPatchTest(122 {x: 3},123 d => {124 d.x++125 },126 [{op: "replace", path: ["x"], value: 4}]127 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 128–130why, one step back128})129130describe("simple assignment - 2", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 131–137why, one step back131 runPatchTest(132 {x: {y: 4}},133 d => {134 d.x.y++135 },136 [{op: "replace", path: ["x", "y"], value: 5}]137 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 138–140why, one step back138})139140describe("simple assignment - 3", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 141–147why, one step back141 runPatchTest(142 {x: [{y: 4}]},143 d => {144 d.x[0].y++145 },146 [{op: "replace", path: ["x", 0, "y"], value: 5}]147 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 148–149why, one step back148})149feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 150–183why, one step back150describe("simple assignment - 4", () => {151 runPatchTest(152 new Map([["x", {y: 4}]]),153 d => {154 d.get("x").y++155 },156 [{op: "replace", path: ["x", "y"], value: 5}],157 [{op: "replace", path: ["x", "y"], value: 4}]158 )159})160161describe("simple assignment - 5", () => {162 runPatchTest(163 {x: new Map([["y", 4]])},164 d => {165 d.x.set("y", 5)166 },167 [{op: "replace", path: ["x", "y"], value: 5}],168 [{op: "replace", path: ["x", "y"], value: 4}]169 )170})171172describe("simple assignment - 6", () => {173 runPatchTest(174 new Map([["x", 1]]),175 d => {176 // Map.prototype.set should return the Map itself177 const res = d.set("x", 2)178 res.set("y", 3)179 },180 [181 {op: "replace", path: ["x"], value: 2},182 {op: "add", path: ["y"], value: 3}183 ],chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 184–187why, one step back184 [185 {op: "replace", path: ["x"], value: 1},186 {op: "remove", path: ["y"]}187 ]feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 188–210why, one step back188 )189})190191describe("simple assignment - 7", () => {192 const key1 = {prop: "val1"}193 const key2 = {prop: "val2"}194 runPatchTest(195 {x: new Map([[key1, 4]])},196 d => {197 d.x.set(key1, 5)198 d.x.set(key2, 6)199 },200 [201 {op: "replace", path: ["x", key1], value: 5},202 {op: "add", path: ["x", key2], value: 6}203 ],204 [205 {op: "replace", path: ["x", key1], value: 4},206 {op: "remove", path: ["x", key2]}207 ]208 )209})210fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 211why, one step back211describe("delete 1", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 212–218why, one step back212 runPatchTest(213 {x: {y: 4}},214 d => {215 delete d.x216 },217 [{op: "remove", path: ["x"]}]218 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 219–220why, one step back219})220feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 221–246why, one step back221describe("delete 2", () => {222 runPatchTest(223 new Map([["x", 1]]),224 d => {225 d.delete("x")226 },227 [{op: "remove", path: ["x"]}],228 [{op: "add", path: ["x"], value: 1}]229 )230})231232describe("delete 3", () => {233 runPatchTest(234 {x: new Map([["y", 1]])},235 d => {236 d.x.delete("y")237 },238 [{op: "remove", path: ["x", "y"]}],239 [{op: "add", path: ["x", "y"], value: 1}]240 )241})242243describe("delete 5", () => {244 const key1 = {prop: "val1"}245 const key2 = {prop: "val2"}246 runPatchTest(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 247–252why, one step back247 {248 x: new Map([249 [key1, 1],250 [key2, 2]251 ])252 },feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 253–257why, one step back253 d => {254 d.x.delete(key1)255 d.x.delete(key2)256 },257 [chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 258–261why, one step back258 {op: "remove", path: ["x", key1]},259 {op: "remove", path: ["x", key2]}260 ],261 [feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 262–289why, one step back262 {op: "add", path: ["x", key1], value: 1},263 {op: "add", path: ["x", key2], value: 2}264 ]265 )266})267268describe("delete 6", () => {269 runPatchTest(270 new Set(["x", 1]),271 d => {272 d.delete("x")273 },274 [{op: "remove", path: [0], value: "x"}],275 [{op: "add", path: [0], value: "x"}]276 )277})278279describe("delete 7", () => {280 runPatchTest(281 {x: new Set(["y", 1])},282 d => {283 d.x.delete("y")284 },285 [{op: "remove", path: ["x", 0], value: "y"}],286 [{op: "add", path: ["x", 0], value: "y"}]287 )288})289fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 290why, one step back290describe("renaming properties", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 291–297why, one step back291 describe("nested object (no changes)", () => {292 runPatchTest(293 {a: {b: 1}},294 d => {295 d.x = d.a296 delete d.a297 },chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 298–301why, one step back298 [299 {op: "add", path: ["x"], value: {b: 1}},300 {op: "remove", path: ["a"]}301 ]style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 302–303why, one step back302 )303 })fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 304why, one step back304Immer 7Michel Weststratebefore adoption467ea5d
lines 305–339why, one step back305 describe("nested change in object", () => {306 runPatchTest(307 {308 a: {b: 1}309 },310 d => {311 d.a.b++312 },313 [{op: "replace", path: ["a", "b"], value: 2}],314 [{op: "replace", path: ["a", "b"], value: 1}]315 )316 })317318 describe("nested change in map", () => {319 runPatchTest(320 new Map([["a", new Map([["b", 1]])]]),321 d => {322 d.get("a").set("b", 2)323 },324 [{op: "replace", path: ["a", "b"], value: 2}],325 [{op: "replace", path: ["a", "b"], value: 1}]326 )327 })328329 describe("nested change in array", () => {330 runPatchTest(331 [[{b: 1}]],332 d => {333 d[0][0].b++334 },335 [{op: "replace", path: [0, 0, "b"], value: 2}],336 [{op: "replace", path: [0, 0, "b"], value: 1}]337 )338 })339feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 340–357why, one step back340 describe("nested map (no changes)", () => {341 runPatchTest(342 new Map([["a", new Map([["b", 1]])]]),343 d => {344 d.set("x", d.get("a"))345 d.delete("a")346 },347 [348 {op: "add", path: ["x"], value: new Map([["b", 1]])},349 {op: "remove", path: ["a"]}350 ],351 [352 {op: "remove", path: ["x"]},353 {op: "add", path: ["a"], value: new Map([["b", 1]])}354 ]355 )356 })357style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 358–365why, one step back358 describe("nested object (with changes)", () => {359 runPatchTest(360 {a: {b: 1, c: 1}},361 d => {362 let a = d.a363 a.b = 2 // change364 delete a.c // delete365 a.y = 2 // addfix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 366why, one step back366style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 367–376why, one step back367 // rename368 d.x = a369 delete d.a370 },371 [372 {op: "add", path: ["x"], value: {b: 2, y: 2}},373 {op: "remove", path: ["a"]}374 ]375 )376 })fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 377why, one step back377feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 378–379why, one step back378 describe("nested map (with changes)", () => {379 runPatchTest(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 380–388why, one step back380 new Map([381 [382 "a",383 new Map([384 ["b", 1],385 ["c", 1]386 ])387 ]388 ]),feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 389–399why, one step back389 d => {390 let a = d.get("a")391 a.set("b", 2) // change392 a.delete("c") // delete393 a.set("y", 2) // add394395 // rename396 d.set("x", a)397 d.delete("a")398 },399 [chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 400–407why, one step back400 {401 op: "add",402 path: ["x"],403 value: new Map([404 ["b", 2],405 ["y", 2]406 ])407 },feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 408–411why, one step back408 {op: "remove", path: ["a"]}409 ],410 [411 {op: "remove", path: ["x"]},chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 412–419why, one step back412 {413 op: "add",414 path: ["a"],415 value: new Map([416 ["b", 1],417 ["c", 1]418 ])419 }feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 420–423why, one step back420 ]421 )422 })423style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 424–431why, one step back424 describe("deeply nested object (with changes)", () => {425 runPatchTest(426 {a: {b: {c: 1, d: 1}}},427 d => {428 let b = d.a.b429 b.c = 2 // change430 delete b.d // delete431 b.y = 2 // addfix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 432why, one step back432style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 433–442why, one step back433 // rename434 d.a.x = b435 delete d.a.b436 },437 [438 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},439 {op: "remove", path: ["a", "b"]}440 ]441 )442 })feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 443–445why, one step back443444 describe("deeply nested map (with changes)", () => {445 runPatchTest(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 446–459why, one step back446 new Map([447 [448 "a",449 new Map([450 [451 "b",452 new Map([453 ["c", 1],454 ["d", 1]455 ])456 ]457 ])458 ]459 ]),feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 460–473why, one step back460 d => {461 let b = d.get("a").get("b")462 b.set("c", 2) // change463 b.delete("d") // delete464 b.set("y", 2) // add465466 // rename467 d.get("a").set("x", b)468 d.get("a").delete("b")469 },470 [471 {472 op: "add",473 path: ["a", "x"],chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 474–477why, one step back474 value: new Map([475 ["c", 2],476 ["y", 2]477 ])feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 478–485why, one step back478 },479 {op: "remove", path: ["a", "b"]}480 ],481 [482 {op: "remove", path: ["a", "x"]},483 {484 op: "add",485 path: ["a", "b"],chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 486–489why, one step back486 value: new Map([487 ["c", 1],488 ["d", 1]489 ])feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 490–493why, one step back490 }491 ]492 )493 })fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 494–496why, one step back494})495496describe("minimum amount of changes", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 497–508why, one step back497 runPatchTest(498 {x: 3, y: {a: 4}, z: 3},499 d => {500 d.y.a = 4501 d.y.b = 5502 Object.assign(d, {x: 4, y: {a: 2}})503 },504 [505 {op: "replace", path: ["x"], value: 4},506 {op: "replace", path: ["y"], value: {a: 2}}507 ]508 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 509–511why, one step back509})510511describe("arrays - prepend", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 512–516why, one step back512 runPatchTest(513 {x: [1, 2, 3]},514 d => {515 d.x.unshift(4)516 },Immer 7Michel Weststratebefore adoption467ea5d
lines 517–522why, one step back517 [518 {op: "replace", path: ["x", 0], value: 4},519 {op: "replace", path: ["x", 1], value: 1},520 {op: "replace", path: ["x", 2], value: 2},521 {op: "add", path: ["x", 3], value: 3}522 ]style: use tabsuse-tabsbefore adoptiona9c0fd0
line 523why, one step back523 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 524–526why, one step back524})525526describe("arrays - multiple prepend", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 527–531why, one step back527 runPatchTest(528 {x: [1, 2, 3]},529 d => {530 d.x.unshift(4)531 d.x.unshift(5)Immer 7Michel Weststratebefore adoption467ea5d
line 532why, one step back532 // 4,5,1,2,3style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 533–534why, one step back533 },534 [Immer 7Michel Weststratebefore adoption467ea5d
lines 535–539why, one step back535 {op: "replace", path: ["x", 0], value: 5},536 {op: "replace", path: ["x", 1], value: 4},537 {op: "replace", path: ["x", 2], value: 1},538 {op: "add", path: ["x", 3], value: 2},539 {op: "add", path: ["x", 4], value: 3}style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 540–541why, one step back540 ]541 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 542–544why, one step back542})543544describe("arrays - splice middle", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 545–549why, one step back545 runPatchTest(546 {x: [1, 2, 3]},547 d => {548 d.x.splice(1, 1)549 },Immer 7Michel Weststratebefore adoption467ea5d
lines 550–553why, one step back550 [551 {op: "replace", path: ["x", 1], value: 3},552 {op: "replace", path: ["x", "length"], value: 2}553 ]style: use tabsuse-tabsbefore adoptiona9c0fd0
line 554why, one step back554 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 555–557why, one step back555})556557describe("arrays - multiple splice", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 558–561why, one step back558 runPatchTest(559 [0, 1, 2, 3, 4, 5, 0],560 d => {561 d.splice(4, 2, 3)Immer 7Michel Weststratebefore adoption467ea5d
line 562why, one step back562 // [0,1,2,3,3,0]style: use tabsuse-tabsbefore adoptiona9c0fd0
line 563why, one step back563 d.splice(1, 2, 3)Immer 7Michel Weststratebefore adoption467ea5d
lines 564–565why, one step back564 // [0,3,3,3,0]565 expect(d.slice()).toEqual([0, 3, 3, 3, 0])style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 566–569why, one step back566 },567 [568 {op: "replace", path: [1], value: 3},569 {op: "replace", path: [2], value: 3},Immer 7Michel Weststratebefore adoption467ea5d
lines 570–571why, one step back570 {op: "replace", path: [4], value: 0},571 {op: "replace", path: ["length"], value: 5}style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 572–573why, one step back572 ]573 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 574–576why, one step back574})575576describe("arrays - modify and shrink", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 577–581why, one step back577 runPatchTest(578 {x: [1, 2, 3]},579 d => {580 d.x[0] = 4581 d.x.length = 2Immer 7Michel Weststratebefore adoption467ea5d
line 582why, one step back582 // [0, 2]style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 583–584why, one step back583 },584 [chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
line 585why, one step back585 {op: "replace", path: ["x", 0], value: 4},Immer 7Michel Weststratebefore adoption467ea5d
line 586why, one step back586 {op: "replace", path: ["x", "length"], value: 2}chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 587–588why, one step back587 ],588 [Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 589–590why, one step back589 {op: "replace", path: ["x", 0], value: 1},590 {op: "add", path: ["x", 2], value: 3}style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 591–592why, one step back591 ]592 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 593–595why, one step back593})594595describe("arrays - prepend then splice middle", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 596–600why, one step back596 runPatchTest(597 {x: [1, 2, 3]},598 d => {599 d.x.unshift(4)600 d.x.splice(2, 1)Immer 7Michel Weststratebefore adoption467ea5d
line 601why, one step back601 // 4, 1, 3style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 602–607why, one step back602 },603 [604 {op: "replace", path: ["x", 0], value: 4},605 {op: "replace", path: ["x", 1], value: 1}606 ]607 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 608–610why, one step back608})609610describe("arrays - splice middle then prepend", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 611–615why, one step back611 runPatchTest(612 {x: [1, 2, 3]},613 d => {614 d.x.splice(1, 1)615 d.x.unshift(4)Immer 7Michel Weststratebefore adoption467ea5d
line 616why, one step back616 // [4, 1, 3]style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 617–622why, one step back617 },618 [619 {op: "replace", path: ["x", 0], value: 4},620 {op: "replace", path: ["x", 1], value: 1}621 ]622 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 623–625why, one step back623})624625describe("arrays - truncate", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 626–630why, one step back626 runPatchTest(627 {x: [1, 2, 3]},628 d => {629 d.x.length -= 2630 },Immer 7Michel Weststratebefore adoption467ea5d
line 631why, one step back631 [{op: "replace", path: ["x", "length"], value: 1}],style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 632–636why, one step back632 [633 {op: "add", path: ["x", 1], value: 2},634 {op: "add", path: ["x", 2], value: 3}635 ]636 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 637–639why, one step back637})638639describe("arrays - pop twice", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 640–645why, one step back640 runPatchTest(641 {x: [1, 2, 3]},642 d => {643 d.x.pop()644 d.x.pop()645 },Immer 7Michel Weststratebefore adoption467ea5d
line 646why, one step back646 [{op: "replace", path: ["x", "length"], value: 1}]style: use tabsuse-tabsbefore adoptiona9c0fd0
line 647why, one step back647 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 648–650why, one step back648})649650describe("arrays - push multiple", () => {Immer 7Michel Weststratebefore adoption467ea5d
line 651why, one step back651 // These patches were more optimal pre immer 7, but not always correctstyle: use tabsuse-tabsbefore adoptiona9c0fd0
lines 652–660why, one step back652 runPatchTest(653 {x: [1, 2, 3]},654 d => {655 d.x.push(4, 5)656 },657 [658 {op: "add", path: ["x", 3], value: 4},659 {op: "add", path: ["x", 4], value: 5}660 ],Immer 7Michel Weststratebefore adoption467ea5d
line 661why, one step back661 [{op: "replace", path: ["x", "length"], value: 3}]style: use tabsuse-tabsbefore adoptiona9c0fd0
line 662why, one step back662 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 663–665why, one step back663})664665describe("arrays - splice (expand)", () => {Immer 7Michel Weststratebefore adoption467ea5d
line 666why, one step back666 // These patches were more optimal pre immer 7, but not always correctstyle: use tabsuse-tabsbefore adoptiona9c0fd0
lines 667–669why, one step back667 runPatchTest(668 {x: [1, 2, 3]},669 d => {Immer 7Michel Weststratebefore adoption467ea5d
line 670why, one step back670 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 671–673why, one step back671 },672 [673 {op: "replace", path: ["x", 1], value: 4},Immer 7Michel Weststratebefore adoption467ea5d
lines 674–676why, one step back674 {op: "replace", path: ["x", 2], value: 5},675 {op: "add", path: ["x", 3], value: 6},676 {op: "add", path: ["x", 4], value: 3}style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 677–679why, one step back677 ],678 [679 {op: "replace", path: ["x", 1], value: 2},Immer 7Michel Weststratebefore adoption467ea5d
lines 680–681why, one step back680 {op: "replace", path: ["x", 2], value: 3},681 {op: "replace", path: ["x", "length"], value: 3}style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 682–683why, one step back682 ]683 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 684–686why, one step back684})685686describe("arrays - splice (shrink)", () => {Immer 7Michel Weststratebefore adoption467ea5d
line 687why, one step back687 // These patches were more optimal pre immer 7, but not always correctstyle: use tabsuse-tabsbefore adoptiona9c0fd0
lines 688–690why, one step back688 runPatchTest(689 {x: [1, 2, 3, 4, 5]},690 d => {Immer 7Michel Weststratebefore adoption467ea5d
line 691why, one step back691 d.x.splice(1, 3, 6) // [1, 6, 5]style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 692–694why, one step back692 },693 [694 {op: "replace", path: ["x", 1], value: 6},Immer 7Michel Weststratebefore adoption467ea5d
lines 695–696why, one step back695 {op: "replace", path: ["x", 2], value: 5},696 {op: "replace", path: ["x", "length"], value: 3}style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 697–699why, one step back697 ],698 [699 {op: "replace", path: ["x", 1], value: 2},Immer 7Michel Weststratebefore adoption467ea5d
lines 700–702why, one step back700 {op: "replace", path: ["x", 2], value: 3},701 {op: "add", path: ["x", 3], value: 4},702 {op: "add", path: ["x", 4], value: 5}style: use tabsuse-tabsbefore adoptiona9c0fd0
line 703why, one step back703 ]Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 704–706why, one step back704 )705})706fix: incorrect patches generated for array if property was deleted insideMichel Weststratebefore adoption1e451a3
lines 707–721why, one step back707describe("arrays - delete", () => {708 runPatchTest(709 {710 x: [711 {a: 1, b: 2},712 {c: 3, d: 4}713 ]714 },715 d => {716 delete d.x[1].c717 },718 [{op: "remove", path: ["x", 1, "c"]}]719 )720})721feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 722–765why, one step back722describe("sets - add - 1", () => {723 runPatchTest(724 new Set([1]),725 d => {726 d.add(2)727 },728 [{op: "add", path: [1], value: 2}],729 [{op: "remove", path: [1], value: 2}]730 )731})732733describe("sets - add, delete, add - 1", () => {734 runPatchTest(735 new Set([1]),736 d => {737 d.add(2)738 d.delete(2)739 d.add(2)740 },741 [{op: "add", path: [1], value: 2}],742 [{op: "remove", path: [1], value: 2}]743 )744})745746describe("sets - add, delete, add - 2", () => {747 runPatchTest(748 new Set([2, 1]),749 d => {750 d.add(2)751 d.delete(2)752 d.add(2)753 },754 [],755 []756 )757})758759describe("sets - mutate - 1", () => {760 const findById = (set, id) => {761 for (const item of set) {762 if (item.id === id) return item763 }764 }765 runPatchTest(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 766–769why, one step back766 new Set([767 {id: 1, val: "We"},768 {id: 2, val: "will"}769 ]),feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 770–790why, one step back770 d => {771 const obj1 = findById(d, 1)772 const obj2 = findById(d, 2)773 obj1.val = "rock"774 obj2.val = "you"775 },776 [777 {op: "remove", path: [0], value: {id: 1, val: "We"}},778 {op: "remove", path: [1], value: {id: 2, val: "will"}},779 {op: "add", path: [0], value: {id: 1, val: "rock"}},780 {op: "add", path: [1], value: {id: 2, val: "you"}}781 ],782 [783 {op: "remove", path: [1], value: {id: 2, val: "you"}},784 {op: "remove", path: [0], value: {id: 1, val: "rock"}},785 {op: "add", path: [1], value: {id: 2, val: "will"}},786 {op: "add", path: [0], value: {id: 1, val: "We"}}787 ]788 )789})790Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
line 791why, one step back791describe("arrays - splice should should result in remove op.", () => {Immer 7Michel Weststratebefore adoption467ea5d
line 792why, one step back792 // 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 793–797why, one step back793 runPatchTest(794 [1, 2],795 d => {796 d.splice(1, 1)797 },Immer 7Michel Weststratebefore adoption467ea5d
line 798why, one step back798 [{op: "replace", path: ["length"], value: 1}],Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 799–803why, one step back799 [{op: "add", path: [1], value: 2}]800 )801})802803describe("arrays - NESTED splice should should result in remove op.", () => {Immer 7Michel Weststratebefore adoption467ea5d
line 804why, one step back804 // 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 805–809why, one step back805 runPatchTest(806 {a: {b: {c: [1, 2]}}},807 d => {808 d.a.b.c.splice(1, 1)809 },Immer 7Michel Weststratebefore adoption467ea5d
line 810why, one step back810 [{op: "replace", path: ["a", "b", "c", "length"], value: 1}],Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
line 811why, one step back811 [{op: "add", path: ["a", "b", "c", 1], value: 2}]style: use tabsuse-tabsbefore adoptiona9c0fd0
line 812why, one step back812 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 813–815why, one step back813})814815describe("simple replacement", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 816why, one step back816 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 817–819why, one step back817})818819describe("same value replacement - 1", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 820–827why, one step back820 runPatchTest(821 {x: {y: 3}},822 d => {823 const a = d.x824 d.x = a825 },826 []827 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 828–830why, one step back828})829830describe("same value replacement - 2", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 831–839why, one step back831 runPatchTest(832 {x: {y: 3}},833 d => {834 const a = d.x835 d.x = 4836 d.x = a837 },838 []839 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 840–842why, one step back840})841842describe("same value replacement - 3", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 843–849why, one step back843 runPatchTest(844 {x: 3},845 d => {846 d.x = 3847 },848 []849 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 850–852why, one step back850})851852describe("same value replacement - 4", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 853–858why, one step back853 runPatchTest(854 {x: 3},855 d => {856 d.x = 4857 d.x = 3858 },feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 859–882why, one step back859 []860 )861})862863describe("same value replacement - 5", () => {864 runPatchTest(865 new Map([["x", 3]]),866 d => {867 d.set("x", 4)868 d.set("x", 3)869 },870 [],871 []872 )873})874875describe("same value replacement - 6", () => {876 runPatchTest(877 new Set(["x", 3]),878 d => {879 d.delete("x")880 d.add("x")881 },882 [],style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 883–884why, one step back883 []884 )fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
lines 885–887why, one step back885})886887describe("simple delete", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 888–899why, one step back888 runPatchTest(889 {x: 2},890 d => {891 delete d.x892 },893 [894 {895 op: "remove",896 path: ["x"]897 }898 ]899 )fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 900–960why, one step back900})901902describe("patch compressions yields correct results", () => {903 let p1, p2904 runPatchTest(905 {},906 d => {907 d.x = {test: true}908 },909 (p1 = [910 {911 op: "add",912 path: ["x"],913 value: {914 test: true915 }916 }917 ])918 )919 runPatchTest(920 {x: {test: true}},921 d => {922 delete d.x923 },924 (p2 = [925 {926 op: "remove",927 path: ["x"]928 }929 ])930 )931 const res = runPatchTest(932 {},933 d => {934 applyPatches(d, [...p1, ...p2])935 },936 []937 )938939 expect(res).toEqual({})940})941942describe("change then delete property", () => {943 const res = runPatchTest(944 {945 x: 1946 },947 d => {948 d.x = 2949 delete d.x950 },951 [952 {953 op: "remove",954 path: ["x"]955 }956 ]957 )958 test("valid result", () => {959 expect(res).toEqual({})960 })fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
lines 961–1005why, one step back961})962963test("replaying patches with interweaved replacements should work correctly", () => {964 const patches = []965 const s0 = {x: 1}966967 const s1 = produce(968 s0,969 draft => {970 draft.x = 2971 },972 p => {973 patches.push(...p)974 }975 )976977 const s2 = produce(978 s1,979 draft => {980 return {x: 0}981 },982 p => {983 patches.push(...p)984 }985 )986987 const s3 = produce(988 s2,989 draft => {990 draft.x--991 },992 p => {993 patches.push(...p)994 }995 )996997 expect(s3).toEqual({x: -1}) // correct result998 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay9991000 // manual replay on a draft should also be correct1001 expect(1002 produce(s0, draft => {1003 return applyPatches(draft, patches)1004 })1005 ).toEqual({x: -1})chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 1006–1007why, one step back1006})1007Immer 7Michel Weststratebefore adoption467ea5d
lines 1008–1015why, one step back1008describe("#468", () => {1009 function run() {1010 const item = {id: 1}1011 const state = [item]1012 const [nextState, patches] = produceWithPatches(state, draft => {1013 draft[0].id = 21014 draft[1] = item1015 })chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
line 1016why, one step back1016Immer 7Michel Weststratebefore adoption467ea5d
lines 1017–1031why, one step back1017 expect(nextState).toEqual([{id: 2}, {id: 1}])1018 expect(patches).toEqual([1019 {1020 op: "replace",1021 path: [0, "id"],1022 value: 21023 },1024 {1025 op: "add",1026 path: [1],1027 value: {1028 id: 11029 }1030 }1031 ])chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
line 1032why, one step back1032Immer 7Michel Weststratebefore adoption467ea5d
lines 1033–1039why, one step back1033 const final = applyPatches(state, patches)1034 expect(final).toEqual(nextState)1035 }10361037 test("es5", () => {1038 setUseProxies(false)1039 run()chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 1040–1041why, one step back1040 })1041Immer 7Michel Weststratebefore adoption467ea5d
lines 1042–1045why, one step back1042 test("proxy", () => {1043 setUseProxies(true)1044 run()1045 })fix(build): add unpkg and jsdelivr fields to package.json (#384)Tom MacWrightbefore adoptionfa1f5e6
line 1046why, one step back1046})fix: `Set` inside patches were not correctly cloned. Fixes #521Michel Weststratebefore adoption9d8d995
lines 1047–1064why, one step back10471048test("#521", () => {1049 const state = new Map()10501051 const [nextState, patches] = produceWithPatches(state, draft => {1052 draft.set("hello", new Set(["world"]))1053 })10541055 let patchedState = applyPatches(state, patches)1056 expect(patchedState).toEqual(nextState)10571058 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1059 draft.get("hello").add("immer")1060 })10611062 expect(applyPatches(nextState, patchesV2)).toEqual(1063 new Map([["hello", new Set(["world", "immer"])]])1064 )fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 1065–1097why, one step back1065})10661067test("#559 patches works in a nested reducer with proxies", () => {1068 setUseProxies(true)10691070 const state = {1071 x: 1,1072 sub: {1073 y: [{a: 0}, {a: 1}]1074 }1075 }10761077 const changes = []1078 const inverseChanges = []10791080 const newState = produce(state, draft => {1081 draft.sub = produce(1082 draft.sub,1083 draft => {1084 draft.y.pop()1085 },1086 (patches, inversePatches) => {1087 expect(isDraft(inversePatches[0].value)).toBeFalsy()1088 expect(inversePatches[0].value).toMatchObject({a: 1})1089 changes.push(...patches)1090 inverseChanges.push(...inversePatches)1091 }1092 )1093 })10941095 const reversedSubState = applyPatches(newState.sub, inverseChanges)10961097 expect(reversedSubState).toMatchObject(state.sub)Immer 7Michel Weststratebefore adoption467ea5d
lines 1098–1122why, one step back1098})10991100describe("#588", () => {1101 const reference = {value: {num: 53}}11021103 class Base {1104 [immerable] = true1105 get nested() {1106 return reference.value1107 }1108 set nested(value) {}1109 }11101111 let base = new Base()11121113 runPatchTest(1114 base,1115 vdraft => {1116 reference.value = vdraft1117 produce(base, bdraft => {1118 bdraft.nested.num = 421119 })1120 },1121 [{op: "add", path: ["num"], value: 42}]1122 )fix: `Set` inside patches were not correctly cloned. Fixes #521Michel Weststratebefore adoption9d8d995
line 1123why, one step back1123})fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 1124–1135why, one step back11241125test("#676 patching Date objects", () => {1126 class Test {1127 constructor() {1128 this.test = true1129 }1130 perform() {1131 return "tested!"1132 }1133 }11341135 const [nextState, patches] = produceWithPatches({}, function(draft) {chore(tests): use UTC date string in tests to be timezone independent (#705)Grzegorz Rozdzialikbefore adoptione8fd805
line 1136why, one step back1136 draft.date = new Date("2020-11-10T08:08:08.003Z")fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
lines 1137–1149why, one step back1137 draft.test = new Test()1138 })11391140 expect(nextState.date.toJSON()).toMatchInlineSnapshot(1141 `"2020-11-10T08:08:08.003Z"`1142 )1143 expect(nextState.test.perform()).toBe("tested!")11441145 const rebuilt = applyPatches({}, patches)1146 expect(rebuilt.date).toBeInstanceOf(Date)1147 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1148 `"2020-11-10T08:08:08.003Z"`1149 )chore(tests): use UTC date string in tests to be timezone independent (#705)Grzegorz Rozdzialikbefore adoptione8fd805
line 1150why, one step back1150 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 1151–1257why, one step back1151})11521153test("do not allow __proto__ polution - 738", () => {1154 const obj = {}11551156 // @ts-ignore1157 expect(obj.polluted).toBe(undefined)1158 expect(() => {1159 applyPatches({}, [1160 {op: "add", path: ["__proto__", "polluted"], value: "yes"}1161 ])1162 }).toThrow(1163 isProd1164 ? "24"1165 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1166 )1167 // @ts-ignore1168 expect(obj.polluted).toBe(undefined)1169})11701171test("do not allow __proto__ polution using arrays - 738", () => {1172 const obj = {}1173 const ar = []11741175 // @ts-ignore1176 expect(obj.polluted).toBe(undefined)1177 // @ts-ignore1178 expect(ar.polluted).toBe(undefined)1179 expect(() => {1180 applyPatches(1181 [],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 // @ts-ignore1192 expect(ar.polluted).toBe(undefined)1193})11941195test("do not allow prototype polution - 738", () => {1196 const obj = {}11971198 // @ts-ignore1199 expect(obj.polluted).toBe(undefined)1200 expect(() => {1201 applyPatches(Object, [1202 {op: "add", path: ["prototype", "polluted"], value: "yes"}1203 ])1204 }).toThrow(1205 isProd1206 ? "24"1207 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1208 )1209 // @ts-ignore1210 expect(obj.polluted).toBe(undefined)1211})12121213test("do not allow constructor polution - 738", () => {1214 const obj = {}12151216 // @ts-ignore1217 expect(obj.polluted).toBe(undefined)1218 const t = {}1219 applyPatches(t, [{op: "replace", path: ["constructor"], value: "yes"}])1220 expect(typeof t.constructor).toBe("function")1221 // @ts-ignore1222 expect(Object.polluted).toBe(undefined)1223})12241225test("do not allow constructor.prototype polution - 738", () => {1226 const obj = {}12271228 // @ts-ignore1229 expect(obj.polluted).toBe(undefined)1230 expect(() => {1231 applyPatches({}, [1232 {op: "add", path: ["constructor", "prototype", "polluted"], value: "yes"}1233 ])1234 }).toThrow(1235 isProd1236 ? "24"1237 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1238 )1239 // @ts-ignore1240 expect(Object.polluted).toBe(undefined)1241})12421243test("maps can store __proto__, prototype and constructor props", () => {1244 const obj = {}1245 const map = new Map()1246 map.set("__proto__", {})1247 map.set("constructor", {})1248 map.set("prototype", {})1249 const newMap = applyPatches(map, [1250 {op: "add", path: ["__proto__", "polluted"], value: "yes"},1251 {op: "add", path: ["constructor", "polluted"], value: "yes"},1252 {op: "add", path: ["prototype", "polluted"], value: "yes"}1253 ])1254 expect(newMap.get("__proto__").polluted).toBe("yes")1255 expect(newMap.get("constructor").polluted).toBe("yes")1256 expect(newMap.get("prototype").polluted).toBe("yes")1257 expect(obj.polluted).toBe(undefined)fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
line 1258why, one step back1258})fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d
lines 1259–1279why, one step back12591260test("#648 assigning object to itself should not change patches", () => {1261 const input = {1262 obj: {1263 value: 2001264 }1265 }12661267 const [nextState, patches] = produceWithPatches(input, draft => {1268 draft.obj.value = 11269 draft.obj = draft.obj1270 })12711272 expect(patches).toEqual([1273 {1274 op: "replace",1275 path: ["obj", "value"],1276 value: 11277 }1278 ])1279})