patch.js
This file
Path
__tests__/patch.js
732 lines · 17.1 KiB · JavaScript
Last changed by
feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
History of this fileTake 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("simple assignment - 7", () => {162 const key1 = {prop: "val1"}163 const key2 = {prop: "val2"}164 runPatchTest(165 {x: new Map([[key1, 4]])},166 d => {167 d.x.set(key1, 5)168 d.x.set(key2, 6)169 },170 [171 {op: "replace", path: ["x", key1], value: 5},172 {op: "add", path: ["x", key2], value: 6}173 ],174 [175 {op: "replace", path: ["x", key1], value: 4},176 {op: "remove", path: ["x", key2]}177 ],178 true179 )180})181182describe("delete 1", () => {183 runPatchTest(184 {x: {y: 4}},185 d => {186 delete d.x187 },188 [{op: "remove", path: ["x"]}]189 )190})191192describe("delete 2", () => {193 runPatchTest(194 new Map([["x", 1]]),195 d => {196 d.delete("x")197 },198 [{op: "remove", path: ["x"]}],199 [{op: "add", path: ["x"], value: 1}],200 true201 )202})203204describe("delete 3", () => {205 runPatchTest(206 {x: new Map([["y", 1]])},207 d => {208 d.x.delete("y")209 },210 [{op: "remove", path: ["x", "y"]}],211 [{op: "add", path: ["x", "y"], value: 1}],212 true213 )214})215216describe("delete 5", () => {217 const key1 = {prop: "val1"}218 const key2 = {prop: "val2"}219 runPatchTest(220 {x: new Map([[key1, 1], [key2, 2]])},221 d => {222 d.x.delete(key1)223 d.x.delete(key2)224 },225 [{op: "remove", path: ["x", key1]}, {op: "remove", path: ["x", key2]}],226 [227 {op: "add", path: ["x", key1], value: 1},228 {op: "add", path: ["x", key2], value: 2}229 ],230 true231 )232})233234describe("delete 6", () => {235 runPatchTest(236 new Set(["x", 1]),237 d => {238 d.delete("x")239 },240 [{op: "remove", path: [0], value: "x"}],241 [{op: "add", path: [0], value: "x"}],242 true243 )244})245246describe("delete 7", () => {247 runPatchTest(248 {x: new Set(["y", 1])},249 d => {250 d.x.delete("y")251 },252 [{op: "remove", path: ["x", 0], value: "y"}],253 [{op: "add", path: ["x", 0], value: "y"}],254 true255 )256})257258describe("renaming properties", () => {259 describe("nested object (no changes)", () => {260 runPatchTest(261 {a: {b: 1}},262 d => {263 d.x = d.a264 delete d.a265 },266 [267 {op: "add", path: ["x"], value: {b: 1}},268 {op: "remove", path: ["a"]}269 ]270 )271 })272273 describe("nested map (no changes)", () => {274 runPatchTest(275 new Map([["a", new Map([["b", 1]])]]),276 d => {277 d.set("x", d.get("a"))278 d.delete("a")279 },280 [281 {op: "add", path: ["x"], value: new Map([["b", 1]])},282 {op: "remove", path: ["a"]}283 ],284 [285 {op: "remove", path: ["x"]},286 {op: "add", path: ["a"], value: new Map([["b", 1]])}287 ],288 true289 )290 })291292 describe("nested object (with changes)", () => {293 runPatchTest(294 {a: {b: 1, c: 1}},295 d => {296 let a = d.a297 a.b = 2 // change298 delete a.c // delete299 a.y = 2 // add300301 // rename302 d.x = a303 delete d.a304 },305 [306 {op: "add", path: ["x"], value: {b: 2, y: 2}},307 {op: "remove", path: ["a"]}308 ]309 )310 })311312 describe("nested map (with changes)", () => {313 runPatchTest(314 new Map([["a", new Map([["b", 1], ["c", 1]])]]),315 d => {316 let a = d.get("a")317 a.set("b", 2) // change318 a.delete("c") // delete319 a.set("y", 2) // add320321 // rename322 d.set("x", a)323 d.delete("a")324 },325 [326 {op: "add", path: ["x"], value: new Map([["b", 2], ["y", 2]])},327 {op: "remove", path: ["a"]}328 ],329 [330 {op: "remove", path: ["x"]},331 {op: "add", path: ["a"], value: new Map([["b", 1], ["c", 1]])}332 ],333 true334 )335 })336337 describe("deeply nested object (with changes)", () => {338 runPatchTest(339 {a: {b: {c: 1, d: 1}}},340 d => {341 let b = d.a.b342 b.c = 2 // change343 delete b.d // delete344 b.y = 2 // add345346 // rename347 d.a.x = b348 delete d.a.b349 },350 [351 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},352 {op: "remove", path: ["a", "b"]}353 ]354 )355 })356357 describe("deeply nested map (with changes)", () => {358 runPatchTest(359 new Map([["a", new Map([["b", new Map([["c", 1], ["d", 1]])]])]]),360 d => {361 let b = d.get("a").get("b")362 b.set("c", 2) // change363 b.delete("d") // delete364 b.set("y", 2) // add365366 // rename367 d.get("a").set("x", b)368 d.get("a").delete("b")369 },370 [371 {372 op: "add",373 path: ["a", "x"],374 value: new Map([["c", 2], ["y", 2]])375 },376 {op: "remove", path: ["a", "b"]}377 ],378 [379 {op: "remove", path: ["a", "x"]},380 {381 op: "add",382 path: ["a", "b"],383 value: new Map([["c", 1], ["d", 1]])384 }385 ],386 true387 )388 })389})390391describe("minimum amount of changes", () => {392 runPatchTest(393 {x: 3, y: {a: 4}, z: 3},394 d => {395 d.y.a = 4396 d.y.b = 5397 Object.assign(d, {x: 4, y: {a: 2}})398 },399 [400 {op: "replace", path: ["x"], value: 4},401 {op: "replace", path: ["y"], value: {a: 2}}402 ]403 )404})405406describe("arrays - prepend", () => {407 runPatchTest(408 {x: [1, 2, 3]},409 d => {410 d.x.unshift(4)411 },412 [{op: "add", path: ["x", 0], value: 4}]413 )414})415416describe("arrays - multiple prepend", () => {417 runPatchTest(418 {x: [1, 2, 3]},419 d => {420 d.x.unshift(4)421 d.x.unshift(5)422 },423 [424 {op: "add", path: ["x", 0], value: 5},425 {op: "add", path: ["x", 1], value: 4}426 ]427 )428})429430describe("arrays - splice middle", () => {431 runPatchTest(432 {x: [1, 2, 3]},433 d => {434 d.x.splice(1, 1)435 },436 [{op: "remove", path: ["x", 1]}]437 )438})439440describe("arrays - multiple splice", () => {441 runPatchTest(442 [0, 1, 2, 3, 4, 5, 0],443 d => {444 d.splice(4, 2, 3)445 d.splice(1, 2, 3)446 },447 [448 {op: "replace", path: [1], value: 3},449 {op: "replace", path: [2], value: 3},450 {op: "remove", path: [5]},451 {op: "remove", path: [4]}452 ]453 )454})455456describe("arrays - modify and shrink", () => {457 runPatchTest(458 {x: [1, 2, 3]},459 d => {460 d.x[0] = 4461 d.x.length = 2462 },463 [464 {op: "replace", path: ["x", 0], value: 4},465 {op: "replace", path: ["x", "length"], value: 2}466 ]467 )468})469470describe("arrays - prepend then splice middle", () => {471 runPatchTest(472 {x: [1, 2, 3]},473 d => {474 d.x.unshift(4)475 d.x.splice(2, 1)476 },477 [478 {op: "replace", path: ["x", 0], value: 4},479 {op: "replace", path: ["x", 1], value: 1}480 ]481 )482})483484describe("arrays - splice middle then prepend", () => {485 runPatchTest(486 {x: [1, 2, 3]},487 d => {488 d.x.splice(1, 1)489 d.x.unshift(4)490 },491 [492 {op: "replace", path: ["x", 0], value: 4},493 {op: "replace", path: ["x", 1], value: 1}494 ]495 )496})497498describe("arrays - truncate", () => {499 runPatchTest(500 {x: [1, 2, 3]},501 d => {502 d.x.length -= 2503 },504 [{op: "replace", path: ["x", "length"], value: 1}],505 [506 {op: "add", path: ["x", 1], value: 2},507 {op: "add", path: ["x", 2], value: 3}508 ]509 )510})511512describe("arrays - pop twice", () => {513 runPatchTest(514 {x: [1, 2, 3]},515 d => {516 d.x.pop()517 d.x.pop()518 },519 [{op: "replace", path: ["x", "length"], value: 1}]520 )521})522523describe("arrays - push multiple", () => {524 runPatchTest(525 {x: [1, 2, 3]},526 d => {527 d.x.push(4, 5)528 },529 [530 {op: "add", path: ["x", 3], value: 4},531 {op: "add", path: ["x", 4], value: 5}532 ],533 [{op: "replace", path: ["x", "length"], value: 3}]534 )535})536537describe("arrays - splice (expand)", () => {538 runPatchTest(539 {x: [1, 2, 3]},540 d => {541 d.x.splice(1, 1, 4, 5, 6)542 },543 [544 {op: "replace", path: ["x", 1], value: 4},545 {op: "add", path: ["x", 2], value: 5},546 {op: "add", path: ["x", 3], value: 6}547 ],548 [549 {op: "replace", path: ["x", 1], value: 2},550 {op: "remove", path: ["x", 3]},551 {op: "remove", path: ["x", 2]}552 ]553 )554})555556describe("arrays - splice (shrink)", () => {557 runPatchTest(558 {x: [1, 2, 3, 4, 5]},559 d => {560 d.x.splice(1, 3, 6)561 },562 [563 {op: "replace", path: ["x", 1], value: 6},564 {op: "remove", path: ["x", 3]},565 {op: "remove", path: ["x", 2]}566 ],567 [568 {op: "replace", path: ["x", 1], value: 2},569 {op: "add", path: ["x", 2], value: 3},570 {op: "add", path: ["x", 3], value: 4}571 ]572 )573})574575describe("sets - add - 1", () => {576 runPatchTest(577 new Set([1]),578 d => {579 d.add(2)580 },581 [{op: "add", path: [1], value: 2}],582 [{op: "remove", path: [1], value: 2}],583 true584 )585})586587describe("sets - add, delete, add - 1", () => {588 runPatchTest(589 new Set([1]),590 d => {591 d.add(2)592 d.delete(2)593 d.add(2)594 },595 [{op: "add", path: [1], value: 2}],596 [{op: "remove", path: [1], value: 2}],597 true598 )599})600601describe("sets - add, delete, add - 2", () => {602 runPatchTest(603 new Set([2, 1]),604 d => {605 d.add(2)606 d.delete(2)607 d.add(2)608 },609 [],610 [],611 true612 )613})614615describe("sets - mutate - 1", () => {616 const findById = (set, id) => {617 for (const item of set) {618 if (item.id === id) return item619 }620 }621 runPatchTest(622 new Set([{id: 1, val: "We"}, {id: 2, val: "will"}]),623 d => {624 const obj1 = findById(d, 1)625 const obj2 = findById(d, 2)626 obj1.val = "rock"627 obj2.val = "you"628 },629 [630 {op: "remove", path: [0], value: {id: 1, val: "We"}},631 {op: "remove", path: [1], value: {id: 2, val: "will"}},632 {op: "add", path: [0], value: {id: 1, val: "rock"}},633 {op: "add", path: [1], value: {id: 2, val: "you"}}634 ],635 [636 {op: "remove", path: [1], value: {id: 2, val: "you"}},637 {op: "remove", path: [0], value: {id: 1, val: "rock"}},638 {op: "add", path: [1], value: {id: 2, val: "will"}},639 {op: "add", path: [0], value: {id: 1, val: "We"}}640 ],641 true642 )643})644645describe("simple replacement", () => {646 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])647})648649describe("same value replacement - 1", () => {650 runPatchTest(651 {x: {y: 3}},652 d => {653 const a = d.x654 d.x = a655 },656 []657 )658})659660describe("same value replacement - 2", () => {661 runPatchTest(662 {x: {y: 3}},663 d => {664 const a = d.x665 d.x = 4666 d.x = a667 },668 []669 )670})671672describe("same value replacement - 3", () => {673 runPatchTest(674 {x: 3},675 d => {676 d.x = 3677 },678 []679 )680})681682describe("same value replacement - 4", () => {683 runPatchTest(684 {x: 3},685 d => {686 d.x = 4687 d.x = 3688 },689 []690 )691})692693describe("same value replacement - 5", () => {694 runPatchTest(695 new Map([["x", 3]]),696 d => {697 d.set("x", 4)698 d.set("x", 3)699 },700 [],701 [],702 true703 )704})705706describe("same value replacement - 6", () => {707 runPatchTest(708 new Set(["x", 3]),709 d => {710 d.delete("x")711 d.add("x")712 },713 [],714 [],715 true716 )717})718719describe("simple delete", () => {720 runPatchTest(721 {x: 2},722 d => {723 delete d.x724 },725 [726 {727 op: "remove",728 path: ["x"]729 }730 ]731 )732})