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 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})142fix: Map.prototype.set should return a draftAndrey Goncharovbefore adoption7001201
lines 143–159why, one step back143describe("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})159test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 160–169why, one step back160describe("delete 1", () => {161 runPatchTest(162 {x: {y: 4}},163 d => {164 delete d.x165 },166 [{op: "remove", path: ["x"]}]167 )168})169feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 170–193why, one step back170describe("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})193test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 194–208why, one step back194describe("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 })208feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 209–227why, one step back209 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 })227test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 228–247why, one step back228 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 })247feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 248–272why, one step back248 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 })272test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 273–291why, one step back273 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 })feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 292–324why, one step back292293 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 })test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 325–347why, one step back325})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 },feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 348–358why, one step back348 [{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 },test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
line 359why, one step back359 [feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 360–361why, one step back360 {op: "add", path: ["x", 0], value: 5},361 {op: "add", path: ["x", 1], value: 4}test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 362–371why, one step back362 ]363 )364})365366describe("arrays - splice middle", () => {367 runPatchTest(368 {x: [1, 2, 3]},369 d => {370 d.x.splice(1, 1)371 },feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 372–382why, one step back372 [{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 },test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
line 383why, one step back383 [feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 384–400why, one step back384 {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},test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 401–469why, one step back401 {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}]feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 470–507why, one step back470 )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 ]test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 508–533why, one step back508 )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 },fix: comparison typoaleclarsonbefore adoption48b371c
line 534why, one step back534 []test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 535–554why, one step back535 )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 },fix: comparison typoaleclarsonbefore adoption48b371c
line 555why, one step back555 []feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 556–568why, one step back556 )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 truetest: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 569–570why, one step back569 )570})test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 571why, one step back571test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 572–585why, one step back572describe("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})