patch.js
This file
Path
__tests__/patch.js
706 lines · 16.7 KiB · JavaScript
Last changed by
feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04
History of this fileTake it
1"use strict"2import produce, {setUseProxies, applyPatches} from "../src/index"34jest.setTimeout(1000)56function runPatchTest(base, producer, patches, inversePathes) {7 function runPatchTestHelper() {8 let recordedPatches9 let recordedInversePatches1011 const res = produce(base, producer, (p, i) => {12 recordedPatches = p13 recordedInversePatches = i14 })1516 test("produces the correct patches", () => {17 expect(recordedPatches).toEqual(patches)18 if (inversePathes)19 expect(recordedInversePatches).toEqual(inversePathes)20 })2122 test("patches are replayable", () => {23 expect(applyPatches(base, recordedPatches)).toEqual(res)24 })2526 test("patches can be reversed", () => {27 expect(applyPatches(res, recordedInversePatches)).toEqual(base)28 })29 }3031 describe(`proxy`, () => {32 beforeAll(() => setUseProxies(true))33 runPatchTestHelper()34 })3536 describe(`es5`, () => {37 beforeAll(() => setUseProxies(false))38 runPatchTestHelper()39 })40}4142describe("applyPatches", () => {43 it("mutates the base state when it is a draft", () => {44 produce({a: 1}, draft => {45 const result = applyPatches(draft, [46 {op: "replace", path: ["a"], value: 2}47 ])48 expect(result).toBe(draft)49 expect(draft.a).toBe(2)50 })51 })52 it("produces a copy of the base state when not a draft", () => {53 const base = {a: 1}54 const result = applyPatches(base, [55 {op: "replace", path: ["a"], value: 2}56 ])57 expect(result).not.toBe(base)58 expect(result.a).toBe(2)59 expect(base.a).toBe(1)60 })61 it('throws when `op` is not "add", "replace", nor "remove"', () => {62 expect(() => {63 const patch = {op: "copy", from: [0], path: [1]}64 applyPatches([2], [patch])65 }).toThrowErrorMatchingSnapshot()66 })67 it("throws when `path` cannot be resolved", () => {68 // missing parent69 expect(() => {70 const patch = {op: "add", path: ["a", "b"], value: 1}71 applyPatches({}, [patch])72 }).toThrowErrorMatchingSnapshot()7374 // missing grand-parent75 expect(() => {76 const patch = {op: "add", path: ["a", "b", "c"], value: 1}77 applyPatches({}, [patch])78 }).toThrowErrorMatchingSnapshot()79 })80})8182describe("simple assignment - 1", () => {83 runPatchTest(84 {x: 3},85 d => {86 d.x++87 },88 [{op: "replace", path: ["x"], value: 4}]89 )90})9192describe("simple assignment - 2", () => {93 runPatchTest(94 {x: {y: 4}},95 d => {96 d.x.y++97 },98 [{op: "replace", path: ["x", "y"], value: 5}]99 )100})101102describe("simple assignment - 3", () => {103 runPatchTest(104 {x: [{y: 4}]},105 d => {106 d.x[0].y++107 },108 [{op: "replace", path: ["x", 0, "y"], value: 5}]109 )110})111112describe("simple assignment - 4", () => {113 runPatchTest(114 new Map([["x", {y: 4}]]),115 d => {116 d.get("x").y++117 },118 [{op: "replace", path: ["x", "y"], value: 5}],119 [{op: "replace", path: ["x", "y"], value: 4}]120 )121})122123describe("simple assignment - 5", () => {124 runPatchTest(125 {x: new Map([["y", 4]])},126 d => {127 d.x.set("y", 5)128 },129 [{op: "replace", path: ["x", "y"], value: 5}],130 [{op: "replace", path: ["x", "y"], value: 4}]131 )132})133134describe("simple assignment - 6", () => {135 runPatchTest(136 new Map([["x", 1]]),137 d => {138 // Map.prototype.set should return the Map itself139 const res = d.set("x", 2)140 res.set("y", 3)141 },142 [143 {op: "replace", path: ["x"], value: 2},144 {op: "add", path: ["y"], value: 3}145 ],146 [{op: "replace", path: ["x"], value: 1}, {op: "remove", path: ["y"]}]147 )148})149150describe("simple assignment - 7", () => {151 const key1 = {prop: "val1"}152 const key2 = {prop: "val2"}153 runPatchTest(154 {x: new Map([[key1, 4]])},155 d => {156 d.x.set(key1, 5)157 d.x.set(key2, 6)158 },159 [160 {op: "replace", path: ["x", key1], value: 5},161 {op: "add", path: ["x", key2], value: 6}162 ],163 [164 {op: "replace", path: ["x", key1], value: 4},165 {op: "remove", path: ["x", key2]}166 ]167 )168})169170describe("delete 1", () => {171 runPatchTest(172 {x: {y: 4}},173 d => {174 delete d.x175 },176 [{op: "remove", path: ["x"]}]177 )178})179180describe("delete 2", () => {181 runPatchTest(182 new Map([["x", 1]]),183 d => {184 d.delete("x")185 },186 [{op: "remove", path: ["x"]}],187 [{op: "add", path: ["x"], value: 1}]188 )189})190191describe("delete 3", () => {192 runPatchTest(193 {x: new Map([["y", 1]])},194 d => {195 d.x.delete("y")196 },197 [{op: "remove", path: ["x", "y"]}],198 [{op: "add", path: ["x", "y"], value: 1}]199 )200})201202describe("delete 5", () => {203 const key1 = {prop: "val1"}204 const key2 = {prop: "val2"}205 runPatchTest(206 {x: new Map([[key1, 1], [key2, 2]])},207 d => {208 d.x.delete(key1)209 d.x.delete(key2)210 },211 [{op: "remove", path: ["x", key1]}, {op: "remove", path: ["x", key2]}],212 [213 {op: "add", path: ["x", key1], value: 1},214 {op: "add", path: ["x", key2], value: 2}215 ]216 )217})218219describe("delete 6", () => {220 runPatchTest(221 new Set(["x", 1]),222 d => {223 d.delete("x")224 },225 [{op: "remove", path: [0], value: "x"}],226 [{op: "add", path: [0], value: "x"}]227 )228})229230describe("delete 7", () => {231 runPatchTest(232 {x: new Set(["y", 1])},233 d => {234 d.x.delete("y")235 },236 [{op: "remove", path: ["x", 0], value: "y"}],237 [{op: "add", path: ["x", 0], value: "y"}]238 )239})240241describe("renaming properties", () => {242 describe("nested object (no changes)", () => {243 runPatchTest(244 {a: {b: 1}},245 d => {246 d.x = d.a247 delete d.a248 },249 [250 {op: "add", path: ["x"], value: {b: 1}},251 {op: "remove", path: ["a"]}252 ]253 )254 })255256 describe("nested map (no changes)", () => {257 runPatchTest(258 new Map([["a", new Map([["b", 1]])]]),259 d => {260 d.set("x", d.get("a"))261 d.delete("a")262 },263 [264 {op: "add", path: ["x"], value: new Map([["b", 1]])},265 {op: "remove", path: ["a"]}266 ],267 [268 {op: "remove", path: ["x"]},269 {op: "add", path: ["a"], value: new Map([["b", 1]])}270 ]271 )272 })273274 describe("nested object (with changes)", () => {275 runPatchTest(276 {a: {b: 1, c: 1}},277 d => {278 let a = d.a279 a.b = 2 // change280 delete a.c // delete281 a.y = 2 // add282283 // rename284 d.x = a285 delete d.a286 },287 [288 {op: "add", path: ["x"], value: {b: 2, y: 2}},289 {op: "remove", path: ["a"]}290 ]291 )292 })293294 describe("nested map (with changes)", () => {295 runPatchTest(296 new Map([["a", new Map([["b", 1], ["c", 1]])]]),297 d => {298 let a = d.get("a")299 a.set("b", 2) // change300 a.delete("c") // delete301 a.set("y", 2) // add302303 // rename304 d.set("x", a)305 d.delete("a")306 },307 [308 {op: "add", path: ["x"], value: new Map([["b", 2], ["y", 2]])},309 {op: "remove", path: ["a"]}310 ],311 [312 {op: "remove", path: ["x"]},313 {op: "add", path: ["a"], value: new Map([["b", 1], ["c", 1]])}314 ]315 )316 })317318 describe("deeply nested object (with changes)", () => {319 runPatchTest(320 {a: {b: {c: 1, d: 1}}},321 d => {322 let b = d.a.b323 b.c = 2 // change324 delete b.d // delete325 b.y = 2 // add326327 // rename328 d.a.x = b329 delete d.a.b330 },331 [332 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},333 {op: "remove", path: ["a", "b"]}334 ]335 )336 })337338 describe("deeply nested map (with changes)", () => {339 runPatchTest(340 new Map([["a", new Map([["b", new Map([["c", 1], ["d", 1]])]])]]),341 d => {342 let b = d.get("a").get("b")343 b.set("c", 2) // change344 b.delete("d") // delete345 b.set("y", 2) // add346347 // rename348 d.get("a").set("x", b)349 d.get("a").delete("b")350 },351 [352 {353 op: "add",354 path: ["a", "x"],355 value: new Map([["c", 2], ["y", 2]])356 },357 {op: "remove", path: ["a", "b"]}358 ],359 [360 {op: "remove", path: ["a", "x"]},361 {362 op: "add",363 path: ["a", "b"],364 value: new Map([["c", 1], ["d", 1]])365 }366 ]367 )368 })369})370371describe("minimum amount of changes", () => {372 runPatchTest(373 {x: 3, y: {a: 4}, z: 3},374 d => {375 d.y.a = 4376 d.y.b = 5377 Object.assign(d, {x: 4, y: {a: 2}})378 },379 [380 {op: "replace", path: ["x"], value: 4},381 {op: "replace", path: ["y"], value: {a: 2}}382 ]383 )384})385386describe("arrays - prepend", () => {387 runPatchTest(388 {x: [1, 2, 3]},389 d => {390 d.x.unshift(4)391 },392 [{op: "add", path: ["x", 0], value: 4}]393 )394})395396describe("arrays - multiple prepend", () => {397 runPatchTest(398 {x: [1, 2, 3]},399 d => {400 d.x.unshift(4)401 d.x.unshift(5)402 },403 [404 {op: "add", path: ["x", 0], value: 5},405 {op: "add", path: ["x", 1], value: 4}406 ]407 )408})409410describe("arrays - splice middle", () => {411 runPatchTest(412 {x: [1, 2, 3]},413 d => {414 d.x.splice(1, 1)415 },416 [{op: "remove", path: ["x", 1]}]417 )418})419420describe("arrays - multiple splice", () => {421 runPatchTest(422 [0, 1, 2, 3, 4, 5, 0],423 d => {424 d.splice(4, 2, 3)425 d.splice(1, 2, 3)426 },427 [428 {op: "replace", path: [1], value: 3},429 {op: "replace", path: [2], value: 3},430 {op: "remove", path: [5]},431 {op: "remove", path: [4]}432 ]433 )434})435436describe("arrays - modify and shrink", () => {437 runPatchTest(438 {x: [1, 2, 3]},439 d => {440 d.x[0] = 4441 d.x.length = 2442 },443 [444 {op: "replace", path: ["x", 0], value: 4},445 {op: "replace", path: ["x", "length"], value: 2}446 ]447 )448})449450describe("arrays - prepend then splice middle", () => {451 runPatchTest(452 {x: [1, 2, 3]},453 d => {454 d.x.unshift(4)455 d.x.splice(2, 1)456 },457 [458 {op: "replace", path: ["x", 0], value: 4},459 {op: "replace", path: ["x", 1], value: 1}460 ]461 )462})463464describe("arrays - splice middle then prepend", () => {465 runPatchTest(466 {x: [1, 2, 3]},467 d => {468 d.x.splice(1, 1)469 d.x.unshift(4)470 },471 [472 {op: "replace", path: ["x", 0], value: 4},473 {op: "replace", path: ["x", 1], value: 1}474 ]475 )476})477478describe("arrays - truncate", () => {479 runPatchTest(480 {x: [1, 2, 3]},481 d => {482 d.x.length -= 2483 },484 [{op: "replace", path: ["x", "length"], value: 1}],485 [486 {op: "add", path: ["x", 1], value: 2},487 {op: "add", path: ["x", 2], value: 3}488 ]489 )490})491492describe("arrays - pop twice", () => {493 runPatchTest(494 {x: [1, 2, 3]},495 d => {496 d.x.pop()497 d.x.pop()498 },499 [{op: "replace", path: ["x", "length"], value: 1}]500 )501})502503describe("arrays - push multiple", () => {504 runPatchTest(505 {x: [1, 2, 3]},506 d => {507 d.x.push(4, 5)508 },509 [510 {op: "add", path: ["x", 3], value: 4},511 {op: "add", path: ["x", 4], value: 5}512 ],513 [{op: "replace", path: ["x", "length"], value: 3}]514 )515})516517describe("arrays - splice (expand)", () => {518 runPatchTest(519 {x: [1, 2, 3]},520 d => {521 d.x.splice(1, 1, 4, 5, 6)522 },523 [524 {op: "replace", path: ["x", 1], value: 4},525 {op: "add", path: ["x", 2], value: 5},526 {op: "add", path: ["x", 3], value: 6}527 ],528 [529 {op: "replace", path: ["x", 1], value: 2},530 {op: "remove", path: ["x", 3]},531 {op: "remove", path: ["x", 2]}532 ]533 )534})535536describe("arrays - splice (shrink)", () => {537 runPatchTest(538 {x: [1, 2, 3, 4, 5]},539 d => {540 d.x.splice(1, 3, 6)541 },542 [543 {op: "replace", path: ["x", 1], value: 6},544 {op: "remove", path: ["x", 3]},545 {op: "remove", path: ["x", 2]}546 ],547 [548 {op: "replace", path: ["x", 1], value: 2},549 {op: "add", path: ["x", 2], value: 3},550 {op: "add", path: ["x", 3], value: 4}551 ]552 )553})554555describe("sets - add - 1", () => {556 runPatchTest(557 new Set([1]),558 d => {559 d.add(2)560 },561 [{op: "add", path: [1], value: 2}],562 [{op: "remove", path: [1], value: 2}]563 )564})565566describe("sets - add, delete, add - 1", () => {567 runPatchTest(568 new Set([1]),569 d => {570 d.add(2)571 d.delete(2)572 d.add(2)573 },574 [{op: "add", path: [1], value: 2}],575 [{op: "remove", path: [1], value: 2}]576 )577})578579describe("sets - add, delete, add - 2", () => {580 runPatchTest(581 new Set([2, 1]),582 d => {583 d.add(2)584 d.delete(2)585 d.add(2)586 },587 [],588 []589 )590})591592describe("sets - mutate - 1", () => {593 const findById = (set, id) => {594 for (const item of set) {595 if (item.id === id) return item596 }597 }598 runPatchTest(599 new Set([{id: 1, val: "We"}, {id: 2, val: "will"}]),600 d => {601 const obj1 = findById(d, 1)602 const obj2 = findById(d, 2)603 obj1.val = "rock"604 obj2.val = "you"605 },606 [607 {op: "remove", path: [0], value: {id: 1, val: "We"}},608 {op: "remove", path: [1], value: {id: 2, val: "will"}},609 {op: "add", path: [0], value: {id: 1, val: "rock"}},610 {op: "add", path: [1], value: {id: 2, val: "you"}}611 ],612 [613 {op: "remove", path: [1], value: {id: 2, val: "you"}},614 {op: "remove", path: [0], value: {id: 1, val: "rock"}},615 {op: "add", path: [1], value: {id: 2, val: "will"}},616 {op: "add", path: [0], value: {id: 1, val: "We"}}617 ]618 )619})620621describe("simple replacement", () => {622 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])623})624625describe("same value replacement - 1", () => {626 runPatchTest(627 {x: {y: 3}},628 d => {629 const a = d.x630 d.x = a631 },632 []633 )634})635636describe("same value replacement - 2", () => {637 runPatchTest(638 {x: {y: 3}},639 d => {640 const a = d.x641 d.x = 4642 d.x = a643 },644 []645 )646})647648describe("same value replacement - 3", () => {649 runPatchTest(650 {x: 3},651 d => {652 d.x = 3653 },654 []655 )656})657658describe("same value replacement - 4", () => {659 runPatchTest(660 {x: 3},661 d => {662 d.x = 4663 d.x = 3664 },665 []666 )667})668669describe("same value replacement - 5", () => {670 runPatchTest(671 new Map([["x", 3]]),672 d => {673 d.set("x", 4)674 d.set("x", 3)675 },676 [],677 []678 )679})680681describe("same value replacement - 6", () => {682 runPatchTest(683 new Set(["x", 3]),684 d => {685 d.delete("x")686 d.add("x")687 },688 [],689 []690 )691})692693describe("simple delete", () => {694 runPatchTest(695 {x: 2},696 d => {697 delete d.x698 },699 [700 {701 op: "remove",702 path: ["x"]703 }704 ]705 )706})