Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

557 lines · 9.5 KiB · JavaScript

Last changed by

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

History of this file

Take it

The walk stopped after 200 commits: lines older than 72850a9 point at it.

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 1why, one step back
1"use strict"

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

lines 2–6why, one step back
2import produce, {
3 setUseProxies,4 applyPatches,5 produceWithPatches6} from "../src/index"

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 7–9why, one step back
7
8jest.setTimeout(1000)9

fix: revert Map supportAlec Larsonbefore adoption47529d3

line 10why, one step back
10function runPatchTest(base, producer, patches, inversePathes) {

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

lines 11–12why, one step back
11 let resultProxies, resultEs5
12

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 13–19why, one step back
13 function runPatchTestHelper() {
14 let recordedPatches15 let recordedInversePatches16 const res = produce(base, producer, (p, i) => {17 recordedPatches = p18 recordedInversePatches = i19 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 20why, one step back
20

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 21–24why, one step back
21 test("produces the correct patches", () => {
22 expect(recordedPatches).toEqual(patches)23 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)24 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 25why, one step back
25

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 26–28why, one step back
26 test("patches are replayable", () => {
27 expect(applyPatches(base, recordedPatches)).toEqual(res)28 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 29why, one step back
29

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 30–32why, one step back
30 test("patches can be reversed", () => {
31 expect(applyPatches(res, recordedInversePatches)).toEqual(base)32 })

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

lines 33–34why, one step back
33
34 return res

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 35why, one step back
35 }

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 36why, one step back
36

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 37–38why, one step back
37 describe(`proxy`, () => {
38 setUseProxies(true)

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

line 39why, one step back
39 resultProxies = runPatchTestHelper()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 40why, one step back
40 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 41why, one step back
41

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 42–43why, one step back
42 describe(`es5`, () => {
43 setUseProxies(false)

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

lines 44–47why, one step back
44 resultEs5 = runPatchTestHelper()
45 test("ES5 and Proxy implementation yield same result", () => {46 expect(resultEs5).toEqual(resultProxies)47 })

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 48why, one step back
48 })

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

lines 49–50why, one step back
49
50 return resultProxies

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 51–53why, one step back
51}
5253describe("applyPatches", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 54–81why, one step back
54 it("mutates the base state when it is a draft", () => {
55 produce({a: 1}, draft => {56 const result = applyPatches(draft, [57 {op: "replace", path: ["a"], value: 2}58 ])59 expect(result).toBe(draft)60 expect(draft.a).toBe(2)61 })62 })63 it("produces a copy of the base state when not a draft", () => {64 const base = {a: 1}65 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])66 expect(result).not.toBe(base)67 expect(result.a).toBe(2)68 expect(base.a).toBe(1)69 })70 it('throws when `op` is not "add", "replace", nor "remove"', () => {71 expect(() => {72 const patch = {op: "copy", from: [0], path: [1]}73 applyPatches([2], [patch])74 }).toThrowErrorMatchingSnapshot()75 })76 it("throws when `path` cannot be resolved", () => {77 // missing parent78 expect(() => {79 const patch = {op: "add", path: ["a", "b"], value: 1}80 applyPatches({}, [patch])81 }).toThrowErrorMatchingSnapshot()

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 82why, one step back
82

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 83–87why, one step back
83 // missing grand-parent
84 expect(() => {85 const patch = {op: "add", path: ["a", "b", "c"], value: 1}86 applyPatches({}, [patch])87 }).toThrowErrorMatchingSnapshot()

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

lines 88–109why, one step back
88 })
89 it("applied patches cannot be modified", () => {90 // see also: https://github.com/immerjs/immer/issues/41191 const s0 = {92 items: [1]93 }9495 const [s1, p1] = produceWithPatches(s0, draft => {96 draft.items = []97 })9899 const replaceValueBefore = p1[0].value.slice()100101 const [s2, p2] = produceWithPatches(s1, draft => {102 draft.items.push(2)103 })104105 applyPatches(s0, [...p1, ...p2])106107 const replaceValueAfter = p1[0].value.slice()108109 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 110why, one step back
110 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 111–112why, one step back
111})
112

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 113why, one step back
113describe("simple assignment - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 114–120why, one step back
114 runPatchTest(
115 {x: 3},116 d => {117 d.x++118 },119 [{op: "replace", path: ["x"], value: 4}]120 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 121–123why, one step back
121})
122123describe("simple assignment - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 124–130why, one step back
124 runPatchTest(
125 {x: {y: 4}},126 d => {127 d.x.y++128 },129 [{op: "replace", path: ["x", "y"], value: 5}]130 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 131–132why, one step back
131})
132

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

line 133why, one step back
133describe("simple assignment - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 134–140why, one step back
134 runPatchTest(
135 {x: [{y: 4}]},136 d => {137 d.x[0].y++138 },139 [{op: "replace", path: ["x", 0, "y"], value: 5}]140 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 141–142why, one step back
141})
142

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 143why, one step back
143describe("delete 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 144–150why, one step back
144 runPatchTest(
145 {x: {y: 4}},146 d => {147 delete d.x148 },149 [{op: "remove", path: ["x"]}]150 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 151–152why, one step back
151})
152

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 153why, one step back
153describe("renaming properties", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 154–163why, one step back
154 describe("nested object (no changes)", () => {
155 runPatchTest(156 {a: {b: 1}},157 d => {158 d.x = d.a159 delete d.a160 },161 [{op: "add", path: ["x"], value: {b: 1}}, {op: "remove", path: ["a"]}]162 )163 })

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 164why, one step back
164

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 165–172why, one step back
165 describe("nested object (with changes)", () => {
166 runPatchTest(167 {a: {b: 1, c: 1}},168 d => {169 let a = d.a170 a.b = 2 // change171 delete a.c // delete172 a.y = 2 // add

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 173why, one step back
173

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 174–183why, one step back
174 // rename
175 d.x = a176 delete d.a177 },178 [179 {op: "add", path: ["x"], value: {b: 2, y: 2}},180 {op: "remove", path: ["a"]}181 ]182 )183 })

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 184why, one step back
184

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 185–192why, one step back
185 describe("deeply nested object (with changes)", () => {
186 runPatchTest(187 {a: {b: {c: 1, d: 1}}},188 d => {189 let b = d.a.b190 b.c = 2 // change191 delete b.d // delete192 b.y = 2 // add

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 193why, one step back
193

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 194–203why, one step back
194 // rename
195 d.a.x = b196 delete d.a.b197 },198 [199 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},200 {op: "remove", path: ["a", "b"]}201 ]202 )203 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 204–206why, one step back
204})
205206describe("minimum amount of changes", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 207–218why, one step back
207 runPatchTest(
208 {x: 3, y: {a: 4}, z: 3},209 d => {210 d.y.a = 4211 d.y.b = 5212 Object.assign(d, {x: 4, y: {a: 2}})213 },214 [215 {op: "replace", path: ["x"], value: 4},216 {op: "replace", path: ["y"], value: {a: 2}}217 ]218 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 219–221why, one step back
219})
220221describe("arrays - prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 222–228why, one step back
222 runPatchTest(
223 {x: [1, 2, 3]},224 d => {225 d.x.unshift(4)226 },227 [{op: "add", path: ["x", 0], value: 4}]228 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 229–231why, one step back
229})
230231describe("arrays - multiple prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 232–242why, one step back
232 runPatchTest(
233 {x: [1, 2, 3]},234 d => {235 d.x.unshift(4)236 d.x.unshift(5)237 },238 [239 {op: "add", path: ["x", 0], value: 5},240 {op: "add", path: ["x", 1], value: 4}241 ]242 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 243–245why, one step back
243})
244245describe("arrays - splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 246–252why, one step back
246 runPatchTest(
247 {x: [1, 2, 3]},248 d => {249 d.x.splice(1, 1)250 },251 [{op: "remove", path: ["x", 1]}]252 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 253–255why, one step back
253})
254255describe("arrays - multiple splice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 256–268why, one step back
256 runPatchTest(
257 [0, 1, 2, 3, 4, 5, 0],258 d => {259 d.splice(4, 2, 3)260 d.splice(1, 2, 3)261 },262 [263 {op: "replace", path: [1], value: 3},264 {op: "replace", path: [2], value: 3},265 {op: "remove", path: [5]},266 {op: "remove", path: [4]}267 ]268 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 269–271why, one step back
269})
270271describe("arrays - modify and shrink", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 272–282why, one step back
272 runPatchTest(
273 {x: [1, 2, 3]},274 d => {275 d.x[0] = 4276 d.x.length = 2277 },278 [279 {op: "replace", path: ["x", 0], value: 4},280 {op: "replace", path: ["x", "length"], value: 2}281 ]282 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 283–285why, one step back
283})
284285describe("arrays - prepend then splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 286–296why, one step back
286 runPatchTest(
287 {x: [1, 2, 3]},288 d => {289 d.x.unshift(4)290 d.x.splice(2, 1)291 },292 [293 {op: "replace", path: ["x", 0], value: 4},294 {op: "replace", path: ["x", 1], value: 1}295 ]296 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 297–299why, one step back
297})
298299describe("arrays - splice middle then prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 300–310why, one step back
300 runPatchTest(
301 {x: [1, 2, 3]},302 d => {303 d.x.splice(1, 1)304 d.x.unshift(4)305 },306 [307 {op: "replace", path: ["x", 0], value: 4},308 {op: "replace", path: ["x", 1], value: 1}309 ]310 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 311–313why, one step back
311})
312313describe("arrays - truncate", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 314–324why, one step back
314 runPatchTest(
315 {x: [1, 2, 3]},316 d => {317 d.x.length -= 2318 },319 [{op: "replace", path: ["x", "length"], value: 1}],320 [321 {op: "add", path: ["x", 1], value: 2},322 {op: "add", path: ["x", 2], value: 3}323 ]324 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 325–327why, one step back
325})
326327describe("arrays - pop twice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 328–335why, one step back
328 runPatchTest(
329 {x: [1, 2, 3]},330 d => {331 d.x.pop()332 d.x.pop()333 },334 [{op: "replace", path: ["x", "length"], value: 1}]335 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 336–338why, one step back
336})
337338describe("arrays - push multiple", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 339–349why, one step back
339 runPatchTest(
340 {x: [1, 2, 3]},341 d => {342 d.x.push(4, 5)343 },344 [345 {op: "add", path: ["x", 3], value: 4},346 {op: "add", path: ["x", 4], value: 5}347 ],348 [{op: "replace", path: ["x", "length"], value: 3}]349 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 350–352why, one step back
350})
351352describe("arrays - splice (expand)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 353–368why, one step back
353 runPatchTest(
354 {x: [1, 2, 3]},355 d => {356 d.x.splice(1, 1, 4, 5, 6)357 },358 [359 {op: "replace", path: ["x", 1], value: 4},360 {op: "add", path: ["x", 2], value: 5},361 {op: "add", path: ["x", 3], value: 6}362 ],363 [364 {op: "replace", path: ["x", 1], value: 2},365 {op: "remove", path: ["x", 3]},366 {op: "remove", path: ["x", 2]}367 ]368 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 369–371why, one step back
369})
370371describe("arrays - splice (shrink)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 372–387why, one step back
372 runPatchTest(
373 {x: [1, 2, 3, 4, 5]},374 d => {375 d.x.splice(1, 3, 6)376 },377 [378 {op: "replace", path: ["x", 1], value: 6},379 {op: "remove", path: ["x", 3]},380 {op: "remove", path: ["x", 2]}381 ],382 [383 {op: "replace", path: ["x", 1], value: 2},384 {op: "add", path: ["x", 2], value: 3},385 {op: "add", path: ["x", 3], value: 4}386 ]387 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 388–390why, one step back
388})
389390describe("simple replacement", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 391why, one step back
391 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 392–394why, one step back
392})
393394describe("same value replacement - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 395–402why, one step back
395 runPatchTest(
396 {x: {y: 3}},397 d => {398 const a = d.x399 d.x = a400 },401 []402 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 403–405why, one step back
403})
404405describe("same value replacement - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 406–414why, one step back
406 runPatchTest(
407 {x: {y: 3}},408 d => {409 const a = d.x410 d.x = 4411 d.x = a412 },413 []414 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 415–417why, one step back
415})
416417describe("same value replacement - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 418–424why, one step back
418 runPatchTest(
419 {x: 3},420 d => {421 d.x = 3422 },423 []424 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 425–427why, one step back
425})
426427describe("same value replacement - 4", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 428–435why, one step back
428 runPatchTest(
429 {x: 3},430 d => {431 d.x = 4432 d.x = 3433 },434 []435 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 436why, one step back
436})

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 437why, one step back
437

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 438why, one step back
438describe("simple delete", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 439–450why, one step back
439 runPatchTest(
440 {x: 2},441 d => {442 delete d.x443 },444 [445 {446 op: "remove",447 path: ["x"]448 }449 ]450 )

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

lines 451–511why, one step back
451})
452453describe("patch compressions yields correct results", () => {454 let p1, p2455 runPatchTest(456 {},457 d => {458 d.x = {test: true}459 },460 (p1 = [461 {462 op: "add",463 path: ["x"],464 value: {465 test: true466 }467 }468 ])469 )470 runPatchTest(471 {x: {test: true}},472 d => {473 delete d.x474 },475 (p2 = [476 {477 op: "remove",478 path: ["x"]479 }480 ])481 )482 const res = runPatchTest(483 {},484 d => {485 applyPatches(d, [...p1, ...p2])486 },487 []488 )489490 expect(res).toEqual({})491})492493describe("change then delete property", () => {494 const res = runPatchTest(495 {496 x: 1497 },498 d => {499 d.x = 2500 delete d.x501 },502 [503 {504 op: "remove",505 path: ["x"]506 }507 ]508 )509 test("valid result", () => {510 expect(res).toEqual({})511 })

fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e

lines 512–556why, one step back
512})
513514test("replaying patches with interweaved replacements should work correctly", () => {515 const patches = []516 const s0 = {x: 1}517518 const s1 = produce(519 s0,520 draft => {521 draft.x = 2522 },523 p => {524 patches.push(...p)525 }526 )527528 const s2 = produce(529 s1,530 draft => {531 return {x: 0}532 },533 p => {534 patches.push(...p)535 }536 )537538 const s3 = produce(539 s2,540 draft => {541 draft.x--542 },543 p => {544 patches.push(...p)545 }546 )547548 expect(s3).toEqual({x: -1}) // correct result549 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay550551 // manual replay on a draft should also be correct552 expect(553 produce(s0, draft => {554 return applyPatches(draft, patches)555 })556 ).toEqual({x: -1})

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 557why, one step back
557})