patch.js
This file
Path
__tests__/patch.js
1,016 lines · 17.2 KiB · JavaScript
Last changed by
New build set. Current statsMichel Weststratebefore adoption9aec0fe
History of this fileTake it
1"use strict"2import produce, {3 setUseProxies,4 applyPatches,5 produceWithPatches,6 enableAllPlugins7} from "../src/immer"89enableAllPlugins()1011jest.setTimeout(1000)1213function runPatchTest(base, producer, patches, inversePathes) {14 let resultProxies, resultEs51516 function runPatchTestHelper() {17 let recordedPatches18 let recordedInversePatches19 const res = produce(base, producer, (p, i) => {20 recordedPatches = p21 recordedInversePatches = i22 })2324 test("produces the correct patches", () => {25 expect(recordedPatches).toEqual(patches)26 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)27 })2829 test("patches are replayable", () => {30 expect(applyPatches(base, recordedPatches)).toEqual(res)31 })3233 test("patches can be reversed", () => {34 expect(applyPatches(res, recordedInversePatches)).toEqual(base)35 })3637 return res38 }3940 describe(`proxy`, () => {41 setUseProxies(true)42 resultProxies = runPatchTestHelper()43 })4445 describe(`es5`, () => {46 setUseProxies(false)47 resultEs5 = runPatchTestHelper()48 test("ES5 and Proxy implementation yield same result", () => {49 expect(resultEs5).toEqual(resultProxies)50 })51 })5253 return resultProxies54}5556describe("applyPatches", () => {57 it("mutates the base state when it is a draft", () => {58 produce({a: 1}, draft => {59 const result = applyPatches(draft, [60 {op: "replace", path: ["a"], value: 2}61 ])62 expect(result).toBe(draft)63 expect(draft.a).toBe(2)64 })65 })66 it("produces a copy of the base state when not a draft", () => {67 const base = {a: 1}68 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])69 expect(result).not.toBe(base)70 expect(result.a).toBe(2)71 expect(base.a).toBe(1)72 })73 it('throws when `op` is not "add", "replace", nor "remove"', () => {74 expect(() => {75 const patch = {op: "copy", from: [0], path: [1]}76 applyPatches([2], [patch])77 }).toThrowErrorMatchingSnapshot()78 })79 it("throws when `path` cannot be resolved", () => {80 // missing parent81 expect(() => {82 const patch = {op: "add", path: ["a", "b"], value: 1}83 applyPatches({}, [patch])84 }).toThrowErrorMatchingSnapshot()8586 // missing grand-parent87 expect(() => {88 const patch = {op: "add", path: ["a", "b", "c"], value: 1}89 applyPatches({}, [patch])90 }).toThrowErrorMatchingSnapshot()91 })92 it("applied patches cannot be modified", () => {93 // see also: https://github.com/immerjs/immer/issues/41194 const s0 = {95 items: [1]96 }9798 const [s1, p1] = produceWithPatches(s0, draft => {99 draft.items = []100 })101102 const replaceValueBefore = p1[0].value.slice()103104 const [s2, p2] = produceWithPatches(s1, draft => {105 draft.items.push(2)106 })107108 applyPatches(s0, [...p1, ...p2])109110 const replaceValueAfter = p1[0].value.slice()111112 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)113 })114})115116describe("simple assignment - 1", () => {117 runPatchTest(118 {x: 3},119 d => {120 d.x++121 },122 [{op: "replace", path: ["x"], value: 4}]123 )124})125126describe("simple assignment - 2", () => {127 runPatchTest(128 {x: {y: 4}},129 d => {130 d.x.y++131 },132 [{op: "replace", path: ["x", "y"], value: 5}]133 )134})135136describe("simple assignment - 3", () => {137 runPatchTest(138 {x: [{y: 4}]},139 d => {140 d.x[0].y++141 },142 [{op: "replace", path: ["x", 0, "y"], value: 5}]143 )144})145146describe("simple assignment - 4", () => {147 runPatchTest(148 new Map([["x", {y: 4}]]),149 d => {150 d.get("x").y++151 },152 [{op: "replace", path: ["x", "y"], value: 5}],153 [{op: "replace", path: ["x", "y"], value: 4}]154 )155})156157describe("simple assignment - 5", () => {158 runPatchTest(159 {x: new Map([["y", 4]])},160 d => {161 d.x.set("y", 5)162 },163 [{op: "replace", path: ["x", "y"], value: 5}],164 [{op: "replace", path: ["x", "y"], value: 4}]165 )166})167168describe("simple assignment - 6", () => {169 runPatchTest(170 new Map([["x", 1]]),171 d => {172 // Map.prototype.set should return the Map itself173 const res = d.set("x", 2)174 res.set("y", 3)175 },176 [177 {op: "replace", path: ["x"], value: 2},178 {op: "add", path: ["y"], value: 3}179 ],180 [181 {op: "replace", path: ["x"], value: 1},182 {op: "remove", path: ["y"]}183 ]184 )185})186187describe("simple assignment - 7", () => {188 const key1 = {prop: "val1"}189 const key2 = {prop: "val2"}190 runPatchTest(191 {x: new Map([[key1, 4]])},192 d => {193 d.x.set(key1, 5)194 d.x.set(key2, 6)195 },196 [197 {op: "replace", path: ["x", key1], value: 5},198 {op: "add", path: ["x", key2], value: 6}199 ],200 [201 {op: "replace", path: ["x", key1], value: 4},202 {op: "remove", path: ["x", key2]}203 ]204 )205})206207describe("delete 1", () => {208 runPatchTest(209 {x: {y: 4}},210 d => {211 delete d.x212 },213 [{op: "remove", path: ["x"]}]214 )215})216217describe("delete 2", () => {218 runPatchTest(219 new Map([["x", 1]]),220 d => {221 d.delete("x")222 },223 [{op: "remove", path: ["x"]}],224 [{op: "add", path: ["x"], value: 1}]225 )226})227228describe("delete 3", () => {229 runPatchTest(230 {x: new Map([["y", 1]])},231 d => {232 d.x.delete("y")233 },234 [{op: "remove", path: ["x", "y"]}],235 [{op: "add", path: ["x", "y"], value: 1}]236 )237})238239describe("delete 5", () => {240 const key1 = {prop: "val1"}241 const key2 = {prop: "val2"}242 runPatchTest(243 {244 x: new Map([245 [key1, 1],246 [key2, 2]247 ])248 },249 d => {250 d.x.delete(key1)251 d.x.delete(key2)252 },253 [254 {op: "remove", path: ["x", key1]},255 {op: "remove", path: ["x", key2]}256 ],257 [258 {op: "add", path: ["x", key1], value: 1},259 {op: "add", path: ["x", key2], value: 2}260 ]261 )262})263264describe("delete 6", () => {265 runPatchTest(266 new Set(["x", 1]),267 d => {268 d.delete("x")269 },270 [{op: "remove", path: [0], value: "x"}],271 [{op: "add", path: [0], value: "x"}]272 )273})274275describe("delete 7", () => {276 runPatchTest(277 {x: new Set(["y", 1])},278 d => {279 d.x.delete("y")280 },281 [{op: "remove", path: ["x", 0], value: "y"}],282 [{op: "add", path: ["x", 0], value: "y"}]283 )284})285286describe("renaming properties", () => {287 describe("nested object (no changes)", () => {288 runPatchTest(289 {a: {b: 1}},290 d => {291 d.x = d.a292 delete d.a293 },294 [295 {op: "add", path: ["x"], value: {b: 1}},296 {op: "remove", path: ["a"]}297 ]298 )299 })300301 describe("nested map (no changes)", () => {302 runPatchTest(303 new Map([["a", new Map([["b", 1]])]]),304 d => {305 d.set("x", d.get("a"))306 d.delete("a")307 },308 [309 {op: "add", path: ["x"], value: new Map([["b", 1]])},310 {op: "remove", path: ["a"]}311 ],312 [313 {op: "remove", path: ["x"]},314 {op: "add", path: ["a"], value: new Map([["b", 1]])}315 ]316 )317 })318319 describe("nested object (with changes)", () => {320 runPatchTest(321 {a: {b: 1, c: 1}},322 d => {323 let a = d.a324 a.b = 2 // change325 delete a.c // delete326 a.y = 2 // add327328 // rename329 d.x = a330 delete d.a331 },332 [333 {op: "add", path: ["x"], value: {b: 2, y: 2}},334 {op: "remove", path: ["a"]}335 ]336 )337 })338339 describe("nested map (with changes)", () => {340 runPatchTest(341 new Map([342 [343 "a",344 new Map([345 ["b", 1],346 ["c", 1]347 ])348 ]349 ]),350 d => {351 let a = d.get("a")352 a.set("b", 2) // change353 a.delete("c") // delete354 a.set("y", 2) // add355356 // rename357 d.set("x", a)358 d.delete("a")359 },360 [361 {362 op: "add",363 path: ["x"],364 value: new Map([365 ["b", 2],366 ["y", 2]367 ])368 },369 {op: "remove", path: ["a"]}370 ],371 [372 {op: "remove", path: ["x"]},373 {374 op: "add",375 path: ["a"],376 value: new Map([377 ["b", 1],378 ["c", 1]379 ])380 }381 ]382 )383 })384385 describe("deeply nested object (with changes)", () => {386 runPatchTest(387 {a: {b: {c: 1, d: 1}}},388 d => {389 let b = d.a.b390 b.c = 2 // change391 delete b.d // delete392 b.y = 2 // add393394 // rename395 d.a.x = b396 delete d.a.b397 },398 [399 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},400 {op: "remove", path: ["a", "b"]}401 ]402 )403 })404405 describe("deeply nested map (with changes)", () => {406 runPatchTest(407 new Map([408 [409 "a",410 new Map([411 [412 "b",413 new Map([414 ["c", 1],415 ["d", 1]416 ])417 ]418 ])419 ]420 ]),421 d => {422 let b = d.get("a").get("b")423 b.set("c", 2) // change424 b.delete("d") // delete425 b.set("y", 2) // add426427 // rename428 d.get("a").set("x", b)429 d.get("a").delete("b")430 },431 [432 {433 op: "add",434 path: ["a", "x"],435 value: new Map([436 ["c", 2],437 ["y", 2]438 ])439 },440 {op: "remove", path: ["a", "b"]}441 ],442 [443 {op: "remove", path: ["a", "x"]},444 {445 op: "add",446 path: ["a", "b"],447 value: new Map([448 ["c", 1],449 ["d", 1]450 ])451 }452 ]453 )454 })455})456457describe("minimum amount of changes", () => {458 runPatchTest(459 {x: 3, y: {a: 4}, z: 3},460 d => {461 d.y.a = 4462 d.y.b = 5463 Object.assign(d, {x: 4, y: {a: 2}})464 },465 [466 {op: "replace", path: ["x"], value: 4},467 {op: "replace", path: ["y"], value: {a: 2}}468 ]469 )470})471472describe("arrays - prepend", () => {473 runPatchTest(474 {x: [1, 2, 3]},475 d => {476 d.x.unshift(4)477 },478 [{op: "add", path: ["x", 0], value: 4}]479 )480})481482describe("arrays - multiple prepend", () => {483 runPatchTest(484 {x: [1, 2, 3]},485 d => {486 d.x.unshift(4)487 d.x.unshift(5)488 },489 [490 {op: "add", path: ["x", 0], value: 5},491 {op: "add", path: ["x", 1], value: 4}492 ]493 )494})495496describe("arrays - splice middle", () => {497 runPatchTest(498 {x: [1, 2, 3]},499 d => {500 d.x.splice(1, 1)501 },502 [{op: "remove", path: ["x", 1]}]503 )504})505506describe("arrays - multiple splice", () => {507 runPatchTest(508 [0, 1, 2, 3, 4, 5, 0],509 d => {510 d.splice(4, 2, 3)511 d.splice(1, 2, 3)512 },513 [514 {op: "replace", path: [1], value: 3},515 {op: "replace", path: [2], value: 3},516 {op: "remove", path: [5]},517 {op: "remove", path: [4]}518 ]519 )520})521522describe("arrays - modify and shrink", () => {523 runPatchTest(524 {x: [1, 2, 3]},525 d => {526 d.x[0] = 4527 d.x.length = 2528 },529 [530 {op: "replace", path: ["x", 0], value: 4},531 {op: "remove", path: ["x", 2]}532 ],533 [534 {op: "replace", path: ["x", 0], value: 1},535 {op: "add", path: ["x", 2], value: 3}536 ]537 )538})539540describe("arrays - prepend then splice middle", () => {541 runPatchTest(542 {x: [1, 2, 3]},543 d => {544 d.x.unshift(4)545 d.x.splice(2, 1)546 },547 [548 {op: "replace", path: ["x", 0], value: 4},549 {op: "replace", path: ["x", 1], value: 1}550 ]551 )552})553554describe("arrays - splice middle then prepend", () => {555 runPatchTest(556 {x: [1, 2, 3]},557 d => {558 d.x.splice(1, 1)559 d.x.unshift(4)560 },561 [562 {op: "replace", path: ["x", 0], value: 4},563 {op: "replace", path: ["x", 1], value: 1}564 ]565 )566})567568describe("arrays - truncate", () => {569 runPatchTest(570 {x: [1, 2, 3]},571 d => {572 d.x.length -= 2573 },574 [575 {op: "remove", path: ["x", 2]},576 {op: "remove", path: ["x", 1]}577 ],578 [579 {op: "add", path: ["x", 1], value: 2},580 {op: "add", path: ["x", 2], value: 3}581 ]582 )583})584585describe("arrays - pop twice", () => {586 runPatchTest(587 {x: [1, 2, 3]},588 d => {589 d.x.pop()590 d.x.pop()591 },592 [593 {op: "remove", path: ["x", 2]},594 {op: "remove", path: ["x", 1]}595 ]596 )597})598599describe("arrays - push multiple", () => {600 runPatchTest(601 {x: [1, 2, 3]},602 d => {603 d.x.push(4, 5)604 },605 [606 {op: "add", path: ["x", 3], value: 4},607 {op: "add", path: ["x", 4], value: 5}608 ],609 [610 {op: "remove", path: ["x", 4]},611 {op: "remove", path: ["x", 3]}612 ]613 )614})615616describe("arrays - splice (expand)", () => {617 runPatchTest(618 {x: [1, 2, 3]},619 d => {620 d.x.splice(1, 1, 4, 5, 6)621 },622 [623 {op: "replace", path: ["x", 1], value: 4},624 {op: "add", path: ["x", 2], value: 5},625 {op: "add", path: ["x", 3], value: 6}626 ],627 [628 {op: "replace", path: ["x", 1], value: 2},629 {op: "remove", path: ["x", 3]},630 {op: "remove", path: ["x", 2]}631 ]632 )633})634635describe("arrays - splice (shrink)", () => {636 runPatchTest(637 {x: [1, 2, 3, 4, 5]},638 d => {639 d.x.splice(1, 3, 6)640 },641 [642 {op: "replace", path: ["x", 1], value: 6},643 {op: "remove", path: ["x", 3]},644 {op: "remove", path: ["x", 2]}645 ],646 [647 {op: "replace", path: ["x", 1], value: 2},648 {op: "add", path: ["x", 2], value: 3},649 {op: "add", path: ["x", 3], value: 4}650 ]651 )652})653654describe("arrays - delete", () => {655 runPatchTest(656 {657 x: [658 {a: 1, b: 2},659 {c: 3, d: 4}660 ]661 },662 d => {663 delete d.x[1].c664 },665 [{op: "remove", path: ["x", 1, "c"]}]666 )667})668669describe("sets - add - 1", () => {670 runPatchTest(671 new Set([1]),672 d => {673 d.add(2)674 },675 [{op: "add", path: [1], value: 2}],676 [{op: "remove", path: [1], value: 2}]677 )678})679680describe("sets - add, delete, add - 1", () => {681 runPatchTest(682 new Set([1]),683 d => {684 d.add(2)685 d.delete(2)686 d.add(2)687 },688 [{op: "add", path: [1], value: 2}],689 [{op: "remove", path: [1], value: 2}]690 )691})692693describe("sets - add, delete, add - 2", () => {694 runPatchTest(695 new Set([2, 1]),696 d => {697 d.add(2)698 d.delete(2)699 d.add(2)700 },701 [],702 []703 )704})705706describe("sets - mutate - 1", () => {707 const findById = (set, id) => {708 for (const item of set) {709 if (item.id === id) return item710 }711 }712 runPatchTest(713 new Set([714 {id: 1, val: "We"},715 {id: 2, val: "will"}716 ]),717 d => {718 const obj1 = findById(d, 1)719 const obj2 = findById(d, 2)720 obj1.val = "rock"721 obj2.val = "you"722 },723 [724 {op: "remove", path: [0], value: {id: 1, val: "We"}},725 {op: "remove", path: [1], value: {id: 2, val: "will"}},726 {op: "add", path: [0], value: {id: 1, val: "rock"}},727 {op: "add", path: [1], value: {id: 2, val: "you"}}728 ],729 [730 {op: "remove", path: [1], value: {id: 2, val: "you"}},731 {op: "remove", path: [0], value: {id: 1, val: "rock"}},732 {op: "add", path: [1], value: {id: 2, val: "will"}},733 {op: "add", path: [0], value: {id: 1, val: "We"}}734 ]735 )736})737738describe("arrays - splice should should result in remove op.", () => {739 runPatchTest(740 [1, 2],741 d => {742 d.splice(1, 1)743 },744 [{op: "remove", path: [1]}],745 [{op: "add", path: [1], value: 2}]746 )747})748749describe("arrays - NESTED splice should should result in remove op.", () => {750 runPatchTest(751 {a: {b: {c: [1, 2]}}},752 d => {753 d.a.b.c.splice(1, 1)754 },755 [{op: "remove", path: ["a", "b", "c", 1]}],756 [{op: "add", path: ["a", "b", "c", 1], value: 2}]757 )758})759760describe("simple replacement", () => {761 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])762})763764describe("same value replacement - 1", () => {765 runPatchTest(766 {x: {y: 3}},767 d => {768 const a = d.x769 d.x = a770 },771 []772 )773})774775describe("same value replacement - 2", () => {776 runPatchTest(777 {x: {y: 3}},778 d => {779 const a = d.x780 d.x = 4781 d.x = a782 },783 []784 )785})786787describe("same value replacement - 3", () => {788 runPatchTest(789 {x: 3},790 d => {791 d.x = 3792 },793 []794 )795})796797describe("same value replacement - 4", () => {798 runPatchTest(799 {x: 3},800 d => {801 d.x = 4802 d.x = 3803 },804 []805 )806})807808describe("same value replacement - 5", () => {809 runPatchTest(810 new Map([["x", 3]]),811 d => {812 d.set("x", 4)813 d.set("x", 3)814 },815 [],816 []817 )818})819820describe("same value replacement - 6", () => {821 runPatchTest(822 new Set(["x", 3]),823 d => {824 d.delete("x")825 d.add("x")826 },827 [],828 []829 )830})831832describe("simple delete", () => {833 runPatchTest(834 {x: 2},835 d => {836 delete d.x837 },838 [839 {840 op: "remove",841 path: ["x"]842 }843 ]844 )845})846847describe("patch compressions yields correct results", () => {848 let p1, p2849 runPatchTest(850 {},851 d => {852 d.x = {test: true}853 },854 (p1 = [855 {856 op: "add",857 path: ["x"],858 value: {859 test: true860 }861 }862 ])863 )864 runPatchTest(865 {x: {test: true}},866 d => {867 delete d.x868 },869 (p2 = [870 {871 op: "remove",872 path: ["x"]873 }874 ])875 )876 const res = runPatchTest(877 {},878 d => {879 applyPatches(d, [...p1, ...p2])880 },881 []882 )883884 expect(res).toEqual({})885})886887describe("change then delete property", () => {888 const res = runPatchTest(889 {890 x: 1891 },892 d => {893 d.x = 2894 delete d.x895 },896 [897 {898 op: "remove",899 path: ["x"]900 }901 ]902 )903 test("valid result", () => {904 expect(res).toEqual({})905 })906})907908test("replaying patches with interweaved replacements should work correctly", () => {909 const patches = []910 const s0 = {x: 1}911912 const s1 = produce(913 s0,914 draft => {915 draft.x = 2916 },917 p => {918 patches.push(...p)919 }920 )921922 const s2 = produce(923 s1,924 draft => {925 return {x: 0}926 },927 p => {928 patches.push(...p)929 }930 )931932 const s3 = produce(933 s2,934 draft => {935 draft.x--936 },937 p => {938 patches.push(...p)939 }940 )941942 expect(s3).toEqual({x: -1}) // correct result943 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay944945 // manual replay on a draft should also be correct946 expect(947 produce(s0, draft => {948 return applyPatches(draft, patches)949 })950 ).toEqual({x: -1})951})952953test.skip("#468", () => {954 const item = {id: 1}955956 const state = [item]957958 const [nextState, patches] = produceWithPatches(state, draft => {959 draft[0].id = 2960 draft[1] = item961 })962963 expect(nextState).toMatchInlineSnapshot(`964 Array [965 Object {966 "id": 2,967 },968 Object {969 "id": 1,970 },971 ]972 `)973 expect(patches).toMatchInlineSnapshot(`974 Array [975 Object {976 "op": "replace",977 "path": Array [978 0,979 "id",980 ],981 "value": 2,982 },983 Object {984 "op": "add",985 "path": Array [986 0,987 ],988 "value": Object {989 "id": 2,990 },991 },992 ]993 `)994995 const final = applyPatches(state, patches)996 expect(final).toEqual(nextState)997})998999test("#521", () => {1000 const state = new Map()10011002 const [nextState, patches] = produceWithPatches(state, draft => {1003 draft.set("hello", new Set(["world"]))1004 })10051006 let patchedState = applyPatches(state, patches)1007 expect(patchedState).toEqual(nextState)10081009 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1010 draft.get("hello").add("immer")1011 })10121013 expect(applyPatches(nextState, patchesV2)).toEqual(1014 new Map([["hello", new Set(["world", "immer"])]])1015 )1016})