patch.js
This file
Path
__tests__/patch.js
336 lines · 7.9 KiB · JavaScript
Last changed by
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
History of this fileTake it
Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1–24why, 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 })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–160why, 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 debugger146 let b = d.a.b147 b.c = 2 // change148 delete b.d // delete149 b.y = 2 // add150151 // rename152 d.a.x = b153 delete d.a.b154 },155 [156 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},157 {op: "remove", path: ["a", "b"]}158 ]159 )160 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 161–177why, one step back161})162163describe("minimum amount of changes", () => {164 runPatchTest(165 {x: 3, y: {a: 4}, z: 3},166 d => {167 d.y.a = 4168 d.y.b = 5169 Object.assign(d, {x: 4, y: {a: 2}})170 },171 [172 {op: "replace", path: ["x"], value: 4},173 {op: "replace", path: ["y"], value: {a: 2}}174 ]175 )176})177Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 178why, one step back178describe("arrays - prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 179–192why, one step back179 runPatchTest(180 {x: [1, 2, 3]},181 d => {182 d.x.unshift(4)183 },184 [185 {op: "replace", path: ["x", 0], value: 4},186 {op: "replace", path: ["x", 1], value: 1},187 {op: "replace", path: ["x", 2], value: 2},188 {op: "add", path: ["x", 3], value: 3}189 ]190 )191})192Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 193why, one step back193describe("arrays - splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 194–205why, one step back194 runPatchTest(195 {x: [1, 2, 3]},196 d => {197 d.x.splice(1, 1)198 },199 [200 {op: "replace", path: ["x", 1], value: 3},201 {op: "replace", path: ["x", "length"], value: 2}202 ]203 )204})205Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 206why, one step back206describe("arrays - prepend then splice middle", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 207–219why, one step back207 runPatchTest(208 {x: [1, 2, 3]},209 d => {210 d.x.unshift(4)211 d.x.splice(2, 1)212 },213 [214 {op: "replace", path: ["x", 0], value: 4},215 {op: "replace", path: ["x", 1], value: 1}216 ]217 )218})219Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 220why, one step back220describe("arrays - splice middle then prepend", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 221–233why, one step back221 runPatchTest(222 {x: [1, 2, 3]},223 d => {224 d.x.splice(1, 1)225 d.x.unshift(4)226 },227 [228 {op: "replace", path: ["x", 0], value: 4},229 {op: "replace", path: ["x", 1], value: 1}230 ]231 )232})233Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 234why, one step back234describe("arrays - truncate", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 235–247why, one step back235 runPatchTest(236 {x: [1, 2, 3]},237 d => {238 d.x.length -= 2239 },240 [{op: "replace", path: ["x", "length"], value: 1}],241 [242 {op: "add", path: ["x", 1], value: 2},243 {op: "add", path: ["x", 2], value: 3}244 ]245 )246})247Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 248why, one step back248describe("arrays - pop twice", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 249–258why, one step back249 runPatchTest(250 {x: [1, 2, 3]},251 d => {252 d.x.pop()253 d.x.pop()254 },255 [{op: "replace", path: ["x", "length"], value: 1}]256 )257})258Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 259why, one step back259describe("arrays - push multiple", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 260–321why, one step back260 runPatchTest(261 {x: [1, 2, 3]},262 d => {263 d.x.push(4, 5)264 },265 [266 {op: "add", path: ["x", 3], value: 4},267 {op: "add", path: ["x", 4], value: 5}268 ],269 [{op: "replace", path: ["x", "length"], value: 3}]270 )271})272273describe("simple replacement", () => {274 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])275})276277describe("same value replacement - 1", () => {278 runPatchTest(279 {x: {y: 3}},280 d => {281 const a = d.x282 d.x = a283 },284 []285 )286})287288describe("same value replacement - 2", () => {289 runPatchTest(290 {x: {y: 3}},291 d => {292 const a = d.x293 d.x = 4294 d.x = a295 },296 // immer does not detect this is not an actual change297 [{op: "replace", path: ["x"], value: {y: 3}}]298 )299})300301describe("same value replacement - 3", () => {302 runPatchTest(303 {x: 3},304 d => {305 d.x = 3306 },307 []308 )309})310311describe("same value replacement - 4", () => {312 runPatchTest(313 {x: 3},314 d => {315 d.x = 4316 d.x = 3317 },318 // immer does not detect this is not an actual change319 [{op: "replace", path: ["x"], value: 3}]320 )321})test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 322why, one step back322Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 323–336why, one step back323describe("simple delete", () => {324 runPatchTest(325 {x: 2},326 d => {327 delete d.x328 },329 [330 {331 op: "remove",332 path: ["x"]333 }334 ]335 )336})