patch.js
This file
Path
__tests__/patch.js
580 lines · 10.1 KiB · JavaScript
Last changed by
Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
History of this fileTake it
1"use strict"2import produce, {3 setUseProxies,4 applyPatches,5 produceWithPatches6} from "../src/index"78jest.setTimeout(1000)910function runPatchTest(base, producer, patches, inversePathes) {11 let resultProxies, resultEs51213 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) expect(recordedInversePatches).toEqual(inversePathes)24 })2526 test("patches are replayable", () => {27 expect(applyPatches(base, recordedPatches)).toEqual(res)28 })2930 test("patches can be reversed", () => {31 expect(applyPatches(res, recordedInversePatches)).toEqual(base)32 })3334 return res35 }3637 describe(`proxy`, () => {38 setUseProxies(true)39 resultProxies = runPatchTestHelper()40 })4142 describe(`es5`, () => {43 setUseProxies(false)44 resultEs5 = runPatchTestHelper()45 test("ES5 and Proxy implementation yield same result", () => {46 expect(resultEs5).toEqual(resultProxies)47 })48 })4950 return resultProxies51}5253describe("applyPatches", () => {54 it("mutates the base state when it is a draft", () => {55 produce({a: 1}, draft => {56 const result = applyPatches(draft, [57 {op: "replace", path: ["a"], value: 2}58 ])59 expect(result).toBe(draft)60 expect(draft.a).toBe(2)61 })62 })63 it("produces a copy of the base state when not a draft", () => {64 const base = {a: 1}65 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])66 expect(result).not.toBe(base)67 expect(result.a).toBe(2)68 expect(base.a).toBe(1)69 })70 it('throws when `op` is not "add", "replace", nor "remove"', () => {71 expect(() => {72 const patch = {op: "copy", from: [0], path: [1]}73 applyPatches([2], [patch])74 }).toThrowErrorMatchingSnapshot()75 })76 it("throws when `path` cannot be resolved", () => {77 // missing parent78 expect(() => {79 const patch = {op: "add", path: ["a", "b"], value: 1}80 applyPatches({}, [patch])81 }).toThrowErrorMatchingSnapshot()8283 // missing grand-parent84 expect(() => {85 const patch = {op: "add", path: ["a", "b", "c"], value: 1}86 applyPatches({}, [patch])87 }).toThrowErrorMatchingSnapshot()88 })89 it("applied patches cannot be modified", () => {90 // see also: https://github.com/immerjs/immer/issues/41191 const s0 = {92 items: [1]93 }9495 const [s1, p1] = produceWithPatches(s0, draft => {96 draft.items = []97 })9899 const replaceValueBefore = p1[0].value.slice()100101 const [s2, p2] = produceWithPatches(s1, draft => {102 draft.items.push(2)103 })104105 applyPatches(s0, [...p1, ...p2])106107 const replaceValueAfter = p1[0].value.slice()108109 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)110 })111})112113describe("simple assignment - 1", () => {114 runPatchTest(115 {x: 3},116 d => {117 d.x++118 },119 [{op: "replace", path: ["x"], value: 4}]120 )121})122123describe("simple assignment - 2", () => {124 runPatchTest(125 {x: {y: 4}},126 d => {127 d.x.y++128 },129 [{op: "replace", path: ["x", "y"], value: 5}]130 )131})132133describe("simple assignment - 3", () => {134 runPatchTest(135 {x: [{y: 4}]},136 d => {137 d.x[0].y++138 },139 [{op: "replace", path: ["x", 0, "y"], value: 5}]140 )141})142143describe("delete 1", () => {144 runPatchTest(145 {x: {y: 4}},146 d => {147 delete d.x148 },149 [{op: "remove", path: ["x"]}]150 )151})152153describe("renaming properties", () => {154 describe("nested object (no changes)", () => {155 runPatchTest(156 {a: {b: 1}},157 d => {158 d.x = d.a159 delete d.a160 },161 [{op: "add", path: ["x"], value: {b: 1}}, {op: "remove", path: ["a"]}]162 )163 })164165 describe("nested object (with changes)", () => {166 runPatchTest(167 {a: {b: 1, c: 1}},168 d => {169 let a = d.a170 a.b = 2 // change171 delete a.c // delete172 a.y = 2 // add173174 // rename175 d.x = a176 delete d.a177 },178 [179 {op: "add", path: ["x"], value: {b: 2, y: 2}},180 {op: "remove", path: ["a"]}181 ]182 )183 })184185 describe("deeply nested object (with changes)", () => {186 runPatchTest(187 {a: {b: {c: 1, d: 1}}},188 d => {189 let b = d.a.b190 b.c = 2 // change191 delete b.d // delete192 b.y = 2 // add193194 // rename195 d.a.x = b196 delete d.a.b197 },198 [199 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},200 {op: "remove", path: ["a", "b"]}201 ]202 )203 })204})205206describe("minimum amount of changes", () => {207 runPatchTest(208 {x: 3, y: {a: 4}, z: 3},209 d => {210 d.y.a = 4211 d.y.b = 5212 Object.assign(d, {x: 4, y: {a: 2}})213 },214 [215 {op: "replace", path: ["x"], value: 4},216 {op: "replace", path: ["y"], value: {a: 2}}217 ]218 )219})220221describe("arrays - prepend", () => {222 runPatchTest(223 {x: [1, 2, 3]},224 d => {225 d.x.unshift(4)226 },227 [{op: "add", path: ["x", 0], value: 4}]228 )229})230231describe("arrays - multiple prepend", () => {232 runPatchTest(233 {x: [1, 2, 3]},234 d => {235 d.x.unshift(4)236 d.x.unshift(5)237 },238 [239 {op: "add", path: ["x", 0], value: 5},240 {op: "add", path: ["x", 1], value: 4}241 ]242 )243})244245describe("arrays - splice middle", () => {246 runPatchTest(247 {x: [1, 2, 3]},248 d => {249 d.x.splice(1, 1)250 },251 [{op: "remove", path: ["x", 1]}]252 )253})254255describe("arrays - multiple splice", () => {256 runPatchTest(257 [0, 1, 2, 3, 4, 5, 0],258 d => {259 d.splice(4, 2, 3)260 d.splice(1, 2, 3)261 },262 [263 {op: "replace", path: [1], value: 3},264 {op: "replace", path: [2], value: 3},265 {op: "remove", path: [5]},266 {op: "remove", path: [4]}267 ]268 )269})270271describe("arrays - modify and shrink", () => {272 runPatchTest(273 {x: [1, 2, 3]},274 d => {275 d.x[0] = 4276 d.x.length = 2277 },278 [{op: "replace", path: ["x", 0], value: 4}, {op: "remove", path: ["x", 2]}],279 [280 {op: "replace", path: ["x", 0], value: 1},281 {op: "add", path: ["x", 2], value: 3}282 ]283 )284})285286describe("arrays - prepend then splice middle", () => {287 runPatchTest(288 {x: [1, 2, 3]},289 d => {290 d.x.unshift(4)291 d.x.splice(2, 1)292 },293 [294 {op: "replace", path: ["x", 0], value: 4},295 {op: "replace", path: ["x", 1], value: 1}296 ]297 )298})299300describe("arrays - splice middle then prepend", () => {301 runPatchTest(302 {x: [1, 2, 3]},303 d => {304 d.x.splice(1, 1)305 d.x.unshift(4)306 },307 [308 {op: "replace", path: ["x", 0], value: 4},309 {op: "replace", path: ["x", 1], value: 1}310 ]311 )312})313314describe("arrays - truncate", () => {315 runPatchTest(316 {x: [1, 2, 3]},317 d => {318 d.x.length -= 2319 },320 [{op: "remove", path: ["x", 2]}, {op: "remove", path: ["x", 1]}],321 [322 {op: "add", path: ["x", 1], value: 2},323 {op: "add", path: ["x", 2], value: 3}324 ]325 )326})327328describe("arrays - pop twice", () => {329 runPatchTest(330 {x: [1, 2, 3]},331 d => {332 d.x.pop()333 d.x.pop()334 },335 [{op: "remove", path: ["x", 2]}, {op: "remove", path: ["x", 1]}]336 )337})338339describe("arrays - push multiple", () => {340 runPatchTest(341 {x: [1, 2, 3]},342 d => {343 d.x.push(4, 5)344 },345 [346 {op: "add", path: ["x", 3], value: 4},347 {op: "add", path: ["x", 4], value: 5}348 ],349 [{op: "remove", path: ["x", 4]}, {op: "remove", path: ["x", 3]}]350 )351})352353describe("arrays - splice (expand)", () => {354 runPatchTest(355 {x: [1, 2, 3]},356 d => {357 d.x.splice(1, 1, 4, 5, 6)358 },359 [360 {op: "replace", path: ["x", 1], value: 4},361 {op: "add", path: ["x", 2], value: 5},362 {op: "add", path: ["x", 3], value: 6}363 ],364 [365 {op: "replace", path: ["x", 1], value: 2},366 {op: "remove", path: ["x", 3]},367 {op: "remove", path: ["x", 2]}368 ]369 )370})371372describe("arrays - splice (shrink)", () => {373 runPatchTest(374 {x: [1, 2, 3, 4, 5]},375 d => {376 d.x.splice(1, 3, 6)377 },378 [379 {op: "replace", path: ["x", 1], value: 6},380 {op: "remove", path: ["x", 3]},381 {op: "remove", path: ["x", 2]}382 ],383 [384 {op: "replace", path: ["x", 1], value: 2},385 {op: "add", path: ["x", 2], value: 3},386 {op: "add", path: ["x", 3], value: 4}387 ]388 )389})390391describe("arrays - splice should should result in remove op.", () => {392 runPatchTest(393 [1, 2],394 d => {395 d.splice(1, 1)396 },397 [{op: "remove", path: [1]}],398 [{op: "add", path: [1], value: 2}]399 )400})401402describe("arrays - NESTED splice should should result in remove op.", () => {403 runPatchTest(404 {a: {b: {c: [1, 2]}}},405 d => {406 d.a.b.c.splice(1, 1)407 },408 [{op: "remove", path: ["a", "b", "c", 1]}],409 [{op: "add", path: ["a", "b", "c", 1], value: 2}]410 )411})412413describe("simple replacement", () => {414 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])415})416417describe("same value replacement - 1", () => {418 runPatchTest(419 {x: {y: 3}},420 d => {421 const a = d.x422 d.x = a423 },424 []425 )426})427428describe("same value replacement - 2", () => {429 runPatchTest(430 {x: {y: 3}},431 d => {432 const a = d.x433 d.x = 4434 d.x = a435 },436 []437 )438})439440describe("same value replacement - 3", () => {441 runPatchTest(442 {x: 3},443 d => {444 d.x = 3445 },446 []447 )448})449450describe("same value replacement - 4", () => {451 runPatchTest(452 {x: 3},453 d => {454 d.x = 4455 d.x = 3456 },457 []458 )459})460461describe("simple delete", () => {462 runPatchTest(463 {x: 2},464 d => {465 delete d.x466 },467 [468 {469 op: "remove",470 path: ["x"]471 }472 ]473 )474})475476describe("patch compressions yields correct results", () => {477 let p1, p2478 runPatchTest(479 {},480 d => {481 d.x = {test: true}482 },483 (p1 = [484 {485 op: "add",486 path: ["x"],487 value: {488 test: true489 }490 }491 ])492 )493 runPatchTest(494 {x: {test: true}},495 d => {496 delete d.x497 },498 (p2 = [499 {500 op: "remove",501 path: ["x"]502 }503 ])504 )505 const res = runPatchTest(506 {},507 d => {508 applyPatches(d, [...p1, ...p2])509 },510 []511 )512513 expect(res).toEqual({})514})515516describe("change then delete property", () => {517 const res = runPatchTest(518 {519 x: 1520 },521 d => {522 d.x = 2523 delete d.x524 },525 [526 {527 op: "remove",528 path: ["x"]529 }530 ]531 )532 test("valid result", () => {533 expect(res).toEqual({})534 })535})536537test("replaying patches with interweaved replacements should work correctly", () => {538 const patches = []539 const s0 = {x: 1}540541 const s1 = produce(542 s0,543 draft => {544 draft.x = 2545 },546 p => {547 patches.push(...p)548 }549 )550551 const s2 = produce(552 s1,553 draft => {554 return {x: 0}555 },556 p => {557 patches.push(...p)558 }559 )560561 const s3 = produce(562 s2,563 draft => {564 draft.x--565 },566 p => {567 patches.push(...p)568 }569 )570571 expect(s3).toEqual({x: -1}) // correct result572 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay573574 // manual replay on a draft should also be correct575 expect(576 produce(s0, draft => {577 return applyPatches(draft, patches)578 })579 ).toEqual({x: -1})580})