patch.js
This file
Path
__tests__/patch.js
333 lines · 7.7 KiB · JavaScript
Last changed by
fix: comparison typoaleclarsonbefore adoption48b371c
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
lines 41–47why, one step back41describe("applyPatches", () => {42 it('throws when `op` is not "add", "replace", nor "remove"', () => {43 expect(() => {44 const patch = {op: "copy", from: [0], path: [1]}45 applyPatches([2], [patch])46 }).toThrowError(/^Unsupported patch operation:/)47 })test: applyPatches coveragealeclarsonbefore adoptionec684a5
lines 48–71why, one step back48 it("throws when `path` cannot be resolved", () => {49 // missing parent50 expect(() => {51 const patch = {op: "add", path: ["a", "b"], value: 1}52 applyPatches({}, [patch])53 }).toThrowError(/^Cannot apply patch, path doesn't resolve:/)5455 // missing grand-parent56 expect(() => {57 const patch = {op: "add", path: ["a", "b", "c"], value: 1}58 applyPatches({}, [patch])59 }).toThrowError(/^Cannot apply patch, path doesn't resolve:/)60 })61 it("throws when a patch tries to splice an array", () => {62 // Pop is ok63 expect(() => {64 const patch = {op: "remove", path: [0]}65 applyPatches([1], [patch])66 }).not.toThrowError()6768 // Splice is unsupported69 expect(() => {70 const patch = {op: "remove", path: [0]}71 applyPatches([1, 2], [patch])feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 72why, one step back72 }).toThrowError(/^Only the last index of an array can be removed/)test: applyPatches coveragealeclarsonbefore adoptionec684a5
line 73why, one step back73 })test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 74–75why, one step back74})75test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 76why, one step back76describe("simple assignment - 1", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 77–105why, one step back77 runPatchTest(78 {x: 3},79 d => {80 d.x++81 },82 [{op: "replace", path: ["x"], value: 4}]83 )84})8586describe("simple assignment - 2", () => {87 runPatchTest(88 {x: {y: 4}},89 d => {90 d.x.y++91 },92 [{op: "replace", path: ["x", "y"], value: 5}]93 )94})9596describe("delete 1", () => {97 runPatchTest(98 {x: {y: 4}},99 d => {100 delete d.x101 },102 [{op: "remove", path: ["x"]}]103 )104})105test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 106–159why, one step back106describe("renaming properties", () => {107 describe("nested object (no changes)", () => {108 runPatchTest(109 {a: {b: 1}},110 d => {111 d.x = d.a112 delete d.a113 },114 [115 {op: "add", path: ["x"], value: {b: 1}},116 {op: "remove", path: ["a"]}117 ]118 )119 })120121 describe("nested object (with changes)", () => {122 runPatchTest(123 {a: {b: 1, c: 1}},124 d => {125 let a = d.a126 a.b = 2 // change127 delete a.c // delete128 a.y = 2 // add129130 // rename131 d.x = a132 delete d.a133 },134 [135 {op: "add", path: ["x"], value: {b: 2, y: 2}},136 {op: "remove", path: ["a"]}137 ]138 )139 })140141 describe("deeply nested object (with changes)", () => {142 runPatchTest(143 {a: {b: {c: 1, d: 1}}},144 d => {145 let b = d.a.b146 b.c = 2 // change147 delete b.d // delete148 b.y = 2 // add149150 // rename151 d.a.x = b152 delete d.a.b153 },154 [155 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},156 {op: "remove", path: ["a", "b"]}157 ]158 )159 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 160–176why, one step back160})161162describe("minimum amount of changes", () => {163 runPatchTest(164 {x: 3, y: {a: 4}, z: 3},165 d => {166 d.y.a = 4167 d.y.b = 5168 Object.assign(d, {x: 4, y: {a: 2}})169 },170 [171 {op: "replace", path: ["x"], value: 4},172 {op: "replace", path: ["y"], value: {a: 2}}173 ]174 )175})176Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 177why, one step back177describe("arrays - prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 178–191why, one step back178 runPatchTest(179 {x: [1, 2, 3]},180 d => {181 d.x.unshift(4)182 },183 [184 {op: "replace", path: ["x", 0], value: 4},185 {op: "replace", path: ["x", 1], value: 1},186 {op: "replace", path: ["x", 2], value: 2},187 {op: "add", path: ["x", 3], value: 3}188 ]189 )190})191Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 192why, one step back192describe("arrays - splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 193–204why, one step back193 runPatchTest(194 {x: [1, 2, 3]},195 d => {196 d.x.splice(1, 1)197 },198 [199 {op: "replace", path: ["x", 1], value: 3},200 {op: "replace", path: ["x", "length"], value: 2}201 ]202 )203})204Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 205why, one step back205describe("arrays - prepend then splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 206–218why, one step back206 runPatchTest(207 {x: [1, 2, 3]},208 d => {209 d.x.unshift(4)210 d.x.splice(2, 1)211 },212 [213 {op: "replace", path: ["x", 0], value: 4},214 {op: "replace", path: ["x", 1], value: 1}215 ]216 )217})218Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 219why, one step back219describe("arrays - splice middle then prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 220–232why, one step back220 runPatchTest(221 {x: [1, 2, 3]},222 d => {223 d.x.splice(1, 1)224 d.x.unshift(4)225 },226 [227 {op: "replace", path: ["x", 0], value: 4},228 {op: "replace", path: ["x", 1], value: 1}229 ]230 )231})232Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 233why, one step back233describe("arrays - truncate", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 234–246why, one step back234 runPatchTest(235 {x: [1, 2, 3]},236 d => {237 d.x.length -= 2238 },239 [{op: "replace", path: ["x", "length"], value: 1}],240 [241 {op: "add", path: ["x", 1], value: 2},242 {op: "add", path: ["x", 2], value: 3}243 ]244 )245})246Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 247why, one step back247describe("arrays - pop twice", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 248–257why, one step back248 runPatchTest(249 {x: [1, 2, 3]},250 d => {251 d.x.pop()252 d.x.pop()253 },254 [{op: "replace", path: ["x", "length"], value: 1}]255 )256})257Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 258why, one step back258describe("arrays - push multiple", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 259–294why, one step back259 runPatchTest(260 {x: [1, 2, 3]},261 d => {262 d.x.push(4, 5)263 },264 [265 {op: "add", path: ["x", 3], value: 4},266 {op: "add", path: ["x", 4], value: 5}267 ],268 [{op: "replace", path: ["x", "length"], value: 3}]269 )270})271272describe("simple replacement", () => {273 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])274})275276describe("same value replacement - 1", () => {277 runPatchTest(278 {x: {y: 3}},279 d => {280 const a = d.x281 d.x = a282 },283 []284 )285})286287describe("same value replacement - 2", () => {288 runPatchTest(289 {x: {y: 3}},290 d => {291 const a = d.x292 d.x = 4293 d.x = a294 },fix: comparison typoaleclarsonbefore adoption48b371c
line 295why, one step back295 []Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 296–315why, one step back296 )297})298299describe("same value replacement - 3", () => {300 runPatchTest(301 {x: 3},302 d => {303 d.x = 3304 },305 []306 )307})308309describe("same value replacement - 4", () => {310 runPatchTest(311 {x: 3},312 d => {313 d.x = 4314 d.x = 3315 },fix: comparison typoaleclarsonbefore adoption48b371c
line 316why, one step back316 []Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 317–318why, one step back317 )318})test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 319why, one step back319Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 320–333why, one step back320describe("simple delete", () => {321 runPatchTest(322 {x: 2},323 d => {324 delete d.x325 },326 [327 {328 op: "remove",329 path: ["x"]330 }331 ]332 )333})