patch.js
This file
Path
__tests__/patch.js
274 lines · 6.0 KiB · JavaScript
Last changed by
test: applyPatches invalid operationaleclarsonbefore adoptiond671c22
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–49why, 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 })48})49Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 50–116why, one step back50describe("simple assignment", () => {51 runPatchTest(52 {x: 3},53 d => {54 d.x++55 },56 [{op: "replace", path: ["x"], value: 4}]57 )58})5960describe("simple assignment - 2", () => {61 runPatchTest(62 {x: {y: 4}},63 d => {64 d.x.y++65 },66 [{op: "replace", path: ["x", "y"], value: 5}]67 )68})6970describe("simple assignment - 2", () => {71 runPatchTest(72 {x: {y: 4}},73 d => {74 d.x.y++75 },76 [{op: "replace", path: ["x", "y"], value: 5}]77 )78})7980describe("delete 1", () => {81 runPatchTest(82 {x: {y: 4}},83 d => {84 delete d.x85 },86 [{op: "remove", path: ["x"]}]87 )88})8990describe("moving stuff", () => {91 runPatchTest(92 {x: {y: 4}},93 d => {94 d.z = d.x95 d.z.y++96 delete d.x97 },98 [{op: "add", path: ["z"], value: {y: 5}}, {op: "remove", path: ["x"]}]99 )100})101102describe("minimum amount of changes", () => {103 runPatchTest(104 {x: 3, y: {a: 4}, z: 3},105 d => {106 d.y.a = 4107 d.y.b = 5108 Object.assign(d, {x: 4, y: {a: 2}})109 },110 [111 {op: "replace", path: ["x"], value: 4},112 {op: "replace", path: ["y"], value: {a: 2}}113 ]114 )115})116Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 117why, one step back117describe("arrays - prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 118–131why, one step back118 runPatchTest(119 {x: [1, 2, 3]},120 d => {121 d.x.unshift(4)122 },123 [124 {op: "replace", path: ["x", 0], value: 4},125 {op: "replace", path: ["x", 1], value: 1},126 {op: "replace", path: ["x", 2], value: 2},127 {op: "add", path: ["x", 3], value: 3}128 ]129 )130})131Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 132why, one step back132describe("arrays - splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 133–144why, one step back133 runPatchTest(134 {x: [1, 2, 3]},135 d => {136 d.x.splice(1, 1)137 },138 [139 {op: "replace", path: ["x", 1], value: 3},140 {op: "replace", path: ["x", "length"], value: 2}141 ]142 )143})144Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 145why, one step back145describe("arrays - prepend then splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 146–158why, one step back146 runPatchTest(147 {x: [1, 2, 3]},148 d => {149 d.x.unshift(4)150 d.x.splice(2, 1)151 },152 [153 {op: "replace", path: ["x", 0], value: 4},154 {op: "replace", path: ["x", 1], value: 1}155 ]156 )157})158Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 159why, one step back159describe("arrays - splice middle then prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 160–172why, one step back160 runPatchTest(161 {x: [1, 2, 3]},162 d => {163 d.x.splice(1, 1)164 d.x.unshift(4)165 },166 [167 {op: "replace", path: ["x", 0], value: 4},168 {op: "replace", path: ["x", 1], value: 1}169 ]170 )171})172Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 173why, one step back173describe("arrays - truncate", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 174–186why, one step back174 runPatchTest(175 {x: [1, 2, 3]},176 d => {177 d.x.length -= 2178 },179 [{op: "replace", path: ["x", "length"], value: 1}],180 [181 {op: "add", path: ["x", 1], value: 2},182 {op: "add", path: ["x", 2], value: 3}183 ]184 )185})186Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 187why, one step back187describe("arrays - pop twice", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 188–197why, one step back188 runPatchTest(189 {x: [1, 2, 3]},190 d => {191 d.x.pop()192 d.x.pop()193 },194 [{op: "replace", path: ["x", "length"], value: 1}]195 )196})197Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 198why, one step back198describe("arrays - push multiple", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 199–274why, one step back199 runPatchTest(200 {x: [1, 2, 3]},201 d => {202 d.x.push(4, 5)203 },204 [205 {op: "add", path: ["x", 3], value: 4},206 {op: "add", path: ["x", 4], value: 5}207 ],208 [{op: "replace", path: ["x", "length"], value: 3}]209 )210})211212describe("simple replacement", () => {213 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])214})215216describe("same value replacement - 1", () => {217 runPatchTest(218 {x: {y: 3}},219 d => {220 const a = d.x221 d.x = a222 },223 []224 )225})226227describe("same value replacement - 2", () => {228 runPatchTest(229 {x: {y: 3}},230 d => {231 const a = d.x232 d.x = 4233 d.x = a234 },235 // immer does not detect this is not an actual change236 [{op: "replace", path: ["x"], value: {y: 3}}]237 )238})239240describe("same value replacement - 3", () => {241 runPatchTest(242 {x: 3},243 d => {244 d.x = 3245 },246 []247 )248})249250describe("same value replacement - 4", () => {251 runPatchTest(252 {x: 3},253 d => {254 d.x = 4255 d.x = 3256 },257 // immer does not detect this is not an actual change258 [{op: "replace", path: ["x"], value: 3}]259 )260})261describe("simple delete", () => {262 runPatchTest(263 {x: 2},264 d => {265 delete d.x266 },267 [268 {269 op: "remove",270 path: ["x"]271 }272 ]273 )274})