patch.js
This file
Path
__tests__/patch.js
300 lines · 6.9 KiB · JavaScript
Last changed by
test: applyPatches coveragealeclarsonbefore adoptionec684a5
History of this fileTake it
Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1–40why, one step back1"use strict"2import produce, {setUseProxies, applyPatches} from "../src/immer"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 revered", () => {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}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–73why, 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])72 }).toThrowError(/^Remove can only remove the last key of an array/)73 })test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
lines 74–75why, one step back74})75Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 76–142why, one step back76describe("simple assignment", () => {77 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("simple assignment - 2", () => {97 runPatchTest(98 {x: {y: 4}},99 d => {100 d.x.y++101 },102 [{op: "replace", path: ["x", "y"], value: 5}]103 )104})105106describe("delete 1", () => {107 runPatchTest(108 {x: {y: 4}},109 d => {110 delete d.x111 },112 [{op: "remove", path: ["x"]}]113 )114})115116describe("moving stuff", () => {117 runPatchTest(118 {x: {y: 4}},119 d => {120 d.z = d.x121 d.z.y++122 delete d.x123 },124 [{op: "add", path: ["z"], value: {y: 5}}, {op: "remove", path: ["x"]}]125 )126})127128describe("minimum amount of changes", () => {129 runPatchTest(130 {x: 3, y: {a: 4}, z: 3},131 d => {132 d.y.a = 4133 d.y.b = 5134 Object.assign(d, {x: 4, y: {a: 2}})135 },136 [137 {op: "replace", path: ["x"], value: 4},138 {op: "replace", path: ["y"], value: {a: 2}}139 ]140 )141})142Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 143why, one step back143describe("arrays - prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 144–157why, one step back144 runPatchTest(145 {x: [1, 2, 3]},146 d => {147 d.x.unshift(4)148 },149 [150 {op: "replace", path: ["x", 0], value: 4},151 {op: "replace", path: ["x", 1], value: 1},152 {op: "replace", path: ["x", 2], value: 2},153 {op: "add", path: ["x", 3], value: 3}154 ]155 )156})157Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 158why, one step back158describe("arrays - splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 159–170why, one step back159 runPatchTest(160 {x: [1, 2, 3]},161 d => {162 d.x.splice(1, 1)163 },164 [165 {op: "replace", path: ["x", 1], value: 3},166 {op: "replace", path: ["x", "length"], value: 2}167 ]168 )169})170Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 171why, one step back171describe("arrays - prepend then splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 172–184why, one step back172 runPatchTest(173 {x: [1, 2, 3]},174 d => {175 d.x.unshift(4)176 d.x.splice(2, 1)177 },178 [179 {op: "replace", path: ["x", 0], value: 4},180 {op: "replace", path: ["x", 1], value: 1}181 ]182 )183})184Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 185why, one step back185describe("arrays - splice middle then prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 186–198why, one step back186 runPatchTest(187 {x: [1, 2, 3]},188 d => {189 d.x.splice(1, 1)190 d.x.unshift(4)191 },192 [193 {op: "replace", path: ["x", 0], value: 4},194 {op: "replace", path: ["x", 1], value: 1}195 ]196 )197})198Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 199why, one step back199describe("arrays - truncate", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 200–212why, one step back200 runPatchTest(201 {x: [1, 2, 3]},202 d => {203 d.x.length -= 2204 },205 [{op: "replace", path: ["x", "length"], value: 1}],206 [207 {op: "add", path: ["x", 1], value: 2},208 {op: "add", path: ["x", 2], value: 3}209 ]210 )211})212Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 213why, one step back213describe("arrays - pop twice", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 214–223why, one step back214 runPatchTest(215 {x: [1, 2, 3]},216 d => {217 d.x.pop()218 d.x.pop()219 },220 [{op: "replace", path: ["x", "length"], value: 1}]221 )222})223Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 224why, one step back224describe("arrays - push multiple", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 225–300why, one step back225 runPatchTest(226 {x: [1, 2, 3]},227 d => {228 d.x.push(4, 5)229 },230 [231 {op: "add", path: ["x", 3], value: 4},232 {op: "add", path: ["x", 4], value: 5}233 ],234 [{op: "replace", path: ["x", "length"], value: 3}]235 )236})237238describe("simple replacement", () => {239 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])240})241242describe("same value replacement - 1", () => {243 runPatchTest(244 {x: {y: 3}},245 d => {246 const a = d.x247 d.x = a248 },249 []250 )251})252253describe("same value replacement - 2", () => {254 runPatchTest(255 {x: {y: 3}},256 d => {257 const a = d.x258 d.x = 4259 d.x = a260 },261 // immer does not detect this is not an actual change262 [{op: "replace", path: ["x"], value: {y: 3}}]263 )264})265266describe("same value replacement - 3", () => {267 runPatchTest(268 {x: 3},269 d => {270 d.x = 3271 },272 []273 )274})275276describe("same value replacement - 4", () => {277 runPatchTest(278 {x: 3},279 d => {280 d.x = 4281 d.x = 3282 },283 // immer does not detect this is not an actual change284 [{op: "replace", path: ["x"], value: 3}]285 )286})287describe("simple delete", () => {288 runPatchTest(289 {x: 2},290 d => {291 delete d.x292 },293 [294 {295 op: "remove",296 path: ["x"]297 }298 ]299 )300})