Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

1,013 lines · 17.2 KiB · JavaScript

Last changed by

fix: incorrect patches generated for array if property was deleted insideMichel Weststratebefore adoption1e451a3

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

fix: incorrect patches generated for array if property was deleted insideMichel Weststratebefore adoption1e451a3

lines 651–665why, one step back
651describe("arrays - delete", () => {
652 runPatchTest(653 {654 x: [655 {a: 1, b: 2},656 {c: 3, d: 4}657 ]658 },659 d => {660 delete d.x[1].c661 },662 [{op: "remove", path: ["x", 1, "c"]}]663 )664})665

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

lines 666–709why, one step back
666describe("sets - add - 1", () => {
667 runPatchTest(668 new Set([1]),669 d => {670 d.add(2)671 },672 [{op: "add", path: [1], value: 2}],673 [{op: "remove", path: [1], value: 2}]674 )675})676677describe("sets - add, delete, add - 1", () => {678 runPatchTest(679 new Set([1]),680 d => {681 d.add(2)682 d.delete(2)683 d.add(2)684 },685 [{op: "add", path: [1], value: 2}],686 [{op: "remove", path: [1], value: 2}]687 )688})689690describe("sets - add, delete, add - 2", () => {691 runPatchTest(692 new Set([2, 1]),693 d => {694 d.add(2)695 d.delete(2)696 d.add(2)697 },698 [],699 []700 )701})702703describe("sets - mutate - 1", () => {704 const findById = (set, id) => {705 for (const item of set) {706 if (item.id === id) return item707 }708 }709 runPatchTest(

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 710–713why, one step back
710 new Set([
711 {id: 1, val: "We"},712 {id: 2, val: "will"}713 ]),

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

lines 714–734why, one step back
714 d => {
715 const obj1 = findById(d, 1)716 const obj2 = findById(d, 2)717 obj1.val = "rock"718 obj2.val = "you"719 },720 [721 {op: "remove", path: [0], value: {id: 1, val: "We"}},722 {op: "remove", path: [1], value: {id: 2, val: "will"}},723 {op: "add", path: [0], value: {id: 1, val: "rock"}},724 {op: "add", path: [1], value: {id: 2, val: "you"}}725 ],726 [727 {op: "remove", path: [1], value: {id: 2, val: "you"}},728 {op: "remove", path: [0], value: {id: 1, val: "rock"}},729 {op: "add", path: [1], value: {id: 2, val: "will"}},730 {op: "add", path: [0], value: {id: 1, val: "We"}}731 ]732 )733})734

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

