Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

979 lines · 16.5 KiB · JavaScript

Last changed by

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

History of this file

Take it

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

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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"

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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 }

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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–176why, 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 ],

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 177–180why, one step back
177 [
178 {op: "replace", path: ["x"], value: 1},179 {op: "remove", path: ["y"]}180 ]

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

lines 181–203why, one step back
181 )
182})183184describe("simple assignment - 7", () => {185 const key1 = {prop: "val1"}186 const key2 = {prop: "val2"}187 runPatchTest(188 {x: new Map([[key1, 4]])},189 d => {190 d.x.set(key1, 5)191 d.x.set(key2, 6)192 },193 [194 {op: "replace", path: ["x", key1], value: 5},195 {op: "add", path: ["x", key2], value: 6}196 ],197 [198 {op: "replace", path: ["x", key1], value: 4},199 {op: "remove", path: ["x", key2]}200 ]201 )202})203

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 205–211why, one step back
205 runPatchTest(
206 {x: {y: 4}},207 d => {208 delete d.x209 },210 [{op: "remove", path: ["x"]}]211 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 212–213why, one step back
212})
213

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

lines 214–239why, one step back
214describe("delete 2", () => {
215 runPatchTest(216 new Map([["x", 1]]),217 d => {218 d.delete("x")219 },220 [{op: "remove", path: ["x"]}],221 [{op: "add", path: ["x"], value: 1}]222 )223})224225describe("delete 3", () => {226 runPatchTest(227 {x: new Map([["y", 1]])},228 d => {229 d.x.delete("y")230 },231 [{op: "remove", path: ["x", "y"]}],232 [{op: "add", path: ["x", "y"], value: 1}]233 )234})235236describe("delete 5", () => {237 const key1 = {prop: "val1"}238 const key2 = {prop: "val2"}239 runPatchTest(

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 240–245why, one step back
240 {
241 x: new Map([242 [key1, 1],243 [key2, 2]244 ])245 },

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

lines 246–250why, one step back
246 d => {
247 d.x.delete(key1)248 d.x.delete(key2)249 },250 [

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 251–254why, one step back
251 {op: "remove", path: ["x", key1]},
252 {op: "remove", path: ["x", key2]}253 ],254 [

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

lines 255–282why, one step back
255 {op: "add", path: ["x", key1], value: 1},
256 {op: "add", path: ["x", key2], value: 2}257 ]258 )259})260261describe("delete 6", () => {262 runPatchTest(263 new Set(["x", 1]),264 d => {265 d.delete("x")266 },267 [{op: "remove", path: [0], value: "x"}],268 [{op: "add", path: [0], value: "x"}]269 )270})271272describe("delete 7", () => {273 runPatchTest(274 {x: new Set(["y", 1])},275 d => {276 d.x.delete("y")277 },278 [{op: "remove", path: ["x", 0], value: "y"}],279 [{op: "add", path: ["x", 0], value: "y"}]280 )281})282

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 284–290why, one step back
284 describe("nested object (no changes)", () => {
285 runPatchTest(286 {a: {b: 1}},287 d => {288 d.x = d.a289 delete d.a290 },

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 291–294why, one step back
291 [
292 {op: "add", path: ["x"], value: {b: 1}},293 {op: "remove", path: ["a"]}294 ]

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 295–296why, one step back
295 )
296 })

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

line 297why, one step back
297

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

lines 298–315why, one step back
298 describe("nested map (no changes)", () => {
299 runPatchTest(300 new Map([["a", new Map([["b", 1]])]]),301 d => {302 d.set("x", d.get("a"))303 d.delete("a")304 },305 [306 {op: "add", path: ["x"], value: new Map([["b", 1]])},307 {op: "remove", path: ["a"]}308 ],309 [310 {op: "remove", path: ["x"]},311 {op: "add", path: ["a"], value: new Map([["b", 1]])}312 ]313 )314 })315

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 316–323why, one step back
316 describe("nested object (with changes)", () => {
317 runPatchTest(318 {a: {b: 1, c: 1}},319 d => {320 let a = d.a321 a.b = 2 // change322 delete a.c // delete323 a.y = 2 // add

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

line 324why, one step back
324

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 325–334why, one step back
325 // rename
326 d.x = a327 delete d.a328 },329 [330 {op: "add", path: ["x"], value: {b: 2, y: 2}},331 {op: "remove", path: ["a"]}332 ]333 )334 })

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

