Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

863 lines · 15.5 KiB · JavaScript

Last changed by

Merge branch 'master' into v4Michel Weststratebefore adoption741e941

History of this file

Take it

The walk stopped after 200 commits: lines older than 53e9ce8 point at it.

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

Merge branch 'master' into v4Michel Weststratebefore adoption741e941

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

Merge pull request #437 from runnez/es5-map-and-set-supportMichel Weststratebefore adoption85656cb

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

Merge branch 'master' into v4Michel Weststratebefore adoption741e941

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 13–32why, one step back
13 function runPatchTestHelper() {
14 let recordedPatches15 let recordedInversePatches16 const res = produce(base, producer, (p, i) => {17 recordedPatches = p18 recordedInversePatches = i19 })2021 test("produces the correct patches", () => {22 expect(recordedPatches).toEqual(patches)23 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)24 })2526 test("patches are replayable", () => {27 expect(applyPatches(base, recordedPatches)).toEqual(res)28 })2930 test("patches can be reversed", () => {31 expect(applyPatches(res, recordedInversePatches)).toEqual(base)32 })

Merge branch 'master' into v4Michel Weststratebefore adoption741e941

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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

Merge branch 'master' into v4Michel Weststratebefore adoption741e941

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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

Merge branch 'master' into v4Michel Weststratebefore adoption741e941

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

line 48why, one step back
48 })

Merge branch 'master' into v4Michel Weststratebefore adoption741e941

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 51why, one step back
51}

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

line 52why, one step back
52

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 53why, one step back
53describe("applyPatches", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 54–88why, 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()8283 // missing grand-parent84 expect(() => {85 const patch = {op: "add", path: ["a", "b", "c"], value: 1}86 applyPatches({}, [patch])87 }).toThrowErrorMatchingSnapshot()88 })

Merge branch 'master' into v4Michel Weststratebefore adoption741e941

lines 89–110why, one step back
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)110 })

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 124–132why, 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 )131})132

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 134–142why, 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 )141})142

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 144–153why, one step back
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})153

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 155–164why, one step back
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})164

fix: Map.prototype.set should return a draftAndrey Goncharovbefore adoption7001201

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 166–180why, one step back
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})180

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 182–200why, one step back
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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 211why, one step back
211describe("delete 2", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 212–221why, one step back
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})221

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 222why, one step back
222describe("delete 3", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 223–232why, one step back
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})232

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

line 233why, one step back
233describe("delete 5", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 234–249why, one step back
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})249

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

line 250why, one step back
250describe("delete 6", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 251–260why, one step back
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})260

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

line 261why, one step back
261describe("delete 7", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 262–271why, one step back
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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 273–398why, 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 })283284 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 })301302 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 // add310311 // rename312 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 })321322 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 })345346 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 // add354355 // rename356 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 })365366 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 })397})398

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 399why, one step back
399describe("minimum amount of changes", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 400–413why, 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 )412})413

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 414why, one step back
414describe("arrays - prepend", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 415–423why, 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 )422})423

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

line 424why, one step back
424describe("arrays - multiple prepend", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 425–437why, 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 )436})437

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 438why, one step back
438describe("arrays - splice middle", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 439–447why, 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 )446})447

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

line 448why, one step back
448describe("arrays - multiple splice", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 449–463why, 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 )462})463

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

line 464why, one step back
464describe("arrays - modify and shrink", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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 branch 'master' into v4Michel Weststratebefore adoption741e941

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

line 472why, one step back
472 [

Merge branch 'master' into v4Michel Weststratebefore adoption741e941

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 475–478why, one step back
475 ]
476 )477})478

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 479why, one step back
479describe("arrays - prepend then splice middle", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 480–492why, 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 )491})492

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 493why, one step back
493describe("arrays - splice middle then prepend", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 494–506why, 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 )505})506

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 507why, one step back
507describe("arrays - truncate", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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

Merge branch 'master' into v4Michel Weststratebefore adoption741e941

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 521why, one step back
521describe("arrays - pop twice", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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 branch 'master' into v4Michel Weststratebefore adoption741e941

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 529–531why, one step back
529 )
530})531

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 532why, one step back
532describe("arrays - push multiple", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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 branch 'master' into v4Michel Weststratebefore adoption741e941

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 543–545why, one step back
543 )
544})545

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

line 546why, one step back
546describe("arrays - splice (expand)", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 547–564why, 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 )563})564

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

line 565why, one step back
565describe("arrays - splice (shrink)", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 566–583why, 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 ]581 )582})583

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 584why, one step back
584describe("sets - add - 1", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 585–594why, one step back
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})594

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 595why, one step back
595describe("sets - add, delete, add - 1", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 596–607why, one step back
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})607

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 608why, one step back
608describe("sets - add, delete, add - 2", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 609–620why, one step back
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})620

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 621why, one step back
621describe("sets - mutate - 1", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 622–649why, one step back
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 branch 'master' into v4Michel Weststratebefore adoption741e941

lines 650–671why, 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}]669 )670})671

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 672why, one step back
672describe("simple replacement", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 676why, one step back
676describe("same value replacement - 1", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 687why, one step back
687describe("same value replacement - 2", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 699why, one step back
699describe("same value replacement - 3", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 709why, one step back
709describe("same value replacement - 4", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 710–719why, one step back
710 runPatchTest(
711 {x: 3},712 d => {713 d.x = 4714 d.x = 3715 },716 []717 )718})719

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 720why, one step back
720describe("same value replacement - 5", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 721–731why, one step back
721 runPatchTest(
722 new Map([["x", 3]]),723 d => {724 d.set("x", 4)725 d.set("x", 3)726 },727 [],728 []729 )730})731

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 732why, one step back
732describe("same value replacement - 6", () => {

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

lines 733–743why, one step back
733 runPatchTest(
734 new Set(["x", 3]),735 d => {736 d.delete("x")737 d.add("x")738 },739 [],740 []741 )742})743

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

run prettier with master config before mergingMichel Weststratebefore adoptionae80361

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 )

Merge branch 'master' into v4Michel Weststratebefore adoption741e941

lines 757–862why, 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 })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})

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 863why, one step back
863})