patch.js
This file
Path
__tests__/patch.js
422 lines · 9.6 KiB · JavaScript
Last changed by
test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91
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 recordedInversePatches10 const res = produce(base, producer, (p, i) => {11 recordedPatches = p12 recordedInversePatches = i13 })1415 test("produces the correct patches", () => {16 expect(recordedPatches).toEqual(patches)17 if (inversePathes)18 expect(recordedInversePatches).toEqual(inversePathes)19 })2021 test("patches are replayable", () => {22 expect(applyPatches(base, recordedPatches)).toEqual(res)23 })2425 test("patches can be reversed", () => {26 expect(applyPatches(res, recordedInversePatches)).toEqual(base)27 })28 }2930 describe(`proxy`, () => {31 setUseProxies(true)32 runPatchTestHelper()33 })3435 describe(`es5`, () => {36 setUseProxies(false)37 runPatchTestHelper()38 })39}4041describe("applyPatches", () => {42 it("mutates the base state when it is a draft", () => {43 produce({a: 1}, draft => {44 const result = applyPatches(draft, [45 {op: "replace", path: ["a"], value: 2}46 ])47 expect(result).toBe(draft)48 expect(draft.a).toBe(2)49 })50 })51 it("produces a copy of the base state when not a draft", () => {52 const base = {a: 1}53 const result = applyPatches(base, [54 {op: "replace", path: ["a"], value: 2}55 ])56 expect(result).not.toBe(base)57 expect(result.a).toBe(2)58 expect(base.a).toBe(1)59 })60 it('throws when `op` is not "add", "replace", nor "remove"', () => {61 expect(() => {62 const patch = {op: "copy", from: [0], path: [1]}63 applyPatches([2], [patch])64 }).toThrowErrorMatchingSnapshot()65 })66 it("throws when `path` cannot be resolved", () => {67 // missing parent68 expect(() => {69 const patch = {op: "add", path: ["a", "b"], value: 1}70 applyPatches({}, [patch])71 }).toThrowErrorMatchingSnapshot()7273 // missing grand-parent74 expect(() => {75 const patch = {op: "add", path: ["a", "b", "c"], value: 1}76 applyPatches({}, [patch])77 }).toThrowErrorMatchingSnapshot()78 })79})8081describe("simple assignment - 1", () => {82 runPatchTest(83 {x: 3},84 d => {85 d.x++86 },87 [{op: "replace", path: ["x"], value: 4}]88 )89})9091describe("simple assignment - 2", () => {92 runPatchTest(93 {x: {y: 4}},94 d => {95 d.x.y++96 },97 [{op: "replace", path: ["x", "y"], value: 5}]98 )99})100101describe("simple assignment - 3", () => {102 runPatchTest(103 {x: [{y: 4}]},104 d => {105 d.x[0].y++106 },107 [{op: "replace", path: ["x", 0, "y"], value: 5}]108 )109})110111describe("delete 1", () => {112 runPatchTest(113 {x: {y: 4}},114 d => {115 delete d.x116 },117 [{op: "remove", path: ["x"]}]118 )119})120121describe("renaming properties", () => {122 describe("nested object (no changes)", () => {123 runPatchTest(124 {a: {b: 1}},125 d => {126 d.x = d.a127 delete d.a128 },129 [130 {op: "add", path: ["x"], value: {b: 1}},131 {op: "remove", path: ["a"]}132 ]133 )134 })135136 describe("nested object (with changes)", () => {137 runPatchTest(138 {a: {b: 1, c: 1}},139 d => {140 let a = d.a141 a.b = 2 // change142 delete a.c // delete143 a.y = 2 // add144145 // rename146 d.x = a147 delete d.a148 },149 [150 {op: "add", path: ["x"], value: {b: 2, y: 2}},151 {op: "remove", path: ["a"]}152 ]153 )154 })155156 describe("deeply nested object (with changes)", () => {157 runPatchTest(158 {a: {b: {c: 1, d: 1}}},159 d => {160 let b = d.a.b161 b.c = 2 // change162 delete b.d // delete163 b.y = 2 // add164165 // rename166 d.a.x = b167 delete d.a.b168 },169 [170 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},171 {op: "remove", path: ["a", "b"]}172 ]173 )174 })175})176177describe("minimum amount of changes", () => {178 runPatchTest(179 {x: 3, y: {a: 4}, z: 3},180 d => {181 d.y.a = 4182 d.y.b = 5183 Object.assign(d, {x: 4, y: {a: 2}})184 },185 [186 {op: "replace", path: ["x"], value: 4},187 {op: "replace", path: ["y"], value: {a: 2}}188 ]189 )190})191192describe("arrays - prepend", () => {193 runPatchTest(194 {x: [1, 2, 3]},195 d => {196 d.x.unshift(4)197 },198 [{op: "add", path: ["x", 0], value: 4}]199 )200})201202describe("arrays - multiple prepend", () => {203 runPatchTest(204 {x: [1, 2, 3]},205 d => {206 d.x.unshift(4)207 d.x.unshift(5)208 },209 [210 {op: "add", path: ["x", 0], value: 5},211 {op: "add", path: ["x", 1], value: 4}212 ]213 )214})215216describe("arrays - splice middle", () => {217 runPatchTest(218 {x: [1, 2, 3]},219 d => {220 d.x.splice(1, 1)221 },222 [{op: "remove", path: ["x", 1]}]223 )224})225226describe("arrays - multiple splice", () => {227 runPatchTest(228 [0, 1, 2, 3, 4, 5, 0],229 d => {230 d.splice(4, 2, 3)231 d.splice(1, 2, 3)232 },233 [234 {op: "replace", path: [1], value: 3},235 {op: "replace", path: [2], value: 3},236 {op: "remove", path: [5]},237 {op: "remove", path: [4]}238 ]239 )240})241242describe("arrays - modify and shrink", () => {243 runPatchTest(244 {x: [1, 2, 3]},245 d => {246 d.x[0] = 4247 d.x.length = 2248 },249 [250 {op: "replace", path: ["x", 0], value: 4},251 {op: "replace", path: ["x", "length"], value: 2}252 ]253 )254})255256describe("arrays - prepend then splice middle", () => {257 runPatchTest(258 {x: [1, 2, 3]},259 d => {260 d.x.unshift(4)261 d.x.splice(2, 1)262 },263 [264 {op: "replace", path: ["x", 0], value: 4},265 {op: "replace", path: ["x", 1], value: 1}266 ]267 )268})269270describe("arrays - splice middle then prepend", () => {271 runPatchTest(272 {x: [1, 2, 3]},273 d => {274 d.x.splice(1, 1)275 d.x.unshift(4)276 },277 [278 {op: "replace", path: ["x", 0], value: 4},279 {op: "replace", path: ["x", 1], value: 1}280 ]281 )282})283284describe("arrays - truncate", () => {285 runPatchTest(286 {x: [1, 2, 3]},287 d => {288 d.x.length -= 2289 },290 [{op: "replace", path: ["x", "length"], value: 1}],291 [292 {op: "add", path: ["x", 1], value: 2},293 {op: "add", path: ["x", 2], value: 3}294 ]295 )296})297298describe("arrays - pop twice", () => {299 runPatchTest(300 {x: [1, 2, 3]},301 d => {302 d.x.pop()303 d.x.pop()304 },305 [{op: "replace", path: ["x", "length"], value: 1}]306 )307})308309describe("arrays - push multiple", () => {310 runPatchTest(311 {x: [1, 2, 3]},312 d => {313 d.x.push(4, 5)314 },315 [316 {op: "add", path: ["x", 3], value: 4},317 {op: "add", path: ["x", 4], value: 5}318 ],319 [{op: "replace", path: ["x", "length"], value: 3}]320 )321})322323describe("arrays - splice (expand)", () => {324 runPatchTest(325 {x: [1, 2, 3]},326 d => {327 d.x.splice(1, 1, 4, 5, 6)328 },329 [330 {op: "replace", path: ["x", 1], value: 4},331 {op: "add", path: ["x", 2], value: 5},332 {op: "add", path: ["x", 3], value: 6}333 ],334 [335 {op: "replace", path: ["x", 1], value: 2},336 {op: "remove", path: ["x", 3]},337 {op: "remove", path: ["x", 2]}338 ]339 )340})341342describe("arrays - splice (shrink)", () => {343 runPatchTest(344 {x: [1, 2, 3, 4, 5]},345 d => {346 d.x.splice(1, 3, 6)347 },348 [349 {op: "replace", path: ["x", 1], value: 6},350 {op: "remove", path: ["x", 3]},351 {op: "remove", path: ["x", 2]}352 ],353 [354 {op: "replace", path: ["x", 1], value: 2},355 {op: "add", path: ["x", 2], value: 3},356 {op: "add", path: ["x", 3], value: 4}357 ]358 )359})360361describe("simple replacement", () => {362 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])363})364365describe("same value replacement - 1", () => {366 runPatchTest(367 {x: {y: 3}},368 d => {369 const a = d.x370 d.x = a371 },372 []373 )374})375376describe("same value replacement - 2", () => {377 runPatchTest(378 {x: {y: 3}},379 d => {380 const a = d.x381 d.x = 4382 d.x = a383 },384 []385 )386})387388describe("same value replacement - 3", () => {389 runPatchTest(390 {x: 3},391 d => {392 d.x = 3393 },394 []395 )396})397398describe("same value replacement - 4", () => {399 runPatchTest(400 {x: 3},401 d => {402 d.x = 4403 d.x = 3404 },405 []406 )407})408409describe("simple delete", () => {410 runPatchTest(411 {x: 2},412 d => {413 delete d.x414 },415 [416 {417 op: "remove",418 path: ["x"]419 }420 ]421 )422})