patch.js
This file
Path
__tests__/patch.js
863 lines · 15.5 KiB · JavaScript
Last changed by
Merge branch 'master' into v4Michel Weststratebefore adoption741e941
History of this fileTake it
The walk stopped after 200 commits: lines older than 53e9ce8 point at it.
perf: proxy assignaleclarsonbefore adoption53e9ce8
line 1why, one step back1"use strict"Merge branch 'master' into v4Michel Weststratebefore adoption741e941
lines 2–6why, one step back2import produce, {3 setUseProxies,4 applyPatches,5 produceWithPatches6} from "../src/index"perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 7–9why, one step back78jest.setTimeout(1000)9Merge pull request #437 from runnez/es5-map-and-set-supportMichel Weststratebefore adoption85656cb
line 10why, one step back10function runPatchTest(base, producer, patches, inversePathes) {Merge branch 'master' into v4Michel Weststratebefore adoption741e941
lines 11–12why, one step back11 let resultProxies, resultEs512run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 13–32why, one step back13 function runPatchTestHelper() {14 let recordedPatches15 let recordedInversePatches16 const res = produce(base, producer, (p, i) => {17 recordedPatches = p18 recordedInversePatches = i19 })2021 test("produces the correct patches", () => {22 expect(recordedPatches).toEqual(patches)23 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)24 })2526 test("patches are replayable", () => {27 expect(applyPatches(base, recordedPatches)).toEqual(res)28 })2930 test("patches can be reversed", () => {31 expect(applyPatches(res, recordedInversePatches)).toEqual(base)32 })Merge branch 'master' into v4Michel Weststratebefore adoption741e941
lines 33–34why, one step back3334 return resrun prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 35–38why, one step back35 }3637 describe(`proxy`, () => {38 setUseProxies(true)Merge branch 'master' into v4Michel Weststratebefore adoption741e941
line 39why, one step back39 resultProxies = runPatchTestHelper()run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 40–43why, one step back40 })4142 describe(`es5`, () => {43 setUseProxies(false)Merge branch 'master' into v4Michel Weststratebefore adoption741e941
lines 44–47why, one step back44 resultEs5 = runPatchTestHelper()45 test("ES5 and Proxy implementation yield same result", () => {46 expect(resultEs5).toEqual(resultProxies)47 })run prettier with master config before mergingMichel Weststratebefore adoptionae80361
line 48why, one step back48 })Merge branch 'master' into v4Michel Weststratebefore adoption741e941
lines 49–50why, one step back4950 return resultProxiesperf: proxy assignaleclarsonbefore adoption53e9ce8
line 51why, one step back51}run prettier with master config before mergingMichel Weststratebefore adoptionae80361
line 52why, one step back52perf: proxy assignaleclarsonbefore adoption53e9ce8
line 53why, one step back53describe("applyPatches", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 54–88why, one step back54 it("mutates the base state when it is a draft", () => {55 produce({a: 1}, draft => {56 const result = applyPatches(draft, [57 {op: "replace", path: ["a"], value: 2}58 ])59 expect(result).toBe(draft)60 expect(draft.a).toBe(2)61 })62 })63 it("produces a copy of the base state when not a draft", () => {64 const base = {a: 1}65 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])66 expect(result).not.toBe(base)67 expect(result.a).toBe(2)68 expect(base.a).toBe(1)69 })70 it('throws when `op` is not "add", "replace", nor "remove"', () => {71 expect(() => {72 const patch = {op: "copy", from: [0], path: [1]}73 applyPatches([2], [patch])74 }).toThrowErrorMatchingSnapshot()75 })76 it("throws when `path` cannot be resolved", () => {77 // missing parent78 expect(() => {79 const patch = {op: "add", path: ["a", "b"], value: 1}80 applyPatches({}, [patch])81 }).toThrowErrorMatchingSnapshot()8283 // missing grand-parent84 expect(() => {85 const patch = {op: "add", path: ["a", "b", "c"], value: 1}86 applyPatches({}, [patch])87 }).toThrowErrorMatchingSnapshot()88 })Merge branch 'master' into v4Michel Weststratebefore adoption741e941
lines 89–110why, one step back89 it("applied patches cannot be modified", () => {90 // see also: https://github.com/immerjs/immer/issues/41191 const s0 = {92 items: [1]93 }9495 const [s1, p1] = produceWithPatches(s0, draft => {96 draft.items = []97 })9899 const replaceValueBefore = p1[0].value.slice()100101 const [s2, p2] = produceWithPatches(s1, draft => {102 draft.items.push(2)103 })104105 applyPatches(s0, [...p1, ...p2])106107 const replaceValueAfter = p1[0].value.slice()108109 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)110 })run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 111–112why, one step back111})112perf: proxy assignaleclarsonbefore adoption53e9ce8
line 113why, one step back113describe("simple assignment - 1", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 114–122why, one step back114 runPatchTest(115 {x: 3},116 d => {117 d.x++118 },119 [{op: "replace", path: ["x"], value: 4}]120 )121})122perf: proxy assignaleclarsonbefore adoption53e9ce8
line 123why, one step back123describe("simple assignment - 2", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 124–132why, one step back124 runPatchTest(125 {x: {y: 4}},126 d => {127 d.x.y++128 },129 [{op: "replace", path: ["x", "y"], value: 5}]130 )131})132feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 133why, one step back133describe("simple assignment - 3", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 134–142why, one step back134 runPatchTest(135 {x: [{y: 4}]},136 d => {137 d.x[0].y++138 },139 [{op: "replace", path: ["x", 0, "y"], value: 5}]140 )141})142feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 143why, one step back143describe("simple assignment - 4", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 144–153why, one step back144 runPatchTest(145 new Map([["x", {y: 4}]]),146 d => {147 d.get("x").y++148 },149 [{op: "replace", path: ["x", "y"], value: 5}],150 [{op: "replace", path: ["x", "y"], value: 4}]151 )152})153feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 154why, one step back154describe("simple assignment - 5", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 155–164why, one step back155 runPatchTest(156 {x: new Map([["y", 4]])},157 d => {158 d.x.set("y", 5)159 },160 [{op: "replace", path: ["x", "y"], value: 5}],161 [{op: "replace", path: ["x", "y"], value: 4}]162 )163})164fix: Map.prototype.set should return a draftAndrey Goncharovbefore adoption7001201
line 165why, one step back165describe("simple assignment - 6", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 166–180why, one step back166 runPatchTest(167 new Map([["x", 1]]),168 d => {169 // Map.prototype.set should return the Map itself170 const res = d.set("x", 2)171 res.set("y", 3)172 },173 [174 {op: "replace", path: ["x"], value: 2},175 {op: "add", path: ["y"], value: 3}176 ],177 [{op: "replace", path: ["x"], value: 1}, {op: "remove", path: ["y"]}]178 )179})180feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
line 181why, one step back181describe("simple assignment - 7", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 182–200why, one step back182 const key1 = {prop: "val1"}183 const key2 = {prop: "val2"}184 runPatchTest(185 {x: new Map([[key1, 4]])},186 d => {187 d.x.set(key1, 5)188 d.x.set(key2, 6)189 },190 [191 {op: "replace", path: ["x", key1], value: 5},192 {op: "add", path: ["x", key2], value: 6}193 ],194 [195 {op: "replace", path: ["x", key1], value: 4},196 {op: "remove", path: ["x", key2]}197 ]198 )199})200perf: proxy assignaleclarsonbefore adoption53e9ce8
line 201why, one step back201describe("delete 1", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 202–210why, one step back202 runPatchTest(203 {x: {y: 4}},204 d => {205 delete d.x206 },207 [{op: "remove", path: ["x"]}]208 )209})210feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 211why, one step back211describe("delete 2", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 212–221why, one step back212 runPatchTest(213 new Map([["x", 1]]),214 d => {215 d.delete("x")216 },217 [{op: "remove", path: ["x"]}],218 [{op: "add", path: ["x"], value: 1}]219 )220})221feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 222why, one step back222describe("delete 3", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 223–232why, one step back223 runPatchTest(224 {x: new Map([["y", 1]])},225 d => {226 d.x.delete("y")227 },228 [{op: "remove", path: ["x", "y"]}],229 [{op: "add", path: ["x", "y"], value: 1}]230 )231})232feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
line 233why, one step back233describe("delete 5", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 234–249why, one step back234 const key1 = {prop: "val1"}235 const key2 = {prop: "val2"}236 runPatchTest(237 {x: new Map([[key1, 1], [key2, 2]])},238 d => {239 d.x.delete(key1)240 d.x.delete(key2)241 },242 [{op: "remove", path: ["x", key1]}, {op: "remove", path: ["x", key2]}],243 [244 {op: "add", path: ["x", key1], value: 1},245 {op: "add", path: ["x", key2], value: 2}246 ]247 )248})249feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
line 250why, one step back250describe("delete 6", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 251–260why, one step back251 runPatchTest(252 new Set(["x", 1]),253 d => {254 d.delete("x")255 },256 [{op: "remove", path: [0], value: "x"}],257 [{op: "add", path: [0], value: "x"}]258 )259})260feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
line 261why, one step back261describe("delete 7", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 262–271why, one step back262 runPatchTest(263 {x: new Set(["y", 1])},264 d => {265 d.x.delete("y")266 },267 [{op: "remove", path: ["x", 0], value: "y"}],268 [{op: "add", path: ["x", 0], value: "y"}]269 )270})271perf: proxy assignaleclarsonbefore adoption53e9ce8
line 272why, one step back272describe("renaming properties", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 273–398why, one step back273 describe("nested object (no changes)", () => {274 runPatchTest(275 {a: {b: 1}},276 d => {277 d.x = d.a278 delete d.a279 },280 [{op: "add", path: ["x"], value: {b: 1}}, {op: "remove", path: ["a"]}]281 )282 })283284 describe("nested map (no changes)", () => {285 runPatchTest(286 new Map([["a", new Map([["b", 1]])]]),287 d => {288 d.set("x", d.get("a"))289 d.delete("a")290 },291 [292 {op: "add", path: ["x"], value: new Map([["b", 1]])},293 {op: "remove", path: ["a"]}294 ],295 [296 {op: "remove", path: ["x"]},297 {op: "add", path: ["a"], value: new Map([["b", 1]])}298 ]299 )300 })301302 describe("nested object (with changes)", () => {303 runPatchTest(304 {a: {b: 1, c: 1}},305 d => {306 let a = d.a307 a.b = 2 // change308 delete a.c // delete309 a.y = 2 // add310311 // rename312 d.x = a313 delete d.a314 },315 [316 {op: "add", path: ["x"], value: {b: 2, y: 2}},317 {op: "remove", path: ["a"]}318 ]319 )320 })321322 describe("nested map (with changes)", () => {323 runPatchTest(324 new Map([["a", new Map([["b", 1], ["c", 1]])]]),325 d => {326 let a = d.get("a")327 a.set("b", 2) // change328 a.delete("c") // delete329 a.set("y", 2) // add330331 // rename332 d.set("x", a)333 d.delete("a")334 },335 [336 {op: "add", path: ["x"], value: new Map([["b", 2], ["y", 2]])},337 {op: "remove", path: ["a"]}338 ],339 [340 {op: "remove", path: ["x"]},341 {op: "add", path: ["a"], value: new Map([["b", 1], ["c", 1]])}342 ]343 )344 })345346 describe("deeply nested object (with changes)", () => {347 runPatchTest(348 {a: {b: {c: 1, d: 1}}},349 d => {350 let b = d.a.b351 b.c = 2 // change352 delete b.d // delete353 b.y = 2 // add354355 // rename356 d.a.x = b357 delete d.a.b358 },359 [360 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},361 {op: "remove", path: ["a", "b"]}362 ]363 )364 })365366 describe("deeply nested map (with changes)", () => {367 runPatchTest(368 new Map([["a", new Map([["b", new Map([["c", 1], ["d", 1]])]])]]),369 d => {370 let b = d.get("a").get("b")371 b.set("c", 2) // change372 b.delete("d") // delete373 b.set("y", 2) // add374375 // rename376 d.get("a").set("x", b)377 d.get("a").delete("b")378 },379 [380 {381 op: "add",382 path: ["a", "x"],383 value: new Map([["c", 2], ["y", 2]])384 },385 {op: "remove", path: ["a", "b"]}386 ],387 [388 {op: "remove", path: ["a", "x"]},389 {390 op: "add",391 path: ["a", "b"],392 value: new Map([["c", 1], ["d", 1]])393 }394 ]395 )396 })397})398perf: proxy assignaleclarsonbefore adoption53e9ce8
line 399why, one step back399describe("minimum amount of changes", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 400–413why, one step back400 runPatchTest(401 {x: 3, y: {a: 4}, z: 3},402 d => {403 d.y.a = 4404 d.y.b = 5405 Object.assign(d, {x: 4, y: {a: 2}})406 },407 [408 {op: "replace", path: ["x"], value: 4},409 {op: "replace", path: ["y"], value: {a: 2}}410 ]411 )412})413perf: proxy assignaleclarsonbefore adoption53e9ce8
line 414why, one step back414describe("arrays - prepend", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 415–423why, one step back415 runPatchTest(416 {x: [1, 2, 3]},417 d => {418 d.x.unshift(4)419 },420 [{op: "add", path: ["x", 0], value: 4}]421 )422})423feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 424why, one step back424describe("arrays - multiple prepend", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 425–437why, one step back425 runPatchTest(426 {x: [1, 2, 3]},427 d => {428 d.x.unshift(4)429 d.x.unshift(5)430 },431 [432 {op: "add", path: ["x", 0], value: 5},433 {op: "add", path: ["x", 1], value: 4}434 ]435 )436})437perf: proxy assignaleclarsonbefore adoption53e9ce8
line 438why, one step back438describe("arrays - splice middle", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 439–447why, one step back439 runPatchTest(440 {x: [1, 2, 3]},441 d => {442 d.x.splice(1, 1)443 },444 [{op: "remove", path: ["x", 1]}]445 )446})447feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 448why, one step back448describe("arrays - multiple splice", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 449–463why, one step back449 runPatchTest(450 [0, 1, 2, 3, 4, 5, 0],451 d => {452 d.splice(4, 2, 3)453 d.splice(1, 2, 3)454 },455 [456 {op: "replace", path: [1], value: 3},457 {op: "replace", path: [2], value: 3},458 {op: "remove", path: [5]},459 {op: "remove", path: [4]}460 ]461 )462})463feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 464why, one step back464describe("arrays - modify and shrink", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 465–470why, one step back465 runPatchTest(466 {x: [1, 2, 3]},467 d => {468 d.x[0] = 4469 d.x.length = 2470 },Merge branch 'master' into v4Michel Weststratebefore adoption741e941
line 471why, one step back471 [{op: "replace", path: ["x", 0], value: 4}, {op: "remove", path: ["x", 2]}],run prettier with master config before mergingMichel Weststratebefore adoptionae80361
line 472why, one step back472 [Merge branch 'master' into v4Michel Weststratebefore adoption741e941
lines 473–474why, one step back473 {op: "replace", path: ["x", 0], value: 1},474 {op: "add", path: ["x", 2], value: 3}run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 475–478why, one step back475 ]476 )477})478perf: proxy assignaleclarsonbefore adoption53e9ce8
line 479why, one step back479describe("arrays - prepend then splice middle", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 480–492why, one step back480 runPatchTest(481 {x: [1, 2, 3]},482 d => {483 d.x.unshift(4)484 d.x.splice(2, 1)485 },486 [487 {op: "replace", path: ["x", 0], value: 4},488 {op: "replace", path: ["x", 1], value: 1}489 ]490 )491})492perf: proxy assignaleclarsonbefore adoption53e9ce8
line 493why, one step back493describe("arrays - splice middle then prepend", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 494–506why, one step back494 runPatchTest(495 {x: [1, 2, 3]},496 d => {497 d.x.splice(1, 1)498 d.x.unshift(4)499 },500 [501 {op: "replace", path: ["x", 0], value: 4},502 {op: "replace", path: ["x", 1], value: 1}503 ]504 )505})506perf: proxy assignaleclarsonbefore adoption53e9ce8
line 507why, one step back507describe("arrays - truncate", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 508–512why, one step back508 runPatchTest(509 {x: [1, 2, 3]},510 d => {511 d.x.length -= 2512 },Merge branch 'master' into v4Michel Weststratebefore adoption741e941
line 513why, one step back513 [{op: "remove", path: ["x", 2]}, {op: "remove", path: ["x", 1]}],run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 514–520why, one step back514 [515 {op: "add", path: ["x", 1], value: 2},516 {op: "add", path: ["x", 2], value: 3}517 ]518 )519})520perf: proxy assignaleclarsonbefore adoption53e9ce8
line 521why, one step back521describe("arrays - pop twice", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 522–527why, one step back522 runPatchTest(523 {x: [1, 2, 3]},524 d => {525 d.x.pop()526 d.x.pop()527 },Merge branch 'master' into v4Michel Weststratebefore adoption741e941
line 528why, one step back528 [{op: "remove", path: ["x", 2]}, {op: "remove", path: ["x", 1]}]run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 529–531why, one step back529 )530})531perf: proxy assignaleclarsonbefore adoption53e9ce8
line 532why, one step back532describe("arrays - push multiple", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 533–541why, one step back533 runPatchTest(534 {x: [1, 2, 3]},535 d => {536 d.x.push(4, 5)537 },538 [539 {op: "add", path: ["x", 3], value: 4},540 {op: "add", path: ["x", 4], value: 5}541 ],Merge branch 'master' into v4Michel Weststratebefore adoption741e941
line 542why, one step back542 [{op: "remove", path: ["x", 4]}, {op: "remove", path: ["x", 3]}]run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 543–545why, one step back543 )544})545feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 546why, one step back546describe("arrays - splice (expand)", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 547–564why, one step back547 runPatchTest(548 {x: [1, 2, 3]},549 d => {550 d.x.splice(1, 1, 4, 5, 6)551 },552 [553 {op: "replace", path: ["x", 1], value: 4},554 {op: "add", path: ["x", 2], value: 5},555 {op: "add", path: ["x", 3], value: 6}556 ],557 [558 {op: "replace", path: ["x", 1], value: 2},559 {op: "remove", path: ["x", 3]},560 {op: "remove", path: ["x", 2]}561 ]562 )563})564feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 565why, one step back565describe("arrays - splice (shrink)", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 566–583why, one step back566 runPatchTest(567 {x: [1, 2, 3, 4, 5]},568 d => {569 d.x.splice(1, 3, 6)570 },571 [572 {op: "replace", path: ["x", 1], value: 6},573 {op: "remove", path: ["x", 3]},574 {op: "remove", path: ["x", 2]}575 ],576 [577 {op: "replace", path: ["x", 1], value: 2},578 {op: "add", path: ["x", 2], value: 3},579 {op: "add", path: ["x", 3], value: 4}580 ]581 )582})583feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 584why, one step back584describe("sets - add - 1", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 585–594why, one step back585 runPatchTest(586 new Set([1]),587 d => {588 d.add(2)589 },590 [{op: "add", path: [1], value: 2}],591 [{op: "remove", path: [1], value: 2}]592 )593})594feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 595why, one step back595describe("sets - add, delete, add - 1", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 596–607why, one step back596 runPatchTest(597 new Set([1]),598 d => {599 d.add(2)600 d.delete(2)601 d.add(2)602 },603 [{op: "add", path: [1], value: 2}],604 [{op: "remove", path: [1], value: 2}]605 )606})607feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 608why, one step back608describe("sets - add, delete, add - 2", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 609–620why, one step back609 runPatchTest(610 new Set([2, 1]),611 d => {612 d.add(2)613 d.delete(2)614 d.add(2)615 },616 [],617 []618 )619})620feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 621why, one step back621describe("sets - mutate - 1", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 622–649why, one step back622 const findById = (set, id) => {623 for (const item of set) {624 if (item.id === id) return item625 }626 }627 runPatchTest(628 new Set([{id: 1, val: "We"}, {id: 2, val: "will"}]),629 d => {630 const obj1 = findById(d, 1)631 const obj2 = findById(d, 2)632 obj1.val = "rock"633 obj2.val = "you"634 },635 [636 {op: "remove", path: [0], value: {id: 1, val: "We"}},637 {op: "remove", path: [1], value: {id: 2, val: "will"}},638 {op: "add", path: [0], value: {id: 1, val: "rock"}},639 {op: "add", path: [1], value: {id: 2, val: "you"}}640 ],641 [642 {op: "remove", path: [1], value: {id: 2, val: "you"}},643 {op: "remove", path: [0], value: {id: 1, val: "rock"}},644 {op: "add", path: [1], value: {id: 2, val: "will"}},645 {op: "add", path: [0], value: {id: 1, val: "We"}}646 ]647 )648})649Merge branch 'master' into v4Michel Weststratebefore adoption741e941
lines 650–671why, one step back650describe("arrays - splice should should result in remove op.", () => {651 runPatchTest(652 [1, 2],653 d => {654 d.splice(1, 1)655 },656 [{op: "remove", path: [1]}],657 [{op: "add", path: [1], value: 2}]658 )659})660661describe("arrays - NESTED splice should should result in remove op.", () => {662 runPatchTest(663 {a: {b: {c: [1, 2]}}},664 d => {665 d.a.b.c.splice(1, 1)666 },667 [{op: "remove", path: ["a", "b", "c", 1]}],668 [{op: "add", path: ["a", "b", "c", 1], value: 2}]669 )670})671perf: proxy assignaleclarsonbefore adoption53e9ce8
line 672why, one step back672describe("simple replacement", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 673–675why, one step back673 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])674})675perf: proxy assignaleclarsonbefore adoption53e9ce8
line 676why, one step back676describe("same value replacement - 1", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 677–686why, one step back677 runPatchTest(678 {x: {y: 3}},679 d => {680 const a = d.x681 d.x = a682 },683 []684 )685})686perf: proxy assignaleclarsonbefore adoption53e9ce8
line 687why, one step back687describe("same value replacement - 2", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 688–698why, one step back688 runPatchTest(689 {x: {y: 3}},690 d => {691 const a = d.x692 d.x = 4693 d.x = a694 },695 []696 )697})698perf: proxy assignaleclarsonbefore adoption53e9ce8
line 699why, one step back699describe("same value replacement - 3", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 700–708why, one step back700 runPatchTest(701 {x: 3},702 d => {703 d.x = 3704 },705 []706 )707})708perf: proxy assignaleclarsonbefore adoption53e9ce8
line 709why, one step back709describe("same value replacement - 4", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 710–719why, one step back710 runPatchTest(711 {x: 3},712 d => {713 d.x = 4714 d.x = 3715 },716 []717 )718})719feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 720why, one step back720describe("same value replacement - 5", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 721–731why, one step back721 runPatchTest(722 new Map([["x", 3]]),723 d => {724 d.set("x", 4)725 d.set("x", 3)726 },727 [],728 []729 )730})731feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 732why, one step back732describe("same value replacement - 6", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 733–743why, one step back733 runPatchTest(734 new Set(["x", 3]),735 d => {736 d.delete("x")737 d.add("x")738 },739 [],740 []741 )742})743perf: proxy assignaleclarsonbefore adoption53e9ce8
line 744why, one step back744describe("simple delete", () => {run prettier with master config before mergingMichel Weststratebefore adoptionae80361
lines 745–756why, one step back745 runPatchTest(746 {x: 2},747 d => {748 delete d.x749 },750 [751 {752 op: "remove",753 path: ["x"]754 }755 ]756 )Merge branch 'master' into v4Michel Weststratebefore adoption741e941
lines 757–862why, one step back757})758759describe("patch compressions yields correct results", () => {760 let p1, p2761 runPatchTest(762 {},763 d => {764 d.x = {test: true}765 },766 (p1 = [767 {768 op: "add",769 path: ["x"],770 value: {771 test: true772 }773 }774 ])775 )776 runPatchTest(777 {x: {test: true}},778 d => {779 delete d.x780 },781 (p2 = [782 {783 op: "remove",784 path: ["x"]785 }786 ])787 )788 const res = runPatchTest(789 {},790 d => {791 applyPatches(d, [...p1, ...p2])792 },793 []794 )795796 expect(res).toEqual({})797})798799describe("change then delete property", () => {800 const res = runPatchTest(801 {802 x: 1803 },804 d => {805 d.x = 2806 delete d.x807 },808 [809 {810 op: "remove",811 path: ["x"]812 }813 ]814 )815 test("valid result", () => {816 expect(res).toEqual({})817 })818})819820test("replaying patches with interweaved replacements should work correctly", () => {821 const patches = []822 const s0 = {x: 1}823824 const s1 = produce(825 s0,826 draft => {827 draft.x = 2828 },829 p => {830 patches.push(...p)831 }832 )833834 const s2 = produce(835 s1,836 draft => {837 return {x: 0}838 },839 p => {840 patches.push(...p)841 }842 )843844 const s3 = produce(845 s2,846 draft => {847 draft.x--848 },849 p => {850 patches.push(...p)851 }852 )853854 expect(s3).toEqual({x: -1}) // correct result855 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay856857 // manual replay on a draft should also be correct858 expect(859 produce(s0, draft => {860 return applyPatches(draft, patches)861 })862 ).toEqual({x: -1})perf: proxy assignaleclarsonbefore adoption53e9ce8
line 863why, one step back863})