patch.js
This file
Path
__tests__/patch.js
422 lines · 9.6 KiB · JavaScript
Last changed by
test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
History of this fileTake it
Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 1why, one step back1"use strict"chore: update import paths in testsaleclarsonbefore adoptioncdc09b1
line 2why, one step back2import produce, {setUseProxies, applyPatches} from "../src/index"Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 3–24why, one step back34jest.setTimeout(1000)56function runPatchTest(base, producer, patches, inversePathes) {7 function runPatchTestHelper() {8 let recordedPatches9 let recordedInversePatches10 const res = produce(base, producer, (p, i) => {11 recordedPatches = p12 recordedInversePatches = i13 })1415 test("produces the correct patches", () => {16 expect(recordedPatches).toEqual(patches)17 if (inversePathes)18 expect(recordedInversePatches).toEqual(inversePathes)19 })2021 test("patches are replayable", () => {22 expect(applyPatches(base, recordedPatches)).toEqual(res)23 })24test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 25why, one step back25 test("patches can be reversed", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 26–40why, one step back26 expect(applyPatches(res, recordedInversePatches)).toEqual(base)27 })28 }2930 describe(`proxy`, () => {31 setUseProxies(true)32 runPatchTestHelper()33 })3435 describe(`es5`, () => {36 setUseProxies(false)37 runPatchTestHelper()38 })39}40test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
line 41why, one step back41describe("applyPatches", () => {fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
lines 42–59why, one step back42 it("mutates the base state when it is a draft", () => {43 produce({a: 1}, draft => {44 const result = applyPatches(draft, [45 {op: "replace", path: ["a"], value: 2}46 ])47 expect(result).toBe(draft)48 expect(draft.a).toBe(2)49 })50 })51 it("produces a copy of the base state when not a draft", () => {52 const base = {a: 1}53 const result = applyPatches(base, [54 {op: "replace", path: ["a"], value: 2}55 ])56 expect(result).not.toBe(base)57 expect(result.a).toBe(2)58 expect(base.a).toBe(1)59 })test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 60–63why, one step back60 it('throws when `op` is not "add", "replace", nor "remove"', () => {61 expect(() => {62 const patch = {op: "copy", from: [0], path: [1]}63 applyPatches([2], [patch])test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 64why, one step back64 }).toThrowErrorMatchingSnapshot()test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
line 65why, one step back65 })test: applyPatches coveragealeclarsonbefore adoptionec684a5
lines 66–70why, one step back66 it("throws when `path` cannot be resolved", () => {67 // missing parent68 expect(() => {69 const patch = {op: "add", path: ["a", "b"], value: 1}70 applyPatches({}, [patch])test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 71why, one step back71 }).toThrowErrorMatchingSnapshot()test: applyPatches coveragealeclarsonbefore adoptionec684a5
lines 72–76why, one step back7273 // missing grand-parent74 expect(() => {75 const patch = {op: "add", path: ["a", "b", "c"], value: 1}76 applyPatches({}, [patch])test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 77why, one step back77 }).toThrowErrorMatchingSnapshot()test: applyPatches coveragealeclarsonbefore adoptionec684a5
line 78why, one step back78 })test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 79–80why, one step back79})80test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 81why, one step back81describe("simple assignment - 1", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 82–100why, one step back82 runPatchTest(83 {x: 3},84 d => {85 d.x++86 },87 [{op: "replace", path: ["x"], value: 4}]88 )89})9091describe("simple assignment - 2", () => {92 runPatchTest(93 {x: {y: 4}},94 d => {95 d.x.y++96 },97 [{op: "replace", path: ["x", "y"], value: 5}]98 )99})100feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 101–110why, one step back101describe("simple assignment - 3", () => {102 runPatchTest(103 {x: [{y: 4}]},104 d => {105 d.x[0].y++106 },107 [{op: "replace", path: ["x", 0, "y"], value: 5}]108 )109})110Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 111–120why, one step back111describe("delete 1", () => {112 runPatchTest(113 {x: {y: 4}},114 d => {115 delete d.x116 },117 [{op: "remove", path: ["x"]}]118 )119})120test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 121–174why, one step back121describe("renaming properties", () => {122 describe("nested object (no changes)", () => {123 runPatchTest(124 {a: {b: 1}},125 d => {126 d.x = d.a127 delete d.a128 },129 [130 {op: "add", path: ["x"], value: {b: 1}},131 {op: "remove", path: ["a"]}132 ]133 )134 })135136 describe("nested object (with changes)", () => {137 runPatchTest(138 {a: {b: 1, c: 1}},139 d => {140 let a = d.a141 a.b = 2 // change142 delete a.c // delete143 a.y = 2 // add144145 // rename146 d.x = a147 delete d.a148 },149 [150 {op: "add", path: ["x"], value: {b: 2, y: 2}},151 {op: "remove", path: ["a"]}152 ]153 )154 })155156 describe("deeply nested object (with changes)", () => {157 runPatchTest(158 {a: {b: {c: 1, d: 1}}},159 d => {160 let b = d.a.b161 b.c = 2 // change162 delete b.d // delete163 b.y = 2 // add164165 // rename166 d.a.x = b167 delete d.a.b168 },169 [170 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},171 {op: "remove", path: ["a", "b"]}172 ]173 )174 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 175–191why, one step back175})176177describe("minimum amount of changes", () => {178 runPatchTest(179 {x: 3, y: {a: 4}, z: 3},180 d => {181 d.y.a = 4182 d.y.b = 5183 Object.assign(d, {x: 4, y: {a: 2}})184 },185 [186 {op: "replace", path: ["x"], value: 4},187 {op: "replace", path: ["y"], value: {a: 2}}188 ]189 )190})191Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 192why, one step back192describe("arrays - prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 193–197why, one step back193 runPatchTest(194 {x: [1, 2, 3]},195 d => {196 d.x.unshift(4)197 },feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 198–208why, one step back198 [{op: "add", path: ["x", 0], value: 4}]199 )200})201202describe("arrays - multiple prepend", () => {203 runPatchTest(204 {x: [1, 2, 3]},205 d => {206 d.x.unshift(4)207 d.x.unshift(5)208 },Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 209why, one step back209 [feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 210–211why, one step back210 {op: "add", path: ["x", 0], value: 5},211 {op: "add", path: ["x", 1], value: 4}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 212–215why, one step back212 ]213 )214})215Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 216why, one step back216describe("arrays - splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 217–221why, one step back217 runPatchTest(218 {x: [1, 2, 3]},219 d => {220 d.x.splice(1, 1)221 },feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 222–232why, one step back222 [{op: "remove", path: ["x", 1]}]223 )224})225226describe("arrays - multiple splice", () => {227 runPatchTest(228 [0, 1, 2, 3, 4, 5, 0],229 d => {230 d.splice(4, 2, 3)231 d.splice(1, 2, 3)232 },Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 233why, one step back233 [feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 234–250why, one step back234 {op: "replace", path: [1], value: 3},235 {op: "replace", path: [2], value: 3},236 {op: "remove", path: [5]},237 {op: "remove", path: [4]}238 ]239 )240})241242describe("arrays - modify and shrink", () => {243 runPatchTest(244 {x: [1, 2, 3]},245 d => {246 d.x[0] = 4247 d.x.length = 2248 },249 [250 {op: "replace", path: ["x", 0], value: 4},Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 251–255why, one step back251 {op: "replace", path: ["x", "length"], value: 2}252 ]253 )254})255Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 256why, one step back256describe("arrays - prepend then splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 257–269why, one step back257 runPatchTest(258 {x: [1, 2, 3]},259 d => {260 d.x.unshift(4)261 d.x.splice(2, 1)262 },263 [264 {op: "replace", path: ["x", 0], value: 4},265 {op: "replace", path: ["x", 1], value: 1}266 ]267 )268})269Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 270why, one step back270describe("arrays - splice middle then prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 271–283why, one step back271 runPatchTest(272 {x: [1, 2, 3]},273 d => {274 d.x.splice(1, 1)275 d.x.unshift(4)276 },277 [278 {op: "replace", path: ["x", 0], value: 4},279 {op: "replace", path: ["x", 1], value: 1}280 ]281 )282})283Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 284why, one step back284describe("arrays - truncate", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 285–297why, one step back285 runPatchTest(286 {x: [1, 2, 3]},287 d => {288 d.x.length -= 2289 },290 [{op: "replace", path: ["x", "length"], value: 1}],291 [292 {op: "add", path: ["x", 1], value: 2},293 {op: "add", path: ["x", 2], value: 3}294 ]295 )296})297Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 298why, one step back298describe("arrays - pop twice", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 299–308why, one step back299 runPatchTest(300 {x: [1, 2, 3]},301 d => {302 d.x.pop()303 d.x.pop()304 },305 [{op: "replace", path: ["x", "length"], value: 1}]306 )307})308Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 309why, one step back309describe("arrays - push multiple", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 310–319why, one step back310 runPatchTest(311 {x: [1, 2, 3]},312 d => {313 d.x.push(4, 5)314 },315 [316 {op: "add", path: ["x", 3], value: 4},317 {op: "add", path: ["x", 4], value: 5}318 ],319 [{op: "replace", path: ["x", "length"], value: 3}]feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 320–357why, one step back320 )321})322323describe("arrays - splice (expand)", () => {324 runPatchTest(325 {x: [1, 2, 3]},326 d => {327 d.x.splice(1, 1, 4, 5, 6)328 },329 [330 {op: "replace", path: ["x", 1], value: 4},331 {op: "add", path: ["x", 2], value: 5},332 {op: "add", path: ["x", 3], value: 6}333 ],334 [335 {op: "replace", path: ["x", 1], value: 2},336 {op: "remove", path: ["x", 3]},337 {op: "remove", path: ["x", 2]}338 ]339 )340})341342describe("arrays - splice (shrink)", () => {343 runPatchTest(344 {x: [1, 2, 3, 4, 5]},345 d => {346 d.x.splice(1, 3, 6)347 },348 [349 {op: "replace", path: ["x", 1], value: 6},350 {op: "remove", path: ["x", 3]},351 {op: "remove", path: ["x", 2]}352 ],353 [354 {op: "replace", path: ["x", 1], value: 2},355 {op: "add", path: ["x", 2], value: 3},356 {op: "add", path: ["x", 3], value: 4}357 ]Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 358–383why, one step back358 )359})360361describe("simple replacement", () => {362 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])363})364365describe("same value replacement - 1", () => {366 runPatchTest(367 {x: {y: 3}},368 d => {369 const a = d.x370 d.x = a371 },372 []373 )374})375376describe("same value replacement - 2", () => {377 runPatchTest(378 {x: {y: 3}},379 d => {380 const a = d.x381 d.x = 4382 d.x = a383 },fix: comparison typoaleclarsonbefore adoption48b371c
line 384why, one step back384 []Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 385–404why, one step back385 )386})387388describe("same value replacement - 3", () => {389 runPatchTest(390 {x: 3},391 d => {392 d.x = 3393 },394 []395 )396})397398describe("same value replacement - 4", () => {399 runPatchTest(400 {x: 3},401 d => {402 d.x = 4403 d.x = 3404 },fix: comparison typoaleclarsonbefore adoption48b371c
line 405why, one step back405 []Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 406–407why, one step back406 )407})test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 408why, one step back408Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 409–422why, one step back409describe("simple delete", () => {410 runPatchTest(411 {x: 2},412 d => {413 delete d.x414 },415 [416 {417 op: "remove",418 path: ["x"]419 }420 ]421 )422})