Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

1,122 lines · 19.9 KiB · JavaScript

Last changed by

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

History of this file

Take it

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

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

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

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

lines 2–4why, one step back
2import produce, {
3 setUseProxies,4 applyPatches,

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 5why, one step back
5 produceWithPatches,

fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6

line 6why, one step back
6 enableAllPlugins,

Added regression test to verify #588Michel Weststratebefore adoption198e05f

lines 7–8why, one step back
7 isDraft,
8 immerable

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 9why, one step back
9} from "../src/immer"

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 10why, one step back
10import {run} from "jest"

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 11–12why, one step back
11
12enableAllPlugins()

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 13–15why, one step back
13
14jest.setTimeout(1000)15

fix: revert Map supportAlec Larsonbefore adoption47529d3

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

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

lines 17–18why, one step back
17 let resultProxies, resultEs5
18

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 19–25why, one step back
19 function runPatchTestHelper() {
20 let recordedPatches21 let recordedInversePatches22 const res = produce(base, producer, (p, i) => {23 recordedPatches = p24 recordedInversePatches = i25 })

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

line 26why, one step back
26

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 27–30why, one step back
27 test("produces the correct patches", () => {
28 expect(recordedPatches).toEqual(patches)29 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)30 })

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

line 31why, one step back
31

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 32–34why, one step back
32 test("patches are replayable", () => {
33 expect(applyPatches(base, recordedPatches)).toEqual(res)34 })

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

line 35why, one step back
35

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 36–38why, one step back
36 test("patches can be reversed", () => {
37 expect(applyPatches(res, recordedInversePatches)).toEqual(base)38 })

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

lines 39–40why, one step back
39
40 return res

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 41why, one step back
41 }

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

line 42why, one step back
42

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 43–44why, one step back
43 describe(`proxy`, () => {
44 setUseProxies(true)

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 46why, one step back
46 })

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

line 47why, one step back
47

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 48–49why, one step back
48 describe(`es5`, () => {
49 setUseProxies(false)

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

lines 50–53why, one step back
50 resultEs5 = runPatchTestHelper()
51 test("ES5 and Proxy implementation yield same result", () => {52 expect(resultEs5).toEqual(resultProxies)53 })

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 54why, one step back
54 })

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

lines 55–56why, one step back
55
56 return resultProxies

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 57–59why, one step back
57}
5859describe("applyPatches", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 60–87why, one step back
60 it("mutates the base state when it is a draft", () => {
61 produce({a: 1}, draft => {62 const result = applyPatches(draft, [63 {op: "replace", path: ["a"], value: 2}64 ])65 expect(result).toBe(draft)66 expect(draft.a).toBe(2)67 })68 })69 it("produces a copy of the base state when not a draft", () => {70 const base = {a: 1}71 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])72 expect(result).not.toBe(base)73 expect(result.a).toBe(2)74 expect(base.a).toBe(1)75 })76 it('throws when `op` is not "add", "replace", nor "remove"', () => {77 expect(() => {78 const patch = {op: "copy", from: [0], path: [1]}79 applyPatches([2], [patch])80 }).toThrowErrorMatchingSnapshot()81 })82 it("throws when `path` cannot be resolved", () => {83 // missing parent84 expect(() => {85 const patch = {op: "add", path: ["a", "b"], value: 1}86 applyPatches({}, [patch])87 }).toThrowErrorMatchingSnapshot()

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

line 88why, one step back
88

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 89–93why, one step back
89 // missing grand-parent
90 expect(() => {91 const patch = {op: "add", path: ["a", "b", "c"], value: 1}92 applyPatches({}, [patch])93 }).toThrowErrorMatchingSnapshot()

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

lines 94–115why, one step back
94 })
95 it("applied patches cannot be modified", () => {96 // see also: https://github.com/immerjs/immer/issues/41197 const s0 = {98 items: [1]99 }100101 const [s1, p1] = produceWithPatches(s0, draft => {102 draft.items = []103 })104105 const replaceValueBefore = p1[0].value.slice()106107 const [s2, p2] = produceWithPatches(s1, draft => {108 draft.items.push(2)109 })110111 applyPatches(s0, [...p1, ...p2])112113 const replaceValueAfter = p1[0].value.slice()114115 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 116why, one step back
116 })

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 117–119why, one step back
117})
118119describe("simple assignment - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 120–126why, one step back
120 runPatchTest(
121 {x: 3},122 d => {123 d.x++124 },125 [{op: "replace", path: ["x"], value: 4}]126 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 127–129why, one step back
127})
128129describe("simple assignment - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 130–136why, one step back
130 runPatchTest(
131 {x: {y: 4}},132 d => {133 d.x.y++134 },135 [{op: "replace", path: ["x", "y"], value: 5}]136 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 137–138why, one step back
137})
138

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 140–146why, one step back
140 runPatchTest(
141 {x: [{y: 4}]},142 d => {143 d.x[0].y++144 },145 [{op: "replace", path: ["x", 0, "y"], value: 5}]146 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 147–148why, one step back
147})
148

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