line 335why, one step back
335

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

lines 336–337why, one step back
336 describe("nested map (with changes)", () => {
337 runPatchTest(

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 338–346why, one step back
338 new Map([
339 [340 "a",341 new Map([342 ["b", 1],343 ["c", 1]344 ])345 ]346 ]),

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

lines 347–357why, one step back
347 d => {
348 let a = d.get("a")349 a.set("b", 2) // change350 a.delete("c") // delete351 a.set("y", 2) // add352353 // rename354 d.set("x", a)355 d.delete("a")356 },357 [

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 358–365why, one step back
358 {
359 op: "add",360 path: ["x"],361 value: new Map([362 ["b", 2],363 ["y", 2]364 ])365 },

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

lines 366–369why, one step back
366 {op: "remove", path: ["a"]}
367 ],368 [369 {op: "remove", path: ["x"]},

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 370–377why, one step back
370 {
371 op: "add",372 path: ["a"],373 value: new Map([374 ["b", 1],375 ["c", 1]376 ])377 }

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

lines 378–381why, one step back
378 ]
379 )380 })381

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 382–389why, one step back
382 describe("deeply nested object (with changes)", () => {
383 runPatchTest(384 {a: {b: {c: 1, d: 1}}},385 d => {386 let b = d.a.b387 b.c = 2 // change388 delete b.d // delete389 b.y = 2 // add

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

line 390why, one step back
390

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 391–400why, one step back
391 // rename
392 d.a.x = b393 delete d.a.b394 },395 [396 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},397 {op: "remove", path: ["a", "b"]}398 ]399 )400 })

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

