Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

1,496 lines · 27.5 KiB · JavaScript

Last changed by

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

History of this file

Take it

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

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

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

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 2why, one step back
2import {vi} from "vitest"

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

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

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 5–7why, one step back
5 applyPatches,
6 produceWithPatches,7 isDraft,

fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f

line 8why, one step back
8 immerable,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 9–11why, one step back
9 nothing,
10 enablePatches,11 enableMapSet

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

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

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 13why, one step back
13

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 14–15why, one step back
14enablePatches()
15enableMapSet()

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 16–20why, one step back
16
17vi.setConfig({18 testTimeout: 100019})20

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 21why, one step back
21const isProd = process.env.NODE_ENV === "production"

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 22–73why, one step back
22
23function createPatchTestData(24 base,25 producer,26 expectedPatches,27 expectedInversePatches,28 expectedResult29) {30 let recordedPatches, recordedInversePatches31 const res = produce(base, producer, (p, i) => {32 recordedPatches = p33 recordedInversePatches = i34 })3536 return {37 result: res,38 patches: recordedPatches,39 inversePatches: recordedInversePatches,40 expectedPatches,41 expectedInversePatches,42 expectedResult,43 base44 }45}4647function runPatchTests(48 testName,49 base,50 producer,51 expectedPatches,52 expectedInversePatches,53 expectedResult,54 options = {}55) {56 const {only = false, skip = false} = options5758 // Choose the appropriate describe function59 let describeFn = describe60 if (testName === "") describeFn = (name, fn) => fn()61 if (only) describeFn = describe.only62 if (skip) describeFn = describe.skip6364 let testData = createPatchTestData(65 base,66 producer,67 expectedPatches,68 expectedInversePatches,69 expectedResult70 )7172 describeFn(testName, () => {73 if (expectedResult !== undefined) {

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

line 74why, one step back
74 test("produced the correct result", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 75–78why, one step back
75 expect(testData.result).toEqual(expectedResult)
76 })77 }78

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 79why, one step back
79 test("produces the correct patches", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 80–85why, one step back
80 expect(testData.patches).toEqual(expectedPatches)
81 if (expectedInversePatches) {82 expect(testData.inversePatches).toEqual(expectedInversePatches)83 }84 })85

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 86why, one step back
86 test("patches are replayable", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 87–89why, one step back
87 expect(applyPatches(base, testData.patches)).toEqual(testData.result)
88 })89

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 90why, one step back
90 test("patches can be reversed", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 91–140why, one step back
91 if (expectedInversePatches) {
92 expect(applyPatches(testData.result, testData.inversePatches)).toEqual(93 base94 )95 }96 })97 })9899 return testData100}101102// Convenience functions for common use cases103runPatchTests.only = function(104 testName,105 base,106 producer,107 expectedPatches,108 expectedInversePatches,109 expectedResult110) {111 return runPatchTests(112 testName,113 base,114 producer,115 expectedPatches,116 expectedInversePatches,117 expectedResult,118 {only: true}119 )120}121122runPatchTests.skip = function(123 testName,124 base,125 producer,126 expectedPatches,127 expectedInversePatches,128 expectedResult129) {130 return runPatchTests(131 testName,132 base,133 producer,134 expectedPatches,135 expectedInversePatches,136 expectedResult,137 {skip: true}138 )139}140

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 141–145why, one step back
141describe("applyPatches", () => {
142 it("mutates the base state when it is a draft", () => {143 produce({a: 1}, draft => {144 const result = applyPatches(draft, [145 {op: "replace", path: ["a"], value: 2}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 146why, one step back
146 ])

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 147–148why, one step back
147 expect(result).toBe(draft)
148 expect(draft.a).toBe(2)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 149–150why, one step back
149 })
150 })

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 151–156why, one step back
151 it("produces a copy of the base state when not a draft", () => {
152 const base = {a: 1}153 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])154 expect(result).not.toBe(base)155 expect(result.a).toBe(2)156 expect(base.a).toBe(1)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 157why, one step back
157 })

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 158why, one step back
158 it('throws when `op` is not "add", "replace", nor "remove"', () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 159why, one step back
159 expect(() => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 160–161why, one step back
160 const patch = {op: "copy", from: [0], path: [1]}
161 applyPatches([2], [patch])

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 162–163why, one step back
162 }).toThrowErrorMatchingSnapshot()
163 })

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 164–165why, one step back
164 it("throws when `path` cannot be resolved", () => {
165 // missing parent

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 166why, one step back
166 expect(() => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 167why, one step back
167 const patch = {op: "add", path: ["a", "b"], value: 1}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 168–170why, one step back
168 applyPatches({}, [patch])
169 }).toThrowErrorMatchingSnapshot()170

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 171why, one step back
171 // missing grand-parent

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 172why, one step back
172 expect(() => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 173why, one step back
173 const patch = {op: "add", path: ["a", "b", "c"], value: 1}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 174–176why, one step back
174 applyPatches({}, [patch])
175 }).toThrowErrorMatchingSnapshot()176 })

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 177–180why, one step back
177 it("applied patches cannot be modified", () => {
178 // see also: https://github.com/immerjs/immer/issues/411179 const s0 = {180 items: [1]

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 181–182why, one step back
181 }
182

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 183–184why, one step back
183 const [s1, p1] = produceWithPatches(s0, draft => {
184 draft.items = []

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 185–186why, one step back
185 })
186

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 187why, one step back
187 const replaceValueBefore = p1[0].value.slice()

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 188why, one step back
188

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 189–190why, one step back
189 const [s2, p2] = produceWithPatches(s1, draft => {
190 draft.items.push(2)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 191–192why, one step back
191 })
192

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 193why, one step back
193 applyPatches(s0, [...p1, ...p2])

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 194why, one step back
194

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 195why, one step back
195 const replaceValueAfter = p1[0].value.slice()

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 196why, one step back
196

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 197why, one step back
197 expect(replaceValueAfter).toStrictEqual(replaceValueBefore)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 198–279why, one step back
198 })
199})200201// New macro-style test202runPatchTests(203 "simple assignment - 1",204 {x: 3},205 d => {206 d.x++207 },208 [{op: "replace", path: ["x"], value: 4}]209)210211// Original test (commented out for comparison)212// describe("simple assignment - 1", () => {213// runPatchTest(214// {x: 3},215// d => {216// d.x++217// },218// [{op: "replace", path: ["x"], value: 4}]219// )220// })221222// New macro-style tests223runPatchTests(224 "simple assignment - 2",225 {x: {y: 4}},226 d => {227 d.x.y++228 },229 [{op: "replace", path: ["x", "y"], value: 5}]230)231232runPatchTests(233 "simple assignment - 3",234 {x: [{y: 4}]},235 d => {236 d.x[0].y++237 },238 [{op: "replace", path: ["x", 0, "y"], value: 5}]239)240241runPatchTests(242 "simple assignment - 4",243 new Map([["x", {y: 4}]]),244 d => {245 d.get("x").y++246 },247 [{op: "replace", path: ["x", "y"], value: 5}],248 [{op: "replace", path: ["x", "y"], value: 4}]249)250251runPatchTests(252 "simple assignment - 5",253 {x: new Map([["y", 4]])},254 d => {255 d.x.set("y", 5)256 },257 [{op: "replace", path: ["x", "y"], value: 5}],258 [{op: "replace", path: ["x", "y"], value: 4}]259)260261runPatchTests(262 "simple assignment - 6",263 new Map([["x", 1]]),264 d => {265 // Map.prototype.set should return the Map itself266 const res = d.set("x", 2)267 res.set("y", 3)268 },269 [270 {op: "replace", path: ["x"], value: 2},271 {op: "add", path: ["y"], value: 3}272 ],273 [274 {op: "replace", path: ["x"], value: 1},275 {op: "remove", path: ["y"]}276 ]277)278279// Complex test with external variables - keep as individual describe

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

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

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 281–285why, one step back
281 const key1 = {prop: "val1"}
282 const key2 = {prop: "val2"}283284 runPatchTests(285 "",

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 286why, one step back
286 {x: new Map([[key1, 4]])},

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 287why, one step back
287 d => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 288–289why, one step back
288 d.x.set(key1, 5)
289 d.x.set(key2, 6)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 290–291why, one step back
290 },
291 [

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 292–293why, one step back
292 {op: "replace", path: ["x", key1], value: 5},
293 {op: "add", path: ["x", key2], value: 6}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 294–295why, one step back
294 ],
295 [

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

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

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 297–347why, one step back
297 {op: "remove", path: ["x", key2]}
298 ]299 )300})301302runPatchTests(303 "simple assignment - 8",304 new Map([[0, new Map([[1, 4]])]]),305 d => {306 d.get(0).set(1, 5)307 d.get(0).set(2, 6)308 },309 [310 {op: "replace", path: [0, 1], value: 5},311 {op: "add", path: [0, 2], value: 6}312 ],313 [314 {op: "replace", path: [0, 1], value: 4},315 {op: "remove", path: [0, 2]}316 ]317)318319runPatchTests(320 "delete 1",321 {x: {y: 4}},322 d => {323 delete d.x324 },325 [{op: "remove", path: ["x"]}]326)327328runPatchTests(329 "delete 2",330 new Map([["x", 1]]),331 d => {332 d.delete("x")333 },334 [{op: "remove", path: ["x"]}],335 [{op: "add", path: ["x"], value: 1}]336)337338runPatchTests(339 "delete 3",340 {x: new Map([["y", 1]])},341 d => {342 d.x.delete("y")343 },344 [{op: "remove", path: ["x", "y"]}],345 [{op: "add", path: ["x", "y"], value: 1}]346)347

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 348why, one step back
348describe("delete 5", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 349–354why, one step back
349 const key1 = {prop: "val1"}
350 const key2 = {prop: "val2"}351352 runPatchTests(353 "",354 {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 355–357why, one step back
355 x: new Map([
356 [key1, 1],357 [key2, 2]

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 358–360why, one step back
358 ])
359 },360 d => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 361–362why, one step back
361 d.x.delete(key1)
362 d.x.delete(key2)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 363–364why, one step back
363 },
364 [

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 365why, one step back
365 {op: "remove", path: ["x", key1]},

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 366–368why, one step back
366 {op: "remove", path: ["x", key2]}
367 ],368 [

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 369–370why, one step back
369 {op: "add", path: ["x", key1], value: 1},
370 {op: "add", path: ["x", key2], value: 2}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 371–394why, one step back
371 ]
372 )373})374375runPatchTests(376 "delete 6",377 new Set(["x", 1]),378 d => {379 d.delete("x")380 },381 [{op: "remove", path: [0], value: "x"}],382 [{op: "add", path: [0], value: "x"}]383)384385runPatchTests(386 "delete 7",387 {x: new Set(["y", 1])},388 d => {389 d.x.delete("y")390 },391 [{op: "remove", path: ["x", 0], value: "y"}],392 [{op: "add", path: ["x", 0], value: "y"}]393)394

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

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

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 396–500why, one step back
396 runPatchTests(
397 "nested object (no changes)",398 {a: {b: 1}},399 d => {400 d.x = d.a401 delete d.a402 },403 [404 {op: "add", path: ["x"], value: {b: 1}},405 {op: "remove", path: ["a"]}406 ]407 )408409 runPatchTests(410 "nested change in object",411 {412 a: {b: 1}413 },414 d => {415 d.a.b++416 },417 [{op: "replace", path: ["a", "b"], value: 2}],418 [{op: "replace", path: ["a", "b"], value: 1}]419 )420421 runPatchTests(422 "nested change in map",423 new Map([["a", new Map([["b", 1]])]]),424 d => {425 d.get("a").set("b", 2)426 },427 [{op: "replace", path: ["a", "b"], value: 2}],428 [{op: "replace", path: ["a", "b"], value: 1}]429 )430431 runPatchTests(432 "nested change in array",433 [[{b: 1}]],434 d => {435 d[0][0].b++436 },437 [{op: "replace", path: [0, 0, "b"], value: 2}],438 [{op: "replace", path: [0, 0, "b"], value: 1}]439 )440441 runPatchTests(442 "nested map (no changes)",443 new Map([["a", new Map([["b", 1]])]]),444 d => {445 d.set("x", d.get("a"))446 d.delete("a")447 },448 [449 {op: "add", path: ["x"], value: new Map([["b", 1]])},450 {op: "remove", path: ["a"]}451 ],452 [453 {op: "remove", path: ["x"]},454 {op: "add", path: ["a"], value: new Map([["b", 1]])}455 ]456 )457458 runPatchTests(459 "nested object (with changes)",460 {a: {b: 1, c: 1}},461 d => {462 let a = d.a463 a.b = 2 // change464 delete a.c // delete465 a.y = 2 // add466467 // rename468 d.x = a469 delete d.a470 },471 [472 {op: "add", path: ["x"], value: {b: 2, y: 2}},473 {op: "remove", path: ["a"]}474 ]475 )476477 runPatchTests(478 "nested map (with changes)",479 new Map([480 [481 "a",482 new Map([483 ["b", 1],484 ["c", 1]485 ])486 ]487 ]),488 d => {489 let a = d.get("a")490 a.set("b", 2) // change491 a.delete("c") // delete492 a.set("y", 2) // add493494 // rename495 d.set("x", a)496 d.delete("a")497 },498 [499 {500 op: "add",

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 501why, one step back
501 path: ["x"],

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 502–1025why, one step back
502 value: new Map([
503 ["b", 2],504 ["y", 2]505 ])506 },507 {op: "remove", path: ["a"]}508 ],509 [510 {op: "remove", path: ["x"]},511 {512 op: "add",513 path: ["a"],514 value: new Map([515 ["b", 1],516 ["c", 1]517 ])518 }519 ]520 )521522 runPatchTests(523 "deeply nested object (with changes)",524 {a: {b: {c: 1, d: 1}}},525 d => {526 let b = d.a.b527 b.c = 2 // change528 delete b.d // delete529 b.y = 2 // add530531 // rename532 d.a.x = b533 delete d.a.b534 },535 [536 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},537 {op: "remove", path: ["a", "b"]}538 ]539 )540541 runPatchTests(542 "deeply nested map (with changes)",543 new Map([544 [545 "a",546 new Map([547 [548 "b",549 new Map([550 ["c", 1],551 ["d", 1]552 ])553 ]554 ])555 ]556 ]),557 d => {558 let b = d.get("a").get("b")559 b.set("c", 2) // change560 b.delete("d") // delete561 b.set("y", 2) // add562563 // rename564 d.get("a").set("x", b)565 d.get("a").delete("b")566 },567 [568 {569 op: "add",570 path: ["a", "x"],571 value: new Map([572 ["c", 2],573 ["y", 2]574 ])575 },576 {op: "remove", path: ["a", "b"]}577 ],578 [579 {op: "remove", path: ["a", "x"]},580 {581 op: "add",582 path: ["a", "b"],583 value: new Map([584 ["c", 1],585 ["d", 1]586 ])587 }588 ]589 )590})591592runPatchTests(593 "minimum amount of changes",594 {x: 3, y: {a: 4}, z: 3},595 d => {596 d.y.a = 4597 d.y.b = 5598 Object.assign(d, {x: 4, y: {a: 2}})599 },600 [601 {op: "replace", path: ["x"], value: 4},602 {op: "replace", path: ["y"], value: {a: 2}}603 ]604)605606runPatchTests(607 "arrays - prepend",608 {x: [1, 2, 3]},609 d => {610 d.x.unshift(4)611 },612 [613 {op: "replace", path: ["x", 0], value: 4},614 {op: "replace", path: ["x", 1], value: 1},615 {op: "replace", path: ["x", 2], value: 2},616 {op: "add", path: ["x", 3], value: 3}617 ]618)619620runPatchTests(621 "arrays - multiple prepend",622 {x: [1, 2, 3]},623 d => {624 d.x.unshift(4)625 d.x.unshift(5)626 // 4,5,1,2,3627 },628 [629 {op: "replace", path: ["x", 0], value: 5},630 {op: "replace", path: ["x", 1], value: 4},631 {op: "replace", path: ["x", 2], value: 1},632 {op: "add", path: ["x", 3], value: 2},633 {op: "add", path: ["x", 4], value: 3}634 ]635)636637runPatchTests(638 "arrays - splice middle",639 {x: [1, 2, 3]},640 d => {641 d.x.splice(1, 1)642 },643 [644 {op: "replace", path: ["x", 1], value: 3},645 {op: "remove", path: ["x", 2]}646 ]647)648649runPatchTests(650 "arrays - multiple splice",651 [0, 1, 2, 3, 4, 5, 0],652 d => {653 d.splice(4, 2, 3)654 // [0,1,2,3,3,0]655 d.splice(1, 2, 3)656 // [0,3,3,3,0]657 expect(d.slice()).toEqual([0, 3, 3, 3, 0])658 },659 [660 {op: "replace", path: [1], value: 3},661 {op: "replace", path: [2], value: 3},662 {op: "replace", path: [4], value: 0},663 {op: "remove", path: [6]},664 {op: "remove", path: [5]}665 ]666)667668runPatchTests(669 "arrays - modify and shrink",670 {x: [1, 2, 3]},671 d => {672 d.x[0] = 4673 d.x.length = 2674 // [0, 2]675 },676 [677 {op: "replace", path: ["x", 0], value: 4},678 {op: "remove", path: ["x", 2]}679 ],680 [681 {op: "replace", path: ["x", 0], value: 1},682 {op: "add", path: ["x", 2], value: 3}683 ]684)685686runPatchTests(687 "arrays - prepend then splice middle",688 {x: [1, 2, 3]},689 d => {690 d.x.unshift(4)691 d.x.splice(2, 1)692 // 4, 1, 3693 },694 [695 {op: "replace", path: ["x", 0], value: 4},696 {op: "replace", path: ["x", 1], value: 1}697 ]698)699700runPatchTests(701 "arrays - splice middle then prepend",702 {x: [1, 2, 3]},703 d => {704 d.x.splice(1, 1)705 d.x.unshift(4)706 // [4, 1, 3]707 },708 [709 {op: "replace", path: ["x", 0], value: 4},710 {op: "replace", path: ["x", 1], value: 1}711 ]712)713714runPatchTests(715 "arrays - truncate",716 {x: [1, 2, 3]},717 d => {718 d.x.length -= 2719 },720 [721 {op: "remove", path: ["x", 2]},722 {op: "remove", path: ["x", 1]}723 ],724 [725 {op: "add", path: ["x", 1], value: 2},726 {op: "add", path: ["x", 2], value: 3}727 ]728)729730runPatchTests(731 "arrays - pop twice",732 {x: [1, 2, 3]},733 d => {734 d.x.pop()735 d.x.pop()736 },737 [738 {op: "remove", path: ["x", 2]},739 {op: "remove", path: ["x", 1]}740 ]741)742743runPatchTests(744 "arrays - push multiple",745 {x: [1, 2, 3]},746 d => {747 d.x.push(4, 5)748 },749 [750 {op: "add", path: ["x", 3], value: 4},751 {op: "add", path: ["x", 4], value: 5}752 ],753 [754 {op: "remove", path: ["x", 4]},755 {op: "remove", path: ["x", 3]}756 ]757)758759runPatchTests(760 "arrays - splice (expand)",761 {x: [1, 2, 3]},762 d => {763 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]764 },765 [766 {op: "replace", path: ["x", 1], value: 4},767 {op: "replace", path: ["x", 2], value: 5},768 {op: "add", path: ["x", 3], value: 6},769 {op: "add", path: ["x", 4], value: 3}770 ],771 [772 {op: "replace", path: ["x", 1], value: 2},773 {op: "replace", path: ["x", 2], value: 3},774 {op: "remove", path: ["x", 4]},775 {op: "remove", path: ["x", 3]}776 ]777)778779runPatchTests(780 "arrays - splice (shrink)",781 {x: [1, 2, 3, 4, 5]},782 d => {783 d.x.splice(1, 3, 6) // [1, 6, 5]784 },785 [786 {op: "replace", path: ["x", 1], value: 6},787 {op: "replace", path: ["x", 2], value: 5},788 {op: "remove", path: ["x", 4]},789 {op: "remove", path: ["x", 3]}790 ],791 [792 {op: "replace", path: ["x", 1], value: 2},793 {op: "replace", path: ["x", 2], value: 3},794 {op: "add", path: ["x", 3], value: 4},795 {op: "add", path: ["x", 4], value: 5}796 ]797)798799runPatchTests(800 "arrays - delete",801 {802 x: [803 {a: 1, b: 2},804 {c: 3, d: 4}805 ]806 },807 d => {808 delete d.x[1].c809 },810 [{op: "remove", path: ["x", 1, "c"]}]811)812813describe("arrays - append", () => {814 test("appends to array when last part of path is '-'", () => {815 const state = {816 list: [1, 2, 3]817 }818 const patch = {819 op: "add",820 value: 4,821 path: ["list", "-"]822 }823 expect(applyPatches(state, [patch])).toEqual({824 list: [1, 2, 3, 4]825 })826 })827})828829runPatchTests(830 "sets - add - 1",831 new Set([1]),832 d => {833 d.add(2)834 },835 [{op: "add", path: [1], value: 2}],836 [{op: "remove", path: [1], value: 2}]837)838839runPatchTests(840 "sets - add, delete, add - 1",841 new Set([1]),842 d => {843 d.add(2)844 d.delete(2)845 d.add(2)846 },847 [{op: "add", path: [1], value: 2}],848 [{op: "remove", path: [1], value: 2}]849)850851runPatchTests(852 "sets - add, delete, add - 2",853 new Set([2, 1]),854 d => {855 d.add(2)856 d.delete(2)857 d.add(2)858 },859 [],860 []861)862863describe("sets - mutate - 1", () => {864 const findById = (set, id) => {865 for (const item of set) {866 if (item.id === id) return item867 }868 }869 runPatchTests(870 "",871 new Set([872 {id: 1, val: "We"},873 {id: 2, val: "will"}874 ]),875 d => {876 const obj1 = findById(d, 1)877 const obj2 = findById(d, 2)878 obj1.val = "rock"879 obj2.val = "you"880 },881 [882 {op: "remove", path: [0], value: {id: 1, val: "We"}},883 {op: "remove", path: [1], value: {id: 2, val: "will"}},884 {op: "add", path: [0], value: {id: 1, val: "rock"}},885 {op: "add", path: [1], value: {id: 2, val: "you"}}886 ],887 [888 {op: "remove", path: [1], value: {id: 2, val: "you"}},889 {op: "remove", path: [0], value: {id: 1, val: "rock"}},890 {op: "add", path: [1], value: {id: 2, val: "will"}},891 {op: "add", path: [0], value: {id: 1, val: "We"}}892 ]893 )894})895896// These patches were more optimal pre immer 7, but not always correct897runPatchTests(898 "arrays - splice should should result in remove op.",899 [1, 2],900 d => {901 d.splice(1, 1)902 },903 [{op: "remove", path: [1]}],904 [{op: "add", path: [1], value: 2}]905)906907// These patches were more optimal pre immer 7, but not always correct908runPatchTests(909 "arrays - NESTED splice should should result in remove op.",910 {a: {b: {c: [1, 2]}}},911 d => {912 d.a.b.c.splice(1, 1)913 },914 [{op: "remove", path: ["a", "b", "c", 1]}],915 [{op: "add", path: ["a", "b", "c", 1], value: 2}]916)917918runPatchTests("simple replacement", {x: 3}, _d => 4, [919 {op: "replace", path: [], value: 4}920])921922runPatchTests(923 "same value replacement - 1",924 {x: {y: 3}},925 d => {926 const a = d.x927 d.x = a928 },929 []930)931932runPatchTests(933 "same value replacement - 2",934 {x: {y: 3}},935 d => {936 const a = d.x937 d.x = 4938 d.x = a939 },940 []941)942943runPatchTests(944 "same value replacement - 3",945 {x: 3},946 d => {947 d.x = 3948 },949 []950)951952runPatchTests(953 "same value replacement - 4",954 {x: 3},955 d => {956 d.x = 4957 d.x = 3958 },959 []960)961962runPatchTests(963 "same value replacement - 5",964 new Map([["x", 3]]),965 d => {966 d.set("x", 4)967 d.set("x", 3)968 },969 [],970 []971)972973runPatchTests(974 "same value replacement - 6",975 new Set(["x", 3]),976 d => {977 d.delete("x")978 d.add("x")979 },980 [],981 []982)983984runPatchTests(985 "simple delete",986 {x: 2},987 d => {988 delete d.x989 },990 [991 {992 op: "remove",993 path: ["x"]994 }995 ]996)997998describe("patch compressions yields correct results", () => {999 let p1 = [1000 {1001 op: "add",1002 path: ["x"],1003 value: {1004 test: true1005 }1006 }1007 ]1008 let p2 = [1009 {1010 op: "remove",1011 path: ["x"]1012 }1013 ]10141015 runPatchTests(1016 "add only",1017 {},1018 d => {1019 d.x = {test: true}1020 },1021 p11022 )10231024 runPatchTests(1025 "delete only",

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1026why, one step back
1026 {x: {test: true}},

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1027–1035why, one step back
1027 d => {
1028 delete d.x1029 },1030 p21031 )1032 const testData = runPatchTests(1033 "add and delete together cancel",1034 {},1035 d => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1036why, one step back
1036 applyPatches(d, [...p1, ...p2])

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1037–1060why, one step back
1037 },
1038 []1039 )10401041 expect(testData.result).toEqual({})1042})10431044runPatchTests(1045 "change then delete property",1046 {1047 x: 11048 },1049 d => {1050 d.x = 21051 delete d.x1052 },1053 [1054 {1055 op: "remove",1056 path: ["x"]1057 }1058 ]1059)1060

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1061–1063why, one step back
1061test("replaying patches with interweaved replacements should work correctly", () => {
1062 const patches = []1063 const s0 = {x: 1}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1064why, one step back
1064

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1065–1066why, one step back
1065 const s1 = produce(
1066 s0,

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1067why, one step back
1067 draft => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1068why, one step back
1068 draft.x = 2

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1069–1074why, one step back
1069 },
1070 p => {1071 patches.push(...p)1072 }1073 )1074

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1075–1076why, one step back
1075 const s2 = produce(
1076 s1,

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1077why, one step back
1077 draft => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1078why, one step back
1078 return {x: 0}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1079–1084why, one step back
1079 },
1080 p => {1081 patches.push(...p)1082 }1083 )1084

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1085–1086why, one step back
1085 const s3 = produce(
1086 s2,

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1087why, one step back
1087 draft => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1088why, one step back
1088 draft.x--

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1089–1094why, one step back
1089 },
1090 p => {1091 patches.push(...p)1092 }1093 )1094

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1095–1096why, one step back
1095 expect(s3).toEqual({x: -1}) // correct result
1096 expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1097why, one step back
1097

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1098–1101why, one step back
1098 // manual replay on a draft should also be correct
1099 expect(1100 produce(s0, draft => {1101 return applyPatches(draft, patches)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1102why, one step back
1102 })

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1103why, one step back
1103 ).toEqual({x: -1})

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1104–1105why, one step back
1104})
1105

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1106–1112why, one step back
1106describe("#468", () => {
1107 function run() {1108 const item = {id: 1}1109 const state = [item]1110 const [nextState, patches] = produceWithPatches(state, draft => {1111 draft[0].id = 21112 draft[1] = item

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1113–1114why, one step back
1113 })
1114

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1115why, one step back
1115 expect(nextState).toEqual([{id: 2}, {id: 1}])

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1116–1117why, one step back
1116 expect(patches).toEqual([
1117 {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1118–1120why, one step back
1118 op: "replace",
1119 path: [0, "id"],1120 value: 2

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1121–1123why, one step back
1121 },
1122 {1123 op: "add",

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1124why, one step back
1124 path: [1],

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1125why, one step back
1125 value: {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1126why, one step back
1126 id: 1

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1127–1130why, one step back
1127 }
1128 }1129 ])1130

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1131–1132why, one step back
1131 const final = applyPatches(state, patches)
1132 expect(final).toEqual(nextState)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1133–1134why, one step back
1133 }
1134

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1135–1136why, one step back
1135 test("proxy", () => {
1136 run()

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1137–1139why, one step back
1137 })
1138})1139

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1140–1141why, one step back
1140test("#521", () => {
1141 const state = new Map()

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1142why, one step back
1142

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1143–1144why, one step back
1143 const [nextState, patches] = produceWithPatches(state, draft => {
1144 draft.set("hello", new Set(["world"]))

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1145–1146why, one step back
1145 })
1146

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1147–1148why, one step back
1147 let patchedState = applyPatches(state, patches)
1148 expect(patchedState).toEqual(nextState)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1149why, one step back
1149

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1150–1151why, one step back
1150 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {
1151 draft.get("hello").add("immer")

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1152–1153why, one step back
1152 })
1153

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1154–1155why, one step back
1154 expect(applyPatches(nextState, patchesV2)).toEqual(
1155 new Map([["hello", new Set(["world", "immer"])]])

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1156–1158why, one step back
1156 )
1157})1158

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1159–1163why, one step back
1159test("#559 patches works in a nested reducer with proxies", () => {
1160 const state = {1161 x: 1,1162 sub: {1163 y: [{a: 0}, {a: 1}]

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1164–1166why, one step back
1164 }
1165 }1166

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1167–1168why, one step back
1167 const changes = []
1168 const inverseChanges = []

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1169why, one step back
1169

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1170–1172why, one step back
1170 const newState = produce(state, draft => {
1171 draft.sub = produce(1172 draft.sub,

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1173why, one step back
1173 draft => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1174why, one step back
1174 draft.y.pop()

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1175why, one step back
1175 },

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1176–1180why, one step back
1176 (patches, inversePatches) => {
1177 expect(isDraft(inversePatches[0].value)).toBeFalsy()1178 expect(inversePatches[0].value).toMatchObject({a: 1})1179 changes.push(...patches)1180 inverseChanges.push(...inversePatches)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1181–1184why, one step back
1181 }
1182 )1183 })1184

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1185why, one step back
1185 const reversedSubState = applyPatches(newState.sub, inverseChanges)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1186why, one step back
1186

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1187why, one step back
1187 expect(reversedSubState).toMatchObject(state.sub)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1188–1189why, one step back
1188})
1189

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1190–1191why, one step back
1190describe("#588", () => {
1191 const reference = {value: {num: 53}}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1192why, one step back
1192

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1193–1196why, one step back
1193 class Base {
1194 [immerable] = true1195 get nested() {1196 return reference.value

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1197why, one step back
1197 }

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1198why, one step back
1198 set nested(value) {}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1199–1200why, one step back
1199 }
1200

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1201why, one step back
1201 let base = new Base()

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1202–1205why, one step back
1202
1203 runPatchTests(1204 "",1205 base,

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1206–1209why, one step back
1206 vdraft => {
1207 reference.value = vdraft1208 produce(base, bdraft => {1209 bdraft.nested.num = 42

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1210–1211why, one step back
1210 })
1211 },

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1212why, one step back
1212 [{op: "add", path: ["num"], value: 42}]

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1213–1215why, one step back
1213 )
1214})1215

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1216–1219why, one step back
1216test("#676 patching Date objects", () => {
1217 class Test {1218 constructor() {1219 this.test = true

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1220why, one step back
1220 }

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1221–1222why, one step back
1221 perform() {
1222 return "tested!"

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1223–1225why, one step back
1223 }
1224 }1225

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1226–1228why, one step back
1226 const [nextState, patches] = produceWithPatches({}, function(draft) {
1227 draft.date = new Date("2020-11-10T08:08:08.003Z")1228 draft.test = new Test()

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1229–1230why, one step back
1229 })
1230

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1231why, one step back
1231 expect(nextState.date.toJSON()).toMatchInlineSnapshot(

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1232–1233why, one step back
1232 `"2020-11-10T08:08:08.003Z"`
1233 )

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1234why, one step back
1234 expect(nextState.test.perform()).toBe("tested!")

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1235why, one step back
1235

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1236–1238why, one step back
1236 const rebuilt = applyPatches({}, patches)
1237 expect(rebuilt.date).toBeInstanceOf(Date)1238 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1239–1240why, one step back
1239 `"2020-11-10T08:08:08.003Z"`
1240 )

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1241why, one step back
1241 expect(rebuilt.date).toEqual(new Date("2020-11-10T08:08:08.003Z"))

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1242–1243why, one step back
1242})
1243

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1244why, one step back
1244test("do not allow __proto__ polution - 738", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1245–1250why, one step back
1245 const obj = {}
12461247 // @ts-ignore1248 expect(obj.polluted).toBe(undefined)1249 expect(() => {1250 applyPatches({}, [

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1251why, one step back
1251 {op: "add", path: ["__proto__", "polluted"], value: "yes"}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1252–1261why, one step back
1252 ])
1253 }).toThrow(1254 isProd1255 ? "19"1256 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1257 )1258 // @ts-ignore1259 expect(obj.polluted).toBe(undefined)1260})1261

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1262why, one step back
1262test("do not allow __proto__ polution using arrays - 738", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1263why, one step back
1263 const obj = {}

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1264why, one step back
1264 const ar = []

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1265–1270why, one step back
1265
1266 // @ts-ignore1267 expect(obj.polluted).toBe(undefined)1268 // @ts-ignore1269 expect(ar.polluted).toBe(undefined)1270 expect(() => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1271–1273why, one step back
1271 applyPatches(
1272 [],1273 [{op: "add", path: ["__proto__", "polluted"], value: "yes"}]

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1274–1285why, one step back
1274 )
1275 }).toThrow(1276 isProd1277 ? "19"1278 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1279 )1280 // @ts-ignore1281 expect(obj.polluted).toBe(undefined)1282 // @ts-ignore1283 expect(ar.polluted).toBe(undefined)1284})1285

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1286why, one step back
1286test("do not allow prototype polution - 738", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1287–1291why, one step back
1287 const obj = {}
12881289 // @ts-ignore1290 expect(obj.polluted).toBe(undefined)1291 expect(() => {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1292–1293why, one step back
1292 applyPatches(Object, [
1293 {op: "add", path: ["prototype", "polluted"], value: "yes"}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1294–1303why, one step back
1294 ])
1295 }).toThrow(1296 isProd1297 ? "19"1298 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1299 )1300 // @ts-ignore1301 expect(obj.polluted).toBe(undefined)1302})1303

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1304why, one step back
1304test("do not allow constructor polution - 738", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1305–1308why, one step back
1305 const obj = {}
13061307 // @ts-ignore1308 expect(obj.polluted).toBe(undefined)

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1309–1311why, one step back
1309 const t = {}
1310 applyPatches(t, [{op: "replace", path: ["constructor"], value: "yes"}])1311 expect(typeof t.constructor).toBe("function")

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1312–1315why, one step back
1312 // @ts-ignore
1313 expect(Object.polluted).toBe(undefined)1314})1315

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1316why, one step back
1316test("do not allow constructor.prototype polution - 738", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1317–1322why, one step back
1317 const obj = {}
13181319 // @ts-ignore1320 expect(obj.polluted).toBe(undefined)1321 expect(() => {1322 applyPatches({}, [

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1323why, one step back
1323 {op: "add", path: ["constructor", "prototype", "polluted"], value: "yes"}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1324–1333why, one step back
1324 ])
1325 }).toThrow(1326 isProd1327 ? "19"1328 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1329 )1330 // @ts-ignore1331 expect(Object.polluted).toBe(undefined)1332})1333

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1334why, one step back
1334test("maps can store __proto__, prototype and constructor props", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1335why, one step back
1335 const obj = {}

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1336–1343why, one step back
1336 const map = new Map()
1337 map.set("__proto__", {})1338 map.set("constructor", {})1339 map.set("prototype", {})1340 const newMap = applyPatches(map, [1341 {op: "add", path: ["__proto__", "polluted"], value: "yes"},1342 {op: "add", path: ["constructor", "polluted"], value: "yes"},1343 {op: "add", path: ["prototype", "polluted"], value: "yes"}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1344why, one step back
1344 ])

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1345–1347why, one step back
1345 expect(newMap.get("__proto__").polluted).toBe("yes")
1346 expect(newMap.get("constructor").polluted).toBe("yes")1347 expect(newMap.get("prototype").polluted).toBe("yes")

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1348–1350why, one step back
1348 expect(obj.polluted).toBe(undefined)
1349})1350

