Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

1,598 lines · 30.2 KiB · JavaScript

Last changed by

chore: regression tests for fixed issue #916 (#1215)Dean Kerrbefore adoption5155a33

History of this file

Take it

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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,

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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) {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 91–102why, 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 cases

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

line 103why, one step back
103runPatchTests.only = function (

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

lines 104–121why, one step back
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}121

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

line 122why, one step back
122runPatchTests.skip = function (

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

lines 123–140why, one step back
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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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 ])

chore: added unit test for #897Michel Weststratebefore adoption607b994

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 })

chore: added unit test for #897Michel Weststratebefore adoption607b994

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 })

chore: added unit test for #897Michel Weststratebefore adoption607b994

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(() => {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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 })

chore: added unit test for #897Michel Weststratebefore adoption607b994

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(() => {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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(() => {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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 })

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 198–200why, one step back
198 })
199})200

chore: regression tests for fixed issue #916 (#1215)Dean Kerrbefore adoption5155a33

lines 201–263why, one step back
201describe("#916 nested patches with moved child draft", () => {
202 const makeBaseState = () => ({203 id: 0,204 type: "root",205 children: [{id: 2, type: "node", value: "A"}]206 })207208 const makeRootWithGroup = groupName => ({209 id: 0,210 type: "root",211 children: [212 {213 id: 3,214 children: [{id: 2, type: "node", value: "A"}],215 groupName216 }217 ]218 })219220 function runNestedPatchScenario() {221 let nestedPatches = []222 const [nextState] = produceWithPatches(makeBaseState(), draft => {223 const node = draft.children[0]224 draft.children.splice(0, 1) // remove node from root225226 const group = {id: 3, children: [node], groupName: ""} // new group227 draft.children.splice(0, 0, group) // add group to root228229 const [, patches] = produceWithPatches(draft, inner => {230 const value = inner.children[0].children[0].value231 inner.children[0].groupName = value + " Group"232 })233234 nestedPatches = patches235 applyPatches(draft, patches)236 })237238 return {nextState, patches: nestedPatches}239 }240241 it("#916 nested patches with moved child draft", () => {242 const {nextState, patches} = runNestedPatchScenario()243244 expect(patches).toEqual(245 expect.arrayContaining([246 {247 op: "replace",248 path: ["children", 0, "groupName"],249 value: "A Group"250 }251 ])252 )253 expect(nextState).toEqual(makeRootWithGroup("A Group"))254 })255256 it("#916 patches apply on non-draft base", () => {257 const {patches} = runNestedPatchScenario()258 expect(applyPatches(makeRootWithGroup(""), patches)).toEqual(259 makeRootWithGroup("A Group")260 )261 })262})263

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

lines 264–342why, one step back
264// New macro-style test
265runPatchTests(266 "simple assignment - 1",267 {x: 3},268 d => {269 d.x++270 },271 [{op: "replace", path: ["x"], value: 4}]272)273274// Original test (commented out for comparison)275// describe("simple assignment - 1", () => {276// runPatchTest(277// {x: 3},278// d => {279// d.x++280// },281// [{op: "replace", path: ["x"], value: 4}]282// )283// })284285// New macro-style tests286runPatchTests(287 "simple assignment - 2",288 {x: {y: 4}},289 d => {290 d.x.y++291 },292 [{op: "replace", path: ["x", "y"], value: 5}]293)294295runPatchTests(296 "simple assignment - 3",297 {x: [{y: 4}]},298 d => {299 d.x[0].y++300 },301 [{op: "replace", path: ["x", 0, "y"], value: 5}]302)303304runPatchTests(305 "simple assignment - 4",306 new Map([["x", {y: 4}]]),307 d => {308 d.get("x").y++309 },310 [{op: "replace", path: ["x", "y"], value: 5}],311 [{op: "replace", path: ["x", "y"], value: 4}]312)313314runPatchTests(315 "simple assignment - 5",316 {x: new Map([["y", 4]])},317 d => {318 d.x.set("y", 5)319 },320 [{op: "replace", path: ["x", "y"], value: 5}],321 [{op: "replace", path: ["x", "y"], value: 4}]322)323324runPatchTests(325 "simple assignment - 6",326 new Map([["x", 1]]),327 d => {328 // Map.prototype.set should return the Map itself329 const res = d.set("x", 2)330 res.set("y", 3)331 },332 [333 {op: "replace", path: ["x"], value: 2},334 {op: "add", path: ["y"], value: 3}335 ],336 [337 {op: "replace", path: ["x"], value: 1},338 {op: "remove", path: ["y"]}339 ]340)341342// Complex test with external variables - keep as individual describe

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 344–348why, one step back
344 const key1 = {prop: "val1"}
345 const key2 = {prop: "val2"}346347 runPatchTests(348 "",

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 353–354why, one step back
353 },
354 [

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 357–358why, one step back
357 ],
358 [

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 360–410why, one step back
360 {op: "remove", path: ["x", key2]}
361 ]362 )363})364365runPatchTests(366 "simple assignment - 8",367 new Map([[0, new Map([[1, 4]])]]),368 d => {369 d.get(0).set(1, 5)370 d.get(0).set(2, 6)371 },372 [373 {op: "replace", path: [0, 1], value: 5},374 {op: "add", path: [0, 2], value: 6}375 ],376 [377 {op: "replace", path: [0, 1], value: 4},378 {op: "remove", path: [0, 2]}379 ]380)381382runPatchTests(383 "delete 1",384 {x: {y: 4}},385 d => {386 delete d.x387 },388 [{op: "remove", path: ["x"]}]389)390391runPatchTests(392 "delete 2",393 new Map([["x", 1]]),394 d => {395 d.delete("x")396 },397 [{op: "remove", path: ["x"]}],398 [{op: "add", path: ["x"], value: 1}]399)400401runPatchTests(402 "delete 3",403 {x: new Map([["y", 1]])},404 d => {405 d.x.delete("y")406 },407 [{op: "remove", path: ["x", "y"]}],408 [{op: "add", path: ["x", "y"], value: 1}]409)410

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 412–417why, one step back
412 const key1 = {prop: "val1"}
413 const key2 = {prop: "val2"}414415 runPatchTests(416 "",417 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 418–420why, one step back
418 x: new Map([
419 [key1, 1],420 [key2, 2]

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

lines 421–423why, one step back
421 ])
422 },423 d => {

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 424–425why, one step back
424 d.x.delete(key1)
425 d.x.delete(key2)

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

lines 426–427why, one step back
426 },
427 [

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 429–431why, one step back
429 {op: "remove", path: ["x", key2]}
430 ],431 [

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 434–457why, one step back
434 ]
435 )436})437438runPatchTests(439 "delete 6",440 new Set(["x", 1]),441 d => {442 d.delete("x")443 },444 [{op: "remove", path: [0], value: "x"}],445 [{op: "add", path: [0], value: "x"}]446)447448runPatchTests(449 "delete 7",450 {x: new Set(["y", 1])},451 d => {452 d.x.delete("y")453 },454 [{op: "remove", path: ["x", 0], value: "y"}],455 [{op: "add", path: ["x", 0], value: "y"}]456)457

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 459–563why, one step back
459 runPatchTests(
460 "nested object (no changes)",461 {a: {b: 1}},462 d => {463 d.x = d.a464 delete d.a465 },466 [467 {op: "add", path: ["x"], value: {b: 1}},468 {op: "remove", path: ["a"]}469 ]470 )471472 runPatchTests(473 "nested change in object",474 {475 a: {b: 1}476 },477 d => {478 d.a.b++479 },480 [{op: "replace", path: ["a", "b"], value: 2}],481 [{op: "replace", path: ["a", "b"], value: 1}]482 )483484 runPatchTests(485 "nested change in map",486 new Map([["a", new Map([["b", 1]])]]),487 d => {488 d.get("a").set("b", 2)489 },490 [{op: "replace", path: ["a", "b"], value: 2}],491 [{op: "replace", path: ["a", "b"], value: 1}]492 )493494 runPatchTests(495 "nested change in array",496 [[{b: 1}]],497 d => {498 d[0][0].b++499 },500 [{op: "replace", path: [0, 0, "b"], value: 2}],501 [{op: "replace", path: [0, 0, "b"], value: 1}]502 )503504 runPatchTests(505 "nested map (no changes)",506 new Map([["a", new Map([["b", 1]])]]),507 d => {508 d.set("x", d.get("a"))509 d.delete("a")510 },511 [512 {op: "add", path: ["x"], value: new Map([["b", 1]])},513 {op: "remove", path: ["a"]}514 ],515 [516 {op: "remove", path: ["x"]},517 {op: "add", path: ["a"], value: new Map([["b", 1]])}518 ]519 )520521 runPatchTests(522 "nested object (with changes)",523 {a: {b: 1, c: 1}},524 d => {525 let a = d.a526 a.b = 2 // change527 delete a.c // delete528 a.y = 2 // add529530 // rename531 d.x = a532 delete d.a533 },534 [535 {op: "add", path: ["x"], value: {b: 2, y: 2}},536 {op: "remove", path: ["a"]}537 ]538 )539540 runPatchTests(541 "nested map (with changes)",542 new Map([543 [544 "a",545 new Map([546 ["b", 1],547 ["c", 1]548 ])549 ]550 ]),551 d => {552 let a = d.get("a")553 a.set("b", 2) // change554 a.delete("c") // delete555 a.set("y", 2) // add556557 // rename558 d.set("x", a)559 d.delete("a")560 },561 [562 {563 op: "add",

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 565–1088why, one step back
565 value: new Map([
566 ["b", 2],567 ["y", 2]568 ])569 },570 {op: "remove", path: ["a"]}571 ],572 [573 {op: "remove", path: ["x"]},574 {575 op: "add",576 path: ["a"],577 value: new Map([578 ["b", 1],579 ["c", 1]580 ])581 }582 ]583 )584585 runPatchTests(586 "deeply nested object (with changes)",587 {a: {b: {c: 1, d: 1}}},588 d => {589 let b = d.a.b590 b.c = 2 // change591 delete b.d // delete592 b.y = 2 // add593594 // rename595 d.a.x = b596 delete d.a.b597 },598 [599 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},600 {op: "remove", path: ["a", "b"]}601 ]602 )603604 runPatchTests(605 "deeply nested map (with changes)",606 new Map([607 [608 "a",609 new Map([610 [611 "b",612 new Map([613 ["c", 1],614 ["d", 1]615 ])616 ]617 ])618 ]619 ]),620 d => {621 let b = d.get("a").get("b")622 b.set("c", 2) // change623 b.delete("d") // delete624 b.set("y", 2) // add625626 // rename627 d.get("a").set("x", b)628 d.get("a").delete("b")629 },630 [631 {632 op: "add",633 path: ["a", "x"],634 value: new Map([635 ["c", 2],636 ["y", 2]637 ])638 },639 {op: "remove", path: ["a", "b"]}640 ],641 [642 {op: "remove", path: ["a", "x"]},643 {644 op: "add",645 path: ["a", "b"],646 value: new Map([647 ["c", 1],648 ["d", 1]649 ])650 }651 ]652 )653})654655runPatchTests(656 "minimum amount of changes",657 {x: 3, y: {a: 4}, z: 3},658 d => {659 d.y.a = 4660 d.y.b = 5661 Object.assign(d, {x: 4, y: {a: 2}})662 },663 [664 {op: "replace", path: ["x"], value: 4},665 {op: "replace", path: ["y"], value: {a: 2}}666 ]667)668669runPatchTests(670 "arrays - prepend",671 {x: [1, 2, 3]},672 d => {673 d.x.unshift(4)674 },675 [676 {op: "replace", path: ["x", 0], value: 4},677 {op: "replace", path: ["x", 1], value: 1},678 {op: "replace", path: ["x", 2], value: 2},679 {op: "add", path: ["x", 3], value: 3}680 ]681)682683runPatchTests(684 "arrays - multiple prepend",685 {x: [1, 2, 3]},686 d => {687 d.x.unshift(4)688 d.x.unshift(5)689 // 4,5,1,2,3690 },691 [692 {op: "replace", path: ["x", 0], value: 5},693 {op: "replace", path: ["x", 1], value: 4},694 {op: "replace", path: ["x", 2], value: 1},695 {op: "add", path: ["x", 3], value: 2},696 {op: "add", path: ["x", 4], value: 3}697 ]698)699700runPatchTests(701 "arrays - splice middle",702 {x: [1, 2, 3]},703 d => {704 d.x.splice(1, 1)705 },706 [707 {op: "replace", path: ["x", 1], value: 3},708 {op: "remove", path: ["x", 2]}709 ]710)711712runPatchTests(713 "arrays - multiple splice",714 [0, 1, 2, 3, 4, 5, 0],715 d => {716 d.splice(4, 2, 3)717 // [0,1,2,3,3,0]718 d.splice(1, 2, 3)719 // [0,3,3,3,0]720 expect(d.slice()).toEqual([0, 3, 3, 3, 0])721 },722 [723 {op: "replace", path: [1], value: 3},724 {op: "replace", path: [2], value: 3},725 {op: "replace", path: [4], value: 0},726 {op: "remove", path: [6]},727 {op: "remove", path: [5]}728 ]729)730731runPatchTests(732 "arrays - modify and shrink",733 {x: [1, 2, 3]},734 d => {735 d.x[0] = 4736 d.x.length = 2737 // [0, 2]738 },739 [740 {op: "replace", path: ["x", 0], value: 4},741 {op: "remove", path: ["x", 2]}742 ],743 [744 {op: "replace", path: ["x", 0], value: 1},745 {op: "add", path: ["x", 2], value: 3}746 ]747)748749runPatchTests(750 "arrays - prepend then splice middle",751 {x: [1, 2, 3]},752 d => {753 d.x.unshift(4)754 d.x.splice(2, 1)755 // 4, 1, 3756 },757 [758 {op: "replace", path: ["x", 0], value: 4},759 {op: "replace", path: ["x", 1], value: 1}760 ]761)762763runPatchTests(764 "arrays - splice middle then prepend",765 {x: [1, 2, 3]},766 d => {767 d.x.splice(1, 1)768 d.x.unshift(4)769 // [4, 1, 3]770 },771 [772 {op: "replace", path: ["x", 0], value: 4},773 {op: "replace", path: ["x", 1], value: 1}774 ]775)776777runPatchTests(778 "arrays - truncate",779 {x: [1, 2, 3]},780 d => {781 d.x.length -= 2782 },783 [784 {op: "remove", path: ["x", 2]},785 {op: "remove", path: ["x", 1]}786 ],787 [788 {op: "add", path: ["x", 1], value: 2},789 {op: "add", path: ["x", 2], value: 3}790 ]791)792793runPatchTests(794 "arrays - pop twice",795 {x: [1, 2, 3]},796 d => {797 d.x.pop()798 d.x.pop()799 },800 [801 {op: "remove", path: ["x", 2]},802 {op: "remove", path: ["x", 1]}803 ]804)805806runPatchTests(807 "arrays - push multiple",808 {x: [1, 2, 3]},809 d => {810 d.x.push(4, 5)811 },812 [813 {op: "add", path: ["x", 3], value: 4},814 {op: "add", path: ["x", 4], value: 5}815 ],816 [817 {op: "remove", path: ["x", 4]},818 {op: "remove", path: ["x", 3]}819 ]820)821822runPatchTests(823 "arrays - splice (expand)",824 {x: [1, 2, 3]},825 d => {826 d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]827 },828 [829 {op: "replace", path: ["x", 1], value: 4},830 {op: "replace", path: ["x", 2], value: 5},831 {op: "add", path: ["x", 3], value: 6},832 {op: "add", path: ["x", 4], value: 3}833 ],834 [835 {op: "replace", path: ["x", 1], value: 2},836 {op: "replace", path: ["x", 2], value: 3},837 {op: "remove", path: ["x", 4]},838 {op: "remove", path: ["x", 3]}839 ]840)841842runPatchTests(843 "arrays - splice (shrink)",844 {x: [1, 2, 3, 4, 5]},845 d => {846 d.x.splice(1, 3, 6) // [1, 6, 5]847 },848 [849 {op: "replace", path: ["x", 1], value: 6},850 {op: "replace", path: ["x", 2], value: 5},851 {op: "remove", path: ["x", 4]},852 {op: "remove", path: ["x", 3]}853 ],854 [855 {op: "replace", path: ["x", 1], value: 2},856 {op: "replace", path: ["x", 2], value: 3},857 {op: "add", path: ["x", 3], value: 4},858 {op: "add", path: ["x", 4], value: 5}859 ]860)861862runPatchTests(863 "arrays - delete",864 {865 x: [866 {a: 1, b: 2},867 {c: 3, d: 4}868 ]869 },870 d => {871 delete d.x[1].c872 },873 [{op: "remove", path: ["x", 1, "c"]}]874)875876describe("arrays - append", () => {877 test("appends to array when last part of path is '-'", () => {878 const state = {879 list: [1, 2, 3]880 }881 const patch = {882 op: "add",883 value: 4,884 path: ["list", "-"]885 }886 expect(applyPatches(state, [patch])).toEqual({887 list: [1, 2, 3, 4]888 })889 })890})891892runPatchTests(893 "sets - add - 1",894 new Set([1]),895 d => {896 d.add(2)897 },898 [{op: "add", path: [1], value: 2}],899 [{op: "remove", path: [1], value: 2}]900)901902runPatchTests(903 "sets - add, delete, add - 1",904 new Set([1]),905 d => {906 d.add(2)907 d.delete(2)908 d.add(2)909 },910 [{op: "add", path: [1], value: 2}],911 [{op: "remove", path: [1], value: 2}]912)913914runPatchTests(915 "sets - add, delete, add - 2",916 new Set([2, 1]),917 d => {918 d.add(2)919 d.delete(2)920 d.add(2)921 },922 [],923 []924)925926describe("sets - mutate - 1", () => {927 const findById = (set, id) => {928 for (const item of set) {929 if (item.id === id) return item930 }931 }932 runPatchTests(933 "",934 new Set([935 {id: 1, val: "We"},936 {id: 2, val: "will"}937 ]),938 d => {939 const obj1 = findById(d, 1)940 const obj2 = findById(d, 2)941 obj1.val = "rock"942 obj2.val = "you"943 },944 [945 {op: "remove", path: [0], value: {id: 1, val: "We"}},946 {op: "remove", path: [1], value: {id: 2, val: "will"}},947 {op: "add", path: [0], value: {id: 1, val: "rock"}},948 {op: "add", path: [1], value: {id: 2, val: "you"}}949 ],950 [951 {op: "remove", path: [1], value: {id: 2, val: "you"}},952 {op: "remove", path: [0], value: {id: 1, val: "rock"}},953 {op: "add", path: [1], value: {id: 2, val: "will"}},954 {op: "add", path: [0], value: {id: 1, val: "We"}}955 ]956 )957})958959// These patches were more optimal pre immer 7, but not always correct960runPatchTests(961 "arrays - splice should should result in remove op.",962 [1, 2],963 d => {964 d.splice(1, 1)965 },966 [{op: "remove", path: [1]}],967 [{op: "add", path: [1], value: 2}]968)969970// These patches were more optimal pre immer 7, but not always correct971runPatchTests(972 "arrays - NESTED splice should should result in remove op.",973 {a: {b: {c: [1, 2]}}},974 d => {975 d.a.b.c.splice(1, 1)976 },977 [{op: "remove", path: ["a", "b", "c", 1]}],978 [{op: "add", path: ["a", "b", "c", 1], value: 2}]979)980981runPatchTests("simple replacement", {x: 3}, _d => 4, [982 {op: "replace", path: [], value: 4}983])984985runPatchTests(986 "same value replacement - 1",987 {x: {y: 3}},988 d => {989 const a = d.x990 d.x = a991 },992 []993)994995runPatchTests(996 "same value replacement - 2",997 {x: {y: 3}},998 d => {999 const a = d.x1000 d.x = 41001 d.x = a1002 },1003 []1004)10051006runPatchTests(1007 "same value replacement - 3",1008 {x: 3},1009 d => {1010 d.x = 31011 },1012 []1013)10141015runPatchTests(1016 "same value replacement - 4",1017 {x: 3},1018 d => {1019 d.x = 41020 d.x = 31021 },1022 []1023)10241025runPatchTests(1026 "same value replacement - 5",1027 new Map([["x", 3]]),1028 d => {1029 d.set("x", 4)1030 d.set("x", 3)1031 },1032 [],1033 []1034)10351036runPatchTests(1037 "same value replacement - 6",1038 new Set(["x", 3]),1039 d => {1040 d.delete("x")1041 d.add("x")1042 },1043 [],1044 []1045)10461047runPatchTests(1048 "simple delete",1049 {x: 2},1050 d => {1051 delete d.x1052 },1053 [1054 {1055 op: "remove",1056 path: ["x"]1057 }1058 ]1059)10601061describe("patch compressions yields correct results", () => {1062 let p1 = [1063 {1064 op: "add",1065 path: ["x"],1066 value: {1067 test: true1068 }1069 }1070 ]1071 let p2 = [1072 {1073 op: "remove",1074 path: ["x"]1075 }1076 ]10771078 runPatchTests(1079 "add only",1080 {},1081 d => {1082 d.x = {test: true}1083 },1084 p11085 )10861087 runPatchTests(1088 "delete only",

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1090–1098why, one step back
1090 d => {
1091 delete d.x1092 },1093 p21094 )1095 const testData = runPatchTests(1096 "add and delete together cancel",1097 {},1098 d => {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1100–1123why, one step back
1100 },
1101 []1102 )11031104 expect(testData.result).toEqual({})1105})11061107runPatchTests(1108 "change then delete property",1109 {1110 x: 11111 },1112 d => {1113 d.x = 21114 delete d.x1115 },1116 [1117 {1118 op: "remove",1119 path: ["x"]1120 }1121 ]1122)1123

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1127why, one step back
1127

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1128–1129why, one step back
1128 const s1 = produce(
1129 s0,

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1132–1137why, one step back
1132 },
1133 p => {1134 patches.push(...p)1135 }1136 )1137

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1138–1139why, one step back
1138 const s2 = produce(
1139 s1,

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1142–1147why, one step back
1142 },
1143 p => {1144 patches.push(...p)1145 }1146 )1147

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1148–1149why, one step back
1148 const s3 = produce(
1149 s2,

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1152–1157why, one step back
1152 },
1153 p => {1154 patches.push(...p)1155 }1156 )1157

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1160why, one step back
1160

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1161–1164why, one step back
1161 // manual replay on a draft should also be correct
1162 expect(1163 produce(s0, draft => {1164 return applyPatches(draft, patches)

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

line 1165why, one step back
1165 })

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1167–1168why, one step back
1167})
1168

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1169–1175why, one step back
1169describe("#468", () => {
1170 function run() {1171 const item = {id: 1}1172 const state = [item]1173 const [nextState, patches] = produceWithPatches(state, draft => {1174 draft[0].id = 21175 draft[1] = item

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

lines 1176–1177why, one step back
1176 })
1177

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1179–1180why, one step back
1179 expect(patches).toEqual([
1180 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1181–1183why, one step back
1181 op: "replace",
1182 path: [0, "id"],1183 value: 2

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

lines 1184–1186why, one step back
1184 },
1185 {1186 op: "add",

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1188why, one step back
1188 value: {

chore: added unit test for #897Michel Weststratebefore adoption607b994

line 1189why, one step back
1189 id: 1

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

lines 1190–1193why, one step back
1190 }
1191 }1192 ])1193

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1196–1197why, one step back
1196 }
1197

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1200–1202why, one step back
1200 })
1201})1202

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1205why, one step back
1205

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1208–1209why, one step back
1208 })
1209

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1212why, one step back
1212

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1215–1216why, one step back
1215 })
1216

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1219–1221why, one step back
1219 )
1220})1221

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1222–1226why, one step back
1222test("#559 patches works in a nested reducer with proxies", () => {
1223 const state = {1224 x: 1,1225 sub: {1226 y: [{a: 0}, {a: 1}]

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

lines 1227–1229why, one step back
1227 }
1228 }1229

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1230–1231why, one step back
1230 const changes = []
1231 const inverseChanges = []

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

line 1232why, one step back
1232

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1233–1235why, one step back
1233 const newState = produce(state, draft => {
1234 draft.sub = produce(1235 draft.sub,

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1238why, one step back
1238 },

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1239–1243why, one step back
1239 (patches, inversePatches) => {
1240 expect(isDraft(inversePatches[0].value)).toBeFalsy()1241 expect(inversePatches[0].value).toMatchObject({a: 1})1242 changes.push(...patches)1243 inverseChanges.push(...inversePatches)

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

lines 1244–1247why, one step back
1244 }
1245 )1246 })1247

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1249why, one step back
1249

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1251–1252why, one step back
1251})
1252

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1255why, one step back
1255

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1256–1259why, one step back
1256 class Base {
1257 [immerable] = true1258 get nested() {1259 return reference.value

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

line 1260why, one step back
1260 }

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1262–1263why, one step back
1262 }
1263

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1265–1268why, one step back
1265
1266 runPatchTests(1267 "",1268 base,

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1269–1272why, one step back
1269 vdraft => {
1270 reference.value = vdraft1271 produce(base, bdraft => {1272 bdraft.nested.num = 42

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

lines 1273–1274why, one step back
1273 })
1274 },

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1276–1278why, one step back
1276 )
1277})1278

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1279–1282why, one step back
1279test("#676 patching Date objects", () => {
1280 class Test {1281 constructor() {1282 this.test = true

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

line 1283why, one step back
1283 }

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1284–1285why, one step back
1284 perform() {
1285 return "tested!"

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

lines 1286–1288why, one step back
1286 }
1287 }1288

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

line 1289why, one step back
1289 const [nextState, patches] = produceWithPatches({}, function (draft) {

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1290–1291why, one step back
1290 draft.date = new Date("2020-11-10T08:08:08.003Z")
1291 draft.test = new Test()

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

lines 1292–1293why, one step back
1292 })
1293

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1298why, one step back
1298

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1299–1301why, one step back
1299 const rebuilt = applyPatches({}, patches)
1300 expect(rebuilt.date).toBeInstanceOf(Date)1301 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1305–1306why, one step back
1305})
1306

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1308–1313why, one step back
1308 const obj = {}
13091310 // @ts-ignore1311 expect(obj.polluted).toBe(undefined)1312 expect(() => {1313 applyPatches({}, [

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1315–1324why, one step back
1315 ])
1316 }).toThrow(1317 isProd1318 ? "19"1319 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1320 )1321 // @ts-ignore1322 expect(obj.polluted).toBe(undefined)1323})1324

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1328–1333why, one step back
1328
1329 // @ts-ignore1330 expect(obj.polluted).toBe(undefined)1331 // @ts-ignore1332 expect(ar.polluted).toBe(undefined)1333 expect(() => {

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1334–1336why, one step back
1334 applyPatches(
1335 [],1336 [{op: "add", path: ["__proto__", "polluted"], value: "yes"}]

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

lines 1337–1348why, one step back
1337 )
1338 }).toThrow(1339 isProd1340 ? "19"1341 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1342 )1343 // @ts-ignore1344 expect(obj.polluted).toBe(undefined)1345 // @ts-ignore1346 expect(ar.polluted).toBe(undefined)1347})1348

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1350–1354why, one step back
1350 const obj = {}
13511352 // @ts-ignore1353 expect(obj.polluted).toBe(undefined)1354 expect(() => {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1357–1366why, one step back
1357 ])
1358 }).toThrow(1359 isProd1360 ? "19"1361 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1362 )1363 // @ts-ignore1364 expect(obj.polluted).toBe(undefined)1365})1366

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1368–1371why, one step back
1368 const obj = {}
13691370 // @ts-ignore1371 expect(obj.polluted).toBe(undefined)

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1375–1378why, one step back
1375 // @ts-ignore
1376 expect(Object.polluted).toBe(undefined)1377})1378

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1380–1385why, one step back
1380 const obj = {}
13811382 // @ts-ignore1383 expect(obj.polluted).toBe(undefined)1384 expect(() => {1385 applyPatches({}, [

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1387–1396why, one step back
1387 ])
1388 }).toThrow(1389 isProd1390 ? "19"1391 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1392 )1393 // @ts-ignore1394 expect(Object.polluted).toBe(undefined)1395})1396

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1399–1406why, one step back
1399 const map = new Map()
1400 map.set("__proto__", {})1401 map.set("constructor", {})1402 map.set("prototype", {})1403 const newMap = applyPatches(map, [1404 {op: "add", path: ["__proto__", "polluted"], value: "yes"},1405 {op: "add", path: ["constructor", "polluted"], value: "yes"},1406 {op: "add", path: ["prototype", "polluted"], value: "yes"}

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

line 1407why, one step back
1407 ])

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1408–1410why, one step back
1408 expect(newMap.get("__proto__").polluted).toBe("yes")
1409 expect(newMap.get("constructor").polluted).toBe("yes")1410 expect(newMap.get("prototype").polluted).toBe("yes")

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