lines 735–753why, one step back
735describe("arrays - splice should should result in remove op.", () => {
736 runPatchTest(737 [1, 2],738 d => {739 d.splice(1, 1)740 },741 [{op: "remove", path: [1]}],742 [{op: "add", path: [1], value: 2}]743 )744})745746describe("arrays - NESTED splice should should result in remove op.", () => {747 runPatchTest(748 {a: {b: {c: [1, 2]}}},749 d => {750 d.a.b.c.splice(1, 1)751 },752 [{op: "remove", path: ["a", "b", "c", 1]}],753 [{op: "add", path: ["a", "b", "c", 1], value: 2}]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 754why, one step back
754 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 755–757why, one step back
755})
756757describe("simple replacement", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 759–761why, one step back
759})
760761describe("same value replacement - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 762–769why, one step back
762 runPatchTest(
763 {x: {y: 3}},764 d => {765 const a = d.x766 d.x = a767 },768 []769 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 770–772why, one step back
770})
771772describe("same value replacement - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 773–781why, one step back
773 runPatchTest(
774 {x: {y: 3}},775 d => {776 const a = d.x777 d.x = 4778 d.x = a779 },780 []781 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 782–784why, one step back
782})
783784describe("same value replacement - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 785–791why, one step back
785 runPatchTest(
786 {x: 3},787 d => {788 d.x = 3789 },790 []791 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 792–794why, one step back
792})
793794describe("same value replacement - 4", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 795–800why, one step back
795 runPatchTest(
796 {x: 3},797 d => {798 d.x = 4799 d.x = 3800 },

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

lines 801–824why, one step back
801 []
802 )803})804805describe("same value replacement - 5", () => {806 runPatchTest(807 new Map([["x", 3]]),808 d => {809 d.set("x", 4)810 d.set("x", 3)811 },812 [],813 []814 )815})816817describe("same value replacement - 6", () => {818 runPatchTest(819 new Set(["x", 3]),820 d => {821 d.delete("x")822 d.add("x")823 },824 [],

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 825–826why, one step back
825 []
826 )

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

lines 827–829why, one step back
827})
828829describe("simple delete", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 830–841why, one step back
830 runPatchTest(
831 {x: 2},832 d => {833 delete d.x834 },835 [836 {837 op: "remove",838 path: ["x"]839 }840 ]841 )

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

lines 842–902why, one step back
842})
843844describe("patch compressions yields correct results", () => {845 let p1, p2846 runPatchTest(847 {},848 d => {849 d.x = {test: true}850 },851 (p1 = [852 {853 op: "add",854 path: ["x"],855 value: {856 test: true857 }858 }859 ])860 )861 runPatchTest(862 {x: {test: true}},863 d => {864 delete d.x865 },866 (p2 = [867 {868 op: "remove",869 path: ["x"]870 }871 ])872 )873 const res = runPatchTest(874 {},875 d => {876 applyPatches(d, [...p1, ...p2])877 },878 []879 )880881 expect(res).toEqual({})882})883884describe("change then delete property", () => {885 const res = runPatchTest(886 {887 x: 1888 },889 d => {890 d.x = 2891 delete d.x892 },893 [894 {895 op: "remove",896 path: ["x"]897 }898 ]899 )900 test("valid result", () => {901 expect(res).toEqual({})902 })

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

lines 903–947why, one step back
903})
904905test("replaying patches with interweaved replacements should work correctly", () => {906 const patches = []907 const s0 = {x: 1}908909 const s1 = produce(910 s0,911 draft => {912 draft.x = 2913 },914 p => {915 patches.push(...p)916 }917 )918919 const s2 = produce(920 s1,921 draft => {922 return {x: 0}923 },924 p => {925 patches.push(...p)926 }927 )928929 const s3 = produce(930 s2,931 draft => {932 draft.x--933 },934 p => {935 patches.push(...p)936 }937 )938939 expect(s3).toEqual({x: -1}) // correct result940 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay941942 // manual replay on a draft should also be correct943 expect(944 produce(s0, draft => {945 return applyPatches(draft, patches)946 })947 ).toEqual({x: -1})

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 948–993why, one step back
948})
949950test.skip("#468", () => {951 const item = {id: 1}952953 const state = [item]954955 const [nextState, patches] = produceWithPatches(state, draft => {956 draft[0].id = 2957 draft[1] = item958 })959960 expect(nextState).toMatchInlineSnapshot(`961 Array [962 Object {963 "id": 2,964 },965 Object {966 "id": 1,967 },968 ]969 `)970 expect(patches).toMatchInlineSnapshot(`971 Array [972 Object {973 "op": "replace",974 "path": Array [975 0,976 "id",977 ],978 "value": 2,979 },980 Object {981 "op": "add",982 "path": Array [983 0,984 ],985 "value": Object {986 "id": 2,987 },988 },989 ]990 `)991992 const final = applyPatches(state, patches)993 expect(final).toEqual(nextState)

chore: include dist in __tests__/tsconfig.jsonaleclarsonbefore adoptiondc19e90

line 994why, one step back
994})

fix: `Set` inside patches were not correctly cloned. Fixes #521Michel Weststratebefore adoption9d8d995

lines 995–1013why, one step back
995
996test("#521", () => {997 const state = new Map()998999 const [nextState, patches] = produceWithPatches(state, draft => {1000 draft.set("hello", new Set(["world"]))1001 })10021003 let patchedState = applyPatches(state, patches)1004 expect(patchedState).toEqual(nextState)10051006 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1007 draft.get("hello").add("immer")1008 })10091010 expect(applyPatches(nextState, patchesV2)).toEqual(1011 new Map([["hello", new Set(["world", "immer"])]])1012 )1013})