Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

1,149 lines · 20.5 KiB · JavaScript

Last changed by

fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5

History of this file

Take it

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

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,

Immer 7Michel Weststratebefore adoption467ea5d

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

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

lines 9–11why, one step back
9} from "../src/immer"
1011enableAllPlugins()

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

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

fix: revert Map supportAlec Larsonbefore adoption47529d3

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 18–24why, one step back
18 function runPatchTestHelper() {
19 let recordedPatches20 let recordedInversePatches21 const res = produce(base, producer, (p, i) => {22 recordedPatches = p23 recordedInversePatches = i24 })

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

line 25why, one step back
25

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

line 30why, one step back
30

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

line 34why, one step back
34

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 40why, one step back
40 }

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

line 41why, one step back
41

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 45why, one step back
45 })

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

line 46why, one step back
46

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 53why, one step back
53 })

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

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 56–58why, one step back
56}
5758describe("applyPatches", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

line 87why, one step back
87

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

lines 93–114why, one step back
93 })
94 it("applied patches cannot be modified", () => {95 // see also: https://github.com/immerjs/immer/issues/41196 const s0 = {97 items: [1]98 }99100 const [s1, p1] = produceWithPatches(s0, draft => {101 draft.items = []102 })103104 const replaceValueBefore = p1[0].value.slice()105106 const [s2, p2] = produceWithPatches(s1, draft => {107 draft.items.push(2)108 })109110 applyPatches(s0, [...p1, ...p2])111112 const replaceValueAfter = p1[0].value.slice()113114 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 115why, one step back
115 })

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 116–118why, one step back
116})
117118describe("simple assignment - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 126–128why, one step back
126})
127128describe("simple assignment - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 136–138why, one step back
136})
137138describe("simple assignment - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

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

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

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

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

line 302why, one step back
302

Immer 7Michel Weststratebefore adoption467ea5d

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 356–363why, one step back
356 describe("nested object (with changes)", () => {
357 runPatchTest(358 {a: {b: 1, c: 1}},359 d => {360 let a = d.a361 a.b = 2 // change362 delete a.c // delete363 a.y = 2 // add

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

line 364why, one step back
364

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

line 375why, one step back
375

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

lines 387–397why, one step back
387 d => {
388 let a = d.get("a")389 a.set("b", 2) // change390 a.delete("c") // delete391 a.set("y", 2) // add392393 // rename394 d.set("x", a)395 d.delete("a")396 },397 [

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 422–429why, one step back
422 describe("deeply nested object (with changes)", () => {
423 runPatchTest(424 {a: {b: {c: 1, d: 1}}},425 d => {426 let b = d.a.b427 b.c = 2 // change428 delete b.d // delete429 b.y = 2 // add

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

line 430why, one step back
430

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

lines 458–471why, one step back
458 d => {
459 let b = d.get("a").get("b")460 b.set("c", 2) // change461 b.delete("d") // delete462 b.set("y", 2) // add463464 // rename465 d.get("a").set("x", b)466 d.get("a").delete("b")467 },468 [469 {470 op: "add",471 path: ["a", "x"],

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 492–494why, one step back
492})
493494describe("minimum amount of changes", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 507–509why, one step back
507})
508509describe("arrays - prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 521why, one step back
521 )

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 522–524why, one step back
522})
523524describe("arrays - multiple prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 540–542why, one step back
540})
541542describe("arrays - splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 552why, one step back
552 )

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 553–555why, one step back
553})
554555describe("arrays - multiple splice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 572–574why, one step back
572})
573574describe("arrays - modify and shrink", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

Immer 7Michel Weststratebefore adoption467ea5d

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 591–593why, one step back
591})
592593describe("arrays - prepend then splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 606–608why, one step back
606})
607608describe("arrays - splice middle then prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 621–623why, one step back
621})
622623describe("arrays - truncate", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 635–637why, one step back
635})
636637describe("arrays - pop twice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 645why, one step back
645 )

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 646–648why, one step back
646})
647648describe("arrays - push multiple", () => {

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 660why, one step back
660 )

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 661–663why, one step back
661})
662663describe("arrays - splice (expand)", () => {

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 682–684why, one step back
682})
683684describe("arrays - splice (shrink)", () => {

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 701why, one step back
701 ]

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

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

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

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

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

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

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

Immer 7Michel Weststratebefore adoption467ea5d

line 790why, one step back
790 // 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 791–795why, one step back
791 runPatchTest(
792 [1, 2],793 d => {794 d.splice(1, 1)795 },

Immer 7Michel Weststratebefore adoption467ea5d

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

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

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

Immer 7Michel Weststratebefore adoption467ea5d

line 802why, one step back
802 // 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 803–807why, one step back
803 runPatchTest(
804 {a: {b: {c: [1, 2]}}},805 d => {806 d.a.b.c.splice(1, 1)807 },

Immer 7Michel Weststratebefore adoption467ea5d

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 810why, one step back
810 )

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 811–813why, one step back
811})
812813describe("simple replacement", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 815–817why, one step back
815})
816817describe("same value replacement - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 818–825why, one step back
818 runPatchTest(
819 {x: {y: 3}},820 d => {821 const a = d.x822 d.x = a823 },824 []825 )

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 826–828why, one step back
826})
827828describe("same value replacement - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 829–837why, one step back
829 runPatchTest(
830 {x: {y: 3}},831 d => {832 const a = d.x833 d.x = 4834 d.x = a835 },836 []837 )

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 838–840why, one step back
838})
839840describe("same value replacement - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 848–850why, one step back
848})
849850describe("same value replacement - 4", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 851–856why, one step back
851 runPatchTest(
852 {x: 3},853 d => {854 d.x = 4855 d.x = 3856 },

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

lines 883–885why, one step back
883})
884885describe("simple delete", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

lines 898–958why, one step back
898})
899900describe("patch compressions yields correct results", () => {901 let p1, p2902 runPatchTest(903 {},904 d => {905 d.x = {test: true}906 },907 (p1 = [908 {909 op: "add",910 path: ["x"],911 value: {912 test: true913 }914 }915 ])916 )917 runPatchTest(918 {x: {test: true}},919 d => {920 delete d.x921 },922 (p2 = [923 {924 op: "remove",925 path: ["x"]926 }927 ])928 )929 const res = runPatchTest(930 {},931 d => {932 applyPatches(d, [...p1, ...p2])933 },934 []935 )936937 expect(res).toEqual({})938})939940describe("change then delete property", () => {941 const res = runPatchTest(942 {943 x: 1944 },945 d => {946 d.x = 2947 delete d.x948 },949 [950 {951 op: "remove",952 path: ["x"]953 }954 ]955 )956 test("valid result", () => {957 expect(res).toEqual({})958 })

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

lines 959–1003why, one step back
959})
960961test("replaying patches with interweaved replacements should work correctly", () => {962 const patches = []963 const s0 = {x: 1}964965 const s1 = produce(966 s0,967 draft => {968 draft.x = 2969 },970 p => {971 patches.push(...p)972 }973 )974975 const s2 = produce(976 s1,977 draft => {978 return {x: 0}979 },980 p => {981 patches.push(...p)982 }983 )984985 const s3 = produce(986 s2,987 draft => {988 draft.x--989 },990 p => {991 patches.push(...p)992 }993 )994995 expect(s3).toEqual({x: -1}) // correct result996 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay997998 // manual replay on a draft should also be correct999 expect(1000 produce(s0, draft => {1001 return applyPatches(draft, patches)1002 })1003 ).toEqual({x: -1})

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

Immer 7Michel Weststratebefore adoption467ea5d

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

line 1014why, one step back
1014

Immer 7Michel Weststratebefore adoption467ea5d

lines 1015–1029why, one step back
1015 expect(nextState).toEqual([{id: 2}, {id: 1}])
1016 expect(patches).toEqual([1017 {1018 op: "replace",1019 path: [0, "id"],1020 value: 21021 },1022 {1023 op: "add",1024 path: [1],1025 value: {1026 id: 11027 }1028 }1029 ])

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

line 1030why, one step back
1030

Immer 7Michel Weststratebefore adoption467ea5d

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

Immer 7Michel Weststratebefore adoption467ea5d

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

feat(ts): Support typescript 3.4Alec Larsonbefore adoption2868065

line 1044why, one step back
1044})

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