lines 1411–1413why, one step back
1411 expect(obj.polluted).toBe(undefined)
1412})1413

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1415–1420why, one step back
1415 const obj = {}
14161417 // @ts-ignore1418 expect(obj.polluted).toBe(undefined)1419 expect(() => {1420 applyPatches({}, [

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1422–1431why, one step back
1422 ])
1423 }).toThrow(1424 isProd1425 ? "19"1426 : "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1427 )1428 // @ts-ignore1429 expect(obj.polluted).toBe(undefined)1430})1431

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1432–1435why, one step back
1432test("#648 assigning object to itself should not change patches", () => {
1433 const input = {1434 obj: {1435 value: 200

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

lines 1436–1438why, one step back
1436 }
1437 }1438

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1439–1441why, one step back
1439 const [nextState, patches] = produceWithPatches(input, draft => {
1440 draft.obj.value = 11441 draft.obj = draft.obj

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

lines 1442–1443why, one step back
1442 })
1443

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1445why, one step back
1445 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1446–1448why, one step back
1446 op: "replace",
1447 path: ["obj", "value"],1448 value: 1

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

lines 1449–1452why, one step back
1449 }
1450 ])1451})1452

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1456why, one step back
1456

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1458–1459why, one step back
1458})
1459

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1461why, one step back
1461 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1467–1468why, one step back
1467 }
1468 )

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1470–1472why, one step back
1470 }
14711472 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1476why, one step back
1476 })

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1478–1480why, one step back
1478 }
1479})1480

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

