Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

568 lines · 13.2 KiB · JavaScript

Last changed by

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

History of this file

Take it

1"use strict"2import produce, {setUseProxies, applyPatches} from "../src/index"34jest.setTimeout(1000)56function runPatchTest(7    base,8    producer,9    patches,10    inversePathes,11    proxyOnly = false12) {13    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)24                expect(recordedInversePatches).toEqual(inversePathes)25        })2627        test("patches are replayable", () => {28            expect(applyPatches(base, recordedPatches)).toEqual(res)29        })3031        test("patches can be reversed", () => {32            expect(applyPatches(res, recordedInversePatches)).toEqual(base)33        })34    }3536    describe(`proxy`, () => {37        beforeAll(() => setUseProxies(true))38        runPatchTestHelper()39    })4041    describe(`es5`, () => {42        if (!proxyOnly) {43            beforeAll(() => setUseProxies(false))44            runPatchTestHelper()45        }46    })47}4849describe("applyPatches", () => {50    it("mutates the base state when it is a draft", () => {51        produce({a: 1}, draft => {52            const result = applyPatches(draft, [53                {op: "replace", path: ["a"], value: 2}54            ])55            expect(result).toBe(draft)56            expect(draft.a).toBe(2)57        })58    })59    it("produces a copy of the base state when not a draft", () => {60        const base = {a: 1}61        const result = applyPatches(base, [62            {op: "replace", path: ["a"], value: 2}63        ])64        expect(result).not.toBe(base)65        expect(result.a).toBe(2)66        expect(base.a).toBe(1)67    })68    it('throws when `op` is not "add", "replace", nor "remove"', () => {69        expect(() => {70            const patch = {op: "copy", from: [0], path: [1]}71            applyPatches([2], [patch])72        }).toThrowErrorMatchingSnapshot()73    })74    it("throws when `path` cannot be resolved", () => {75        // missing parent76        expect(() => {77            const patch = {op: "add", path: ["a", "b"], value: 1}78            applyPatches({}, [patch])79        }).toThrowErrorMatchingSnapshot()8081        // missing grand-parent82        expect(() => {83            const patch = {op: "add", path: ["a", "b", "c"], value: 1}84            applyPatches({}, [patch])85        }).toThrowErrorMatchingSnapshot()86    })87})8889describe("simple assignment - 1", () => {90    runPatchTest(91        {x: 3},92        d => {93            d.x++94        },95        [{op: "replace", path: ["x"], value: 4}]96    )97})9899describe("simple assignment - 2", () => {100    runPatchTest(101        {x: {y: 4}},102        d => {103            d.x.y++104        },105        [{op: "replace", path: ["x", "y"], value: 5}]106    )107})108109describe("simple assignment - 3", () => {110    runPatchTest(111        {x: [{y: 4}]},112        d => {113            d.x[0].y++114        },115        [{op: "replace", path: ["x", 0, "y"], value: 5}]116    )117})118119describe("simple assignment - 4", () => {120    runPatchTest(121        new Map([["x", {y: 4}]]),122        d => {123            d.get("x").y++124        },125        [{op: "replace", path: ["x", "y"], value: 5}],126        [{op: "replace", path: ["x", "y"], value: 4}],127        true128    )129})130131describe("simple assignment - 5", () => {132    runPatchTest(133        {x: new Map([["y", 4]])},134        d => {135            d.x.set("y", 5)136        },137        [{op: "replace", path: ["x", "y"], value: 5}],138        [{op: "replace", path: ["x", "y"], value: 4}],139        true140    )141})142143describe("delete 1", () => {144    runPatchTest(145        {x: {y: 4}},146        d => {147            delete d.x148        },149        [{op: "remove", path: ["x"]}]150    )151})152153describe("delete 2", () => {154    runPatchTest(155        new Map([["x", 1]]),156        d => {157            d.delete("x")158        },159        [{op: "remove", path: ["x"]}],160        [{op: "add", path: ["x"], value: 1}],161        true162    )163})164165describe("delete 3", () => {166    runPatchTest(167        {x: new Map([["y", 1]])},168        d => {169            d.x.delete("y")170        },171        [{op: "remove", path: ["x", "y"]}],172        [{op: "add", path: ["x", "y"], value: 1}],173        true174    )175})176177describe("renaming properties", () => {178    describe("nested object (no changes)", () => {179        runPatchTest(180            {a: {b: 1}},181            d => {182                d.x = d.a183                delete d.a184            },185            [186                {op: "add", path: ["x"], value: {b: 1}},187                {op: "remove", path: ["a"]}188            ]189        )190    })191192    describe("nested map (no changes)", () => {193        runPatchTest(194            new Map([["a", new Map([["b", 1]])]]),195            d => {196                d.set("x", d.get("a"))197                d.delete("a")198            },199            [200                {op: "add", path: ["x"], value: new Map([["b", 1]])},201                {op: "remove", path: ["a"]}202            ],203            [204                {op: "remove", path: ["x"]},205                {op: "add", path: ["a"], value: new Map([["b", 1]])}206            ],207            true208        )209    })210211    describe("nested object (with changes)", () => {212        runPatchTest(213            {a: {b: 1, c: 1}},214            d => {215                let a = d.a216                a.b = 2 // change217                delete a.c // delete218                a.y = 2 // add219220                // rename221                d.x = a222                delete d.a223            },224            [225                {op: "add", path: ["x"], value: {b: 2, y: 2}},226                {op: "remove", path: ["a"]}227            ]228        )229    })230231    describe("nested map (with changes)", () => {232        runPatchTest(233            new Map([["a", new Map([["b", 1], ["c", 1]])]]),234            d => {235                let a = d.get("a")236                a.set("b", 2) // change237                a.delete("c") // delete238                a.set("y", 2) // add239240                // rename241                d.set("x", a)242                d.delete("a")243            },244            [245                {op: "add", path: ["x"], value: new Map([["b", 2], ["y", 2]])},246                {op: "remove", path: ["a"]}247            ],248            [249                {op: "remove", path: ["x"]},250                {op: "add", path: ["a"], value: new Map([["b", 1], ["c", 1]])}251            ],252            true253        )254    })255256    describe("deeply nested object (with changes)", () => {257        runPatchTest(258            {a: {b: {c: 1, d: 1}}},259            d => {260                let b = d.a.b261                b.c = 2 // change262                delete b.d // delete263                b.y = 2 // add264265                // rename266                d.a.x = b267                delete d.a.b268            },269            [270                {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},271                {op: "remove", path: ["a", "b"]}272            ]273        )274    })275276    describe("deeply nested map (with changes)", () => {277        runPatchTest(278            new Map([["a", new Map([["b", new Map([["c", 1], ["d", 1]])]])]]),279            d => {280                let b = d.get("a").get("b")281                b.set("c", 2) // change282                b.delete("d") // delete283                b.set("y", 2) // add284285                // rename286                d.get("a").set("x", b)287                d.get("a").delete("b")288            },289            [290                {291                    op: "add",292                    path: ["a", "x"],293                    value: new Map([["c", 2], ["y", 2]])294                },295                {op: "remove", path: ["a", "b"]}296            ],297            [298                {op: "remove", path: ["a", "x"]},299                {300                    op: "add",301                    path: ["a", "b"],302                    value: new Map([["c", 1], ["d", 1]])303                }304            ],305            true306        )307    })308})309310describe("minimum amount of changes", () => {311    runPatchTest(312        {x: 3, y: {a: 4}, z: 3},313        d => {314            d.y.a = 4315            d.y.b = 5316            Object.assign(d, {x: 4, y: {a: 2}})317        },318        [319            {op: "replace", path: ["x"], value: 4},320            {op: "replace", path: ["y"], value: {a: 2}}321        ]322    )323})324325describe("arrays - prepend", () => {326    runPatchTest(327        {x: [1, 2, 3]},328        d => {329            d.x.unshift(4)330        },331        [{op: "add", path: ["x", 0], value: 4}]332    )333})334335describe("arrays - multiple prepend", () => {336    runPatchTest(337        {x: [1, 2, 3]},338        d => {339            d.x.unshift(4)340            d.x.unshift(5)341        },342        [343            {op: "add", path: ["x", 0], value: 5},344            {op: "add", path: ["x", 1], value: 4}345        ]346    )347})348349describe("arrays - splice middle", () => {350    runPatchTest(351        {x: [1, 2, 3]},352        d => {353            d.x.splice(1, 1)354        },355        [{op: "remove", path: ["x", 1]}]356    )357})358359describe("arrays - multiple splice", () => {360    runPatchTest(361        [0, 1, 2, 3, 4, 5, 0],362        d => {363            d.splice(4, 2, 3)364            d.splice(1, 2, 3)365        },366        [367            {op: "replace", path: [1], value: 3},368            {op: "replace", path: [2], value: 3},369            {op: "remove", path: [5]},370            {op: "remove", path: [4]}371        ]372    )373})374375describe("arrays - modify and shrink", () => {376    runPatchTest(377        {x: [1, 2, 3]},378        d => {379            d.x[0] = 4380            d.x.length = 2381        },382        [383            {op: "replace", path: ["x", 0], value: 4},384            {op: "replace", path: ["x", "length"], value: 2}385        ]386    )387})388389describe("arrays - prepend then splice middle", () => {390    runPatchTest(391        {x: [1, 2, 3]},392        d => {393            d.x.unshift(4)394            d.x.splice(2, 1)395        },396        [397            {op: "replace", path: ["x", 0], value: 4},398            {op: "replace", path: ["x", 1], value: 1}399        ]400    )401})402403describe("arrays - splice middle then prepend", () => {404    runPatchTest(405        {x: [1, 2, 3]},406        d => {407            d.x.splice(1, 1)408            d.x.unshift(4)409        },410        [411            {op: "replace", path: ["x", 0], value: 4},412            {op: "replace", path: ["x", 1], value: 1}413        ]414    )415})416417describe("arrays - truncate", () => {418    runPatchTest(419        {x: [1, 2, 3]},420        d => {421            d.x.length -= 2422        },423        [{op: "replace", path: ["x", "length"], value: 1}],424        [425            {op: "add", path: ["x", 1], value: 2},426            {op: "add", path: ["x", 2], value: 3}427        ]428    )429})430431describe("arrays - pop twice", () => {432    runPatchTest(433        {x: [1, 2, 3]},434        d => {435            d.x.pop()436            d.x.pop()437        },438        [{op: "replace", path: ["x", "length"], value: 1}]439    )440})441442describe("arrays - push multiple", () => {443    runPatchTest(444        {x: [1, 2, 3]},445        d => {446            d.x.push(4, 5)447        },448        [449            {op: "add", path: ["x", 3], value: 4},450            {op: "add", path: ["x", 4], value: 5}451        ],452        [{op: "replace", path: ["x", "length"], value: 3}]453    )454})455456describe("arrays - splice (expand)", () => {457    runPatchTest(458        {x: [1, 2, 3]},459        d => {460            d.x.splice(1, 1, 4, 5, 6)461        },462        [463            {op: "replace", path: ["x", 1], value: 4},464            {op: "add", path: ["x", 2], value: 5},465            {op: "add", path: ["x", 3], value: 6}466        ],467        [468            {op: "replace", path: ["x", 1], value: 2},469            {op: "remove", path: ["x", 3]},470            {op: "remove", path: ["x", 2]}471        ]472    )473})474475describe("arrays - splice (shrink)", () => {476    runPatchTest(477        {x: [1, 2, 3, 4, 5]},478        d => {479            d.x.splice(1, 3, 6)480        },481        [482            {op: "replace", path: ["x", 1], value: 6},483            {op: "remove", path: ["x", 3]},484            {op: "remove", path: ["x", 2]}485        ],486        [487            {op: "replace", path: ["x", 1], value: 2},488            {op: "add", path: ["x", 2], value: 3},489            {op: "add", path: ["x", 3], value: 4}490        ]491    )492})493494describe("simple replacement", () => {495    runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])496})497498describe("same value replacement - 1", () => {499    runPatchTest(500        {x: {y: 3}},501        d => {502            const a = d.x503            d.x = a504        },505        []506    )507})508509describe("same value replacement - 2", () => {510    runPatchTest(511        {x: {y: 3}},512        d => {513            const a = d.x514            d.x = 4515            d.x = a516        },517        []518    )519})520521describe("same value replacement - 3", () => {522    runPatchTest(523        {x: 3},524        d => {525            d.x = 3526        },527        []528    )529})530531describe("same value replacement - 4", () => {532    runPatchTest(533        {x: 3},534        d => {535            d.x = 4536            d.x = 3537        },538        []539    )540})541542describe("same value replacement - 5", () => {543    runPatchTest(544        new Map([["x", 3]]),545        d => {546            d.set("x", 4)547            d.set("x", 3)548        },549        [],550        [],551        true552    )553})554555describe("simple delete", () => {556    runPatchTest(557        {x: 2},558        d => {559            delete d.x560        },561        [562            {563                op: "remove",564                path: ["x"]565            }566        ]567    )568})