lines 401–403why, one step back
401
402 describe("deeply nested map (with changes)", () => {403 runPatchTest(

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 404–417why, one step back
404 new Map([
405 [406 "a",407 new Map([408 [409 "b",410 new Map([411 ["c", 1],412 ["d", 1]413 ])414 ]415 ])416 ]417 ]),

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

lines 418–431why, one step back
418 d => {
419 let b = d.get("a").get("b")420 b.set("c", 2) // change421 b.delete("d") // delete422 b.set("y", 2) // add423424 // rename425 d.get("a").set("x", b)426 d.get("a").delete("b")427 },428 [429 {430 op: "add",431 path: ["a", "x"],

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 432–435why, one step back
432 value: new Map([
433 ["c", 2],434 ["y", 2]435 ])

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

lines 436–443why, one step back
436 },
437 {op: "remove", path: ["a", "b"]}438 ],439 [440 {op: "remove", path: ["a", "x"]},441 {442 op: "add",443 path: ["a", "b"],

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 444–447why, one step back
444 value: new Map([
445 ["c", 1],446 ["d", 1]447 ])

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

lines 448–451why, one step back
448 }
449 ]450 )451 })

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 452–454why, one step back
452})
453454describe("minimum amount of changes", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 455–466why, one step back
455 runPatchTest(
456 {x: 3, y: {a: 4}, z: 3},457 d => {458 d.y.a = 4459 d.y.b = 5460 Object.assign(d, {x: 4, y: {a: 2}})461 },462 [463 {op: "replace", path: ["x"], value: 4},464 {op: "replace", path: ["y"], value: {a: 2}}465 ]466 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 467–469why, one step back
467})
468469describe("arrays - prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 470–476why, one step back
470 runPatchTest(
471 {x: [1, 2, 3]},472 d => {473 d.x.unshift(4)474 },475 [{op: "add", path: ["x", 0], value: 4}]476 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

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

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.unshift(5)485 },486 [487 {op: "add", path: ["x", 0], value: 5},488 {op: "add", path: ["x", 1], value: 4}489 ]490 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 494–500why, one step back
494 runPatchTest(
495 {x: [1, 2, 3]},496 d => {497 d.x.splice(1, 1)498 },499 [{op: "remove", path: ["x", 1]}]500 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 501–503why, one step back
501})
502503describe("arrays - multiple splice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 504–516why, one step back
504 runPatchTest(
505 [0, 1, 2, 3, 4, 5, 0],506 d => {507 d.splice(4, 2, 3)508 d.splice(1, 2, 3)509 },510 [511 {op: "replace", path: [1], value: 3},512 {op: "replace", path: [2], value: 3},513 {op: "remove", path: [5]},514 {op: "remove", path: [4]}515 ]516 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 517–519why, one step back
517})
518519describe("arrays - modify and shrink", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 520–526why, one step back
520 runPatchTest(
521 {x: [1, 2, 3]},522 d => {523 d.x[0] = 4524 d.x.length = 2525 },526 [

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 527–530why, one step back
527 {op: "replace", path: ["x", 0], value: 4},
528 {op: "remove", path: ["x", 2]}529 ],530 [

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 533–534why, one step back
533 ]
534 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 535–537why, one step back
535})
536537describe("arrays - prepend then splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 538–548why, one step back
538 runPatchTest(
539 {x: [1, 2, 3]},540 d => {541 d.x.unshift(4)542 d.x.splice(2, 1)543 },544 [545 {op: "replace", path: ["x", 0], value: 4},546 {op: "replace", path: ["x", 1], value: 1}547 ]548 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 549–551why, one step back
549})
550551describe("arrays - splice middle then prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 552–562why, one step back
552 runPatchTest(
553 {x: [1, 2, 3]},554 d => {555 d.x.splice(1, 1)556 d.x.unshift(4)557 },558 [559 {op: "replace", path: ["x", 0], value: 4},560 {op: "replace", path: ["x", 1], value: 1}561 ]562 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 566–571why, one step back
566 runPatchTest(
567 {x: [1, 2, 3]},568 d => {569 d.x.length -= 2570 },571 [

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 572–575why, one step back
572 {op: "remove", path: ["x", 2]},
573 {op: "remove", path: ["x", 1]}574 ],575 [

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 576–579why, one step back
576 {op: "add", path: ["x", 1], value: 2},
577 {op: "add", path: ["x", 2], value: 3}578 ]579 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 580–582why, one step back
580})
581582describe("arrays - pop twice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 583–588why, one step back
583 runPatchTest(
584 {x: [1, 2, 3]},585 d => {586 d.x.pop()587 d.x.pop()588 },

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 589–592why, one step back
589 [
590 {op: "remove", path: ["x", 2]},591 {op: "remove", path: ["x", 1]}592 ]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 593why, one step back
593 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 594–596why, one step back
594})
595596describe("arrays - push multiple", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 597–605why, one step back
597 runPatchTest(
598 {x: [1, 2, 3]},599 d => {600 d.x.push(4, 5)601 },602 [603 {op: "add", path: ["x", 3], value: 4},604 {op: "add", path: ["x", 4], value: 5}605 ],

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 606–609why, one step back
606 [
607 {op: "remove", path: ["x", 4]},608 {op: "remove", path: ["x", 3]}609 ]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 610why, one step back
610 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 611–613why, one step back
611})
612613describe("arrays - splice (expand)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 614–629why, one step back
614 runPatchTest(
615 {x: [1, 2, 3]},616 d => {617 d.x.splice(1, 1, 4, 5, 6)618 },619 [620 {op: "replace", path: ["x", 1], value: 4},621 {op: "add", path: ["x", 2], value: 5},622 {op: "add", path: ["x", 3], value: 6}623 ],624 [625 {op: "replace", path: ["x", 1], value: 2},626 {op: "remove", path: ["x", 3]},627 {op: "remove", path: ["x", 2]}628 ]629 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 630–632why, one step back
630})
631632describe("arrays - splice (shrink)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 633–647why, one step back
633 runPatchTest(
634 {x: [1, 2, 3, 4, 5]},635 d => {636 d.x.splice(1, 3, 6)637 },638 [639 {op: "replace", path: ["x", 1], value: 6},640 {op: "remove", path: ["x", 3]},641 {op: "remove", path: ["x", 2]}642 ],643 [644 {op: "replace", path: ["x", 1], value: 2},645 {op: "add", path: ["x", 2], value: 3},646 {op: "add", path: ["x", 3], value: 4}647 ]

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

lines 648–650why, one step back
648 )
649})650

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