line 1482why, one step back
1482 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1484–1487why, one step back
1484 expect(res).toEqual(undefined)
1485 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1486 }1487 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1489–1492why, one step back
1489 expect(res).toEqual(undefined)
1490 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1491 }1492 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1494–1497why, one step back
1494 expect(res).toEqual(undefined)
1495 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1496 }1497 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1499–1502why, one step back
1499 expect(res).toEqual(undefined)
1500 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1501 }1502 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1504–1507why, one step back
1504 expect(res).toEqual(undefined)
1505 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1506 }1507 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1509–1512why, one step back
1509 expect(res).toEqual(undefined)
1510 expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1511 }1512 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1513–1515why, one step back
1513 const [res, patches] = produceWithPatches(
1514 "foobar",1515 draft => "something else"

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

line 1516why, one step back
1516 )

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1520–1522why, one step back
1520 ])
1521 }1522 {

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

lines 1526–1549why, one step back
1526 }
1527})15281529runPatchTests(1530 "#879 delete item from array",1531 [1, 2, 3],1532 draft => {1533 delete draft[1]1534 },1535 [{op: "replace", path: [1], value: undefined}],1536 [{op: "replace", path: [1], value: 2}],1537 [1, undefined, 3]1538)15391540runPatchTests(1541 "#879 delete item from array - 2",1542 [1, 2, 3],1543 draft => {1544 delete draft[2]1545 },1546 [{op: "replace", path: [2], value: undefined}],1547 [{op: "replace", path: [2], value: 3}],1548 [1, 2, undefined]1549)

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 1550–1559why, one step back
1550
1551test("#897 appendPatch", () => {1552 const state0 = {a: []}1553 const state1 = applyPatches(state0, [{op: "add", path: ["a", "-"], value: 1}])1554 const state2 = applyPatches(state1, [{op: "add", path: ["a", "-"], value: 2}])1555 const state3 = applyPatches(state2, [{op: "add", path: ["a", "-"], value: 3}])1556 expect(state3).toEqual({1557 a: [1, 2, 3]1558 })1559})

fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58

lines 1560–1598why, one step back
1560
1561// Bug reproduction: Patch path truncation in production build1562// This bug is caused by the "key_" in state check failing after minification1563// The patch path should include the full path for nested array modifications1564describe("RTK-5159: Patch path truncation bug", () => {1565 test("patch paths should include full path for nested array modifications", () => {1566 const baseState = {1567 lists: [{items: [{id: 1}]}]1568 }15691570 const [result, patches] = produceWithPatches(baseState, draft => {1571 draft.lists[0].items.push({id: 2})1572 })15731574 // The patch path should be ["lists", 0, "items", 1], not just [1]1575 expect(patches[0].path).toEqual(["lists", 0, "items", 1])1576 expect(patches).toEqual([1577 {op: "add", path: ["lists", 0, "items", 1], value: {id: 2}}1578 ])1579 })15801581 test("patch paths correct for deeply nested object in array", () => {1582 const baseState = {1583 queries: {1584 queryKey: {1585 data: {1586 items: [{name: "item1"}]1587 }1588 }1589 }1590 }15911592 const [result, patches] = produceWithPatches(baseState, draft => {1593 draft.queries.queryKey.data.items.push({name: "item2"})1594 })15951596 expect(patches[0].path).toEqual(["queries", "queryKey", "data", "items", 1])1597 })1598})