lines 149–182why, one step back
149describe("simple assignment - 4", () => {
150 runPatchTest(151 new Map([["x", {y: 4}]]),152 d => {153 d.get("x").y++154 },155 [{op: "replace", path: ["x", "y"], value: 5}],156 [{op: "replace", path: ["x", "y"], value: 4}]157 )158})159160describe("simple assignment - 5", () => {161 runPatchTest(162 {x: new Map([["y", 4]])},163 d => {164 d.x.set("y", 5)165 },166 [{op: "replace", path: ["x", "y"], value: 5}],167 [{op: "replace", path: ["x", "y"], value: 4}]168 )169})170171describe("simple assignment - 6", () => {172 runPatchTest(173 new Map([["x", 1]]),174 d => {175 // Map.prototype.set should return the Map itself176 const res = d.set("x", 2)177 res.set("y", 3)178 },179 [180 {op: "replace", path: ["x"], value: 2},181 {op: "add", path: ["y"], value: 3}182 ],

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 183–186why, one step back
183 [
184 {op: "replace", path: ["x"], value: 1},185 {op: "remove", path: ["y"]}186 ]

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

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

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 211–217why, one step back
211 runPatchTest(
212 {x: {y: 4}},213 d => {214 delete d.x215 },216 [{op: "remove", path: ["x"]}]217 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 218–219why, one step back
218})
219

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

lines 220–245why, one step back
220describe("delete 2", () => {
221 runPatchTest(222 new Map([["x", 1]]),223 d => {224 d.delete("x")225 },226 [{op: "remove", path: ["x"]}],227 [{op: "add", path: ["x"], value: 1}]228 )229})230231describe("delete 3", () => {232 runPatchTest(233 {x: new Map([["y", 1]])},234 d => {235 d.x.delete("y")236 },237 [{op: "remove", path: ["x", "y"]}],238 [{op: "add", path: ["x", "y"], value: 1}]239 )240})241242describe("delete 5", () => {243 const key1 = {prop: "val1"}244 const key2 = {prop: "val2"}245 runPatchTest(

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 246–251why, one step back
246 {
247 x: new Map([248 [key1, 1],249 [key2, 2]250 ])251 },

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

lines 252–256why, one step back
252 d => {
253 d.x.delete(key1)254 d.x.delete(key2)255 },256 [

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 257–260why, one step back
257 {op: "remove", path: ["x", key1]},
258 {op: "remove", path: ["x", key2]}259 ],260 [

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

lines 261–288why, one step back
261 {op: "add", path: ["x", key1], value: 1},
262 {op: "add", path: ["x", key2], value: 2}263 ]264 )265})266267describe("delete 6", () => {268 runPatchTest(269 new Set(["x", 1]),270 d => {271 d.delete("x")272 },273 [{op: "remove", path: [0], value: "x"}],274 [{op: "add", path: [0], value: "x"}]275 )276})277278describe("delete 7", () => {279 runPatchTest(280 {x: new Set(["y", 1])},281 d => {282 d.x.delete("y")283 },284 [{op: "remove", path: ["x", 0], value: "y"}],285 [{op: "add", path: ["x", 0], value: "y"}]286 )287})288

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 290–296why, one step back
290 describe("nested object (no changes)", () => {
291 runPatchTest(292 {a: {b: 1}},293 d => {294 d.x = d.a295 delete d.a296 },

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 297–300why, one step back
297 [
298 {op: "add", path: ["x"], value: {b: 1}},299 {op: "remove", path: ["a"]}300 ]

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 301–302why, one step back
301 )
302 })

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

line 303why, one step back
303

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 304–338why, one step back
304 describe("nested change in object", () => {
305 runPatchTest(306 {307 a: {b: 1}308 },309 d => {310 d.a.b++311 },312 [{op: "replace", path: ["a", "b"], value: 2}],313 [{op: "replace", path: ["a", "b"], value: 1}]314 )315 })316317 describe("nested change in map", () => {318 runPatchTest(319 new Map([["a", new Map([["b", 1]])]]),320 d => {321 d.get("a").set("b", 2)322 },323 [{op: "replace", path: ["a", "b"], value: 2}],324 [{op: "replace", path: ["a", "b"], value: 1}]325 )326 })327328 describe("nested change in array", () => {329 runPatchTest(330 [[{b: 1}]],331 d => {332 d[0][0].b++333 },334 [{op: "replace", path: [0, 0, "b"], value: 2}],335 [{op: "replace", path: [0, 0, "b"], value: 1}]336 )337 })338

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

lines 339–356why, one step back
339 describe("nested map (no changes)", () => {
340 runPatchTest(341 new Map([["a", new Map([["b", 1]])]]),342 d => {343 d.set("x", d.get("a"))344 d.delete("a")345 },346 [347 {op: "add", path: ["x"], value: new Map([["b", 1]])},348 {op: "remove", path: ["a"]}349 ],350 [351 {op: "remove", path: ["x"]},352 {op: "add", path: ["a"], value: new Map([["b", 1]])}353 ]354 )355 })356

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 357–364why, one step back
357 describe("nested object (with changes)", () => {
358 runPatchTest(359 {a: {b: 1, c: 1}},360 d => {361 let a = d.a362 a.b = 2 // change363 delete a.c // delete364 a.y = 2 // add

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

line 365why, one step back
365

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 366–375why, one step back
366 // rename
367 d.x = a368 delete d.a369 },370 [371 {op: "add", path: ["x"], value: {b: 2, y: 2}},372 {op: "remove", path: ["a"]}373 ]374 )375 })

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

line 376why, one step back
376

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 379–387why, one step back
379 new Map([
380 [381 "a",382 new Map([383 ["b", 1],384 ["c", 1]385 ])386 ]387 ]),

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

lines 388–398why, one step back
388 d => {
389 let a = d.get("a")390 a.set("b", 2) // change391 a.delete("c") // delete392 a.set("y", 2) // add393394 // rename395 d.set("x", a)396 d.delete("a")397 },398 [

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 399–406why, one step back
399 {
400 op: "add",401 path: ["x"],402 value: new Map([403 ["b", 2],404 ["y", 2]405 ])406 },

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

lines 407–410why, one step back
407 {op: "remove", path: ["a"]}
408 ],409 [410 {op: "remove", path: ["x"]},

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 411–418why, one step back
411 {
412 op: "add",413 path: ["a"],414 value: new Map([415 ["b", 1],416 ["c", 1]417 ])418 }

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

lines 419–422why, one step back
419 ]
420 )421 })422

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 423–430why, one step back
423 describe("deeply nested object (with changes)", () => {
424 runPatchTest(425 {a: {b: {c: 1, d: 1}}},426 d => {427 let b = d.a.b428 b.c = 2 // change429 delete b.d // delete430 b.y = 2 // add

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

line 431why, one step back
431

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 432–441why, one step back
432 // rename
433 d.a.x = b434 delete d.a.b435 },436 [437 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},438 {op: "remove", path: ["a", "b"]}439 ]440 )441 })

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

