patch.js
This file
Path
__tests__/patch.js
557 lines · 9.5 KiB · JavaScript
Last changed by
fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
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 [279 {op: "replace", path: ["x", 0], value: 4},280 {op: "replace", path: ["x", "length"], value: 2}281 ]282 )283})284285describe("arrays - prepend then splice middle", () => {286 runPatchTest(287 {x: [1, 2, 3]},288 d => {289 d.x.unshift(4)290 d.x.splice(2, 1)291 },292 [293 {op: "replace", path: ["x", 0], value: 4},294 {op: "replace", path: ["x", 1], value: 1}295 ]296 )297})298299describe("arrays - splice middle then prepend", () => {300 runPatchTest(301 {x: [1, 2, 3]},302 d => {303 d.x.splice(1, 1)304 d.x.unshift(4)305 },306 [307 {op: "replace", path: ["x", 0], value: 4},308 {op: "replace", path: ["x", 1], value: 1}309 ]310 )311})312313describe("arrays - truncate", () => {314 runPatchTest(315 {x: [1, 2, 3]},316 d => {317 d.x.length -= 2318 },319 [{op: "replace", path: ["x", "length"], value: 1}],320 [321 {op: "add", path: ["x", 1], value: 2},322 {op: "add", path: ["x", 2], value: 3}323 ]324 )325})326327describe("arrays - pop twice", () => {328 runPatchTest(329 {x: [1, 2, 3]},330 d => {331 d.x.pop()332 d.x.pop()333 },334 [{op: "replace", path: ["x", "length"], value: 1}]335 )336})337338describe("arrays - push multiple", () => {339 runPatchTest(340 {x: [1, 2, 3]},341 d => {342 d.x.push(4, 5)343 },344 [345 {op: "add", path: ["x", 3], value: 4},346 {op: "add", path: ["x", 4], value: 5}347 ],348 [{op: "replace", path: ["x", "length"], value: 3}]349 )350})351352describe("arrays - splice (expand)", () => {353 runPatchTest(354 {x: [1, 2, 3]},355 d => {356 d.x.splice(1, 1, 4, 5, 6)357 },358 [359 {op: "replace", path: ["x", 1], value: 4},360 {op: "add", path: ["x", 2], value: 5},361 {op: "add", path: ["x", 3], value: 6}362 ],363 [364 {op: "replace", path: ["x", 1], value: 2},365 {op: "remove", path: ["x", 3]},366 {op: "remove", path: ["x", 2]}367 ]368 )369})370371describe("arrays - splice (shrink)", () => {372 runPatchTest(373 {x: [1, 2, 3, 4, 5]},374 d => {375 d.x.splice(1, 3, 6)376 },377 [378 {op: "replace", path: ["x", 1], value: 6},379 {op: "remove", path: ["x", 3]},380 {op: "remove", path: ["x", 2]}381 ],382 [383 {op: "replace", path: ["x", 1], value: 2},384 {op: "add", path: ["x", 2], value: 3},385 {op: "add", path: ["x", 3], value: 4}386 ]387 )388})389390describe("simple replacement", () => {391 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])392})393394describe("same value replacement - 1", () => {395 runPatchTest(396 {x: {y: 3}},397 d => {398 const a = d.x399 d.x = a400 },401 []402 )403})404405describe("same value replacement - 2", () => {406 runPatchTest(407 {x: {y: 3}},408 d => {409 const a = d.x410 d.x = 4411 d.x = a412 },413 []414 )415})416417describe("same value replacement - 3", () => {418 runPatchTest(419 {x: 3},420 d => {421 d.x = 3422 },423 []424 )425})426427describe("same value replacement - 4", () => {428 runPatchTest(429 {x: 3},430 d => {431 d.x = 4432 d.x = 3433 },434 []435 )436})437438describe("simple delete", () => {439 runPatchTest(440 {x: 2},441 d => {442 delete d.x443 },444 [445 {446 op: "remove",447 path: ["x"]448 }449 ]450 )451})452453describe("patch compressions yields correct results", () => {454 let p1, p2455 runPatchTest(456 {},457 d => {458 d.x = {test: true}459 },460 (p1 = [461 {462 op: "add",463 path: ["x"],464 value: {465 test: true466 }467 }468 ])469 )470 runPatchTest(471 {x: {test: true}},472 d => {473 delete d.x474 },475 (p2 = [476 {477 op: "remove",478 path: ["x"]479 }480 ])481 )482 const res = runPatchTest(483 {},484 d => {485 applyPatches(d, [...p1, ...p2])486 },487 []488 )489490 expect(res).toEqual({})491})492493describe("change then delete property", () => {494 const res = runPatchTest(495 {496 x: 1497 },498 d => {499 d.x = 2500 delete d.x501 },502 [503 {504 op: "remove",505 path: ["x"]506 }507 ]508 )509 test("valid result", () => {510 expect(res).toEqual({})511 })512})513514test("replaying patches with interweaved replacements should work correctly", () => {515 const patches = []516 const s0 = {x: 1}517518 const s1 = produce(519 s0,520 draft => {521 draft.x = 2522 },523 p => {524 patches.push(...p)525 }526 )527528 const s2 = produce(529 s1,530 draft => {531 return {x: 0}532 },533 p => {534 patches.push(...p)535 }536 )537538 const s3 = produce(539 s2,540 draft => {541 draft.x--542 },543 p => {544 patches.push(...p)545 }546 )547548 expect(s3).toEqual({x: -1}) // correct result549 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay550551 // manual replay on a draft should also be correct552 expect(553 produce(s0, draft => {554 return applyPatches(draft, patches)555 })556 ).toEqual({x: -1})557})