lines 651–694why, one step back
651describe("sets - add - 1", () => {
652 runPatchTest(653 new Set([1]),654 d => {655 d.add(2)656 },657 [{op: "add", path: [1], value: 2}],658 [{op: "remove", path: [1], value: 2}]659 )660})661662describe("sets - add, delete, add - 1", () => {663 runPatchTest(664 new Set([1]),665 d => {666 d.add(2)667 d.delete(2)668 d.add(2)669 },670 [{op: "add", path: [1], value: 2}],671 [{op: "remove", path: [1], value: 2}]672 )673})674675describe("sets - add, delete, add - 2", () => {676 runPatchTest(677 new Set([2, 1]),678 d => {679 d.add(2)680 d.delete(2)681 d.add(2)682 },683 [],684 []685 )686})687688describe("sets - mutate - 1", () => {689 const findById = (set, id) => {690 for (const item of set) {691 if (item.id === id) return item692 }693 }694 runPatchTest(

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 695–698why, one step back
695 new Set([
696 {id: 1, val: "We"},697 {id: 2, val: "will"}698 ]),

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

lines 699–719why, one step back
699 d => {
700 const obj1 = findById(d, 1)701 const obj2 = findById(d, 2)702 obj1.val = "rock"703 obj2.val = "you"704 },705 [706 {op: "remove", path: [0], value: {id: 1, val: "We"}},707 {op: "remove", path: [1], value: {id: 2, val: "will"}},708 {op: "add", path: [0], value: {id: 1, val: "rock"}},709 {op: "add", path: [1], value: {id: 2, val: "you"}}710 ],711 [712 {op: "remove", path: [1], value: {id: 2, val: "you"}},713 {op: "remove", path: [0], value: {id: 1, val: "rock"}},714 {op: "add", path: [1], value: {id: 2, val: "will"}},715 {op: "add", path: [0], value: {id: 1, val: "We"}}716 ]717 )718})719

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

