patch.js
This file
Path
__tests__/patch.js
1,016 lines · 17.2 KiB · JavaScript
Last changed by
New build set. Current statsMichel Weststratebefore adoption9aec0fe
History of this fileTake it
The walk stopped after 200 commits: lines older than f820657 point at it.
test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 1why, one step back1"use strict"fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
lines 2–4why, one step back2import produce, {3 setUseProxies,4 applyPatches,New build set. Current statsMichel Weststratebefore adoption9aec0fe
lines 5–9why, one step back5 produceWithPatches,6 enableAllPlugins7} from "../src/immer"89enableAllPlugins()test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 10–12why, one step back1011jest.setTimeout(1000)12fix: revert Map supportAlec Larsonbefore adoption47529d3
line 13why, one step back13function runPatchTest(base, producer, patches, inversePathes) {fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 14–15why, one step back14 let resultProxies, resultEs515style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 16–22why, one step back16 function runPatchTestHelper() {17 let recordedPatches18 let recordedInversePatches19 const res = produce(base, producer, (p, i) => {20 recordedPatches = p21 recordedInversePatches = i22 })test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 23why, one step back23style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 24–27why, one step back24 test("produces the correct patches", () => {25 expect(recordedPatches).toEqual(patches)26 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)27 })test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 28why, one step back28style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 29–31why, one step back29 test("patches are replayable", () => {30 expect(applyPatches(base, recordedPatches)).toEqual(res)31 })test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 32why, one step back32style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 33–35why, one step back33 test("patches can be reversed", () => {34 expect(applyPatches(res, recordedInversePatches)).toEqual(base)35 })fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 36–37why, one step back3637 return resstyle: use tabsuse-tabsbefore adoptiona9c0fd0
line 38why, one step back38 }test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 39why, one step back39style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 40–41why, one step back40 describe(`proxy`, () => {41 setUseProxies(true)fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
line 42why, one step back42 resultProxies = runPatchTestHelper()style: use tabsuse-tabsbefore adoptiona9c0fd0
line 43why, one step back43 })test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 44why, one step back44style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 45–46why, one step back45 describe(`es5`, () => {46 setUseProxies(false)fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 47–50why, one step back47 resultEs5 = runPatchTestHelper()48 test("ES5 and Proxy implementation yield same result", () => {49 expect(resultEs5).toEqual(resultProxies)50 })style: use tabsuse-tabsbefore adoptiona9c0fd0
line 51why, one step back51 })fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 52–53why, one step back5253 return resultProxiestest(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 54–56why, one step back54}5556describe("applyPatches", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 57–84why, one step back57 it("mutates the base state when it is a draft", () => {58 produce({a: 1}, draft => {59 const result = applyPatches(draft, [60 {op: "replace", path: ["a"], value: 2}61 ])62 expect(result).toBe(draft)63 expect(draft.a).toBe(2)64 })65 })66 it("produces a copy of the base state when not a draft", () => {67 const base = {a: 1}68 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])69 expect(result).not.toBe(base)70 expect(result.a).toBe(2)71 expect(base.a).toBe(1)72 })73 it('throws when `op` is not "add", "replace", nor "remove"', () => {74 expect(() => {75 const patch = {op: "copy", from: [0], path: [1]}76 applyPatches([2], [patch])77 }).toThrowErrorMatchingSnapshot()78 })79 it("throws when `path` cannot be resolved", () => {80 // missing parent81 expect(() => {82 const patch = {op: "add", path: ["a", "b"], value: 1}83 applyPatches({}, [patch])84 }).toThrowErrorMatchingSnapshot()test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 85why, one step back85style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 86–90why, one step back86 // missing grand-parent87 expect(() => {88 const patch = {op: "add", path: ["a", "b", "c"], value: 1}89 applyPatches({}, [patch])90 }).toThrowErrorMatchingSnapshot()fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
lines 91–112why, one step back91 })92 it("applied patches cannot be modified", () => {93 // see also: https://github.com/immerjs/immer/issues/41194 const s0 = {95 items: [1]96 }9798 const [s1, p1] = produceWithPatches(s0, draft => {99 draft.items = []100 })101102 const replaceValueBefore = p1[0].value.slice()103104 const [s2, p2] = produceWithPatches(s1, draft => {105 draft.items.push(2)106 })107108 applyPatches(s0, [...p1, ...p2])109110 const replaceValueAfter = p1[0].value.slice()111112 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 113why, one step back113 })test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 114–116why, one step back114})115116describe("simple assignment - 1", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 117–123why, one step back117 runPatchTest(118 {x: 3},119 d => {120 d.x++121 },122 [{op: "replace", path: ["x"], value: 4}]123 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 124–126why, one step back124})125126describe("simple assignment - 2", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 127–133why, one step back127 runPatchTest(128 {x: {y: 4}},129 d => {130 d.x.y++131 },132 [{op: "replace", path: ["x", "y"], value: 5}]133 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 134–135why, one step back134})135feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 136why, one step back136describe("simple assignment - 3", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 137–143why, one step back137 runPatchTest(138 {x: [{y: 4}]},139 d => {140 d.x[0].y++141 },142 [{op: "replace", path: ["x", 0, "y"], value: 5}]143 )feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 144–145why, one step back144})145feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 146–179why, one step back146describe("simple assignment - 4", () => {147 runPatchTest(148 new Map([["x", {y: 4}]]),149 d => {150 d.get("x").y++151 },152 [{op: "replace", path: ["x", "y"], value: 5}],153 [{op: "replace", path: ["x", "y"], value: 4}]154 )155})156157describe("simple assignment - 5", () => {158 runPatchTest(159 {x: new Map([["y", 4]])},160 d => {161 d.x.set("y", 5)162 },163 [{op: "replace", path: ["x", "y"], value: 5}],164 [{op: "replace", path: ["x", "y"], value: 4}]165 )166})167168describe("simple assignment - 6", () => {169 runPatchTest(170 new Map([["x", 1]]),171 d => {172 // Map.prototype.set should return the Map itself173 const res = d.set("x", 2)174 res.set("y", 3)175 },176 [177 {op: "replace", path: ["x"], value: 2},178 {op: "add", path: ["y"], value: 3}179 ],chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 180–183why, one step back180 [181 {op: "replace", path: ["x"], value: 1},182 {op: "remove", path: ["y"]}183 ]feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 184–206why, one step back184 )185})186187describe("simple assignment - 7", () => {188 const key1 = {prop: "val1"}189 const key2 = {prop: "val2"}190 runPatchTest(191 {x: new Map([[key1, 4]])},192 d => {193 d.x.set(key1, 5)194 d.x.set(key2, 6)195 },196 [197 {op: "replace", path: ["x", key1], value: 5},198 {op: "add", path: ["x", key2], value: 6}199 ],200 [201 {op: "replace", path: ["x", key1], value: 4},202 {op: "remove", path: ["x", key2]}203 ]204 )205})206test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 207why, one step back207describe("delete 1", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 208–214why, one step back208 runPatchTest(209 {x: {y: 4}},210 d => {211 delete d.x212 },213 [{op: "remove", path: ["x"]}]214 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 215–216why, one step back215})216feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 217–242why, one step back217describe("delete 2", () => {218 runPatchTest(219 new Map([["x", 1]]),220 d => {221 d.delete("x")222 },223 [{op: "remove", path: ["x"]}],224 [{op: "add", path: ["x"], value: 1}]225 )226})227228describe("delete 3", () => {229 runPatchTest(230 {x: new Map([["y", 1]])},231 d => {232 d.x.delete("y")233 },234 [{op: "remove", path: ["x", "y"]}],235 [{op: "add", path: ["x", "y"], value: 1}]236 )237})238239describe("delete 5", () => {240 const key1 = {prop: "val1"}241 const key2 = {prop: "val2"}242 runPatchTest(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 243–248why, one step back243 {244 x: new Map([245 [key1, 1],246 [key2, 2]247 ])248 },feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 249–253why, one step back249 d => {250 d.x.delete(key1)251 d.x.delete(key2)252 },253 [chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 254–257why, one step back254 {op: "remove", path: ["x", key1]},255 {op: "remove", path: ["x", key2]}256 ],257 [feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 258–285why, one step back258 {op: "add", path: ["x", key1], value: 1},259 {op: "add", path: ["x", key2], value: 2}260 ]261 )262})263264describe("delete 6", () => {265 runPatchTest(266 new Set(["x", 1]),267 d => {268 d.delete("x")269 },270 [{op: "remove", path: [0], value: "x"}],271 [{op: "add", path: [0], value: "x"}]272 )273})274275describe("delete 7", () => {276 runPatchTest(277 {x: new Set(["y", 1])},278 d => {279 d.x.delete("y")280 },281 [{op: "remove", path: ["x", 0], value: "y"}],282 [{op: "add", path: ["x", 0], value: "y"}]283 )284})285test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 286why, one step back286describe("renaming properties", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 287–293why, one step back287 describe("nested object (no changes)", () => {288 runPatchTest(289 {a: {b: 1}},290 d => {291 d.x = d.a292 delete d.a293 },chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 294–297why, one step back294 [295 {op: "add", path: ["x"], value: {b: 1}},296 {op: "remove", path: ["a"]}297 ]style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 298–299why, one step back298 )299 })test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 300why, one step back300feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 301–318why, one step back301 describe("nested map (no changes)", () => {302 runPatchTest(303 new Map([["a", new Map([["b", 1]])]]),304 d => {305 d.set("x", d.get("a"))306 d.delete("a")307 },308 [309 {op: "add", path: ["x"], value: new Map([["b", 1]])},310 {op: "remove", path: ["a"]}311 ],312 [313 {op: "remove", path: ["x"]},314 {op: "add", path: ["a"], value: new Map([["b", 1]])}315 ]316 )317 })318style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 319–326why, one step back319 describe("nested object (with changes)", () => {320 runPatchTest(321 {a: {b: 1, c: 1}},322 d => {323 let a = d.a324 a.b = 2 // change325 delete a.c // delete326 a.y = 2 // addtest(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 327why, one step back327style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 328–337why, one step back328 // rename329 d.x = a330 delete d.a331 },332 [333 {op: "add", path: ["x"], value: {b: 2, y: 2}},334 {op: "remove", path: ["a"]}335 ]336 )337 })test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 338why, one step back338feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 339–340why, one step back339 describe("nested map (with changes)", () => {340 runPatchTest(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 341–349why, one step back341 new Map([342 [343 "a",344 new Map([345 ["b", 1],346 ["c", 1]347 ])348 ]349 ]),feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 350–360why, one step back350 d => {351 let a = d.get("a")352 a.set("b", 2) // change353 a.delete("c") // delete354 a.set("y", 2) // add355356 // rename357 d.set("x", a)358 d.delete("a")359 },360 [chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 361–368why, one step back361 {362 op: "add",363 path: ["x"],364 value: new Map([365 ["b", 2],366 ["y", 2]367 ])368 },feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 369–372why, one step back369 {op: "remove", path: ["a"]}370 ],371 [372 {op: "remove", path: ["x"]},chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 373–380why, one step back373 {374 op: "add",375 path: ["a"],376 value: new Map([377 ["b", 1],378 ["c", 1]379 ])380 }feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 381–384why, one step back381 ]382 )383 })384style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 385–392why, one step back385 describe("deeply nested object (with changes)", () => {386 runPatchTest(387 {a: {b: {c: 1, d: 1}}},388 d => {389 let b = d.a.b390 b.c = 2 // change391 delete b.d // delete392 b.y = 2 // addtest(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 393why, one step back393style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 394–403why, one step back394 // rename395 d.a.x = b396 delete d.a.b397 },398 [399 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},400 {op: "remove", path: ["a", "b"]}401 ]402 )403 })feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 404–406why, one step back404405 describe("deeply nested map (with changes)", () => {406 runPatchTest(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 407–420why, one step back407 new Map([408 [409 "a",410 new Map([411 [412 "b",413 new Map([414 ["c", 1],415 ["d", 1]416 ])417 ]418 ])419 ]420 ]),feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 421–434why, one step back421 d => {422 let b = d.get("a").get("b")423 b.set("c", 2) // change424 b.delete("d") // delete425 b.set("y", 2) // add426427 // rename428 d.get("a").set("x", b)429 d.get("a").delete("b")430 },431 [432 {433 op: "add",434 path: ["a", "x"],chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 435–438why, one step back435 value: new Map([436 ["c", 2],437 ["y", 2]438 ])feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 439–446why, one step back439 },440 {op: "remove", path: ["a", "b"]}441 ],442 [443 {op: "remove", path: ["a", "x"]},444 {445 op: "add",446 path: ["a", "b"],chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 447–450why, one step back447 value: new Map([448 ["c", 1],449 ["d", 1]450 ])feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 451–454why, one step back451 }452 ]453 )454 })test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 455–457why, one step back455})456457describe("minimum amount of changes", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 458–469why, one step back458 runPatchTest(459 {x: 3, y: {a: 4}, z: 3},460 d => {461 d.y.a = 4462 d.y.b = 5463 Object.assign(d, {x: 4, y: {a: 2}})464 },465 [466 {op: "replace", path: ["x"], value: 4},467 {op: "replace", path: ["y"], value: {a: 2}}468 ]469 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 470–472why, one step back470})471472describe("arrays - prepend", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 473–479why, one step back473 runPatchTest(474 {x: [1, 2, 3]},475 d => {476 d.x.unshift(4)477 },478 [{op: "add", path: ["x", 0], value: 4}]479 )feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 480–482why, one step back480})481482describe("arrays - multiple prepend", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 483–493why, one step back483 runPatchTest(484 {x: [1, 2, 3]},485 d => {486 d.x.unshift(4)487 d.x.unshift(5)488 },489 [490 {op: "add", path: ["x", 0], value: 5},491 {op: "add", path: ["x", 1], value: 4}492 ]493 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 494–496why, one step back494})495496describe("arrays - splice middle", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 497–503why, one step back497 runPatchTest(498 {x: [1, 2, 3]},499 d => {500 d.x.splice(1, 1)501 },502 [{op: "remove", path: ["x", 1]}]503 )feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 504–506why, one step back504})505506describe("arrays - multiple splice", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 507–519why, one step back507 runPatchTest(508 [0, 1, 2, 3, 4, 5, 0],509 d => {510 d.splice(4, 2, 3)511 d.splice(1, 2, 3)512 },513 [514 {op: "replace", path: [1], value: 3},515 {op: "replace", path: [2], value: 3},516 {op: "remove", path: [5]},517 {op: "remove", path: [4]}518 ]519 )feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 520–522why, one step back520})521522describe("arrays - modify and shrink", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 523–529why, one step back523 runPatchTest(524 {x: [1, 2, 3]},525 d => {526 d.x[0] = 4527 d.x.length = 2528 },529 [chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 530–533why, one step back530 {op: "replace", path: ["x", 0], value: 4},531 {op: "remove", path: ["x", 2]}532 ],533 [Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 534–535why, one step back534 {op: "replace", path: ["x", 0], value: 1},535 {op: "add", path: ["x", 2], value: 3}style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 536–537why, one step back536 ]537 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 538–540why, one step back538})539540describe("arrays - prepend then splice middle", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 541–551why, one step back541 runPatchTest(542 {x: [1, 2, 3]},543 d => {544 d.x.unshift(4)545 d.x.splice(2, 1)546 },547 [548 {op: "replace", path: ["x", 0], value: 4},549 {op: "replace", path: ["x", 1], value: 1}550 ]551 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 552–554why, one step back552})553554describe("arrays - splice middle then prepend", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 555–565why, one step back555 runPatchTest(556 {x: [1, 2, 3]},557 d => {558 d.x.splice(1, 1)559 d.x.unshift(4)560 },561 [562 {op: "replace", path: ["x", 0], value: 4},563 {op: "replace", path: ["x", 1], value: 1}564 ]565 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 566–568why, one step back566})567568describe("arrays - truncate", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 569–574why, one step back569 runPatchTest(570 {x: [1, 2, 3]},571 d => {572 d.x.length -= 2573 },574 [chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 575–578why, one step back575 {op: "remove", path: ["x", 2]},576 {op: "remove", path: ["x", 1]}577 ],578 [style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 579–582why, one step back579 {op: "add", path: ["x", 1], value: 2},580 {op: "add", path: ["x", 2], value: 3}581 ]582 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 583–585why, one step back583})584585describe("arrays - pop twice", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 586–591why, one step back586 runPatchTest(587 {x: [1, 2, 3]},588 d => {589 d.x.pop()590 d.x.pop()591 },chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 592–595why, one step back592 [593 {op: "remove", path: ["x", 2]},594 {op: "remove", path: ["x", 1]}595 ]style: use tabsuse-tabsbefore adoptiona9c0fd0
line 596why, one step back596 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 597–599why, one step back597})598599describe("arrays - push multiple", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 600–608why, one step back600 runPatchTest(601 {x: [1, 2, 3]},602 d => {603 d.x.push(4, 5)604 },605 [606 {op: "add", path: ["x", 3], value: 4},607 {op: "add", path: ["x", 4], value: 5}608 ],chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 609–612why, one step back609 [610 {op: "remove", path: ["x", 4]},611 {op: "remove", path: ["x", 3]}612 ]style: use tabsuse-tabsbefore adoptiona9c0fd0
line 613why, one step back613 )feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 614–616why, one step back614})615616describe("arrays - splice (expand)", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 617–632why, one step back617 runPatchTest(618 {x: [1, 2, 3]},619 d => {620 d.x.splice(1, 1, 4, 5, 6)621 },622 [623 {op: "replace", path: ["x", 1], value: 4},624 {op: "add", path: ["x", 2], value: 5},625 {op: "add", path: ["x", 3], value: 6}626 ],627 [628 {op: "replace", path: ["x", 1], value: 2},629 {op: "remove", path: ["x", 3]},630 {op: "remove", path: ["x", 2]}631 ]632 )feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 633–635why, one step back633})634635describe("arrays - splice (shrink)", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 636–650why, one step back636 runPatchTest(637 {x: [1, 2, 3, 4, 5]},638 d => {639 d.x.splice(1, 3, 6)640 },641 [642 {op: "replace", path: ["x", 1], value: 6},643 {op: "remove", path: ["x", 3]},644 {op: "remove", path: ["x", 2]}645 ],646 [647 {op: "replace", path: ["x", 1], value: 2},648 {op: "add", path: ["x", 2], value: 3},649 {op: "add", path: ["x", 3], value: 4}650 ]Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 651–653why, one step back651 )652})653fix: incorrect patches generated for array if property was deleted insideMichel Weststratebefore adoption1e451a3
lines 654–668why, one step back654describe("arrays - delete", () => {655 runPatchTest(656 {657 x: [658 {a: 1, b: 2},659 {c: 3, d: 4}660 ]661 },662 d => {663 delete d.x[1].c664 },665 [{op: "remove", path: ["x", 1, "c"]}]666 )667})668feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 669–712why, one step back669describe("sets - add - 1", () => {670 runPatchTest(671 new Set([1]),672 d => {673 d.add(2)674 },675 [{op: "add", path: [1], value: 2}],676 [{op: "remove", path: [1], value: 2}]677 )678})679680describe("sets - add, delete, add - 1", () => {681 runPatchTest(682 new Set([1]),683 d => {684 d.add(2)685 d.delete(2)686 d.add(2)687 },688 [{op: "add", path: [1], value: 2}],689 [{op: "remove", path: [1], value: 2}]690 )691})692693describe("sets - add, delete, add - 2", () => {694 runPatchTest(695 new Set([2, 1]),696 d => {697 d.add(2)698 d.delete(2)699 d.add(2)700 },701 [],702 []703 )704})705706describe("sets - mutate - 1", () => {707 const findById = (set, id) => {708 for (const item of set) {709 if (item.id === id) return item710 }711 }712 runPatchTest(chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 713–716why, one step back713 new Set([714 {id: 1, val: "We"},715 {id: 2, val: "will"}716 ]),feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 717–737why, one step back717 d => {718 const obj1 = findById(d, 1)719 const obj2 = findById(d, 2)720 obj1.val = "rock"721 obj2.val = "you"722 },723 [724 {op: "remove", path: [0], value: {id: 1, val: "We"}},725 {op: "remove", path: [1], value: {id: 2, val: "will"}},726 {op: "add", path: [0], value: {id: 1, val: "rock"}},727 {op: "add", path: [1], value: {id: 2, val: "you"}}728 ],729 [730 {op: "remove", path: [1], value: {id: 2, val: "you"}},731 {op: "remove", path: [0], value: {id: 1, val: "rock"}},732 {op: "add", path: [1], value: {id: 2, val: "will"}},733 {op: "add", path: [0], value: {id: 1, val: "We"}}734 ]735 )736})737Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 738–756why, one step back738describe("arrays - splice should should result in remove op.", () => {739 runPatchTest(740 [1, 2],741 d => {742 d.splice(1, 1)743 },744 [{op: "remove", path: [1]}],745 [{op: "add", path: [1], value: 2}]746 )747})748749describe("arrays - NESTED splice should should result in remove op.", () => {750 runPatchTest(751 {a: {b: {c: [1, 2]}}},752 d => {753 d.a.b.c.splice(1, 1)754 },755 [{op: "remove", path: ["a", "b", "c", 1]}],756 [{op: "add", path: ["a", "b", "c", 1], value: 2}]style: use tabsuse-tabsbefore adoptiona9c0fd0
line 757why, one step back757 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 758–760why, one step back758})759760describe("simple replacement", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 761why, one step back761 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 762–764why, one step back762})763764describe("same value replacement - 1", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 765–772why, one step back765 runPatchTest(766 {x: {y: 3}},767 d => {768 const a = d.x769 d.x = a770 },771 []772 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 773–775why, one step back773})774775describe("same value replacement - 2", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 776–784why, one step back776 runPatchTest(777 {x: {y: 3}},778 d => {779 const a = d.x780 d.x = 4781 d.x = a782 },783 []784 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 785–787why, one step back785})786787describe("same value replacement - 3", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 788–794why, one step back788 runPatchTest(789 {x: 3},790 d => {791 d.x = 3792 },793 []794 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 795–797why, one step back795})796797describe("same value replacement - 4", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 798–803why, one step back798 runPatchTest(799 {x: 3},800 d => {801 d.x = 4802 d.x = 3803 },feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 804–827why, one step back804 []805 )806})807808describe("same value replacement - 5", () => {809 runPatchTest(810 new Map([["x", 3]]),811 d => {812 d.set("x", 4)813 d.set("x", 3)814 },815 [],816 []817 )818})819820describe("same value replacement - 6", () => {821 runPatchTest(822 new Set(["x", 3]),823 d => {824 d.delete("x")825 d.add("x")826 },827 [],style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 828–829why, one step back828 []829 )test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
lines 830–832why, one step back830})831832describe("simple delete", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 833–844why, one step back833 runPatchTest(834 {x: 2},835 d => {836 delete d.x837 },838 [839 {840 op: "remove",841 path: ["x"]842 }843 ]844 )fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1
lines 845–905why, one step back845})846847describe("patch compressions yields correct results", () => {848 let p1, p2849 runPatchTest(850 {},851 d => {852 d.x = {test: true}853 },854 (p1 = [855 {856 op: "add",857 path: ["x"],858 value: {859 test: true860 }861 }862 ])863 )864 runPatchTest(865 {x: {test: true}},866 d => {867 delete d.x868 },869 (p2 = [870 {871 op: "remove",872 path: ["x"]873 }874 ])875 )876 const res = runPatchTest(877 {},878 d => {879 applyPatches(d, [...p1, ...p2])880 },881 []882 )883884 expect(res).toEqual({})885})886887describe("change then delete property", () => {888 const res = runPatchTest(889 {890 x: 1891 },892 d => {893 d.x = 2894 delete d.x895 },896 [897 {898 op: "remove",899 path: ["x"]900 }901 ]902 )903 test("valid result", () => {904 expect(res).toEqual({})905 })fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
lines 906–950why, one step back906})907908test("replaying patches with interweaved replacements should work correctly", () => {909 const patches = []910 const s0 = {x: 1}911912 const s1 = produce(913 s0,914 draft => {915 draft.x = 2916 },917 p => {918 patches.push(...p)919 }920 )921922 const s2 = produce(923 s1,924 draft => {925 return {x: 0}926 },927 p => {928 patches.push(...p)929 }930 )931932 const s3 = produce(933 s2,934 draft => {935 draft.x--936 },937 p => {938 patches.push(...p)939 }940 )941942 expect(s3).toEqual({x: -1}) // correct result943 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay944945 // manual replay on a draft should also be correct946 expect(947 produce(s0, draft => {948 return applyPatches(draft, patches)949 })950 ).toEqual({x: -1})chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4
lines 951–996why, one step back951})952953test.skip("#468", () => {954 const item = {id: 1}955956 const state = [item]957958 const [nextState, patches] = produceWithPatches(state, draft => {959 draft[0].id = 2960 draft[1] = item961 })962963 expect(nextState).toMatchInlineSnapshot(`964 Array [965 Object {966 "id": 2,967 },968 Object {969 "id": 1,970 },971 ]972 `)973 expect(patches).toMatchInlineSnapshot(`974 Array [975 Object {976 "op": "replace",977 "path": Array [978 0,979 "id",980 ],981 "value": 2,982 },983 Object {984 "op": "add",985 "path": Array [986 0,987 ],988 "value": Object {989 "id": 2,990 },991 },992 ]993 `)994995 const final = applyPatches(state, patches)996 expect(final).toEqual(nextState)test(ts): parameter types of curried producersaleclarsonbefore adoptionf820657
line 997why, one step back997})fix: `Set` inside patches were not correctly cloned. Fixes #521Michel Weststratebefore adoption9d8d995
lines 998–1016why, one step back998999test("#521", () => {1000 const state = new Map()10011002 const [nextState, patches] = produceWithPatches(state, draft => {1003 draft.set("hello", new Set(["world"]))1004 })10051006 let patchedState = applyPatches(state, patches)1007 expect(patchedState).toEqual(nextState)10081009 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1010 draft.get("hello").add("immer")1011 })10121013 expect(applyPatches(nextState, patchesV2)).toEqual(1014 new Map([["hello", new Set(["world", "immer"])]])1015 )1016})