fix(security): Follow up on CVE-2020-28477 where `path: [["__proto__"], "x"]` could still pollute the prototypeMichel Weststratebefore adoptionfa671e5

line 1351why, one step back
1351test("CVE-2020-28477 (https://snyk.io/vuln/SNYK-JS-IMMER-1019369) follow up", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1352–1357why, one step back
1352 const obj = {}
13531354 // @ts-ignore1355 expect(obj.polluted).toBe(undefined)1356 expect(() => {1357 applyPatches({}, [

fix(security): Follow up on CVE-2020-28477 where `path: [["__proto__"], "x"]` could still pollute the prototypeMichel Weststratebefore adoptionfa671e5

line 1358why, one step back
1358 {op: "add", path: [["__proto__"], "polluted"], value: "yes"}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1359–1368why, one step back
1359 ])
1360 }).toThrow(1361 isProd1362 ? "19"1363 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1364 )1365 // @ts-ignore1366 expect(obj.polluted).toBe(undefined)1367})1368

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1369–1372why, one step back
1369test("#648 assigning object to itself should not change patches", () => {
1370 const input = {1371 obj: {1372 value: 200

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1373–1375why, one step back
1373 }
1374 }1375

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1376–1378why, one step back
1376 const [nextState, patches] = produceWithPatches(input, draft => {
1377 draft.obj.value = 11378 draft.obj = draft.obj

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1379–1380why, one step back
1379 })
1380

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 1381why, one step back
1381 expect(patches).toEqual([

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1382why, one step back
1382 {

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

lines 1383–1385why, one step back
1383 op: "replace",
1384 path: ["obj", "value"],1385 value: 1

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1386–1389why, one step back
1386 }
1387 ])1388})1389

fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f

lines 1390–1392why, one step back
1390test("#791 patch for nothing is stored as undefined", () => {
1391 const [newState, patches] = produceWithPatches({abc: 123}, draft => nothing)1392 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1393why, one step back
1393

fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f

line 1394why, one step back
1394 expect(applyPatches({}, patches)).toEqual(undefined)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1395–1396why, one step back
1395})
1396

fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (#878)Julian Scheidbefore adoptione140918

line 1397why, one step back
1397test("#876 Ensure empty patch set for atomic set+delete on Map", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1398why, one step back
1398 {

fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (#878)Julian Scheidbefore adoptione140918

lines 1399–1400why, one step back
1399 const [newState, patches] = produceWithPatches(
1400 new Map([["foo", "baz"]]),

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1401why, one step back
1401 draft => {

fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (#878)Julian Scheidbefore adoptione140918

lines 1402–1403why, one step back
1402 draft.set("foo", "bar")
1403 draft.delete("foo")

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1404–1405why, one step back
1404 }
1405 )

fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (#878)Julian Scheidbefore adoptione140918

line 1406why, one step back
1406 expect(patches).toEqual([{op: "remove", path: ["foo"]}])

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1407–1409why, one step back
1407 }
14081409 {

fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (#878)Julian Scheidbefore adoptione140918

lines 1410–1412why, one step back
1410 const [newState, patches] = produceWithPatches(new Map(), draft => {
1411 draft.set("foo", "bar")1412 draft.delete("foo")

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1413why, one step back
1413 })

fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (#878)Julian Scheidbefore adoptione140918

line 1414why, one step back
1414 expect(patches).toEqual([])

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1415–1417why, one step back
1415 }
1416})1417

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 1418why, one step back
1418test("#888 patch to a primitive produces the primitive", () => {

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1419why, one step back
1419 {

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 1420why, one step back
1420 const [res, patches] = produceWithPatches({abc: 123}, draft => nothing)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1421–1424why, one step back
1421 expect(res).toEqual(undefined)
1422 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1423 }1424 {

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 1425why, one step back
1425 const [res, patches] = produceWithPatches(null, draft => nothing)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1426–1429why, one step back
1426 expect(res).toEqual(undefined)
1427 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1428 }1429 {

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 1430why, one step back
1430 const [res, patches] = produceWithPatches(0, draft => nothing)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1431–1434why, one step back
1431 expect(res).toEqual(undefined)
1432 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1433 }1434 {

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 1435why, one step back
1435 const [res, patches] = produceWithPatches("foobar", draft => nothing)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1436–1439why, one step back
1436 expect(res).toEqual(undefined)
1437 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1438 }1439 {

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 1440why, one step back
1440 const [res, patches] = produceWithPatches([], draft => nothing)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1441–1444why, one step back
1441 expect(res).toEqual(undefined)
1442 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1443 }1444 {

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 1445why, one step back
1445 const [res, patches] = produceWithPatches(false, draft => nothing)

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1446–1449why, one step back
1446 expect(res).toEqual(undefined)
1447 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1448 }1449 {

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

lines 1450–1452why, one step back
1450 const [res, patches] = produceWithPatches(
1451 "foobar",1452 draft => "something else"

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1453why, one step back
1453 )

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 1454why, one step back
1454 expect(res).toEqual("something else")

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

line 1455why, one step back
1455 expect(patches).toEqual([

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 1456why, one step back
1456 {op: "replace", path: [], value: "something else"}

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1457–1459why, one step back
1457 ])
1458 }1459 {

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

lines 1460–1462why, one step back
1460 const [res, patches] = produceWithPatches(false, draft => true)
1461 expect(res).toEqual(true)1462 expect(patches).toEqual([{op: "replace", path: [], value: true}])

chore: Migrate test setup to Vitest (#1162)Mark Eriksonbefore adoption4b8bcc5

lines 1463–1486why, one step back
1463 }
1464})14651466runPatchTests(1467 "#879 delete item from array",1468 [1, 2, 3],1469 draft => {1470 delete draft[1]1471 },1472 [{op: "replace", path: [1], value: undefined}],1473 [{op: "replace", path: [1], value: 2}],1474 [1, undefined, 3]1475)14761477runPatchTests(1478 "#879 delete item from array - 2",1479 [1, 2, 3],1480 draft => {1481 delete draft[2]1482 },1483 [{op: "replace", path: [2], value: undefined}],1484 [{op: "replace", path: [2], value: 3}],1485 [1, 2, undefined]1486)

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1487–1496why, one step back
1487
1488test("#897 appendPatch", () => {1489 const state0 = {a: []}1490 const state1 = applyPatches(state0, [{op: "add", path: ["a", "-"], value: 1}])1491 const state2 = applyPatches(state1, [{op: "add", path: ["a", "-"], value: 2}])1492 const state3 = applyPatches(state2, [{op: "add", path: ["a", "-"], value: 3}])1493 expect(state3).toEqual({1494 a: [1, 2, 3]1495 })1496})