patch.js
This file
Path
__tests__/patch.js
1,013 lines · 17.2 KiB · JavaScript
Last changed by
fix: incorrect patches generated for array if property was deleted insideMichel Weststratebefore adoption1e451a3
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 [178 {op: "replace", path: ["x"], value: 1},179 {op: "remove", path: ["y"]}180 ]181 )182})183184describe("simple assignment - 7", () => {185 const key1 = {prop: "val1"}186 const key2 = {prop: "val2"}187 runPatchTest(188 {x: new Map([[key1, 4]])},189 d => {190 d.x.set(key1, 5)191 d.x.set(key2, 6)192 },193 [194 {op: "replace", path: ["x", key1], value: 5},195 {op: "add", path: ["x", key2], value: 6}196 ],197 [198 {op: "replace", path: ["x", key1], value: 4},199 {op: "remove", path: ["x", key2]}200 ]201 )202})203204describe("delete 1", () => {205 runPatchTest(206 {x: {y: 4}},207 d => {208 delete d.x209 },210 [{op: "remove", path: ["x"]}]211 )212})213214describe("delete 2", () => {215 runPatchTest(216 new Map([["x", 1]]),217 d => {218 d.delete("x")219 },220 [{op: "remove", path: ["x"]}],221 [{op: "add", path: ["x"], value: 1}]222 )223})224225describe("delete 3", () => {226 runPatchTest(227 {x: new Map([["y", 1]])},228 d => {229 d.x.delete("y")230 },231 [{op: "remove", path: ["x", "y"]}],232 [{op: "add", path: ["x", "y"], value: 1}]233 )234})235236describe("delete 5", () => {237 const key1 = {prop: "val1"}238 const key2 = {prop: "val2"}239 runPatchTest(240 {241 x: new Map([242 [key1, 1],243 [key2, 2]244 ])245 },246 d => {247 d.x.delete(key1)248 d.x.delete(key2)249 },250 [251 {op: "remove", path: ["x", key1]},252 {op: "remove", path: ["x", key2]}253 ],254 [255 {op: "add", path: ["x", key1], value: 1},256 {op: "add", path: ["x", key2], value: 2}257 ]258 )259})260261describe("delete 6", () => {262 runPatchTest(263 new Set(["x", 1]),264 d => {265 d.delete("x")266 },267 [{op: "remove", path: [0], value: "x"}],268 [{op: "add", path: [0], value: "x"}]269 )270})271272describe("delete 7", () => {273 runPatchTest(274 {x: new Set(["y", 1])},275 d => {276 d.x.delete("y")277 },278 [{op: "remove", path: ["x", 0], value: "y"}],279 [{op: "add", path: ["x", 0], value: "y"}]280 )281})282283describe("renaming properties", () => {284 describe("nested object (no changes)", () => {285 runPatchTest(286 {a: {b: 1}},287 d => {288 d.x = d.a289 delete d.a290 },291 [292 {op: "add", path: ["x"], value: {b: 1}},293 {op: "remove", path: ["a"]}294 ]295 )296 })297298 describe("nested map (no changes)", () => {299 runPatchTest(300 new Map([["a", new Map([["b", 1]])]]),301 d => {302 d.set("x", d.get("a"))303 d.delete("a")304 },305 [306 {op: "add", path: ["x"], value: new Map([["b", 1]])},307 {op: "remove", path: ["a"]}308 ],309 [310 {op: "remove", path: ["x"]},311 {op: "add", path: ["a"], value: new Map([["b", 1]])}312 ]313 )314 })315316 describe("nested object (with changes)", () => {317 runPatchTest(318 {a: {b: 1, c: 1}},319 d => {320 let a = d.a321 a.b = 2 // change322 delete a.c // delete323 a.y = 2 // add324325 // rename326 d.x = a327 delete d.a328 },329 [330 {op: "add", path: ["x"], value: {b: 2, y: 2}},331 {op: "remove", path: ["a"]}332 ]333 )334 })335336 describe("nested map (with changes)", () => {337 runPatchTest(338 new Map([339 [340 "a",341 new Map([342 ["b", 1],343 ["c", 1]344 ])345 ]346 ]),347 d => {348 let a = d.get("a")349 a.set("b", 2) // change350 a.delete("c") // delete351 a.set("y", 2) // add352353 // rename354 d.set("x", a)355 d.delete("a")356 },357 [358 {359 op: "add",360 path: ["x"],361 value: new Map([362 ["b", 2],363 ["y", 2]364 ])365 },366 {op: "remove", path: ["a"]}367 ],368 [369 {op: "remove", path: ["x"]},370 {371 op: "add",372 path: ["a"],373 value: new Map([374 ["b", 1],375 ["c", 1]376 ])377 }378 ]379 )380 })381382 describe("deeply nested object (with changes)", () => {383 runPatchTest(384 {a: {b: {c: 1, d: 1}}},385 d => {386 let b = d.a.b387 b.c = 2 // change388 delete b.d // delete389 b.y = 2 // add390391 // rename392 d.a.x = b393 delete d.a.b394 },395 [396 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},397 {op: "remove", path: ["a", "b"]}398 ]399 )400 })401402 describe("deeply nested map (with changes)", () => {403 runPatchTest(404 new Map([405 [406 "a",407 new Map([408 [409 "b",410 new Map([411 ["c", 1],412 ["d", 1]413 ])414 ]415 ])416 ]417 ]),418 d => {419 let b = d.get("a").get("b")420 b.set("c", 2) // change421 b.delete("d") // delete422 b.set("y", 2) // add423424 // rename425 d.get("a").set("x", b)426 d.get("a").delete("b")427 },428 [429 {430 op: "add",431 path: ["a", "x"],432 value: new Map([433 ["c", 2],434 ["y", 2]435 ])436 },437 {op: "remove", path: ["a", "b"]}438 ],439 [440 {op: "remove", path: ["a", "x"]},441 {442 op: "add",443 path: ["a", "b"],444 value: new Map([445 ["c", 1],446 ["d", 1]447 ])448 }449 ]450 )451 })452})453454describe("minimum amount of changes", () => {455 runPatchTest(456 {x: 3, y: {a: 4}, z: 3},457 d => {458 d.y.a = 4459 d.y.b = 5460 Object.assign(d, {x: 4, y: {a: 2}})461 },462 [463 {op: "replace", path: ["x"], value: 4},464 {op: "replace", path: ["y"], value: {a: 2}}465 ]466 )467})468469describe("arrays - prepend", () => {470 runPatchTest(471 {x: [1, 2, 3]},472 d => {473 d.x.unshift(4)474 },475 [{op: "add", path: ["x", 0], value: 4}]476 )477})478479describe("arrays - multiple prepend", () => {480 runPatchTest(481 {x: [1, 2, 3]},482 d => {483 d.x.unshift(4)484 d.x.unshift(5)485 },486 [487 {op: "add", path: ["x", 0], value: 5},488 {op: "add", path: ["x", 1], value: 4}489 ]490 )491})492493describe("arrays - splice middle", () => {494 runPatchTest(495 {x: [1, 2, 3]},496 d => {497 d.x.splice(1, 1)498 },499 [{op: "remove", path: ["x", 1]}]500 )501})502503describe("arrays - multiple splice", () => {504 runPatchTest(505 [0, 1, 2, 3, 4, 5, 0],506 d => {507 d.splice(4, 2, 3)508 d.splice(1, 2, 3)509 },510 [511 {op: "replace", path: [1], value: 3},512 {op: "replace", path: [2], value: 3},513 {op: "remove", path: [5]},514 {op: "remove", path: [4]}515 ]516 )517})518519describe("arrays - modify and shrink", () => {520 runPatchTest(521 {x: [1, 2, 3]},522 d => {523 d.x[0] = 4524 d.x.length = 2525 },526 [527 {op: "replace", path: ["x", 0], value: 4},528 {op: "remove", path: ["x", 2]}529 ],530 [531 {op: "replace", path: ["x", 0], value: 1},532 {op: "add", path: ["x", 2], value: 3}533 ]534 )535})536537describe("arrays - prepend then splice middle", () => {538 runPatchTest(539 {x: [1, 2, 3]},540 d => {541 d.x.unshift(4)542 d.x.splice(2, 1)543 },544 [545 {op: "replace", path: ["x", 0], value: 4},546 {op: "replace", path: ["x", 1], value: 1}547 ]548 )549})550551describe("arrays - splice middle then prepend", () => {552 runPatchTest(553 {x: [1, 2, 3]},554 d => {555 d.x.splice(1, 1)556 d.x.unshift(4)557 },558 [559 {op: "replace", path: ["x", 0], value: 4},560 {op: "replace", path: ["x", 1], value: 1}561 ]562 )563})564565describe("arrays - truncate", () => {566 runPatchTest(567 {x: [1, 2, 3]},568 d => {569 d.x.length -= 2570 },571 [572 {op: "remove", path: ["x", 2]},573 {op: "remove", path: ["x", 1]}574 ],575 [576 {op: "add", path: ["x", 1], value: 2},577 {op: "add", path: ["x", 2], value: 3}578 ]579 )580})581582describe("arrays - pop twice", () => {583 runPatchTest(584 {x: [1, 2, 3]},585 d => {586 d.x.pop()587 d.x.pop()588 },589 [590 {op: "remove", path: ["x", 2]},591 {op: "remove", path: ["x", 1]}592 ]593 )594})595596describe("arrays - push multiple", () => {597 runPatchTest(598 {x: [1, 2, 3]},599 d => {600 d.x.push(4, 5)601 },602 [603 {op: "add", path: ["x", 3], value: 4},604 {op: "add", path: ["x", 4], value: 5}605 ],606 [607 {op: "remove", path: ["x", 4]},608 {op: "remove", path: ["x", 3]}609 ]610 )611})612613describe("arrays - splice (expand)", () => {614 runPatchTest(615 {x: [1, 2, 3]},616 d => {617 d.x.splice(1, 1, 4, 5, 6)618 },619 [620 {op: "replace", path: ["x", 1], value: 4},621 {op: "add", path: ["x", 2], value: 5},622 {op: "add", path: ["x", 3], value: 6}623 ],624 [625 {op: "replace", path: ["x", 1], value: 2},626 {op: "remove", path: ["x", 3]},627 {op: "remove", path: ["x", 2]}628 ]629 )630})631632describe("arrays - splice (shrink)", () => {633 runPatchTest(634 {x: [1, 2, 3, 4, 5]},635 d => {636 d.x.splice(1, 3, 6)637 },638 [639 {op: "replace", path: ["x", 1], value: 6},640 {op: "remove", path: ["x", 3]},641 {op: "remove", path: ["x", 2]}642 ],643 [644 {op: "replace", path: ["x", 1], value: 2},645 {op: "add", path: ["x", 2], value: 3},646 {op: "add", path: ["x", 3], value: 4}647 ]648 )649})650651describe("arrays - delete", () => {652 runPatchTest(653 {654 x: [655 {a: 1, b: 2},656 {c: 3, d: 4}657 ]658 },659 d => {660 delete d.x[1].c661 },662 [{op: "remove", path: ["x", 1, "c"]}]663 )664})665666describe("sets - add - 1", () => {667 runPatchTest(668 new Set([1]),669 d => {670 d.add(2)671 },672 [{op: "add", path: [1], value: 2}],673 [{op: "remove", path: [1], value: 2}]674 )675})676677describe("sets - add, delete, add - 1", () => {678 runPatchTest(679 new Set([1]),680 d => {681 d.add(2)682 d.delete(2)683 d.add(2)684 },685 [{op: "add", path: [1], value: 2}],686 [{op: "remove", path: [1], value: 2}]687 )688})689690describe("sets - add, delete, add - 2", () => {691 runPatchTest(692 new Set([2, 1]),693 d => {694 d.add(2)695 d.delete(2)696 d.add(2)697 },698 [],699 []700 )701})702703describe("sets - mutate - 1", () => {704 const findById = (set, id) => {705 for (const item of set) {706 if (item.id === id) return item707 }708 }709 runPatchTest(710 new Set([711 {id: 1, val: "We"},712 {id: 2, val: "will"}713 ]),714 d => {715 const obj1 = findById(d, 1)716 const obj2 = findById(d, 2)717 obj1.val = "rock"718 obj2.val = "you"719 },720 [721 {op: "remove", path: [0], value: {id: 1, val: "We"}},722 {op: "remove", path: [1], value: {id: 2, val: "will"}},723 {op: "add", path: [0], value: {id: 1, val: "rock"}},724 {op: "add", path: [1], value: {id: 2, val: "you"}}725 ],726 [727 {op: "remove", path: [1], value: {id: 2, val: "you"}},728 {op: "remove", path: [0], value: {id: 1, val: "rock"}},729 {op: "add", path: [1], value: {id: 2, val: "will"}},730 {op: "add", path: [0], value: {id: 1, val: "We"}}731 ]732 )733})734735describe("arrays - splice should should result in remove op.", () => {736 runPatchTest(737 [1, 2],738 d => {739 d.splice(1, 1)740 },741 [{op: "remove", path: [1]}],742 [{op: "add", path: [1], value: 2}]743 )744})745746describe("arrays - NESTED splice should should result in remove op.", () => {747 runPatchTest(748 {a: {b: {c: [1, 2]}}},749 d => {750 d.a.b.c.splice(1, 1)751 },752 [{op: "remove", path: ["a", "b", "c", 1]}],753 [{op: "add", path: ["a", "b", "c", 1], value: 2}]754 )755})756757describe("simple replacement", () => {758 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])759})760761describe("same value replacement - 1", () => {762 runPatchTest(763 {x: {y: 3}},764 d => {765 const a = d.x766 d.x = a767 },768 []769 )770})771772describe("same value replacement - 2", () => {773 runPatchTest(774 {x: {y: 3}},775 d => {776 const a = d.x777 d.x = 4778 d.x = a779 },780 []781 )782})783784describe("same value replacement - 3", () => {785 runPatchTest(786 {x: 3},787 d => {788 d.x = 3789 },790 []791 )792})793794describe("same value replacement - 4", () => {795 runPatchTest(796 {x: 3},797 d => {798 d.x = 4799 d.x = 3800 },801 []802 )803})804805describe("same value replacement - 5", () => {806 runPatchTest(807 new Map([["x", 3]]),808 d => {809 d.set("x", 4)810 d.set("x", 3)811 },812 [],813 []814 )815})816817describe("same value replacement - 6", () => {818 runPatchTest(819 new Set(["x", 3]),820 d => {821 d.delete("x")822 d.add("x")823 },824 [],825 []826 )827})828829describe("simple delete", () => {830 runPatchTest(831 {x: 2},832 d => {833 delete d.x834 },835 [836 {837 op: "remove",838 path: ["x"]839 }840 ]841 )842})843844describe("patch compressions yields correct results", () => {845 let p1, p2846 runPatchTest(847 {},848 d => {849 d.x = {test: true}850 },851 (p1 = [852 {853 op: "add",854 path: ["x"],855 value: {856 test: true857 }858 }859 ])860 )861 runPatchTest(862 {x: {test: true}},863 d => {864 delete d.x865 },866 (p2 = [867 {868 op: "remove",869 path: ["x"]870 }871 ])872 )873 const res = runPatchTest(874 {},875 d => {876 applyPatches(d, [...p1, ...p2])877 },878 []879 )880881 expect(res).toEqual({})882})883884describe("change then delete property", () => {885 const res = runPatchTest(886 {887 x: 1888 },889 d => {890 d.x = 2891 delete d.x892 },893 [894 {895 op: "remove",896 path: ["x"]897 }898 ]899 )900 test("valid result", () => {901 expect(res).toEqual({})902 })903})904905test("replaying patches with interweaved replacements should work correctly", () => {906 const patches = []907 const s0 = {x: 1}908909 const s1 = produce(910 s0,911 draft => {912 draft.x = 2913 },914 p => {915 patches.push(...p)916 }917 )918919 const s2 = produce(920 s1,921 draft => {922 return {x: 0}923 },924 p => {925 patches.push(...p)926 }927 )928929 const s3 = produce(930 s2,931 draft => {932 draft.x--933 },934 p => {935 patches.push(...p)936 }937 )938939 expect(s3).toEqual({x: -1}) // correct result940 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay941942 // manual replay on a draft should also be correct943 expect(944 produce(s0, draft => {945 return applyPatches(draft, patches)946 })947 ).toEqual({x: -1})948})949950test.skip("#468", () => {951 const item = {id: 1}952953 const state = [item]954955 const [nextState, patches] = produceWithPatches(state, draft => {956 draft[0].id = 2957 draft[1] = item958 })959960 expect(nextState).toMatchInlineSnapshot(`961 Array [962 Object {963 "id": 2,964 },965 Object {966 "id": 1,967 },968 ]969 `)970 expect(patches).toMatchInlineSnapshot(`971 Array [972 Object {973 "op": "replace",974 "path": Array [975 0,976 "id",977 ],978 "value": 2,979 },980 Object {981 "op": "add",982 "path": Array [983 0,984 ],985 "value": Object {986 "id": 2,987 },988 },989 ]990 `)991992 const final = applyPatches(state, patches)993 expect(final).toEqual(nextState)994})995996test("#521", () => {997 const state = new Map()998999 const [nextState, patches] = produceWithPatches(state, draft => {1000 draft.set("hello", new Set(["world"]))1001 })10021003 let patchedState = applyPatches(state, patches)1004 expect(patchedState).toEqual(nextState)10051006 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1007 draft.get("hello").add("immer")1008 })10091010 expect(applyPatches(nextState, patchesV2)).toEqual(1011 new Map([["hello", new Set(["world", "immer"])]])1012 )1013})