Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

1,050 lines · 17.9 KiB · JavaScript

Last changed by

removed `.only` specifier from testMichel Weststratebefore adoption2b54ac4

History of this file

Take it

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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,

Ensure values in patches are never a draftRenaud Chaputbefore adoption2454a51

lines 6–7why, one step back
6 enableAllPlugins,
7 isDraft

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

lines 8–10why, one step back
8} from "../src/immer"
910enableAllPlugins()

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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

fix: revert Map supportAlec Larsonbefore adoption47529d3

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 17–23why, one step back
17 function runPatchTestHelper() {
18 let recordedPatches19 let recordedInversePatches20 const res = produce(base, producer, (p, i) => {21 recordedPatches = p22 recordedInversePatches = i23 })

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 24why, one step back
24

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 29why, one step back
29

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 33why, one step back
33

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 39why, one step back
39 }

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 40why, one step back
40

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 44why, one step back
44 })

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 45why, one step back
45

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 52why, one step back
52 })

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

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 86why, one step back
86

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

lines 92–113why, one step back
92 })
93 it("applied patches cannot be modified", () => {94 // see also: https://github.com/immerjs/immer/issues/41195 const s0 = {96 items: [1]97 }9899 const [s1, p1] = produceWithPatches(s0, draft => {100 draft.items = []101 })102103 const replaceValueBefore = p1[0].value.slice()104105 const [s2, p2] = produceWithPatches(s1, draft => {106 draft.items.push(2)107 })108109 applyPatches(s0, [...p1, ...p2])110111 const replaceValueAfter = p1[0].value.slice()112113 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 114why, one step back
114 })

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 135–136why, one step back
135})
136

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

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

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

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

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 301why, one step back
301

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 320–327why, one step back
320 describe("nested object (with changes)", () => {
321 runPatchTest(322 {a: {b: 1, c: 1}},323 d => {324 let a = d.a325 a.b = 2 // change326 delete a.c // delete327 a.y = 2 // add

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 328why, one step back
328

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 329–338why, one step back
329 // rename
330 d.x = a331 delete d.a332 },333 [334 {op: "add", path: ["x"], value: {b: 2, y: 2}},335 {op: "remove", path: ["a"]}336 ]337 )338 })

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 339why, one step back
339

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

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 342–350why, one step back
342 new Map([
343 [344 "a",345 new Map([346 ["b", 1],347 ["c", 1]348 ])349 ]350 ]),

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

lines 351–361why, one step back
351 d => {
352 let a = d.get("a")353 a.set("b", 2) // change354 a.delete("c") // delete355 a.set("y", 2) // add356357 // rename358 d.set("x", a)359 d.delete("a")360 },361 [

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 362–369why, one step back
362 {
363 op: "add",364 path: ["x"],365 value: new Map([366 ["b", 2],367 ["y", 2]368 ])369 },

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

lines 370–373why, one step back
370 {op: "remove", path: ["a"]}
371 ],372 [373 {op: "remove", path: ["x"]},

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 374–381why, one step back
374 {
375 op: "add",376 path: ["a"],377 value: new Map([378 ["b", 1],379 ["c", 1]380 ])381 }

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

lines 382–385why, one step back
382 ]
383 )384 })385

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 386–393why, one step back
386 describe("deeply nested object (with changes)", () => {
387 runPatchTest(388 {a: {b: {c: 1, d: 1}}},389 d => {390 let b = d.a.b391 b.c = 2 // change392 delete b.d // delete393 b.y = 2 // add

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 394why, one step back
394

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 395–404why, one step back
395 // rename
396 d.a.x = b397 delete d.a.b398 },399 [400 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},401 {op: "remove", path: ["a", "b"]}402 ]403 )404 })

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

