patch.js
This file
Path
__tests__/patch.js
487 lines · 8.4 KiB · JavaScript
Last changed by
fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
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 let resultProxies, resultEs589 function runPatchTestHelper() {10 let recordedPatches11 let recordedInversePatches12 const res = produce(base, producer, (p, i) => {13 recordedPatches = p14 recordedInversePatches = i15 })1617 test("produces the correct patches", () => {18 expect(recordedPatches).toEqual(patches)19 if (inversePathes) 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 })2930 return res31 }3233 describe(`proxy`, () => {34 setUseProxies(true)35 resultProxies = runPatchTestHelper()36 })3738 describe(`es5`, () => {39 setUseProxies(false)40 resultEs5 = runPatchTestHelper()41 test("ES5 and Proxy implementation yield same result", () => {42 expect(resultEs5).toEqual(resultProxies)43 })44 })4546 return resultProxies47}4849describe("applyPatches", () => {50 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, [{op: "replace", path: ["a"], value: 2}])62 expect(result).not.toBe(base)63 expect(result.a).toBe(2)64 expect(base.a).toBe(1)65 })66 it('throws when `op` is not "add", "replace", nor "remove"', () => {67 expect(() => {68 const patch = {op: "copy", from: [0], path: [1]}69 applyPatches([2], [patch])70 }).toThrowErrorMatchingSnapshot()71 })72 it("throws when `path` cannot be resolved", () => {73 // missing parent74 expect(() => {75 const patch = {op: "add", path: ["a", "b"], value: 1}76 applyPatches({}, [patch])77 }).toThrowErrorMatchingSnapshot()7879 // missing grand-parent80 expect(() => {81 const patch = {op: "add", path: ["a", "b", "c"], value: 1}82 applyPatches({}, [patch])83 }).toThrowErrorMatchingSnapshot()84 })85})8687describe("simple assignment - 1", () => {88 runPatchTest(89 {x: 3},90 d => {91 d.x++92 },93 [{op: "replace", path: ["x"], value: 4}]94 )95})9697describe("simple assignment - 2", () => {98 runPatchTest(99 {x: {y: 4}},100 d => {101 d.x.y++102 },103 [{op: "replace", path: ["x", "y"], value: 5}]104 )105})106107describe("simple assignment - 3", () => {108 runPatchTest(109 {x: [{y: 4}]},110 d => {111 d.x[0].y++112 },113 [{op: "replace", path: ["x", 0, "y"], value: 5}]114 )115})116117describe("delete 1", () => {118 runPatchTest(119 {x: {y: 4}},120 d => {121 delete d.x122 },123 [{op: "remove", path: ["x"]}]124 )125})126127describe("renaming properties", () => {128 describe("nested object (no changes)", () => {129 runPatchTest(130 {a: {b: 1}},131 d => {132 d.x = d.a133 delete d.a134 },135 [{op: "add", path: ["x"], value: {b: 1}}, {op: "remove", path: ["a"]}]136 )137 })138139 describe("nested object (with changes)", () => {140 runPatchTest(141 {a: {b: 1, c: 1}},142 d => {143 let a = d.a144 a.b = 2 // change145 delete a.c // delete146 a.y = 2 // add147148 // rename149 d.x = a150 delete d.a151 },152 [153 {op: "add", path: ["x"], value: {b: 2, y: 2}},154 {op: "remove", path: ["a"]}155 ]156 )157 })158159 describe("deeply nested object (with changes)", () => {160 runPatchTest(161 {a: {b: {c: 1, d: 1}}},162 d => {163 let b = d.a.b164 b.c = 2 // change165 delete b.d // delete166 b.y = 2 // add167168 // rename169 d.a.x = b170 delete d.a.b171 },172 [173 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},174 {op: "remove", path: ["a", "b"]}175 ]176 )177 })178})179180describe("minimum amount of changes", () => {181 runPatchTest(182 {x: 3, y: {a: 4}, z: 3},183 d => {184 d.y.a = 4185 d.y.b = 5186 Object.assign(d, {x: 4, y: {a: 2}})187 },188 [189 {op: "replace", path: ["x"], value: 4},190 {op: "replace", path: ["y"], value: {a: 2}}191 ]192 )193})194195describe("arrays - prepend", () => {196 runPatchTest(197 {x: [1, 2, 3]},198 d => {199 d.x.unshift(4)200 },201 [{op: "add", path: ["x", 0], value: 4}]202 )203})204205describe("arrays - multiple prepend", () => {206 runPatchTest(207 {x: [1, 2, 3]},208 d => {209 d.x.unshift(4)210 d.x.unshift(5)211 },212 [213 {op: "add", path: ["x", 0], value: 5},214 {op: "add", path: ["x", 1], value: 4}215 ]216 )217})218219describe("arrays - splice middle", () => {220 runPatchTest(221 {x: [1, 2, 3]},222 d => {223 d.x.splice(1, 1)224 },225 [{op: "remove", path: ["x", 1]}]226 )227})228229describe("arrays - multiple splice", () => {230 runPatchTest(231 [0, 1, 2, 3, 4, 5, 0],232 d => {233 d.splice(4, 2, 3)234 d.splice(1, 2, 3)235 },236 [237 {op: "replace", path: [1], value: 3},238 {op: "replace", path: [2], value: 3},239 {op: "remove", path: [5]},240 {op: "remove", path: [4]}241 ]242 )243})244245describe("arrays - modify and shrink", () => {246 runPatchTest(247 {x: [1, 2, 3]},248 d => {249 d.x[0] = 4250 d.x.length = 2251 },252 [253 {op: "replace", path: ["x", 0], value: 4},254 {op: "replace", path: ["x", "length"], value: 2}255 ]256 )257})258259describe("arrays - prepend then splice middle", () => {260 runPatchTest(261 {x: [1, 2, 3]},262 d => {263 d.x.unshift(4)264 d.x.splice(2, 1)265 },266 [267 {op: "replace", path: ["x", 0], value: 4},268 {op: "replace", path: ["x", 1], value: 1}269 ]270 )271})272273describe("arrays - splice middle then prepend", () => {274 runPatchTest(275 {x: [1, 2, 3]},276 d => {277 d.x.splice(1, 1)278 d.x.unshift(4)279 },280 [281 {op: "replace", path: ["x", 0], value: 4},282 {op: "replace", path: ["x", 1], value: 1}283 ]284 )285})286287describe("arrays - truncate", () => {288 runPatchTest(289 {x: [1, 2, 3]},290 d => {291 d.x.length -= 2292 },293 [{op: "replace", path: ["x", "length"], value: 1}],294 [295 {op: "add", path: ["x", 1], value: 2},296 {op: "add", path: ["x", 2], value: 3}297 ]298 )299})300301describe("arrays - pop twice", () => {302 runPatchTest(303 {x: [1, 2, 3]},304 d => {305 d.x.pop()306 d.x.pop()307 },308 [{op: "replace", path: ["x", "length"], value: 1}]309 )310})311312describe("arrays - push multiple", () => {313 runPatchTest(314 {x: [1, 2, 3]},315 d => {316 d.x.push(4, 5)317 },318 [319 {op: "add", path: ["x", 3], value: 4},320 {op: "add", path: ["x", 4], value: 5}321 ],322 [{op: "replace", path: ["x", "length"], value: 3}]323 )324})325326describe("arrays - splice (expand)", () => {327 runPatchTest(328 {x: [1, 2, 3]},329 d => {330 d.x.splice(1, 1, 4, 5, 6)331 },332 [333 {op: "replace", path: ["x", 1], value: 4},334 {op: "add", path: ["x", 2], value: 5},335 {op: "add", path: ["x", 3], value: 6}336 ],337 [338 {op: "replace", path: ["x", 1], value: 2},339 {op: "remove", path: ["x", 3]},340 {op: "remove", path: ["x", 2]}341 ]342 )343})344345describe("arrays - splice (shrink)", () => {346 runPatchTest(347 {x: [1, 2, 3, 4, 5]},348 d => {349 d.x.splice(1, 3, 6)350 },351 [352 {op: "replace", path: ["x", 1], value: 6},353 {op: "remove", path: ["x", 3]},354 {op: "remove", path: ["x", 2]}355 ],356 [357 {op: "replace", path: ["x", 1], value: 2},358 {op: "add", path: ["x", 2], value: 3},359 {op: "add", path: ["x", 3], value: 4}360 ]361 )362})363364describe("simple replacement", () => {365 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])366})367368describe("same value replacement - 1", () => {369 runPatchTest(370 {x: {y: 3}},371 d => {372 const a = d.x373 d.x = a374 },375 []376 )377})378379describe("same value replacement - 2", () => {380 runPatchTest(381 {x: {y: 3}},382 d => {383 const a = d.x384 d.x = 4385 d.x = a386 },387 []388 )389})390391describe("same value replacement - 3", () => {392 runPatchTest(393 {x: 3},394 d => {395 d.x = 3396 },397 []398 )399})400401describe("same value replacement - 4", () => {402 runPatchTest(403 {x: 3},404 d => {405 d.x = 4406 d.x = 3407 },408 []409 )410})411412describe("simple delete", () => {413 runPatchTest(414 {x: 2},415 d => {416 delete d.x417 },418 [419 {420 op: "remove",421 path: ["x"]422 }423 ]424 )425})426427describe("patch compressions yields correct results", () => {428 let p1, p2429 runPatchTest(430 {},431 d => {432 d.x = {test: true}433 },434 (p1 = [435 {436 op: "add",437 path: ["x"],438 value: {439 test: true440 }441 }442 ])443 )444 runPatchTest(445 {x: {test: true}},446 d => {447 delete d.x448 },449 (p2 = [450 {451 op: "remove",452 path: ["x"]453 }454 ])455 )456 const res = runPatchTest(457 {},458 d => {459 debugger460 applyPatches(d, [...p1, ...p2])461 },462 []463 )464465 expect(res).toEqual({})466})467468describe("change then delete property", () => {469 const res = runPatchTest(470 {471 x: 1472 },473 d => {474 d.x = 2475 delete d.x476 },477 [478 {479 op: "remove",480 path: ["x"]481 }482 ]483 )484 test("valid result", () => {485 expect(res).toEqual({})486 })487})