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 fileTake it
The walk stopped after 200 commits: lines older than d671c22 point at it.
test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
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"test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 3–5why, one step back34jest.setTimeout(1000)5feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 6–12why, one step back6function runPatchTest(7 base,8 producer,9 patches,10 inversePathes,11 proxyOnly = false12) {test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 13–30why, one step back13 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 })30test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 31why, one step back31 test("patches can be reversed", () => {test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 32–36why, one step back32 expect(applyPatches(res, recordedInversePatches)).toEqual(base)33 })34 }3536 describe(`proxy`, () => {feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 37why, one step back37 beforeAll(() => setUseProxies(true))test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 38–41why, one step back38 runPatchTestHelper()39 })4041 describe(`es5`, () => {feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 42–45why, one step back42 if (!proxyOnly) {43 beforeAll(() => setUseProxies(false))44 runPatchTestHelper()45 }test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 46–49why, one step back46 })47}4849describe("applyPatches", () => {fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
lines 50–67why, one step back50 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 })test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 68–71why, one step back68 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])test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 72why, one step back72 }).toThrowErrorMatchingSnapshot()test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
line 73why, one step back73 })test: applyPatches coveragealeclarsonbefore adoptionec684a5
lines 74–78why, one step back74 it("throws when `path` cannot be resolved", () => {75 // missing parent76 expect(() => {77 const patch = {op: "add", path: ["a", "b"], value: 1}78 applyPatches({}, [patch])test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 79why, one step back79 }).toThrowErrorMatchingSnapshot()test: applyPatches coveragealeclarsonbefore adoptionec684a5
lines 80–84why, one step back8081 // missing grand-parent82 expect(() => {83 const patch = {op: "add", path: ["a", "b", "c"], value: 1}84 applyPatches({}, [patch])test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
line 85why, one step back85 }).toThrowErrorMatchingSnapshot()test: applyPatches coveragealeclarsonbefore adoptionec684a5
line 86why, one step back86 })test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 87–88why, one step back87})88test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 89why, one step back89describe("simple assignment - 1", () => {test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 90–108why, one step back90 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})108feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 109–118why, one step back109describe("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})118feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 119–142why, one step back119describe("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})142test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 143–152why, one step back143describe("delete 1", () => {144 runPatchTest(145 {x: {y: 4}},146 d => {147 delete d.x148 },149 [{op: "remove", path: ["x"]}]150 )151})152feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 153–176why, one step back153describe("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})176test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 177–191why, one step back177describe("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 })191feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 192–210why, one step back192 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 })210test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 211–230why, one step back211 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 })230feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 231–255why, one step back231 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 })255test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 256–274why, one step back256 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 })feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 275–307why, one step back275276 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 })test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 308–330why, one step back308})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 },feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 331–341why, one step back331 [{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 },test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
line 342why, one step back342 [feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 343–344why, one step back343 {op: "add", path: ["x", 0], value: 5},344 {op: "add", path: ["x", 1], value: 4}test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 345–354why, one step back345 ]346 )347})348349describe("arrays - splice middle", () => {350 runPatchTest(351 {x: [1, 2, 3]},352 d => {353 d.x.splice(1, 1)354 },feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 355–365why, one step back355 [{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 },test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
line 366why, one step back366 [feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 367–383why, one step back367 {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},test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 384–452why, one step back384 {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}]feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 453–490why, one step back453 )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 ]test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 491–516why, one step back491 )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 },fix: comparison typoaleclarsonbefore adoption48b371c
line 517why, one step back517 []test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 518–537why, one step back518 )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 },fix: comparison typoaleclarsonbefore adoption48b371c
line 538why, one step back538 []feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 539–551why, one step back539 )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 truetest: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 552–553why, one step back552 )553})test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 554why, one step back554test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 555–568why, one step back555describe("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})