lines 405–407why, one step back
405
406 describe("deeply nested map (with changes)", () => {407 runPatchTest(

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 408–421why, one step back
408 new Map([
409 [410 "a",411 new Map([412 [413 "b",414 new Map([415 ["c", 1],416 ["d", 1]417 ])418 ]419 ])420 ]421 ]),

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

lines 422–435why, one step back
422 d => {
423 let b = d.get("a").get("b")424 b.set("c", 2) // change425 b.delete("d") // delete426 b.set("y", 2) // add427428 // rename429 d.get("a").set("x", b)430 d.get("a").delete("b")431 },432 [433 {434 op: "add",435 path: ["a", "x"],

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 436–439why, one step back
436 value: new Map([
437 ["c", 2],438 ["y", 2]439 ])

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

lines 440–447why, one step back
440 },
441 {op: "remove", path: ["a", "b"]}442 ],443 [444 {op: "remove", path: ["a", "x"]},445 {446 op: "add",447 path: ["a", "b"],

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 448–451why, one step back
448 value: new Map([
449 ["c", 1],450 ["d", 1]451 ])

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

lines 452–455why, one step back
452 }
453 ]454 )455 })

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 456–458why, one step back
456})
457458describe("minimum amount of changes", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 459–470why, one step back
459 runPatchTest(
460 {x: 3, y: {a: 4}, z: 3},461 d => {462 d.y.a = 4463 d.y.b = 5464 Object.assign(d, {x: 4, y: {a: 2}})465 },466 [467 {op: "replace", path: ["x"], value: 4},468 {op: "replace", path: ["y"], value: {a: 2}}469 ]470 )

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 471–473why, one step back
471})
472473describe("arrays - prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 481–483why, one step back
481})
482483describe("arrays - multiple prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 484–494why, one step back
484 runPatchTest(
485 {x: [1, 2, 3]},486 d => {487 d.x.unshift(4)488 d.x.unshift(5)489 },490 [491 {op: "add", path: ["x", 0], value: 5},492 {op: "add", path: ["x", 1], value: 4}493 ]494 )

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 495–497why, one step back
495})
496497describe("arrays - splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 498–504why, one step back
498 runPatchTest(
499 {x: [1, 2, 3]},500 d => {501 d.x.splice(1, 1)502 },503 [{op: "remove", path: ["x", 1]}]504 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 505–507why, one step back
505})
506507describe("arrays - multiple splice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 521–523why, one step back
521})
522523describe("arrays - modify and shrink", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 524–530why, one step back
524 runPatchTest(
525 {x: [1, 2, 3]},526 d => {527 d.x[0] = 4528 d.x.length = 2529 },530 [

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 531–534why, one step back
531 {op: "replace", path: ["x", 0], value: 4},
532 {op: "remove", path: ["x", 2]}533 ],534 [

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 539–541why, one step back
539})
540541describe("arrays - prepend then splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 567–569why, one step back
567})
568569describe("arrays - truncate", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 570–575why, one step back
570 runPatchTest(
571 {x: [1, 2, 3]},572 d => {573 d.x.length -= 2574 },575 [

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 580–583why, one step back
580 {op: "add", path: ["x", 1], value: 2},
581 {op: "add", path: ["x", 2], value: 3}582 ]583 )

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 584–586why, one step back
584})
585586describe("arrays - pop twice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 587–592why, one step back
587 runPatchTest(
588 {x: [1, 2, 3]},589 d => {590 d.x.pop()591 d.x.pop()592 },

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 593–596why, one step back
593 [
594 {op: "remove", path: ["x", 2]},595 {op: "remove", path: ["x", 1]}596 ]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 597why, one step back
597 )

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 598–600why, one step back
598})
599600describe("arrays - push multiple", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 610–613why, one step back
610 [
611 {op: "remove", path: ["x", 4]},612 {op: "remove", path: ["x", 3]}613 ]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 614why, one step back
614 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 615–617why, one step back
615})
616617describe("arrays - splice (expand)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 634–636why, one step back
634})
635636describe("arrays - splice (shrink)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

lines 652–654why, one step back
652 )
653})654

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

lines 655–669why, one step back
655describe("arrays - delete", () => {
656 runPatchTest(657 {658 x: [659 {a: 1, b: 2},660 {c: 3, d: 4}661 ]662 },663 d => {664 delete d.x[1].c665 },666 [{op: "remove", path: ["x", 1, "c"]}]667 )668})669

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

