Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

693 lines · 16.1 KiB · JavaScript

Last changed by

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

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 recordedInversePatches1617        const res = produce(base, producer, (p, i) => {18            recordedPatches = p19            recordedInversePatches = i20        })2122        test("produces the correct patches", () => {23            expect(recordedPatches).toEqual(patches)24            if (inversePathes)25                expect(recordedInversePatches).toEqual(inversePathes)26        })2728        test("patches are replayable", () => {29            expect(applyPatches(base, recordedPatches)).toEqual(res)30        })3132        test("patches can be reversed", () => {33            expect(applyPatches(res, recordedInversePatches)).toEqual(base)34        })35    }3637    describe(`proxy`, () => {38        beforeAll(() => setUseProxies(true))39        runPatchTestHelper()40    })4142    describe(`es5`, () => {43        if (!proxyOnly) {44            beforeAll(() => setUseProxies(false))45            runPatchTestHelper()46        }47    })48}4950describe("applyPatches", () => {51    it("mutates the base state when it is a draft", () => {52        produce({a: 1}, draft => {53            const result = applyPatches(draft, [54                {op: "replace", path: ["a"], value: 2}55            ])56            expect(result).toBe(draft)57            expect(draft.a).toBe(2)58        })59    })60    it("produces a copy of the base state when not a draft", () => {61        const base = {a: 1}62        const result = applyPatches(base, [63            {op: "replace", path: ["a"], value: 2}64        ])65        expect(result).not.toBe(base)66        expect(result.a).toBe(2)67        expect(base.a).toBe(1)68    })69    it('throws when `op` is not "add", "replace", nor "remove"', () => {70        expect(() => {71            const patch = {op: "copy", from: [0], path: [1]}72            applyPatches([2], [patch])73        }).toThrowErrorMatchingSnapshot()74    })75    it("throws when `path` cannot be resolved", () => {76        // missing parent77        expect(() => {78            const patch = {op: "add", path: ["a", "b"], value: 1}79            applyPatches({}, [patch])80        }).toThrowErrorMatchingSnapshot()8182        // missing grand-parent83        expect(() => {84            const patch = {op: "add", path: ["a", "b", "c"], value: 1}85            applyPatches({}, [patch])86        }).toThrowErrorMatchingSnapshot()87    })88})8990describe("simple assignment - 1", () => {91    runPatchTest(92        {x: 3},93        d => {94            d.x++95        },96        [{op: "replace", path: ["x"], value: 4}]97    )98})99100describe("simple assignment - 2", () => {101    runPatchTest(102        {x: {y: 4}},103        d => {104            d.x.y++105        },106        [{op: "replace", path: ["x", "y"], value: 5}]107    )108})109110describe("simple assignment - 3", () => {111    runPatchTest(112        {x: [{y: 4}]},113        d => {114            d.x[0].y++115        },116        [{op: "replace", path: ["x", 0, "y"], value: 5}]117    )118})119120describe("simple assignment - 4", () => {121    runPatchTest(122        new Map([["x", {y: 4}]]),123        d => {124            d.get("x").y++125        },126        [{op: "replace", path: ["x", "y"], value: 5}],127        [{op: "replace", path: ["x", "y"], value: 4}],128        true129    )130})131132describe("simple assignment - 5", () => {133    runPatchTest(134        {x: new Map([["y", 4]])},135        d => {136            d.x.set("y", 5)137        },138        [{op: "replace", path: ["x", "y"], value: 5}],139        [{op: "replace", path: ["x", "y"], value: 4}],140        true141    )142})143144describe("simple assignment - 6", () => {145    runPatchTest(146        new Map([["x", 1]]),147        d => {148            // Map.prototype.set should return the Map itself149            const res = d.set("x", 2)150            res.set("y", 3)151        },152        [153            {op: "replace", path: ["x"], value: 2},154            {op: "add", path: ["y"], value: 3}155        ],156        [{op: "replace", path: ["x"], value: 1}, {op: "remove", path: ["y"]}],157        true158    )159})160161describe("delete 1", () => {162    runPatchTest(163        {x: {y: 4}},164        d => {165            delete d.x166        },167        [{op: "remove", path: ["x"]}]168    )169})170171describe("delete 2", () => {172    runPatchTest(173        new Map([["x", 1]]),174        d => {175            d.delete("x")176        },177        [{op: "remove", path: ["x"]}],178        [{op: "add", path: ["x"], value: 1}],179        true180    )181})182183describe("delete 3", () => {184    runPatchTest(185        {x: new Map([["y", 1]])},186        d => {187            d.x.delete("y")188        },189        [{op: "remove", path: ["x", "y"]}],190        [{op: "add", path: ["x", "y"], value: 1}],191        true192    )193})194195describe("delete 4", () => {196    runPatchTest(197        new Set(["x", 1]),198        d => {199            d.delete("x")200        },201        [{op: "remove", path: [0], value: "x"}],202        [{op: "add", path: [0], value: "x"}],203        true204    )205})206207describe("delete 5", () => {208    runPatchTest(209        {x: new Set(["y", 1])},210        d => {211            d.x.delete("y")212        },213        [{op: "remove", path: ["x", 0], value: "y"}],214        [{op: "add", path: ["x", 0], value: "y"}],215        true216    )217})218219describe("renaming properties", () => {220    describe("nested object (no changes)", () => {221        runPatchTest(222            {a: {b: 1}},223            d => {224                d.x = d.a225                delete d.a226            },227            [228                {op: "add", path: ["x"], value: {b: 1}},229                {op: "remove", path: ["a"]}230            ]231        )232    })233234    describe("nested map (no changes)", () => {235        runPatchTest(236            new Map([["a", new Map([["b", 1]])]]),237            d => {238                d.set("x", d.get("a"))239                d.delete("a")240            },241            [242                {op: "add", path: ["x"], value: new Map([["b", 1]])},243                {op: "remove", path: ["a"]}244            ],245            [246                {op: "remove", path: ["x"]},247                {op: "add", path: ["a"], value: new Map([["b", 1]])}248            ],249            true250        )251    })252253    describe("nested object (with changes)", () => {254        runPatchTest(255            {a: {b: 1, c: 1}},256            d => {257                let a = d.a258                a.b = 2 // change259                delete a.c // delete260                a.y = 2 // add261262                // rename263                d.x = a264                delete d.a265            },266            [267                {op: "add", path: ["x"], value: {b: 2, y: 2}},268                {op: "remove", path: ["a"]}269            ]270        )271    })272273    describe("nested map (with changes)", () => {274        runPatchTest(275            new Map([["a", new Map([["b", 1], ["c", 1]])]]),276            d => {277                let a = d.get("a")278                a.set("b", 2) // change279                a.delete("c") // delete280                a.set("y", 2) // add281282                // rename283                d.set("x", a)284                d.delete("a")285            },286            [287                {op: "add", path: ["x"], value: new Map([["b", 2], ["y", 2]])},288                {op: "remove", path: ["a"]}289            ],290            [291                {op: "remove", path: ["x"]},292                {op: "add", path: ["a"], value: new Map([["b", 1], ["c", 1]])}293            ],294            true295        )296    })297298    describe("deeply nested object (with changes)", () => {299        runPatchTest(300            {a: {b: {c: 1, d: 1}}},301            d => {302                let b = d.a.b303                b.c = 2 // change304                delete b.d // delete305                b.y = 2 // add306307                // rename308                d.a.x = b309                delete d.a.b310            },311            [312                {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},313                {op: "remove", path: ["a", "b"]}314            ]315        )316    })317318    describe("deeply nested map (with changes)", () => {319        runPatchTest(320            new Map([["a", new Map([["b", new Map([["c", 1], ["d", 1]])]])]]),321            d => {322                let b = d.get("a").get("b")323                b.set("c", 2) // change324                b.delete("d") // delete325                b.set("y", 2) // add326327                // rename328                d.get("a").set("x", b)329                d.get("a").delete("b")330            },331            [332                {333                    op: "add",334                    path: ["a", "x"],335                    value: new Map([["c", 2], ["y", 2]])336                },337                {op: "remove", path: ["a", "b"]}338            ],339            [340                {op: "remove", path: ["a", "x"]},341                {342                    op: "add",343                    path: ["a", "b"],344                    value: new Map([["c", 1], ["d", 1]])345                }346            ],347            true348        )349    })350})351352describe("minimum amount of changes", () => {353    runPatchTest(354        {x: 3, y: {a: 4}, z: 3},355        d => {356            d.y.a = 4357            d.y.b = 5358            Object.assign(d, {x: 4, y: {a: 2}})359        },360        [361            {op: "replace", path: ["x"], value: 4},362            {op: "replace", path: ["y"], value: {a: 2}}363        ]364    )365})366367describe("arrays - prepend", () => {368    runPatchTest(369        {x: [1, 2, 3]},370        d => {371            d.x.unshift(4)372        },373        [{op: "add", path: ["x", 0], value: 4}]374    )375})376377describe("arrays - multiple prepend", () => {378    runPatchTest(379        {x: [1, 2, 3]},380        d => {381            d.x.unshift(4)382            d.x.unshift(5)383        },384        [385            {op: "add", path: ["x", 0], value: 5},386            {op: "add", path: ["x", 1], value: 4}387        ]388    )389})390391describe("arrays - splice middle", () => {392    runPatchTest(393        {x: [1, 2, 3]},394        d => {395            d.x.splice(1, 1)396        },397        [{op: "remove", path: ["x", 1]}]398    )399})400401describe("arrays - multiple splice", () => {402    runPatchTest(403        [0, 1, 2, 3, 4, 5, 0],404        d => {405            d.splice(4, 2, 3)406            d.splice(1, 2, 3)407        },408        [409            {op: "replace", path: [1], value: 3},410            {op: "replace", path: [2], value: 3},411            {op: "remove", path: [5]},412            {op: "remove", path: [4]}413        ]414    )415})416417describe("arrays - modify and shrink", () => {418    runPatchTest(419        {x: [1, 2, 3]},420        d => {421            d.x[0] = 4422            d.x.length = 2423        },424        [425            {op: "replace", path: ["x", 0], value: 4},426            {op: "replace", path: ["x", "length"], value: 2}427        ]428    )429})430431describe("arrays - prepend then splice middle", () => {432    runPatchTest(433        {x: [1, 2, 3]},434        d => {435            d.x.unshift(4)436            d.x.splice(2, 1)437        },438        [439            {op: "replace", path: ["x", 0], value: 4},440            {op: "replace", path: ["x", 1], value: 1}441        ]442    )443})444445describe("arrays - splice middle then prepend", () => {446    runPatchTest(447        {x: [1, 2, 3]},448        d => {449            d.x.splice(1, 1)450            d.x.unshift(4)451        },452        [453            {op: "replace", path: ["x", 0], value: 4},454            {op: "replace", path: ["x", 1], value: 1}455        ]456    )457})458459describe("arrays - truncate", () => {460    runPatchTest(461        {x: [1, 2, 3]},462        d => {463            d.x.length -= 2464        },465        [{op: "replace", path: ["x", "length"], value: 1}],466        [467            {op: "add", path: ["x", 1], value: 2},468            {op: "add", path: ["x", 2], value: 3}469        ]470    )471})472473describe("arrays - pop twice", () => {474    runPatchTest(475        {x: [1, 2, 3]},476        d => {477            d.x.pop()478            d.x.pop()479        },480        [{op: "replace", path: ["x", "length"], value: 1}]481    )482})483484describe("arrays - push multiple", () => {485    runPatchTest(486        {x: [1, 2, 3]},487        d => {488            d.x.push(4, 5)489        },490        [491            {op: "add", path: ["x", 3], value: 4},492            {op: "add", path: ["x", 4], value: 5}493        ],494        [{op: "replace", path: ["x", "length"], value: 3}]495    )496})497498describe("arrays - splice (expand)", () => {499    runPatchTest(500        {x: [1, 2, 3]},501        d => {502            d.x.splice(1, 1, 4, 5, 6)503        },504        [505            {op: "replace", path: ["x", 1], value: 4},506            {op: "add", path: ["x", 2], value: 5},507            {op: "add", path: ["x", 3], value: 6}508        ],509        [510            {op: "replace", path: ["x", 1], value: 2},511            {op: "remove", path: ["x", 3]},512            {op: "remove", path: ["x", 2]}513        ]514    )515})516517describe("arrays - splice (shrink)", () => {518    runPatchTest(519        {x: [1, 2, 3, 4, 5]},520        d => {521            d.x.splice(1, 3, 6)522        },523        [524            {op: "replace", path: ["x", 1], value: 6},525            {op: "remove", path: ["x", 3]},526            {op: "remove", path: ["x", 2]}527        ],528        [529            {op: "replace", path: ["x", 1], value: 2},530            {op: "add", path: ["x", 2], value: 3},531            {op: "add", path: ["x", 3], value: 4}532        ]533    )534})535536describe("sets - add - 1", () => {537    runPatchTest(538        new Set([1]),539        d => {540            d.add(2)541        },542        [{op: "add", path: [1], value: 2}],543        [{op: "remove", path: [1], value: 2}],544        true545    )546})547548describe("sets - add, delete, add - 1", () => {549    runPatchTest(550        new Set([1]),551        d => {552            d.add(2)553            d.delete(2)554            d.add(2)555        },556        [{op: "add", path: [1], value: 2}],557        [{op: "remove", path: [1], value: 2}],558        true559    )560})561562describe("sets - add, delete, add - 2", () => {563    runPatchTest(564        new Set([2, 1]),565        d => {566            d.add(2)567            d.delete(2)568            d.add(2)569        },570        [],571        [],572        true573    )574})575576describe("sets - mutate - 1", () => {577    const findById = (set, id) => {578        for (const item of set) {579            if (item.id === id) return item580        }581    }582    runPatchTest(583        new Set([{id: 1, val: "We"}, {id: 2, val: "will"}]),584        d => {585            const obj1 = findById(d, 1)586            const obj2 = findById(d, 2)587            obj1.val = "rock"588            obj2.val = "you"589        },590        [591            {op: "remove", path: [0], value: {id: 1, val: "We"}},592            {op: "remove", path: [1], value: {id: 2, val: "will"}},593            {op: "add", path: [0], value: {id: 1, val: "rock"}},594            {op: "add", path: [1], value: {id: 2, val: "you"}}595        ],596        [597            {op: "remove", path: [1], value: {id: 2, val: "you"}},598            {op: "remove", path: [0], value: {id: 1, val: "rock"}},599            {op: "add", path: [1], value: {id: 2, val: "will"}},600            {op: "add", path: [0], value: {id: 1, val: "We"}}601        ],602        true603    )604})605606describe("simple replacement", () => {607    runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])608})609610describe("same value replacement - 1", () => {611    runPatchTest(612        {x: {y: 3}},613        d => {614            const a = d.x615            d.x = a616        },617        []618    )619})620621describe("same value replacement - 2", () => {622    runPatchTest(623        {x: {y: 3}},624        d => {625            const a = d.x626            d.x = 4627            d.x = a628        },629        []630    )631})632633describe("same value replacement - 3", () => {634    runPatchTest(635        {x: 3},636        d => {637            d.x = 3638        },639        []640    )641})642643describe("same value replacement - 4", () => {644    runPatchTest(645        {x: 3},646        d => {647            d.x = 4648            d.x = 3649        },650        []651    )652})653654describe("same value replacement - 5", () => {655    runPatchTest(656        new Map([["x", 3]]),657        d => {658            d.set("x", 4)659            d.set("x", 3)660        },661        [],662        [],663        true664    )665})666667describe("same value replacement - 6", () => {668    runPatchTest(669        new Set(["x", 3]),670        d => {671            d.delete("x")672            d.add("x")673        },674        [],675        [],676        true677    )678})679680describe("simple delete", () => {681    runPatchTest(682        {x: 2},683        d => {684            delete d.x685        },686        [687            {688                op: "remove",689                path: ["x"]690            }691        ]692    )693})