patch.js
This file
Path
__tests__/patch.js
863 lines · 15.5 KiB · JavaScript
Last changed by
fixed most testsMichel Weststratebefore adoption385c0a4
History of this fileTake it
1"use strict"2import produce, {3 setUseProxies,4 applyPatches,5 produceWithPatches6} from "../src/index"78jest.setTimeout(1000)910function runPatchTest(base, producer, patches, inversePathes) {11 let resultProxies, resultEs51213 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 })3334 return res35 }3637 describe(`proxy`, () => {38 setUseProxies(true)39 resultProxies = runPatchTestHelper()40 })4142 describe(`es5`, () => {43 setUseProxies(false)44 resultEs5 = runPatchTestHelper()45 test("ES5 and Proxy implementation yield same result", () => {46 expect(resultEs5).toEqual(resultProxies)47 })48 })4950 return resultProxies51}5253describe("applyPatches", () => {54 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 })89 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 })111})112113describe("simple assignment - 1", () => {114 runPatchTest(115 {x: 3},116 d => {117 d.x++118 },119 [{op: "replace", path: ["x"], value: 4}]120 )121})122123describe("simple assignment - 2", () => {124 runPatchTest(125 {x: {y: 4}},126 d => {127 d.x.y++128 },129 [{op: "replace", path: ["x", "y"], value: 5}]130 )131})132133describe("simple assignment - 3", () => {134 runPatchTest(135 {x: [{y: 4}]},136 d => {137 d.x[0].y++138 },139 [{op: "replace", path: ["x", 0, "y"], value: 5}]140 )141})142143describe("simple assignment - 4", () => {144 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})153154describe("simple assignment - 5", () => {155 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})164165describe("simple assignment - 6", () => {166 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})180181describe("simple assignment - 7", () => {182 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})200201describe("delete 1", () => {202 runPatchTest(203 {x: {y: 4}},204 d => {205 delete d.x206 },207 [{op: "remove", path: ["x"]}]208 )209})210211describe("delete 2", () => {212 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})221222describe("delete 3", () => {223 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})232233describe("delete 5", () => {234 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})249250describe("delete 6", () => {251 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})260261describe("delete 7", () => {262 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})271272describe("renaming properties", () => {273 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})398399describe("minimum amount of changes", () => {400 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})413414describe("arrays - prepend", () => {415 runPatchTest(416 {x: [1, 2, 3]},417 d => {418 d.x.unshift(4)419 },420 [{op: "add", path: ["x", 0], value: 4}]421 )422})423424describe("arrays - multiple prepend", () => {425 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})437438describe("arrays - splice middle", () => {439 runPatchTest(440 {x: [1, 2, 3]},441 d => {442 d.x.splice(1, 1)443 },444 [{op: "remove", path: ["x", 1]}]445 )446})447448describe("arrays - multiple splice", () => {449 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})463464describe("arrays - modify and shrink", () => {465 runPatchTest(466 {x: [1, 2, 3]},467 d => {468 d.x[0] = 4469 d.x.length = 2470 },471 [{op: "replace", path: ["x", 0], value: 4}, {op: "remove", path: ["x", 2]}],472 [473 {op: "replace", path: ["x", 0], value: 1},474 {op: "add", path: ["x", 2], value: 3}475 ]476 )477})478479describe("arrays - prepend then splice middle", () => {480 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})492493describe("arrays - splice middle then prepend", () => {494 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})506507describe("arrays - truncate", () => {508 runPatchTest(509 {x: [1, 2, 3]},510 d => {511 d.x.length -= 2512 },513 [{op: "remove", path: ["x", 2]}, {op: "remove", path: ["x", 1]}],514 [515 {op: "add", path: ["x", 1], value: 2},516 {op: "add", path: ["x", 2], value: 3}517 ]518 )519})520521describe("arrays - pop twice", () => {522 runPatchTest(523 {x: [1, 2, 3]},524 d => {525 d.x.pop()526 d.x.pop()527 },528 [{op: "remove", path: ["x", 2]}, {op: "remove", path: ["x", 1]}]529 )530})531532describe("arrays - push multiple", () => {533 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 ],542 [{op: "remove", path: ["x", 4]}, {op: "remove", path: ["x", 3]}]543 )544})545546describe("arrays - splice (expand)", () => {547 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})564565describe("arrays - splice (shrink)", () => {566 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})583584describe("sets - add - 1", () => {585 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})594595describe("sets - add, delete, add - 1", () => {596 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})607608describe("sets - add, delete, add - 2", () => {609 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})620621describe.skip("sets - mutate - 1", () => {622 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})649650describe("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})671672describe("simple replacement", () => {673 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])674})675676describe("same value replacement - 1", () => {677 runPatchTest(678 {x: {y: 3}},679 d => {680 const a = d.x681 d.x = a682 },683 []684 )685})686687describe("same value replacement - 2", () => {688 runPatchTest(689 {x: {y: 3}},690 d => {691 const a = d.x692 d.x = 4693 d.x = a694 },695 []696 )697})698699describe("same value replacement - 3", () => {700 runPatchTest(701 {x: 3},702 d => {703 d.x = 3704 },705 []706 )707})708709describe("same value replacement - 4", () => {710 runPatchTest(711 {x: 3},712 d => {713 d.x = 4714 d.x = 3715 },716 []717 )718})719720describe("same value replacement - 5", () => {721 runPatchTest(722 new Map([["x", 3]]),723 d => {724 d.set("x", 4)725 d.set("x", 3)726 },727 [],728 []729 )730})731732describe.skip("same value replacement - 6", () => {733 runPatchTest(734 new Set(["x", 3]),735 d => {736 d.delete("x")737 d.add("x")738 },739 [],740 []741 )742})743744describe("simple delete", () => {745 runPatchTest(746 {x: 2},747 d => {748 delete d.x749 },750 [751 {752 op: "remove",753 path: ["x"]754 }755 ]756 )757})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})863})