Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

863 lines · 15.5 KiB · JavaScript

Last changed by

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

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

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 143–200why, one step back
143describe("simple assignment - 4", () => {
144 runPatchTest(145 new Map([["x", {y: 4}]]),146 d => {147 d.get("x").y++148 },149 [{op: "replace", path: ["x", "y"], value: 5}],150 [{op: "replace", path: ["x", "y"], value: 4}]151 )152})153154describe("simple assignment - 5", () => {155 runPatchTest(156 {x: new Map([["y", 4]])},157 d => {158 d.x.set("y", 5)159 },160 [{op: "replace", path: ["x", "y"], value: 5}],161 [{op: "replace", path: ["x", "y"], value: 4}]162 )163})164165describe("simple assignment - 6", () => {166 runPatchTest(167 new Map([["x", 1]]),168 d => {169 // Map.prototype.set should return the Map itself170 const res = d.set("x", 2)171 res.set("y", 3)172 },173 [174 {op: "replace", path: ["x"], value: 2},175 {op: "add", path: ["y"], value: 3}176 ],177 [{op: "replace", path: ["x"], value: 1}, {op: "remove", path: ["y"]}]178 )179})180181describe("simple assignment - 7", () => {182 const key1 = {prop: "val1"}183 const key2 = {prop: "val2"}184 runPatchTest(185 {x: new Map([[key1, 4]])},186 d => {187 d.x.set(key1, 5)188 d.x.set(key2, 6)189 },190 [191 {op: "replace", path: ["x", key1], value: 5},192 {op: "add", path: ["x", key2], value: 6}193 ],194 [195 {op: "replace", path: ["x", key1], value: 4},196 {op: "remove", path: ["x", key2]}197 ]198 )199})200

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 202–208why, one step back
202 runPatchTest(
203 {x: {y: 4}},204 d => {205 delete d.x206 },207 [{op: "remove", path: ["x"]}]208 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 209–210why, one step back
209})
210

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 211–271why, one step back
211describe("delete 2", () => {
212 runPatchTest(213 new Map([["x", 1]]),214 d => {215 d.delete("x")216 },217 [{op: "remove", path: ["x"]}],218 [{op: "add", path: ["x"], value: 1}]219 )220})221222describe("delete 3", () => {223 runPatchTest(224 {x: new Map([["y", 1]])},225 d => {226 d.x.delete("y")227 },228 [{op: "remove", path: ["x", "y"]}],229 [{op: "add", path: ["x", "y"], value: 1}]230 )231})232233describe("delete 5", () => {234 const key1 = {prop: "val1"}235 const key2 = {prop: "val2"}236 runPatchTest(237 {x: new Map([[key1, 1], [key2, 2]])},238 d => {239 d.x.delete(key1)240 d.x.delete(key2)241 },242 [{op: "remove", path: ["x", key1]}, {op: "remove", path: ["x", key2]}],243 [244 {op: "add", path: ["x", key1], value: 1},245 {op: "add", path: ["x", key2], value: 2}246 ]247 )248})249250describe("delete 6", () => {251 runPatchTest(252 new Set(["x", 1]),253 d => {254 d.delete("x")255 },256 [{op: "remove", path: [0], value: "x"}],257 [{op: "add", path: [0], value: "x"}]258 )259})260261describe("delete 7", () => {262 runPatchTest(263 {x: new Set(["y", 1])},264 d => {265 d.x.delete("y")266 },267 [{op: "remove", path: ["x", 0], value: "y"}],268 [{op: "add", path: ["x", 0], value: "y"}]269 )270})271

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 273–282why, one step back
273 describe("nested object (no changes)", () => {
274 runPatchTest(275 {a: {b: 1}},276 d => {277 d.x = d.a278 delete d.a279 },280 [{op: "add", path: ["x"], value: {b: 1}}, {op: "remove", path: ["a"]}]281 )282 })

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

line 283why, one step back
283

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 284–301why, one step back
284 describe("nested map (no changes)", () => {
285 runPatchTest(286 new Map([["a", new Map([["b", 1]])]]),287 d => {288 d.set("x", d.get("a"))289 d.delete("a")290 },291 [292 {op: "add", path: ["x"], value: new Map([["b", 1]])},293 {op: "remove", path: ["a"]}294 ],295 [296 {op: "remove", path: ["x"]},297 {op: "add", path: ["a"], value: new Map([["b", 1]])}298 ]299 )300 })301

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 302–309why, one step back
302 describe("nested object (with changes)", () => {
303 runPatchTest(304 {a: {b: 1, c: 1}},305 d => {306 let a = d.a307 a.b = 2 // change308 delete a.c // delete309 a.y = 2 // add

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

line 310why, one step back
310

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 311–320why, one step back
311 // rename
312 d.x = a313 delete d.a314 },315 [316 {op: "add", path: ["x"], value: {b: 2, y: 2}},317 {op: "remove", path: ["a"]}318 ]319 )320 })

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

line 321why, one step back
321

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 322–345why, one step back
322 describe("nested map (with changes)", () => {
323 runPatchTest(324 new Map([["a", new Map([["b", 1], ["c", 1]])]]),325 d => {326 let a = d.get("a")327 a.set("b", 2) // change328 a.delete("c") // delete329 a.set("y", 2) // add330331 // rename332 d.set("x", a)333 d.delete("a")334 },335 [336 {op: "add", path: ["x"], value: new Map([["b", 2], ["y", 2]])},337 {op: "remove", path: ["a"]}338 ],339 [340 {op: "remove", path: ["x"]},341 {op: "add", path: ["a"], value: new Map([["b", 1], ["c", 1]])}342 ]343 )344 })345

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 346–353why, one step back
346 describe("deeply nested object (with changes)", () => {
347 runPatchTest(348 {a: {b: {c: 1, d: 1}}},349 d => {350 let b = d.a.b351 b.c = 2 // change352 delete b.d // delete353 b.y = 2 // add

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

line 354why, one step back
354

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 355–364why, one step back
355 // rename
356 d.a.x = b357 delete d.a.b358 },359 [360 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},361 {op: "remove", path: ["a", "b"]}362 ]363 )364 })

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 365–396why, one step back
365
366 describe("deeply nested map (with changes)", () => {367 runPatchTest(368 new Map([["a", new Map([["b", new Map([["c", 1], ["d", 1]])]])]]),369 d => {370 let b = d.get("a").get("b")371 b.set("c", 2) // change372 b.delete("d") // delete373 b.set("y", 2) // add374375 // rename376 d.get("a").set("x", b)377 d.get("a").delete("b")378 },379 [380 {381 op: "add",382 path: ["a", "x"],383 value: new Map([["c", 2], ["y", 2]])384 },385 {op: "remove", path: ["a", "b"]}386 ],387 [388 {op: "remove", path: ["a", "x"]},389 {390 op: "add",391 path: ["a", "b"],392 value: new Map([["c", 1], ["d", 1]])393 }394 ]395 )396 })

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 397–399why, one step back
397})
398399describe("minimum amount of changes", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 400–411why, one step back
400 runPatchTest(
401 {x: 3, y: {a: 4}, z: 3},402 d => {403 d.y.a = 4404 d.y.b = 5405 Object.assign(d, {x: 4, y: {a: 2}})406 },407 [408 {op: "replace", path: ["x"], value: 4},409 {op: "replace", path: ["y"], value: {a: 2}}410 ]411 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 412–414why, one step back
412})
413414describe("arrays - prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 415–421why, one step back
415 runPatchTest(
416 {x: [1, 2, 3]},417 d => {418 d.x.unshift(4)419 },420 [{op: "add", path: ["x", 0], value: 4}]421 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 422–424why, one step back
422})
423424describe("arrays - multiple prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 425–435why, one step back
425 runPatchTest(
426 {x: [1, 2, 3]},427 d => {428 d.x.unshift(4)429 d.x.unshift(5)430 },431 [432 {op: "add", path: ["x", 0], value: 5},433 {op: "add", path: ["x", 1], value: 4}434 ]435 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 436–438why, one step back
436})
437438describe("arrays - splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 439–445why, one step back
439 runPatchTest(
440 {x: [1, 2, 3]},441 d => {442 d.x.splice(1, 1)443 },444 [{op: "remove", path: ["x", 1]}]445 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 446–448why, one step back
446})
447448describe("arrays - multiple splice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 449–461why, one step back
449 runPatchTest(
450 [0, 1, 2, 3, 4, 5, 0],451 d => {452 d.splice(4, 2, 3)453 d.splice(1, 2, 3)454 },455 [456 {op: "replace", path: [1], value: 3},457 {op: "replace", path: [2], value: 3},458 {op: "remove", path: [5]},459 {op: "remove", path: [4]}460 ]461 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 462–464why, one step back
462})
463464describe("arrays - modify and shrink", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 465–470why, one step back
465 runPatchTest(
466 {x: [1, 2, 3]},467 d => {468 d.x[0] = 4469 d.x.length = 2470 },

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 472why, one step back
472 [

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 475–476why, one step back
475 ]
476 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 477–479why, one step back
477})
478479describe("arrays - prepend then splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 480–490why, one step back
480 runPatchTest(
481 {x: [1, 2, 3]},482 d => {483 d.x.unshift(4)484 d.x.splice(2, 1)485 },486 [487 {op: "replace", path: ["x", 0], value: 4},488 {op: "replace", path: ["x", 1], value: 1}489 ]490 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 491–493why, one step back
491})
492493describe("arrays - splice middle then prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 494–504why, one step back
494 runPatchTest(
495 {x: [1, 2, 3]},496 d => {497 d.x.splice(1, 1)498 d.x.unshift(4)499 },500 [501 {op: "replace", path: ["x", 0], value: 4},502 {op: "replace", path: ["x", 1], value: 1}503 ]504 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 505–507why, one step back
505})
506507describe("arrays - truncate", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 508–512why, one step back
508 runPatchTest(
509 {x: [1, 2, 3]},510 d => {511 d.x.length -= 2512 },

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 514–518why, one step back
514 [
515 {op: "add", path: ["x", 1], value: 2},516 {op: "add", path: ["x", 2], value: 3}517 ]518 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 519–521why, one step back
519})
520521describe("arrays - pop twice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 522–527why, one step back
522 runPatchTest(
523 {x: [1, 2, 3]},524 d => {525 d.x.pop()526 d.x.pop()527 },

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 529why, one step back
529 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 530–532why, one step back
530})
531532describe("arrays - push multiple", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 533–541why, one step back
533 runPatchTest(
534 {x: [1, 2, 3]},535 d => {536 d.x.push(4, 5)537 },538 [539 {op: "add", path: ["x", 3], value: 4},540 {op: "add", path: ["x", 4], value: 5}541 ],

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 543why, one step back
543 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 544–546why, one step back
544})
545546describe("arrays - splice (expand)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 547–562why, one step back
547 runPatchTest(
548 {x: [1, 2, 3]},549 d => {550 d.x.splice(1, 1, 4, 5, 6)551 },552 [553 {op: "replace", path: ["x", 1], value: 4},554 {op: "add", path: ["x", 2], value: 5},555 {op: "add", path: ["x", 3], value: 6}556 ],557 [558 {op: "replace", path: ["x", 1], value: 2},559 {op: "remove", path: ["x", 3]},560 {op: "remove", path: ["x", 2]}561 ]562 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 563–565why, one step back
563})
564565describe("arrays - splice (shrink)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 566–580why, one step back
566 runPatchTest(
567 {x: [1, 2, 3, 4, 5]},568 d => {569 d.x.splice(1, 3, 6)570 },571 [572 {op: "replace", path: ["x", 1], value: 6},573 {op: "remove", path: ["x", 3]},574 {op: "remove", path: ["x", 2]}575 ],576 [577 {op: "replace", path: ["x", 1], value: 2},578 {op: "add", path: ["x", 2], value: 3},579 {op: "add", path: ["x", 3], value: 4}580 ]

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

lines 581–583why, one step back
581 )
582})583

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 584–649why, one step back
584describe("sets - add - 1", () => {
585 runPatchTest(586 new Set([1]),587 d => {588 d.add(2)589 },590 [{op: "add", path: [1], value: 2}],591 [{op: "remove", path: [1], value: 2}]592 )593})594595describe("sets - add, delete, add - 1", () => {596 runPatchTest(597 new Set([1]),598 d => {599 d.add(2)600 d.delete(2)601 d.add(2)602 },603 [{op: "add", path: [1], value: 2}],604 [{op: "remove", path: [1], value: 2}]605 )606})607608describe("sets - add, delete, add - 2", () => {609 runPatchTest(610 new Set([2, 1]),611 d => {612 d.add(2)613 d.delete(2)614 d.add(2)615 },616 [],617 []618 )619})620621describe("sets - mutate - 1", () => {622 const findById = (set, id) => {623 for (const item of set) {624 if (item.id === id) return item625 }626 }627 runPatchTest(628 new Set([{id: 1, val: "We"}, {id: 2, val: "will"}]),629 d => {630 const obj1 = findById(d, 1)631 const obj2 = findById(d, 2)632 obj1.val = "rock"633 obj2.val = "you"634 },635 [636 {op: "remove", path: [0], value: {id: 1, val: "We"}},637 {op: "remove", path: [1], value: {id: 2, val: "will"}},638 {op: "add", path: [0], value: {id: 1, val: "rock"}},639 {op: "add", path: [1], value: {id: 2, val: "you"}}640 ],641 [642 {op: "remove", path: [1], value: {id: 2, val: "you"}},643 {op: "remove", path: [0], value: {id: 1, val: "rock"}},644 {op: "add", path: [1], value: {id: 2, val: "will"}},645 {op: "add", path: [0], value: {id: 1, val: "We"}}646 ]647 )648})649

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

lines 650–668why, one step back
650describe("arrays - splice should should result in remove op.", () => {
651 runPatchTest(652 [1, 2],653 d => {654 d.splice(1, 1)655 },656 [{op: "remove", path: [1]}],657 [{op: "add", path: [1], value: 2}]658 )659})660661describe("arrays - NESTED splice should should result in remove op.", () => {662 runPatchTest(663 {a: {b: {c: [1, 2]}}},664 d => {665 d.a.b.c.splice(1, 1)666 },667 [{op: "remove", path: ["a", "b", "c", 1]}],668 [{op: "add", path: ["a", "b", "c", 1], value: 2}]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 669why, one step back
669 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 670–672why, one step back
670})
671672describe("simple replacement", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 674–676why, one step back
674})
675676describe("same value replacement - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 677–684why, one step back
677 runPatchTest(
678 {x: {y: 3}},679 d => {680 const a = d.x681 d.x = a682 },683 []684 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 685–687why, one step back
685})
686687describe("same value replacement - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 688–696why, one step back
688 runPatchTest(
689 {x: {y: 3}},690 d => {691 const a = d.x692 d.x = 4693 d.x = a694 },695 []696 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 697–699why, one step back
697})
698699describe("same value replacement - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 700–706why, one step back
700 runPatchTest(
701 {x: 3},702 d => {703 d.x = 3704 },705 []706 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 707–709why, one step back
707})
708709describe("same value replacement - 4", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 710–715why, one step back
710 runPatchTest(
711 {x: 3},712 d => {713 d.x = 4714 d.x = 3715 },

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 716–739why, one step back
716 []
717 )718})719720describe("same value replacement - 5", () => {721 runPatchTest(722 new Map([["x", 3]]),723 d => {724 d.set("x", 4)725 d.set("x", 3)726 },727 [],728 []729 )730})731732describe("same value replacement - 6", () => {733 runPatchTest(734 new Set(["x", 3]),735 d => {736 d.delete("x")737 d.add("x")738 },739 [],

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 740–741why, one step back
740 []
741 )

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

lines 742–744why, one step back
742})
743744describe("simple delete", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 745–756why, one step back
745 runPatchTest(
746 {x: 2},747 d => {748 delete d.x749 },750 [751 {752 op: "remove",753 path: ["x"]754 }755 ]756 )

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

lines 757–817why, one step back
757})
758759describe("patch compressions yields correct results", () => {760 let p1, p2761 runPatchTest(762 {},763 d => {764 d.x = {test: true}765 },766 (p1 = [767 {768 op: "add",769 path: ["x"],770 value: {771 test: true772 }773 }774 ])775 )776 runPatchTest(777 {x: {test: true}},778 d => {779 delete d.x780 },781 (p2 = [782 {783 op: "remove",784 path: ["x"]785 }786 ])787 )788 const res = runPatchTest(789 {},790 d => {791 applyPatches(d, [...p1, ...p2])792 },793 []794 )795796 expect(res).toEqual({})797})798799describe("change then delete property", () => {800 const res = runPatchTest(801 {802 x: 1803 },804 d => {805 d.x = 2806 delete d.x807 },808 [809 {810 op: "remove",811 path: ["x"]812 }813 ]814 )815 test("valid result", () => {816 expect(res).toEqual({})817 })

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

lines 818–862why, one step back
818})
819820test("replaying patches with interweaved replacements should work correctly", () => {821 const patches = []822 const s0 = {x: 1}823824 const s1 = produce(825 s0,826 draft => {827 draft.x = 2828 },829 p => {830 patches.push(...p)831 }832 )833834 const s2 = produce(835 s1,836 draft => {837 return {x: 0}838 },839 p => {840 patches.push(...p)841 }842 )843844 const s3 = produce(845 s2,846 draft => {847 draft.x--848 },849 p => {850 patches.push(...p)851 }852 )853854 expect(s3).toEqual({x: -1}) // correct result855 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay856857 // manual replay on a draft should also be correct858 expect(859 produce(s0, draft => {860 return applyPatches(draft, patches)861 })862 ).toEqual({x: -1})

fix(ci): build before releasealeclarsonbefore adoption2f2eb9f

line 863why, one step back
863})