lines 1045–1062why, one step back
1045
1046test("#521", () => {1047 const state = new Map()10481049 const [nextState, patches] = produceWithPatches(state, draft => {1050 draft.set("hello", new Set(["world"]))1051 })10521053 let patchedState = applyPatches(state, patches)1054 expect(patchedState).toEqual(nextState)10551056 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1057 draft.get("hello").add("immer")1058 })10591060 expect(applyPatches(nextState, patchesV2)).toEqual(1061 new Map([["hello", new Set(["world", "immer"])]])1062 )

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

lines 1063–1095why, one step back
1063})
10641065test("#559 patches works in a nested reducer with proxies", () => {1066 setUseProxies(true)10671068 const state = {1069 x: 1,1070 sub: {1071 y: [{a: 0}, {a: 1}]1072 }1073 }10741075 const changes = []1076 const inverseChanges = []10771078 const newState = produce(state, draft => {1079 draft.sub = produce(1080 draft.sub,1081 draft => {1082 draft.y.pop()1083 },1084 (patches, inversePatches) => {1085 expect(isDraft(inversePatches[0].value)).toBeFalsy()1086 expect(inversePatches[0].value).toMatchObject({a: 1})1087 changes.push(...patches)1088 inverseChanges.push(...inversePatches)1089 }1090 )1091 })10921093 const reversedSubState = applyPatches(newState.sub, inverseChanges)10941095 expect(reversedSubState).toMatchObject(state.sub)

Immer 7Michel Weststratebefore adoption467ea5d

lines 1096–1120why, one step back
1096})
10971098describe("#588", () => {1099 const reference = {value: {num: 53}}11001101 class Base {1102 [immerable] = true1103 get nested() {1104 return reference.value1105 }1106 set nested(value) {}1107 }11081109 let base = new Base()11101111 runPatchTest(1112 base,1113 vdraft => {1114 reference.value = vdraft1115 produce(base, bdraft => {1116 bdraft.nested.num = 421117 })1118 },1119 [{op: "add", path: ["num"], value: 42}]1120 )

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

line 1121why, one step back
1121})

fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5

lines 1122–1149why, one step back
1122
1123test("#676 patching Date objects", () => {1124 class Test {1125 constructor() {1126 this.test = true1127 }1128 perform() {1129 return "tested!"1130 }1131 }11321133 const [nextState, patches] = produceWithPatches({}, function(draft) {1134 draft.date = new Date(2020, 10, 10, 8, 8, 8, 3)1135 draft.test = new Test()1136 })11371138 expect(nextState.date.toJSON()).toMatchInlineSnapshot(1139 `"2020-11-10T08:08:08.003Z"`1140 )1141 expect(nextState.test.perform()).toBe("tested!")11421143 const rebuilt = applyPatches({}, patches)1144 expect(rebuilt.date).toBeInstanceOf(Date)1145 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1146 `"2020-11-10T08:08:08.003Z"`1147 )1148 expect(rebuilt.date).toEqual(new Date(2020, 10, 10, 8, 8, 8, 3))1149})