lines 670–713why, one step back
670describe("sets - add - 1", () => {
671 runPatchTest(672 new Set([1]),673 d => {674 d.add(2)675 },676 [{op: "add", path: [1], value: 2}],677 [{op: "remove", path: [1], value: 2}]678 )679})680681describe("sets - add, delete, add - 1", () => {682 runPatchTest(683 new Set([1]),684 d => {685 d.add(2)686 d.delete(2)687 d.add(2)688 },689 [{op: "add", path: [1], value: 2}],690 [{op: "remove", path: [1], value: 2}]691 )692})693694describe("sets - add, delete, add - 2", () => {695 runPatchTest(696 new Set([2, 1]),697 d => {698 d.add(2)699 d.delete(2)700 d.add(2)701 },702 [],703 []704 )705})706707describe("sets - mutate - 1", () => {708 const findById = (set, id) => {709 for (const item of set) {710 if (item.id === id) return item711 }712 }713 runPatchTest(

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 714–717why, one step back
714 new Set([
715 {id: 1, val: "We"},716 {id: 2, val: "will"}717 ]),

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

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

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

lines 739–757why, one step back
739describe("arrays - splice should should result in remove op.", () => {
740 runPatchTest(741 [1, 2],742 d => {743 d.splice(1, 1)744 },745 [{op: "remove", path: [1]}],746 [{op: "add", path: [1], value: 2}]747 )748})749750describe("arrays - NESTED splice should should result in remove op.", () => {751 runPatchTest(752 {a: {b: {c: [1, 2]}}},753 d => {754 d.a.b.c.splice(1, 1)755 },756 [{op: "remove", path: ["a", "b", "c", 1]}],757 [{op: "add", path: ["a", "b", "c", 1], value: 2}]

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 758why, one step back
758 )

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 759–761why, one step back
759})
760761describe("simple replacement", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 763–765why, one step back
763})
764765describe("same value replacement - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 766–773why, one step back
766 runPatchTest(
767 {x: {y: 3}},768 d => {769 const a = d.x770 d.x = a771 },772 []773 )

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 774–776why, one step back
774})
775776describe("same value replacement - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 777–785why, one step back
777 runPatchTest(
778 {x: {y: 3}},779 d => {780 const a = d.x781 d.x = 4782 d.x = a783 },784 []785 )

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 786–788why, one step back
786})
787788describe("same value replacement - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 789–795why, one step back
789 runPatchTest(
790 {x: 3},791 d => {792 d.x = 3793 },794 []795 )

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 796–798why, one step back
796})
797798describe("same value replacement - 4", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 799–804why, one step back
799 runPatchTest(
800 {x: 3},801 d => {802 d.x = 4803 d.x = 3804 },

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

lines 805–828why, one step back
805 []
806 )807})808809describe("same value replacement - 5", () => {810 runPatchTest(811 new Map([["x", 3]]),812 d => {813 d.set("x", 4)814 d.set("x", 3)815 },816 [],817 []818 )819})820821describe("same value replacement - 6", () => {822 runPatchTest(823 new Set(["x", 3]),824 d => {825 d.delete("x")826 d.add("x")827 },828 [],

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 829–830why, one step back
829 []
830 )

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 831–833why, one step back
831})
832833describe("simple delete", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 834–845why, one step back
834 runPatchTest(
835 {x: 2},836 d => {837 delete d.x838 },839 [840 {841 op: "remove",842 path: ["x"]843 }844 ]845 )

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

lines 846–906why, one step back
846})
847848describe("patch compressions yields correct results", () => {849 let p1, p2850 runPatchTest(851 {},852 d => {853 d.x = {test: true}854 },855 (p1 = [856 {857 op: "add",858 path: ["x"],859 value: {860 test: true861 }862 }863 ])864 )865 runPatchTest(866 {x: {test: true}},867 d => {868 delete d.x869 },870 (p2 = [871 {872 op: "remove",873 path: ["x"]874 }875 ])876 )877 const res = runPatchTest(878 {},879 d => {880 applyPatches(d, [...p1, ...p2])881 },882 []883 )884885 expect(res).toEqual({})886})887888describe("change then delete property", () => {889 const res = runPatchTest(890 {891 x: 1892 },893 d => {894 d.x = 2895 delete d.x896 },897 [898 {899 op: "remove",900 path: ["x"]901 }902 ]903 )904 test("valid result", () => {905 expect(res).toEqual({})906 })

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

lines 907–951why, one step back
907})
908909test("replaying patches with interweaved replacements should work correctly", () => {910 const patches = []911 const s0 = {x: 1}912913 const s1 = produce(914 s0,915 draft => {916 draft.x = 2917 },918 p => {919 patches.push(...p)920 }921 )922923 const s2 = produce(924 s1,925 draft => {926 return {x: 0}927 },928 p => {929 patches.push(...p)930 }931 )932933 const s3 = produce(934 s2,935 draft => {936 draft.x--937 },938 p => {939 patches.push(...p)940 }941 )942943 expect(s3).toEqual({x: -1}) // correct result944 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay945946 // manual replay on a draft should also be correct947 expect(948 produce(s0, draft => {949 return applyPatches(draft, patches)950 })951 ).toEqual({x: -1})

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

lines 952–997why, one step back
952})
953954test.skip("#468", () => {955 const item = {id: 1}956957 const state = [item]958959 const [nextState, patches] = produceWithPatches(state, draft => {960 draft[0].id = 2961 draft[1] = item962 })963964 expect(nextState).toMatchInlineSnapshot(`965 Array [966 Object {967 "id": 2,968 },969 Object {970 "id": 1,971 },972 ]973 `)974 expect(patches).toMatchInlineSnapshot(`975 Array [976 Object {977 "op": "replace",978 "path": Array [979 0,980 "id",981 ],982 "value": 2,983 },984 Object {985 "op": "add",986 "path": Array [987 0,988 ],989 "value": Object {990 "id": 2,991 },992 },993 ]994 `)995996 const final = applyPatches(state, patches)997 expect(final).toEqual(nextState)

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

line 998why, one step back
998})

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