lines 720–738why, one step back
720describe("arrays - splice should should result in remove op.", () => {
721 runPatchTest(722 [1, 2],723 d => {724 d.splice(1, 1)725 },726 [{op: "remove", path: [1]}],727 [{op: "add", path: [1], value: 2}]728 )729})730731describe("arrays - NESTED splice should should result in remove op.", () => {732 runPatchTest(733 {a: {b: {c: [1, 2]}}},734 d => {735 d.a.b.c.splice(1, 1)736 },737 [{op: "remove", path: ["a", "b", "c", 1]}],738 [{op: "add", path: ["a", "b", "c", 1], value: 2}]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 739why, one step back
739 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 740–742why, one step back
740})
741742describe("simple replacement", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 744–746why, one step back
744})
745746describe("same value replacement - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 747–754why, one step back
747 runPatchTest(
748 {x: {y: 3}},749 d => {750 const a = d.x751 d.x = a752 },753 []754 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 755–757why, one step back
755})
756757describe("same value replacement - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 758–766why, one step back
758 runPatchTest(
759 {x: {y: 3}},760 d => {761 const a = d.x762 d.x = 4763 d.x = a764 },765 []766 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 767–769why, one step back
767})
768769describe("same value replacement - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 770–776why, one step back
770 runPatchTest(
771 {x: 3},772 d => {773 d.x = 3774 },775 []776 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 777–779why, one step back
777})
778779describe("same value replacement - 4", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 780–785why, one step back
780 runPatchTest(
781 {x: 3},782 d => {783 d.x = 4784 d.x = 3785 },

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

lines 786–809why, one step back
786 []
787 )788})789790describe("same value replacement - 5", () => {791 runPatchTest(792 new Map([["x", 3]]),793 d => {794 d.set("x", 4)795 d.set("x", 3)796 },797 [],798 []799 )800})801802describe("same value replacement - 6", () => {803 runPatchTest(804 new Set(["x", 3]),805 d => {806 d.delete("x")807 d.add("x")808 },809 [],

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 810–811why, one step back
810 []
811 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 812–814why, one step back
812})
813814describe("simple delete", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 815–826why, one step back
815 runPatchTest(
816 {x: 2},817 d => {818 delete d.x819 },820 [821 {822 op: "remove",823 path: ["x"]824 }825 ]826 )

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

lines 827–887why, one step back
827})
828829describe("patch compressions yields correct results", () => {830 let p1, p2831 runPatchTest(832 {},833 d => {834 d.x = {test: true}835 },836 (p1 = [837 {838 op: "add",839 path: ["x"],840 value: {841 test: true842 }843 }844 ])845 )846 runPatchTest(847 {x: {test: true}},848 d => {849 delete d.x850 },851 (p2 = [852 {853 op: "remove",854 path: ["x"]855 }856 ])857 )858 const res = runPatchTest(859 {},860 d => {861 applyPatches(d, [...p1, ...p2])862 },863 []864 )865866 expect(res).toEqual({})867})868869describe("change then delete property", () => {870 const res = runPatchTest(871 {872 x: 1873 },874 d => {875 d.x = 2876 delete d.x877 },878 [879 {880 op: "remove",881 path: ["x"]882 }883 ]884 )885 test("valid result", () => {886 expect(res).toEqual({})887 })

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

lines 888–932why, one step back
888})
889890test("replaying patches with interweaved replacements should work correctly", () => {891 const patches = []892 const s0 = {x: 1}893894 const s1 = produce(895 s0,896 draft => {897 draft.x = 2898 },899 p => {900 patches.push(...p)901 }902 )903904 const s2 = produce(905 s1,906 draft => {907 return {x: 0}908 },909 p => {910 patches.push(...p)911 }912 )913914 const s3 = produce(915 s2,916 draft => {917 draft.x--918 },919 p => {920 patches.push(...p)921 }922 )923924 expect(s3).toEqual({x: -1}) // correct result925 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay926927 // manual replay on a draft should also be correct928 expect(929 produce(s0, draft => {930 return applyPatches(draft, patches)931 })932 ).toEqual({x: -1})

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 933–978why, one step back
933})
934935test.skip("#468", () => {936 const item = {id: 1}937938 const state = [item]939940 const [nextState, patches] = produceWithPatches(state, draft => {941 draft[0].id = 2942 draft[1] = item943 })944945 expect(nextState).toMatchInlineSnapshot(`946 Array [947 Object {948 "id": 2,949 },950 Object {951 "id": 1,952 },953 ]954 `)955 expect(patches).toMatchInlineSnapshot(`956 Array [957 Object {958 "op": "replace",959 "path": Array [960 0,961 "id",962 ],963 "value": 2,964 },965 Object {966 "op": "add",967 "path": Array [968 0,969 ],970 "value": Object {971 "id": 2,972 },973 },974 ]975 `)976977 const final = applyPatches(state, patches)978 expect(final).toEqual(nextState)

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

line 979why, one step back
979})