Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

585 lines · 13.7 KiB · JavaScript

Last changed by

fix: Map.prototype.set should return a draftAndrey Goncharovbefore adoption7001201

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("simple assignment - 6", () => {144    runPatchTest(145        new Map([["x", 1]]),146        d => {147            // Map.prototype.set should return the Map itself148            const res = d.set("x", 2)149            res.set("y", 3)150        },151        [152            {op: "replace", path: ["x"], value: 2},153            {op: "add", path: ["y"], value: 3}154        ],155        [{op: "replace", path: ["x"], value: 1}, {op: "remove", path: ["y"]}],156        true157    )158})159160describe("delete 1", () => {161    runPatchTest(162        {x: {y: 4}},163        d => {164            delete d.x165        },166        [{op: "remove", path: ["x"]}]167    )168})169170describe("delete 2", () => {171    runPatchTest(172        new Map([["x", 1]]),173        d => {174            d.delete("x")175        },176        [{op: "remove", path: ["x"]}],177        [{op: "add", path: ["x"], value: 1}],178        true179    )180})181182describe("delete 3", () => {183    runPatchTest(184        {x: new Map([["y", 1]])},185        d => {186            d.x.delete("y")187        },188        [{op: "remove", path: ["x", "y"]}],189        [{op: "add", path: ["x", "y"], value: 1}],190        true191    )192})193194describe("renaming properties", () => {195    describe("nested object (no changes)", () => {196        runPatchTest(197            {a: {b: 1}},198            d => {199                d.x = d.a200                delete d.a201            },202            [203                {op: "add", path: ["x"], value: {b: 1}},204                {op: "remove", path: ["a"]}205            ]206        )207    })208209    describe("nested map (no changes)", () => {210        runPatchTest(211            new Map([["a", new Map([["b", 1]])]]),212            d => {213                d.set("x", d.get("a"))214                d.delete("a")215            },216            [217                {op: "add", path: ["x"], value: new Map([["b", 1]])},218                {op: "remove", path: ["a"]}219            ],220            [221                {op: "remove", path: ["x"]},222                {op: "add", path: ["a"], value: new Map([["b", 1]])}223            ],224            true225        )226    })227228    describe("nested object (with changes)", () => {229        runPatchTest(230            {a: {b: 1, c: 1}},231            d => {232                let a = d.a233                a.b = 2 // change234                delete a.c // delete235                a.y = 2 // add236237                // rename238                d.x = a239                delete d.a240            },241            [242                {op: "add", path: ["x"], value: {b: 2, y: 2}},243                {op: "remove", path: ["a"]}244            ]245        )246    })247248    describe("nested map (with changes)", () => {249        runPatchTest(250            new Map([["a", new Map([["b", 1], ["c", 1]])]]),251            d => {252                let a = d.get("a")253                a.set("b", 2) // change254                a.delete("c") // delete255                a.set("y", 2) // add256257                // rename258                d.set("x", a)259                d.delete("a")260            },261            [262                {op: "add", path: ["x"], value: new Map([["b", 2], ["y", 2]])},263                {op: "remove", path: ["a"]}264            ],265            [266                {op: "remove", path: ["x"]},267                {op: "add", path: ["a"], value: new Map([["b", 1], ["c", 1]])}268            ],269            true270        )271    })272273    describe("deeply nested object (with changes)", () => {274        runPatchTest(275            {a: {b: {c: 1, d: 1}}},276            d => {277                let b = d.a.b278                b.c = 2 // change279                delete b.d // delete280                b.y = 2 // add281282                // rename283                d.a.x = b284                delete d.a.b285            },286            [287                {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},288                {op: "remove", path: ["a", "b"]}289            ]290        )291    })292293    describe("deeply nested map (with changes)", () => {294        runPatchTest(295            new Map([["a", new Map([["b", new Map([["c", 1], ["d", 1]])]])]]),296            d => {297                let b = d.get("a").get("b")298                b.set("c", 2) // change299                b.delete("d") // delete300                b.set("y", 2) // add301302                // rename303                d.get("a").set("x", b)304                d.get("a").delete("b")305            },306            [307                {308                    op: "add",309                    path: ["a", "x"],310                    value: new Map([["c", 2], ["y", 2]])311                },312                {op: "remove", path: ["a", "b"]}313            ],314            [315                {op: "remove", path: ["a", "x"]},316                {317                    op: "add",318                    path: ["a", "b"],319                    value: new Map([["c", 1], ["d", 1]])320                }321            ],322            true323        )324    })325})326327describe("minimum amount of changes", () => {328    runPatchTest(329        {x: 3, y: {a: 4}, z: 3},330        d => {331            d.y.a = 4332            d.y.b = 5333            Object.assign(d, {x: 4, y: {a: 2}})334        },335        [336            {op: "replace", path: ["x"], value: 4},337            {op: "replace", path: ["y"], value: {a: 2}}338        ]339    )340})341342describe("arrays - prepend", () => {343    runPatchTest(344        {x: [1, 2, 3]},345        d => {346            d.x.unshift(4)347        },348        [{op: "add", path: ["x", 0], value: 4}]349    )350})351352describe("arrays - multiple prepend", () => {353    runPatchTest(354        {x: [1, 2, 3]},355        d => {356            d.x.unshift(4)357            d.x.unshift(5)358        },359        [360            {op: "add", path: ["x", 0], value: 5},361            {op: "add", path: ["x", 1], value: 4}362        ]363    )364})365366describe("arrays - splice middle", () => {367    runPatchTest(368        {x: [1, 2, 3]},369        d => {370            d.x.splice(1, 1)371        },372        [{op: "remove", path: ["x", 1]}]373    )374})375376describe("arrays - multiple splice", () => {377    runPatchTest(378        [0, 1, 2, 3, 4, 5, 0],379        d => {380            d.splice(4, 2, 3)381            d.splice(1, 2, 3)382        },383        [384            {op: "replace", path: [1], value: 3},385            {op: "replace", path: [2], value: 3},386            {op: "remove", path: [5]},387            {op: "remove", path: [4]}388        ]389    )390})391392describe("arrays - modify and shrink", () => {393    runPatchTest(394        {x: [1, 2, 3]},395        d => {396            d.x[0] = 4397            d.x.length = 2398        },399        [400            {op: "replace", path: ["x", 0], value: 4},401            {op: "replace", path: ["x", "length"], value: 2}402        ]403    )404})405406describe("arrays - prepend then splice middle", () => {407    runPatchTest(408        {x: [1, 2, 3]},409        d => {410            d.x.unshift(4)411            d.x.splice(2, 1)412        },413        [414            {op: "replace", path: ["x", 0], value: 4},415            {op: "replace", path: ["x", 1], value: 1}416        ]417    )418})419420describe("arrays - splice middle then prepend", () => {421    runPatchTest(422        {x: [1, 2, 3]},423        d => {424            d.x.splice(1, 1)425            d.x.unshift(4)426        },427        [428            {op: "replace", path: ["x", 0], value: 4},429            {op: "replace", path: ["x", 1], value: 1}430        ]431    )432})433434describe("arrays - truncate", () => {435    runPatchTest(436        {x: [1, 2, 3]},437        d => {438            d.x.length -= 2439        },440        [{op: "replace", path: ["x", "length"], value: 1}],441        [442            {op: "add", path: ["x", 1], value: 2},443            {op: "add", path: ["x", 2], value: 3}444        ]445    )446})447448describe("arrays - pop twice", () => {449    runPatchTest(450        {x: [1, 2, 3]},451        d => {452            d.x.pop()453            d.x.pop()454        },455        [{op: "replace", path: ["x", "length"], value: 1}]456    )457})458459describe("arrays - push multiple", () => {460    runPatchTest(461        {x: [1, 2, 3]},462        d => {463            d.x.push(4, 5)464        },465        [466            {op: "add", path: ["x", 3], value: 4},467            {op: "add", path: ["x", 4], value: 5}468        ],469        [{op: "replace", path: ["x", "length"], value: 3}]470    )471})472473describe("arrays - splice (expand)", () => {474    runPatchTest(475        {x: [1, 2, 3]},476        d => {477            d.x.splice(1, 1, 4, 5, 6)478        },479        [480            {op: "replace", path: ["x", 1], value: 4},481            {op: "add", path: ["x", 2], value: 5},482            {op: "add", path: ["x", 3], value: 6}483        ],484        [485            {op: "replace", path: ["x", 1], value: 2},486            {op: "remove", path: ["x", 3]},487            {op: "remove", path: ["x", 2]}488        ]489    )490})491492describe("arrays - splice (shrink)", () => {493    runPatchTest(494        {x: [1, 2, 3, 4, 5]},495        d => {496            d.x.splice(1, 3, 6)497        },498        [499            {op: "replace", path: ["x", 1], value: 6},500            {op: "remove", path: ["x", 3]},501            {op: "remove", path: ["x", 2]}502        ],503        [504            {op: "replace", path: ["x", 1], value: 2},505            {op: "add", path: ["x", 2], value: 3},506            {op: "add", path: ["x", 3], value: 4}507        ]508    )509})510511describe("simple replacement", () => {512    runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])513})514515describe("same value replacement - 1", () => {516    runPatchTest(517        {x: {y: 3}},518        d => {519            const a = d.x520            d.x = a521        },522        []523    )524})525526describe("same value replacement - 2", () => {527    runPatchTest(528        {x: {y: 3}},529        d => {530            const a = d.x531            d.x = 4532            d.x = a533        },534        []535    )536})537538describe("same value replacement - 3", () => {539    runPatchTest(540        {x: 3},541        d => {542            d.x = 3543        },544        []545    )546})547548describe("same value replacement - 4", () => {549    runPatchTest(550        {x: 3},551        d => {552            d.x = 4553            d.x = 3554        },555        []556    )557})558559describe("same value replacement - 5", () => {560    runPatchTest(561        new Map([["x", 3]]),562        d => {563            d.set("x", 4)564            d.set("x", 3)565        },566        [],567        [],568        true569    )570})571572describe("simple delete", () => {573    runPatchTest(574        {x: 2},575        d => {576            delete d.x577        },578        [579            {580                op: "remove",581                path: ["x"]582            }583        ]584    )585})