lines 999–1016why, one step back
999
1000test("#521", () => {1001 const state = new Map()10021003 const [nextState, patches] = produceWithPatches(state, draft => {1004 draft.set("hello", new Set(["world"]))1005 })10061007 let patchedState = applyPatches(state, patches)1008 expect(patchedState).toEqual(nextState)10091010 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1011 draft.get("hello").add("immer")1012 })10131014 expect(applyPatches(nextState, patchesV2)).toEqual(1015 new Map([["hello", new Set(["world", "immer"])]])1016 )

Ensure values in patches are never a draftRenaud Chaputbefore adoption2454a51

lines 1017–1018why, one step back
1017})
1018

removed `.only` specifier from testMichel Weststratebefore adoption2b54ac4

line 1019why, one step back
1019test("#559 patches works in a nested reducer with proxies", () => {

Ensure values in patches are never a draftRenaud Chaputbefore adoption2454a51

lines 1020–1049why, one step back
1020 setUseProxies(true)
10211022 const state = {1023 x: 1,1024 sub: {1025 y: [{a: 0}, {a: 1}]1026 }1027 }10281029 const changes = []1030 const inverseChanges = []10311032 const newState = produce(state, draft => {1033 draft.sub = produce(1034 draft.sub,1035 draft => {1036 draft.y.pop()1037 },1038 (patches, inversePatches) => {1039 expect(isDraft(inversePatches[0].value)).toBeFalsy()1040 expect(inversePatches[0].value).toMatchObject({a: 1})1041 changes.push(...patches)1042 inverseChanges.push(...inversePatches)1043 }1044 )1045 })10461047 const reversedSubState = applyPatches(newState.sub, inverseChanges)10481049 expect(reversedSubState).toMatchObject(state.sub)

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

line 1050why, one step back
1050})