lines 442–444why, one step back
442
443 describe("deeply nested map (with changes)", () => {444 runPatchTest(

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 445–458why, one step back
445 new Map([
446 [447 "a",448 new Map([449 [450 "b",451 new Map([452 ["c", 1],453 ["d", 1]454 ])455 ]456 ])457 ]458 ]),

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

lines 459–472why, one step back
459 d => {
460 let b = d.get("a").get("b")461 b.set("c", 2) // change462 b.delete("d") // delete463 b.set("y", 2) // add464465 // rename466 d.get("a").set("x", b)467 d.get("a").delete("b")468 },469 [470 {471 op: "add",472 path: ["a", "x"],

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 473–476why, one step back
473 value: new Map([
474 ["c", 2],475 ["y", 2]476 ])

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

lines 477–484why, one step back
477 },
478 {op: "remove", path: ["a", "b"]}479 ],480 [481 {op: "remove", path: ["a", "x"]},482 {483 op: "add",484 path: ["a", "b"],

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 485–488why, one step back
485 value: new Map([
486 ["c", 1],487 ["d", 1]488 ])

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

lines 489–492why, one step back
489 }
490 ]491 )492 })

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 493–495why, one step back
493})
494495describe("minimum amount of changes", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 496–507why, one step back
496 runPatchTest(
497 {x: 3, y: {a: 4}, z: 3},498 d => {499 d.y.a = 4500 d.y.b = 5501 Object.assign(d, {x: 4, y: {a: 2}})502 },503 [504 {op: "replace", path: ["x"], value: 4},505 {op: "replace", path: ["y"], value: {a: 2}}506 ]507 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 508–510why, one step back
508})
509510describe("arrays - prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 511–515why, one step back
511 runPatchTest(
512 {x: [1, 2, 3]},513 d => {514 d.x.unshift(4)515 },

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 516–521why, one step back
516 [
517 {op: "replace", path: ["x", 0], value: 4},518 {op: "replace", path: ["x", 1], value: 1},519 {op: "replace", path: ["x", 2], value: 2},520 {op: "add", path: ["x", 3], value: 3}521 ]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 522why, one step back
522 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 523–525why, one step back
523})
524525describe("arrays - multiple prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 526–530why, one step back
526 runPatchTest(
527 {x: [1, 2, 3]},528 d => {529 d.x.unshift(4)530 d.x.unshift(5)

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 531why, one step back
531 // 4,5,1,2,3

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 532–533why, one step back
532 },
533 [

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 534–538why, one step back
534 {op: "replace", path: ["x", 0], value: 5},
535 {op: "replace", path: ["x", 1], value: 4},536 {op: "replace", path: ["x", 2], value: 1},537 {op: "add", path: ["x", 3], value: 2},538 {op: "add", path: ["x", 4], value: 3}

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 539–540why, one step back
539 ]
540 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 541–543why, one step back
541})
542543describe("arrays - splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 544–548why, one step back
544 runPatchTest(
545 {x: [1, 2, 3]},546 d => {547 d.x.splice(1, 1)548 },

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 549–552why, one step back
549 [
550 {op: "replace", path: ["x", 1], value: 3},551 {op: "replace", path: ["x", "length"], value: 2}552 ]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 553why, one step back
553 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 554–556why, one step back
554})
555556describe("arrays - multiple splice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 557–560why, one step back
557 runPatchTest(
558 [0, 1, 2, 3, 4, 5, 0],559 d => {560 d.splice(4, 2, 3)

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 561why, one step back
561 // [0,1,2,3,3,0]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 562why, one step back
562 d.splice(1, 2, 3)

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 563–564why, one step back
563 // [0,3,3,3,0]
564 expect(d.slice()).toEqual([0, 3, 3, 3, 0])

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 565–568why, one step back
565 },
566 [567 {op: "replace", path: [1], value: 3},568 {op: "replace", path: [2], value: 3},

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 569–570why, one step back
569 {op: "replace", path: [4], value: 0},
570 {op: "replace", path: ["length"], value: 5}

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 571–572why, one step back
571 ]
572 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 573–575why, one step back
573})
574575describe("arrays - modify and shrink", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 576–580why, one step back
576 runPatchTest(
577 {x: [1, 2, 3]},578 d => {579 d.x[0] = 4580 d.x.length = 2

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 581why, one step back
581 // [0, 2]

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 582–583why, one step back
582 },
583 [

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 585why, one step back
585 {op: "replace", path: ["x", "length"], value: 2}

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 586–587why, one step back
586 ],
587 [

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 590–591why, one step back
590 ]
591 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 592–594why, one step back
592})
593594describe("arrays - prepend then splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 595–599why, one step back
595 runPatchTest(
596 {x: [1, 2, 3]},597 d => {598 d.x.unshift(4)599 d.x.splice(2, 1)

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 600why, one step back
600 // 4, 1, 3

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 601–606why, one step back
601 },
602 [603 {op: "replace", path: ["x", 0], value: 4},604 {op: "replace", path: ["x", 1], value: 1}605 ]606 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 607–609why, one step back
607})
608609describe("arrays - splice middle then prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 610–614why, one step back
610 runPatchTest(
611 {x: [1, 2, 3]},612 d => {613 d.x.splice(1, 1)614 d.x.unshift(4)

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 615why, one step back
615 // [4, 1, 3]

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 616–621why, one step back
616 },
617 [618 {op: "replace", path: ["x", 0], value: 4},619 {op: "replace", path: ["x", 1], value: 1}620 ]621 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 622–624why, one step back
622})
623624describe("arrays - truncate", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 625–629why, one step back
625 runPatchTest(
626 {x: [1, 2, 3]},627 d => {628 d.x.length -= 2629 },

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 630why, one step back
630 [{op: "replace", path: ["x", "length"], value: 1}],

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 631–635why, one step back
631 [
632 {op: "add", path: ["x", 1], value: 2},633 {op: "add", path: ["x", 2], value: 3}634 ]635 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 636–638why, one step back
636})
637638describe("arrays - pop twice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 639–644why, one step back
639 runPatchTest(
640 {x: [1, 2, 3]},641 d => {642 d.x.pop()643 d.x.pop()644 },

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 645why, one step back
645 [{op: "replace", path: ["x", "length"], value: 1}]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 646why, one step back
646 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 647–649why, one step back
647})
648649describe("arrays - push multiple", () => {

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 650why, one step back
650 // These patches were more optimal pre immer 7, but not always correct

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 651–659why, one step back
651 runPatchTest(
652 {x: [1, 2, 3]},653 d => {654 d.x.push(4, 5)655 },656 [657 {op: "add", path: ["x", 3], value: 4},658 {op: "add", path: ["x", 4], value: 5}659 ],

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 660why, one step back
660 [{op: "replace", path: ["x", "length"], value: 3}]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 661why, one step back
661 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 662–664why, one step back
662})
663664describe("arrays - splice (expand)", () => {

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 665why, one step back
665 // These patches were more optimal pre immer 7, but not always correct

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 666–668why, one step back
666 runPatchTest(
667 {x: [1, 2, 3]},668 d => {

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 669why, one step back
669 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 670–672why, one step back
670 },
671 [672 {op: "replace", path: ["x", 1], value: 4},

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 673–675why, one step back
673 {op: "replace", path: ["x", 2], value: 5},
674 {op: "add", path: ["x", 3], value: 6},675 {op: "add", path: ["x", 4], value: 3}

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 676–678why, one step back
676 ],
677 [678 {op: "replace", path: ["x", 1], value: 2},

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 679–680why, one step back
679 {op: "replace", path: ["x", 2], value: 3},
680 {op: "replace", path: ["x", "length"], value: 3}

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 681–682why, one step back
681 ]
682 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 683–685why, one step back
683})
684685describe("arrays - splice (shrink)", () => {

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 686why, one step back
686 // These patches were more optimal pre immer 7, but not always correct

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 687–689why, one step back
687 runPatchTest(
688 {x: [1, 2, 3, 4, 5]},689 d => {

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 690why, one step back
690 d.x.splice(1, 3, 6) // [1, 6, 5]

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 691–693why, one step back
691 },
692 [693 {op: "replace", path: ["x", 1], value: 6},

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 694–695why, one step back
694 {op: "replace", path: ["x", 2], value: 5},
695 {op: "replace", path: ["x", "length"], value: 3}

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 696–698why, one step back
696 ],
697 [698 {op: "replace", path: ["x", 1], value: 2},

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 699–701why, one step back
699 {op: "replace", path: ["x", 2], value: 3},
700 {op: "add", path: ["x", 3], value: 4},701 {op: "add", path: ["x", 4], value: 5}

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 702why, one step back
702 ]

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

lines 703–705why, one step back
703 )
704})705

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

lines 706–720why, one step back
706describe("arrays - delete", () => {
707 runPatchTest(708 {709 x: [710 {a: 1, b: 2},711 {c: 3, d: 4}712 ]713 },714 d => {715 delete d.x[1].c716 },717 [{op: "remove", path: ["x", 1, "c"]}]718 )719})720

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

lines 721–764why, one step back
721describe("sets - add - 1", () => {
722 runPatchTest(723 new Set([1]),724 d => {725 d.add(2)726 },727 [{op: "add", path: [1], value: 2}],728 [{op: "remove", path: [1], value: 2}]729 )730})731732describe("sets - add, delete, add - 1", () => {733 runPatchTest(734 new Set([1]),735 d => {736 d.add(2)737 d.delete(2)738 d.add(2)739 },740 [{op: "add", path: [1], value: 2}],741 [{op: "remove", path: [1], value: 2}]742 )743})744745describe("sets - add, delete, add - 2", () => {746 runPatchTest(747 new Set([2, 1]),748 d => {749 d.add(2)750 d.delete(2)751 d.add(2)752 },753 [],754 []755 )756})757758describe("sets - mutate - 1", () => {759 const findById = (set, id) => {760 for (const item of set) {761 if (item.id === id) return item762 }763 }764 runPatchTest(

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 765–768why, one step back
765 new Set([
766 {id: 1, val: "We"},767 {id: 2, val: "will"}768 ]),

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

lines 769–789why, one step back
769 d => {
770 const obj1 = findById(d, 1)771 const obj2 = findById(d, 2)772 obj1.val = "rock"773 obj2.val = "you"774 },775 [776 {op: "remove", path: [0], value: {id: 1, val: "We"}},777 {op: "remove", path: [1], value: {id: 2, val: "will"}},778 {op: "add", path: [0], value: {id: 1, val: "rock"}},779 {op: "add", path: [1], value: {id: 2, val: "you"}}780 ],781 [782 {op: "remove", path: [1], value: {id: 2, val: "you"}},783 {op: "remove", path: [0], value: {id: 1, val: "rock"}},784 {op: "add", path: [1], value: {id: 2, val: "will"}},785 {op: "add", path: [0], value: {id: 1, val: "We"}}786 ]787 )788})789

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

line 790why, one step back
790describe("arrays - splice should should result in remove op.", () => {

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 791why, one step back
791 // These patches were more optimal pre immer 7, but not always correct

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

lines 792–796why, one step back
792 runPatchTest(
793 [1, 2],794 d => {795 d.splice(1, 1)796 },

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 797why, one step back
797 [{op: "replace", path: ["length"], value: 1}],

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

lines 798–802why, one step back
798 [{op: "add", path: [1], value: 2}]
799 )800})801802describe("arrays - NESTED splice should should result in remove op.", () => {

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 803why, one step back
803 // These patches were more optimal pre immer 7, but not always correct

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

lines 804–808why, one step back
804 runPatchTest(
805 {a: {b: {c: [1, 2]}}},806 d => {807 d.a.b.c.splice(1, 1)808 },

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

line 809why, one step back
809 [{op: "replace", path: ["a", "b", "c", "length"], value: 1}],

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

line 810why, one step back
810 [{op: "add", path: ["a", "b", "c", 1], value: 2}]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 811why, one step back
811 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

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

Restore: allow produce to be called with primitivesMichel Weststratebefore adoptione7983c4

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

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 816–818why, one step back
816})
817818describe("same value replacement - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 819–826why, one step back
819 runPatchTest(
820 {x: {y: 3}},821 d => {822 const a = d.x823 d.x = a824 },825 []826 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 827–829why, one step back
827})
828829describe("same value replacement - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 830–838why, one step back
830 runPatchTest(
831 {x: {y: 3}},832 d => {833 const a = d.x834 d.x = 4835 d.x = a836 },837 []838 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 839–841why, one step back
839})
840841describe("same value replacement - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 842–848why, one step back
842 runPatchTest(
843 {x: 3},844 d => {845 d.x = 3846 },847 []848 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 849–851why, one step back
849})
850851describe("same value replacement - 4", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 852–857why, one step back
852 runPatchTest(
853 {x: 3},854 d => {855 d.x = 4856 d.x = 3857 },

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

lines 858–881why, one step back
858 []
859 )860})861862describe("same value replacement - 5", () => {863 runPatchTest(864 new Map([["x", 3]]),865 d => {866 d.set("x", 4)867 d.set("x", 3)868 },869 [],870 []871 )872})873874describe("same value replacement - 6", () => {875 runPatchTest(876 new Set(["x", 3]),877 d => {878 d.delete("x")879 d.add("x")880 },881 [],

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 882–883why, one step back
882 []
883 )

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 884–886why, one step back
884})
885886describe("simple delete", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 887–898why, one step back
887 runPatchTest(
888 {x: 2},889 d => {890 delete d.x891 },892 [893 {894 op: "remove",895 path: ["x"]896 }897 ]898 )

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

lines 899–959why, one step back
899})
900901describe("patch compressions yields correct results", () => {902 let p1, p2903 runPatchTest(904 {},905 d => {906 d.x = {test: true}907 },908 (p1 = [909 {910 op: "add",911 path: ["x"],912 value: {913 test: true914 }915 }916 ])917 )918 runPatchTest(919 {x: {test: true}},920 d => {921 delete d.x922 },923 (p2 = [924 {925 op: "remove",926 path: ["x"]927 }928 ])929 )930 const res = runPatchTest(931 {},932 d => {933 applyPatches(d, [...p1, ...p2])934 },935 []936 )937938 expect(res).toEqual({})939})940941describe("change then delete property", () => {942 const res = runPatchTest(943 {944 x: 1945 },946 d => {947 d.x = 2948 delete d.x949 },950 [951 {952 op: "remove",953 path: ["x"]954 }955 ]956 )957 test("valid result", () => {958 expect(res).toEqual({})959 })

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

lines 960–1004why, one step back
960})
961962test("replaying patches with interweaved replacements should work correctly", () => {963 const patches = []964 const s0 = {x: 1}965966 const s1 = produce(967 s0,968 draft => {969 draft.x = 2970 },971 p => {972 patches.push(...p)973 }974 )975976 const s2 = produce(977 s1,978 draft => {979 return {x: 0}980 },981 p => {982 patches.push(...p)983 }984 )985986 const s3 = produce(987 s2,988 draft => {989 draft.x--990 },991 p => {992 patches.push(...p)993 }994 )995996 expect(s3).toEqual({x: -1}) // correct result997 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay998999 // manual replay on a draft should also be correct1000 expect(1001 produce(s0, draft => {1002 return applyPatches(draft, patches)1003 })1004 ).toEqual({x: -1})

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 1005–1006why, one step back
1005})
1006

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 1007–1014why, one step back
1007describe("#468", () => {
1008 function run() {1009 const item = {id: 1}1010 const state = [item]1011 const [nextState, patches] = produceWithPatches(state, draft => {1012 draft[0].id = 21013 draft[1] = item1014 })

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

line 1015why, one step back
1015

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 1016–1030why, one step back
1016 expect(nextState).toEqual([{id: 2}, {id: 1}])
1017 expect(patches).toEqual([1018 {1019 op: "replace",1020 path: [0, "id"],1021 value: 21022 },1023 {1024 op: "add",1025 path: [1],1026 value: {1027 id: 11028 }1029 }1030 ])

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

line 1031why, one step back
1031

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 1032–1038why, one step back
1032 const final = applyPatches(state, patches)
1033 expect(final).toEqual(nextState)1034 }10351036 test("es5", () => {1037 setUseProxies(false)1038 run()

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 1039–1040why, one step back
1039 })
1040

fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5

lines 1041–1044why, one step back
1041 test("proxy", () => {
1042 setUseProxies(true)1043 run()1044 })

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

line 1045why, one step back
1045})

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

lines 1046–1063why, one step back
1046
1047test("#521", () => {1048 const state = new Map()10491050 const [nextState, patches] = produceWithPatches(state, draft => {1051 draft.set("hello", new Set(["world"]))1052 })10531054 let patchedState = applyPatches(state, patches)1055 expect(patchedState).toEqual(nextState)10561057 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1058 draft.get("hello").add("immer")1059 })10601061 expect(applyPatches(nextState, patchesV2)).toEqual(1062 new Map([["hello", new Set(["world", "immer"])]])1063 )

fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6

lines 1064–1096why, one step back
1064})
10651066test("#559 patches works in a nested reducer with proxies", () => {1067 setUseProxies(true)10681069 const state = {1070 x: 1,1071 sub: {1072 y: [{a: 0}, {a: 1}]1073 }1074 }10751076 const changes = []1077 const inverseChanges = []10781079 const newState = produce(state, draft => {1080 draft.sub = produce(1081 draft.sub,1082 draft => {1083 draft.y.pop()1084 },1085 (patches, inversePatches) => {1086 expect(isDraft(inversePatches[0].value)).toBeFalsy()1087 expect(inversePatches[0].value).toMatchObject({a: 1})1088 changes.push(...patches)1089 inverseChanges.push(...inversePatches)1090 }1091 )1092 })10931094 const reversedSubState = applyPatches(newState.sub, inverseChanges)10951096 expect(reversedSubState).toMatchObject(state.sub)

Added regression test to verify #588Michel Weststratebefore adoption198e05f

lines 1097–1121why, one step back
1097})
10981099describe("#588", () => {1100 const reference = {value: {num: 53}}11011102 class Base {1103 [immerable] = true1104 get nested() {1105 return reference.value1106 }1107 set nested(value) {}1108 }11091110 let base = new Base()11111112 runPatchTest(1113 base,1114 vdraft => {1115 reference.value = vdraft1116 produce(base, bdraft => {1117 bdraft.nested.num = 421118 })1119 },1120 [{op: "add", path: ["num"], value: 42}]1121 )

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

line 1122why, one step back
1122})