Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

580 lines · 10.1 KiB · JavaScript

Last changed by

Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d

History of this file

Take it

The walk stopped after 200 commits: lines older than 2f2eb9f point at it.

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 111–113why, one step back
111})
112113describe("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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 151–153why, one step back
151})
152153describe("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 })

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

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

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

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 })

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

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

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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(ci): build before releasealeclarsonbefore adoption2f2eb9f

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–277why, one step back
272 runPatchTest(
273 {x: [1, 2, 3]},274 d => {275 d.x[0] = 4276 d.x.length = 2277 },

Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d

line 278why, one step back
278 [{op: "replace", path: ["x", 0], value: 4}, {op: "remove", path: ["x", 2]}],

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 279why, one step back
279 [

Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d

lines 280–281why, one step back
280 {op: "replace", path: ["x", 0], value: 1},
281 {op: "add", path: ["x", 2], value: 3}

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 282–283why, one step back
282 ]
283 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 284–286why, one step back
284})
285286describe("arrays - prepend then splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 298–300why, one step back
298})
299300describe("arrays - splice middle then prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 312–314why, one step back
312})
313314describe("arrays - truncate", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 315–319why, one step back
315 runPatchTest(
316 {x: [1, 2, 3]},317 d => {318 d.x.length -= 2319 },

Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d

line 320why, one step back
320 [{op: "remove", path: ["x", 2]}, {op: "remove", path: ["x", 1]}],

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 321–325why, one step back
321 [
322 {op: "add", path: ["x", 1], value: 2},323 {op: "add", path: ["x", 2], value: 3}324 ]325 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 326–328why, one step back
326})
327328describe("arrays - pop twice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 329–334why, one step back
329 runPatchTest(
330 {x: [1, 2, 3]},331 d => {332 d.x.pop()333 d.x.pop()334 },

Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d

line 335why, one step back
335 [{op: "remove", path: ["x", 2]}, {op: "remove", path: ["x", 1]}]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 336why, one step back
336 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 337–339why, one step back
337})
338339describe("arrays - push multiple", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d

line 349why, one step back
349 [{op: "remove", path: ["x", 4]}, {op: "remove", path: ["x", 3]}]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 350why, one step back
350 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 351–353why, one step back
351})
352353describe("arrays - splice (expand)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 370–372why, one step back
370})
371372describe("arrays - splice (shrink)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d

lines 388–409why, one step back
388 )
389})390391describe("arrays - splice should should result in remove op.", () => {392 runPatchTest(393 [1, 2],394 d => {395 d.splice(1, 1)396 },397 [{op: "remove", path: [1]}],398 [{op: "add", path: [1], value: 2}]399 )400})401402describe("arrays - NESTED splice should should result in remove op.", () => {403 runPatchTest(404 {a: {b: {c: [1, 2]}}},405 d => {406 d.a.b.c.splice(1, 1)407 },408 [{op: "remove", path: ["a", "b", "c", 1]}],409 [{op: "add", path: ["a", "b", "c", 1], value: 2}]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 410why, one step back
410 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 411–413why, one step back
411})
412413describe("simple replacement", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 418–425why, one step back
418 runPatchTest(
419 {x: {y: 3}},420 d => {421 const a = d.x422 d.x = a423 },424 []425 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 426–428why, one step back
426})
427428describe("same value replacement - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 429–437why, one step back
429 runPatchTest(
430 {x: {y: 3}},431 d => {432 const a = d.x433 d.x = 4434 d.x = a435 },436 []437 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 438–440why, one step back
438})
439440describe("same value replacement - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 441–447why, one step back
441 runPatchTest(
442 {x: 3},443 d => {444 d.x = 3445 },446 []447 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 448–450why, one step back
448})
449450describe("same value replacement - 4", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 451–458why, one step back
451 runPatchTest(
452 {x: 3},453 d => {454 d.x = 4455 d.x = 3456 },457 []458 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 459–461why, one step back
459})
460461describe("simple delete", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 462–473why, one step back
462 runPatchTest(
463 {x: 2},464 d => {465 delete d.x466 },467 [468 {469 op: "remove",470 path: ["x"]471 }472 ]473 )

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

lines 474–534why, one step back
474})
475476describe("patch compressions yields correct results", () => {477 let p1, p2478 runPatchTest(479 {},480 d => {481 d.x = {test: true}482 },483 (p1 = [484 {485 op: "add",486 path: ["x"],487 value: {488 test: true489 }490 }491 ])492 )493 runPatchTest(494 {x: {test: true}},495 d => {496 delete d.x497 },498 (p2 = [499 {500 op: "remove",501 path: ["x"]502 }503 ])504 )505 const res = runPatchTest(506 {},507 d => {508 applyPatches(d, [...p1, ...p2])509 },510 []511 )512513 expect(res).toEqual({})514})515516describe("change then delete property", () => {517 const res = runPatchTest(518 {519 x: 1520 },521 d => {522 d.x = 2523 delete d.x524 },525 [526 {527 op: "remove",528 path: ["x"]529 }530 ]531 )532 test("valid result", () => {533 expect(res).toEqual({})534 })

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

lines 535–579why, one step back
535})
536537test("replaying patches with interweaved replacements should work correctly", () => {538 const patches = []539 const s0 = {x: 1}540541 const s1 = produce(542 s0,543 draft => {544 draft.x = 2545 },546 p => {547 patches.push(...p)548 }549 )550551 const s2 = produce(552 s1,553 draft => {554 return {x: 0}555 },556 p => {557 patches.push(...p)558 }559 )560561 const s3 = produce(562 s2,563 draft => {564 draft.x--565 },566 p => {567 patches.push(...p)568 }569 )570571 expect(s3).toEqual({x: -1}) // correct result572 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay573574 // manual replay on a draft should also be correct575 expect(576 produce(s0, draft => {577 return applyPatches(draft, patches)578 })579 ).toEqual({x: -1})

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

line 580why, one step back
580})