patch.js
This file
Path
__tests__/patch.js
351 lines · 8.3 KiB · JavaScript
Last changed by
fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
History of this fileTake it
Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 1why, one step back1"use strict"chore: update import paths in testsaleclarsonbefore adoptioncdc09b1
line 2why, one step back2import produce, {setUseProxies, applyPatches} from "../src/index"Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 3–24why, one step back34jest.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 })24test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 25why, one step back25 test("patches can be reversed", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 26–40why, one step back26 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}40test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
line 41why, one step back41describe("applyPatches", () => {fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
lines 42–59why, one step back42 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 })test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 60–65why, one step back60 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 }).toThrowError(/^Unsupported patch operation:/)65 })test: applyPatches coveragealeclarsonbefore adoptionec684a5
lines 66–89why, one step back66 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 }).toThrowError(/^Cannot apply patch, path doesn't resolve:/)7273 // missing grand-parent74 expect(() => {75 const patch = {op: "add", path: ["a", "b", "c"], value: 1}76 applyPatches({}, [patch])77 }).toThrowError(/^Cannot apply patch, path doesn't resolve:/)78 })79 it("throws when a patch tries to splice an array", () => {80 // Pop is ok81 expect(() => {82 const patch = {op: "remove", path: [0]}83 applyPatches([1], [patch])84 }).not.toThrowError()8586 // Splice is unsupported87 expect(() => {88 const patch = {op: "remove", path: [0]}89 applyPatches([1, 2], [patch])feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 90why, one step back90 }).toThrowError(/^Only the last index of an array can be removed/)test: applyPatches coveragealeclarsonbefore adoptionec684a5
line 91why, one step back91 })test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 92–93why, one step back92})93test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 94why, one step back94describe("simple assignment - 1", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 95–123why, one step back95 runPatchTest(96 {x: 3},97 d => {98 d.x++99 },100 [{op: "replace", path: ["x"], value: 4}]101 )102})103104describe("simple assignment - 2", () => {105 runPatchTest(106 {x: {y: 4}},107 d => {108 d.x.y++109 },110 [{op: "replace", path: ["x", "y"], value: 5}]111 )112})113114describe("delete 1", () => {115 runPatchTest(116 {x: {y: 4}},117 d => {118 delete d.x119 },120 [{op: "remove", path: ["x"]}]121 )122})123test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 124–177why, one step back124describe("renaming properties", () => {125 describe("nested object (no changes)", () => {126 runPatchTest(127 {a: {b: 1}},128 d => {129 d.x = d.a130 delete d.a131 },132 [133 {op: "add", path: ["x"], value: {b: 1}},134 {op: "remove", path: ["a"]}135 ]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 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 178–194why, one step back178})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})194Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 195why, one step back195describe("arrays - prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 196–209why, one step back196 runPatchTest(197 {x: [1, 2, 3]},198 d => {199 d.x.unshift(4)200 },201 [202 {op: "replace", path: ["x", 0], value: 4},203 {op: "replace", path: ["x", 1], value: 1},204 {op: "replace", path: ["x", 2], value: 2},205 {op: "add", path: ["x", 3], value: 3}206 ]207 )208})209Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 210why, one step back210describe("arrays - splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 211–222why, one step back211 runPatchTest(212 {x: [1, 2, 3]},213 d => {214 d.x.splice(1, 1)215 },216 [217 {op: "replace", path: ["x", 1], value: 3},218 {op: "replace", path: ["x", "length"], value: 2}219 ]220 )221})222Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 223why, one step back223describe("arrays - prepend then splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 224–236why, one step back224 runPatchTest(225 {x: [1, 2, 3]},226 d => {227 d.x.unshift(4)228 d.x.splice(2, 1)229 },230 [231 {op: "replace", path: ["x", 0], value: 4},232 {op: "replace", path: ["x", 1], value: 1}233 ]234 )235})236Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 237why, one step back237describe("arrays - splice middle then prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 238–250why, one step back238 runPatchTest(239 {x: [1, 2, 3]},240 d => {241 d.x.splice(1, 1)242 d.x.unshift(4)243 },244 [245 {op: "replace", path: ["x", 0], value: 4},246 {op: "replace", path: ["x", 1], value: 1}247 ]248 )249})250Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 251why, one step back251describe("arrays - truncate", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 252–264why, one step back252 runPatchTest(253 {x: [1, 2, 3]},254 d => {255 d.x.length -= 2256 },257 [{op: "replace", path: ["x", "length"], value: 1}],258 [259 {op: "add", path: ["x", 1], value: 2},260 {op: "add", path: ["x", 2], value: 3}261 ]262 )263})264Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 265why, one step back265describe("arrays - pop twice", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 266–275why, one step back266 runPatchTest(267 {x: [1, 2, 3]},268 d => {269 d.x.pop()270 d.x.pop()271 },272 [{op: "replace", path: ["x", "length"], value: 1}]273 )274})275Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 276why, one step back276describe("arrays - push multiple", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 277–312why, one step back277 runPatchTest(278 {x: [1, 2, 3]},279 d => {280 d.x.push(4, 5)281 },282 [283 {op: "add", path: ["x", 3], value: 4},284 {op: "add", path: ["x", 4], value: 5}285 ],286 [{op: "replace", path: ["x", "length"], value: 3}]287 )288})289290describe("simple replacement", () => {291 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])292})293294describe("same value replacement - 1", () => {295 runPatchTest(296 {x: {y: 3}},297 d => {298 const a = d.x299 d.x = a300 },301 []302 )303})304305describe("same value replacement - 2", () => {306 runPatchTest(307 {x: {y: 3}},308 d => {309 const a = d.x310 d.x = 4311 d.x = a312 },fix: comparison typoaleclarsonbefore adoption48b371c
line 313why, one step back313 []Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 314–333why, one step back314 )315})316317describe("same value replacement - 3", () => {318 runPatchTest(319 {x: 3},320 d => {321 d.x = 3322 },323 []324 )325})326327describe("same value replacement - 4", () => {328 runPatchTest(329 {x: 3},330 d => {331 d.x = 4332 d.x = 3333 },fix: comparison typoaleclarsonbefore adoption48b371c
line 334why, one step back334 []Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 335–336why, one step back335 )336})test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 337why, one step back337Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 338–351why, one step back338describe("simple delete", () => {339 runPatchTest(340 {x: 2},341 d => {342 delete d.x343 },344 [345 {346 op: "remove",347 path: ["x"]348 }349 ]350 )351})