Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

1,447 lines · 27.8 KiB · JavaScript

Last changed by

fix: patching maps failed when using number keys (#1025)Marco Schumacherbefore adoptiondd83e2e

History of this file

Take it

1"use strict"2import produce, {3	setUseProxies,4	applyPatches,5	produceWithPatches,6	enableAllPlugins,7	isDraft,8	immerable,9	nothing10} from "../src/immer"1112enableAllPlugins()1314jest.setTimeout(1000)1516const isProd = process.env.NODE_ENV === "production"1718function runPatchTest(base, producer, patches, inversePathes, expectedResult) {19	let resultProxies, resultEs52021	function runPatchTestHelper() {22		let recordedPatches23		let recordedInversePatches24		const res = produce(base, producer, (p, i) => {25			recordedPatches = p26			recordedInversePatches = i27		})2829		if (expectedResult !== undefined)30			test("produced the correct result", () => {31				expect(res).toEqual(expectedResult)32			})3334		test("produces the correct patches", () => {35			expect(recordedPatches).toEqual(patches)36			if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)37		})3839		test("patches are replayable", () => {40			expect(applyPatches(base, recordedPatches)).toEqual(res)41		})4243		test("patches can be reversed", () => {44			expect(applyPatches(res, recordedInversePatches)).toEqual(base)45		})4647		return res48	}4950	describe(`proxy`, () => {51		setUseProxies(true)52		resultProxies = runPatchTestHelper()53	})5455	describe(`es5`, () => {56		setUseProxies(false)57		resultEs5 = runPatchTestHelper()58		test("ES5 and Proxy implementation yield same result", () => {59			expect(resultEs5).toEqual(resultProxies)60		})61	})6263	return resultProxies64}6566describe("applyPatches", () => {67	it("mutates the base state when it is a draft", () => {68		produce({a: 1}, draft => {69			const result = applyPatches(draft, [70				{op: "replace", path: ["a"], value: 2}71			])72			expect(result).toBe(draft)73			expect(draft.a).toBe(2)74		})75	})76	it("produces a copy of the base state when not a draft", () => {77		const base = {a: 1}78		const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])79		expect(result).not.toBe(base)80		expect(result.a).toBe(2)81		expect(base.a).toBe(1)82	})83	it('throws when `op` is not "add", "replace", nor "remove"', () => {84		expect(() => {85			const patch = {op: "copy", from: [0], path: [1]}86			applyPatches([2], [patch])87		}).toThrowErrorMatchingSnapshot()88	})89	it("throws when `path` cannot be resolved", () => {90		// missing parent91		expect(() => {92			const patch = {op: "add", path: ["a", "b"], value: 1}93			applyPatches({}, [patch])94		}).toThrowErrorMatchingSnapshot()9596		// missing grand-parent97		expect(() => {98			const patch = {op: "add", path: ["a", "b", "c"], value: 1}99			applyPatches({}, [patch])100		}).toThrowErrorMatchingSnapshot()101	})102	it("applied patches cannot be modified", () => {103		// see also: https://github.com/immerjs/immer/issues/411104		const s0 = {105			items: [1]106		}107108		const [s1, p1] = produceWithPatches(s0, draft => {109			draft.items = []110		})111112		const replaceValueBefore = p1[0].value.slice()113114		const [s2, p2] = produceWithPatches(s1, draft => {115			draft.items.push(2)116		})117118		applyPatches(s0, [...p1, ...p2])119120		const replaceValueAfter = p1[0].value.slice()121122		expect(replaceValueAfter).toStrictEqual(replaceValueBefore)123	})124})125126describe("simple assignment - 1", () => {127	runPatchTest(128		{x: 3},129		d => {130			d.x++131		},132		[{op: "replace", path: ["x"], value: 4}]133	)134})135136describe("simple assignment - 2", () => {137	runPatchTest(138		{x: {y: 4}},139		d => {140			d.x.y++141		},142		[{op: "replace", path: ["x", "y"], value: 5}]143	)144})145146describe("simple assignment - 3", () => {147	runPatchTest(148		{x: [{y: 4}]},149		d => {150			d.x[0].y++151		},152		[{op: "replace", path: ["x", 0, "y"], value: 5}]153	)154})155156describe("simple assignment - 4", () => {157	runPatchTest(158		new Map([["x", {y: 4}]]),159		d => {160			d.get("x").y++161		},162		[{op: "replace", path: ["x", "y"], value: 5}],163		[{op: "replace", path: ["x", "y"], value: 4}]164	)165})166167describe("simple assignment - 5", () => {168	runPatchTest(169		{x: new Map([["y", 4]])},170		d => {171			d.x.set("y", 5)172		},173		[{op: "replace", path: ["x", "y"], value: 5}],174		[{op: "replace", path: ["x", "y"], value: 4}]175	)176})177178describe("simple assignment - 6", () => {179	runPatchTest(180		new Map([["x", 1]]),181		d => {182			// Map.prototype.set should return the Map itself183			const res = d.set("x", 2)184			res.set("y", 3)185		},186		[187			{op: "replace", path: ["x"], value: 2},188			{op: "add", path: ["y"], value: 3}189		],190		[191			{op: "replace", path: ["x"], value: 1},192			{op: "remove", path: ["y"]}193		]194	)195})196197describe("simple assignment - 7", () => {198	const key1 = {prop: "val1"}199	const key2 = {prop: "val2"}200	runPatchTest(201		{x: new Map([[key1, 4]])},202		d => {203			d.x.set(key1, 5)204			d.x.set(key2, 6)205		},206		[207			{op: "replace", path: ["x", key1], value: 5},208			{op: "add", path: ["x", key2], value: 6}209		],210		[211			{op: "replace", path: ["x", key1], value: 4},212			{op: "remove", path: ["x", key2]}213		]214	)215})216217describe("simple assignment - 8", () => {218	runPatchTest(219		new Map([[0, new Map([[1, 4]])]]),220		d => {221			d.get(0).set(1, 5)222			d.get(0).set(2, 6)223		},224		[225			{op: "replace", path: [0, 1], value: 5},226			{op: "add", path: [0, 2], value: 6}227		],228		[229			{op: "replace", path: [0, 1], value: 4},230			{op: "remove", path: [0, 2]}231		]232	)233})234235describe("delete 1", () => {236	runPatchTest(237		{x: {y: 4}},238		d => {239			delete d.x240		},241		[{op: "remove", path: ["x"]}]242	)243})244245describe("delete 2", () => {246	runPatchTest(247		new Map([["x", 1]]),248		d => {249			d.delete("x")250		},251		[{op: "remove", path: ["x"]}],252		[{op: "add", path: ["x"], value: 1}]253	)254})255256describe("delete 3", () => {257	runPatchTest(258		{x: new Map([["y", 1]])},259		d => {260			d.x.delete("y")261		},262		[{op: "remove", path: ["x", "y"]}],263		[{op: "add", path: ["x", "y"], value: 1}]264	)265})266267describe("delete 5", () => {268	const key1 = {prop: "val1"}269	const key2 = {prop: "val2"}270	runPatchTest(271		{272			x: new Map([273				[key1, 1],274				[key2, 2]275			])276		},277		d => {278			d.x.delete(key1)279			d.x.delete(key2)280		},281		[282			{op: "remove", path: ["x", key1]},283			{op: "remove", path: ["x", key2]}284		],285		[286			{op: "add", path: ["x", key1], value: 1},287			{op: "add", path: ["x", key2], value: 2}288		]289	)290})291292describe("delete 6", () => {293	runPatchTest(294		new Set(["x", 1]),295		d => {296			d.delete("x")297		},298		[{op: "remove", path: [0], value: "x"}],299		[{op: "add", path: [0], value: "x"}]300	)301})302303describe("delete 7", () => {304	runPatchTest(305		{x: new Set(["y", 1])},306		d => {307			d.x.delete("y")308		},309		[{op: "remove", path: ["x", 0], value: "y"}],310		[{op: "add", path: ["x", 0], value: "y"}]311	)312})313314describe("renaming properties", () => {315	describe("nested object (no changes)", () => {316		runPatchTest(317			{a: {b: 1}},318			d => {319				d.x = d.a320				delete d.a321			},322			[323				{op: "add", path: ["x"], value: {b: 1}},324				{op: "remove", path: ["a"]}325			]326		)327	})328329	describe("nested change in object", () => {330		runPatchTest(331			{332				a: {b: 1}333			},334			d => {335				d.a.b++336			},337			[{op: "replace", path: ["a", "b"], value: 2}],338			[{op: "replace", path: ["a", "b"], value: 1}]339		)340	})341342	describe("nested change in map", () => {343		runPatchTest(344			new Map([["a", new Map([["b", 1]])]]),345			d => {346				d.get("a").set("b", 2)347			},348			[{op: "replace", path: ["a", "b"], value: 2}],349			[{op: "replace", path: ["a", "b"], value: 1}]350		)351	})352353	describe("nested change in array", () => {354		runPatchTest(355			[[{b: 1}]],356			d => {357				d[0][0].b++358			},359			[{op: "replace", path: [0, 0, "b"], value: 2}],360			[{op: "replace", path: [0, 0, "b"], value: 1}]361		)362	})363364	describe("nested map (no changes)", () => {365		runPatchTest(366			new Map([["a", new Map([["b", 1]])]]),367			d => {368				d.set("x", d.get("a"))369				d.delete("a")370			},371			[372				{op: "add", path: ["x"], value: new Map([["b", 1]])},373				{op: "remove", path: ["a"]}374			],375			[376				{op: "remove", path: ["x"]},377				{op: "add", path: ["a"], value: new Map([["b", 1]])}378			]379		)380	})381382	describe("nested object (with changes)", () => {383		runPatchTest(384			{a: {b: 1, c: 1}},385			d => {386				let a = d.a387				a.b = 2 // change388				delete a.c // delete389				a.y = 2 // add390391				// rename392				d.x = a393				delete d.a394			},395			[396				{op: "add", path: ["x"], value: {b: 2, y: 2}},397				{op: "remove", path: ["a"]}398			]399		)400	})401402	describe("nested map (with changes)", () => {403		runPatchTest(404			new Map([405				[406					"a",407					new Map([408						["b", 1],409						["c", 1]410					])411				]412			]),413			d => {414				let a = d.get("a")415				a.set("b", 2) // change416				a.delete("c") // delete417				a.set("y", 2) // add418419				// rename420				d.set("x", a)421				d.delete("a")422			},423			[424				{425					op: "add",426					path: ["x"],427					value: new Map([428						["b", 2],429						["y", 2]430					])431				},432				{op: "remove", path: ["a"]}433			],434			[435				{op: "remove", path: ["x"]},436				{437					op: "add",438					path: ["a"],439					value: new Map([440						["b", 1],441						["c", 1]442					])443				}444			]445		)446	})447448	describe("deeply nested object (with changes)", () => {449		runPatchTest(450			{a: {b: {c: 1, d: 1}}},451			d => {452				let b = d.a.b453				b.c = 2 // change454				delete b.d // delete455				b.y = 2 // add456457				// rename458				d.a.x = b459				delete d.a.b460			},461			[462				{op: "add", path: ["a", "x"], value: {c: 2, y: 2}},463				{op: "remove", path: ["a", "b"]}464			]465		)466	})467468	describe("deeply nested map (with changes)", () => {469		runPatchTest(470			new Map([471				[472					"a",473					new Map([474						[475							"b",476							new Map([477								["c", 1],478								["d", 1]479							])480						]481					])482				]483			]),484			d => {485				let b = d.get("a").get("b")486				b.set("c", 2) // change487				b.delete("d") // delete488				b.set("y", 2) // add489490				// rename491				d.get("a").set("x", b)492				d.get("a").delete("b")493			},494			[495				{496					op: "add",497					path: ["a", "x"],498					value: new Map([499						["c", 2],500						["y", 2]501					])502				},503				{op: "remove", path: ["a", "b"]}504			],505			[506				{op: "remove", path: ["a", "x"]},507				{508					op: "add",509					path: ["a", "b"],510					value: new Map([511						["c", 1],512						["d", 1]513					])514				}515			]516		)517	})518})519520describe("minimum amount of changes", () => {521	runPatchTest(522		{x: 3, y: {a: 4}, z: 3},523		d => {524			d.y.a = 4525			d.y.b = 5526			Object.assign(d, {x: 4, y: {a: 2}})527		},528		[529			{op: "replace", path: ["x"], value: 4},530			{op: "replace", path: ["y"], value: {a: 2}}531		]532	)533})534535describe("arrays - prepend", () => {536	runPatchTest(537		{x: [1, 2, 3]},538		d => {539			d.x.unshift(4)540		},541		[542			{op: "replace", path: ["x", 0], value: 4},543			{op: "replace", path: ["x", 1], value: 1},544			{op: "replace", path: ["x", 2], value: 2},545			{op: "add", path: ["x", 3], value: 3}546		]547	)548})549550describe("arrays - multiple prepend", () => {551	runPatchTest(552		{x: [1, 2, 3]},553		d => {554			d.x.unshift(4)555			d.x.unshift(5)556			// 4,5,1,2,3557		},558		[559			{op: "replace", path: ["x", 0], value: 5},560			{op: "replace", path: ["x", 1], value: 4},561			{op: "replace", path: ["x", 2], value: 1},562			{op: "add", path: ["x", 3], value: 2},563			{op: "add", path: ["x", 4], value: 3}564		]565	)566})567568describe("arrays - splice middle", () => {569	runPatchTest(570		{x: [1, 2, 3]},571		d => {572			d.x.splice(1, 1)573		},574		[575			{op: "replace", path: ["x", 1], value: 3},576			{op: "replace", path: ["x", "length"], value: 2}577		]578	)579})580581describe("arrays - multiple splice", () => {582	runPatchTest(583		[0, 1, 2, 3, 4, 5, 0],584		d => {585			d.splice(4, 2, 3)586			// [0,1,2,3,3,0]587			d.splice(1, 2, 3)588			// [0,3,3,3,0]589			expect(d.slice()).toEqual([0, 3, 3, 3, 0])590		},591		[592			{op: "replace", path: [1], value: 3},593			{op: "replace", path: [2], value: 3},594			{op: "replace", path: [4], value: 0},595			{op: "replace", path: ["length"], value: 5}596		]597	)598})599600describe("arrays - modify and shrink", () => {601	runPatchTest(602		{x: [1, 2, 3]},603		d => {604			d.x[0] = 4605			d.x.length = 2606			// [0, 2]607		},608		[609			{op: "replace", path: ["x", 0], value: 4},610			{op: "replace", path: ["x", "length"], value: 2}611		],612		[613			{op: "replace", path: ["x", 0], value: 1},614			{op: "add", path: ["x", 2], value: 3}615		]616	)617})618619describe("arrays - prepend then splice middle", () => {620	runPatchTest(621		{x: [1, 2, 3]},622		d => {623			d.x.unshift(4)624			d.x.splice(2, 1)625			// 4, 1, 3626		},627		[628			{op: "replace", path: ["x", 0], value: 4},629			{op: "replace", path: ["x", 1], value: 1}630		]631	)632})633634describe("arrays - splice middle then prepend", () => {635	runPatchTest(636		{x: [1, 2, 3]},637		d => {638			d.x.splice(1, 1)639			d.x.unshift(4)640			// [4, 1, 3]641		},642		[643			{op: "replace", path: ["x", 0], value: 4},644			{op: "replace", path: ["x", 1], value: 1}645		]646	)647})648649describe("arrays - truncate", () => {650	runPatchTest(651		{x: [1, 2, 3]},652		d => {653			d.x.length -= 2654		},655		[{op: "replace", path: ["x", "length"], value: 1}],656		[657			{op: "add", path: ["x", 1], value: 2},658			{op: "add", path: ["x", 2], value: 3}659		]660	)661})662663describe("arrays - pop twice", () => {664	runPatchTest(665		{x: [1, 2, 3]},666		d => {667			d.x.pop()668			d.x.pop()669		},670		[{op: "replace", path: ["x", "length"], value: 1}]671	)672})673674describe("arrays - push multiple", () => {675	// These patches were more optimal pre immer 7, but not always correct676	runPatchTest(677		{x: [1, 2, 3]},678		d => {679			d.x.push(4, 5)680		},681		[682			{op: "add", path: ["x", 3], value: 4},683			{op: "add", path: ["x", 4], value: 5}684		],685		[{op: "replace", path: ["x", "length"], value: 3}]686	)687})688689describe("arrays - splice (expand)", () => {690	// These patches were more optimal pre immer 7, but not always correct691	runPatchTest(692		{x: [1, 2, 3]},693		d => {694			d.x.splice(1, 1, 4, 5, 6) // [1,4,5,6,3]695		},696		[697			{op: "replace", path: ["x", 1], value: 4},698			{op: "replace", path: ["x", 2], value: 5},699			{op: "add", path: ["x", 3], value: 6},700			{op: "add", path: ["x", 4], value: 3}701		],702		[703			{op: "replace", path: ["x", 1], value: 2},704			{op: "replace", path: ["x", 2], value: 3},705			{op: "replace", path: ["x", "length"], value: 3}706		]707	)708})709710describe("arrays - splice (shrink)", () => {711	// These patches were more optimal pre immer 7, but not always correct712	runPatchTest(713		{x: [1, 2, 3, 4, 5]},714		d => {715			d.x.splice(1, 3, 6) // [1, 6, 5]716		},717		[718			{op: "replace", path: ["x", 1], value: 6},719			{op: "replace", path: ["x", 2], value: 5},720			{op: "replace", path: ["x", "length"], value: 3}721		],722		[723			{op: "replace", path: ["x", 1], value: 2},724			{op: "replace", path: ["x", 2], value: 3},725			{op: "add", path: ["x", 3], value: 4},726			{op: "add", path: ["x", 4], value: 5}727		]728	)729})730731describe("arrays - delete", () => {732	runPatchTest(733		{734			x: [735				{a: 1, b: 2},736				{c: 3, d: 4}737			]738		},739		d => {740			delete d.x[1].c741		},742		[{op: "remove", path: ["x", 1, "c"]}]743	)744})745746describe("arrays - append", () => {747	test("appends to array when last part of path is '-'", () => {748		const state = {749			list: [1, 2, 3]750		}751		const patch = {752			op: "add",753			value: 4,754			path: ["list", "-"]755		}756		expect(applyPatches(state, [patch])).toEqual({757			list: [1, 2, 3, 4]758		})759	})760})761762describe("sets - add - 1", () => {763	runPatchTest(764		new Set([1]),765		d => {766			d.add(2)767		},768		[{op: "add", path: [1], value: 2}],769		[{op: "remove", path: [1], value: 2}]770	)771})772773describe("sets - add, delete, add - 1", () => {774	runPatchTest(775		new Set([1]),776		d => {777			d.add(2)778			d.delete(2)779			d.add(2)780		},781		[{op: "add", path: [1], value: 2}],782		[{op: "remove", path: [1], value: 2}]783	)784})785786describe("sets - add, delete, add - 2", () => {787	runPatchTest(788		new Set([2, 1]),789		d => {790			d.add(2)791			d.delete(2)792			d.add(2)793		},794		[],795		[]796	)797})798799describe("sets - mutate - 1", () => {800	const findById = (set, id) => {801		for (const item of set) {802			if (item.id === id) return item803		}804	}805	runPatchTest(806		new Set([807			{id: 1, val: "We"},808			{id: 2, val: "will"}809		]),810		d => {811			const obj1 = findById(d, 1)812			const obj2 = findById(d, 2)813			obj1.val = "rock"814			obj2.val = "you"815		},816		[817			{op: "remove", path: [0], value: {id: 1, val: "We"}},818			{op: "remove", path: [1], value: {id: 2, val: "will"}},819			{op: "add", path: [0], value: {id: 1, val: "rock"}},820			{op: "add", path: [1], value: {id: 2, val: "you"}}821		],822		[823			{op: "remove", path: [1], value: {id: 2, val: "you"}},824			{op: "remove", path: [0], value: {id: 1, val: "rock"}},825			{op: "add", path: [1], value: {id: 2, val: "will"}},826			{op: "add", path: [0], value: {id: 1, val: "We"}}827		]828	)829})830831describe("arrays - splice should should result in remove op.", () => {832	// These patches were more optimal pre immer 7, but not always correct833	runPatchTest(834		[1, 2],835		d => {836			d.splice(1, 1)837		},838		[{op: "replace", path: ["length"], value: 1}],839		[{op: "add", path: [1], value: 2}]840	)841})842843describe("arrays - NESTED splice should should result in remove op.", () => {844	// These patches were more optimal pre immer 7, but not always correct845	runPatchTest(846		{a: {b: {c: [1, 2]}}},847		d => {848			d.a.b.c.splice(1, 1)849		},850		[{op: "replace", path: ["a", "b", "c", "length"], value: 1}],851		[{op: "add", path: ["a", "b", "c", 1], value: 2}]852	)853})854855describe("simple replacement", () => {856	runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])857})858859describe("same value replacement - 1", () => {860	runPatchTest(861		{x: {y: 3}},862		d => {863			const a = d.x864			d.x = a865		},866		[]867	)868})869870describe("same value replacement - 2", () => {871	runPatchTest(872		{x: {y: 3}},873		d => {874			const a = d.x875			d.x = 4876			d.x = a877		},878		[]879	)880})881882describe("same value replacement - 3", () => {883	runPatchTest(884		{x: 3},885		d => {886			d.x = 3887		},888		[]889	)890})891892describe("same value replacement - 4", () => {893	runPatchTest(894		{x: 3},895		d => {896			d.x = 4897			d.x = 3898		},899		[]900	)901})902903describe("same value replacement - 5", () => {904	runPatchTest(905		new Map([["x", 3]]),906		d => {907			d.set("x", 4)908			d.set("x", 3)909		},910		[],911		[]912	)913})914915describe("same value replacement - 6", () => {916	runPatchTest(917		new Set(["x", 3]),918		d => {919			d.delete("x")920			d.add("x")921		},922		[],923		[]924	)925})926927describe("simple delete", () => {928	runPatchTest(929		{x: 2},930		d => {931			delete d.x932		},933		[934			{935				op: "remove",936				path: ["x"]937			}938		]939	)940})941942describe("patch compressions yields correct results", () => {943	let p1, p2944	runPatchTest(945		{},946		d => {947			d.x = {test: true}948		},949		(p1 = [950			{951				op: "add",952				path: ["x"],953				value: {954					test: true955				}956			}957		])958	)959	runPatchTest(960		{x: {test: true}},961		d => {962			delete d.x963		},964		(p2 = [965			{966				op: "remove",967				path: ["x"]968			}969		])970	)971	const res = runPatchTest(972		{},973		d => {974			applyPatches(d, [...p1, ...p2])975		},976		[]977	)978979	expect(res).toEqual({})980})981982describe("change then delete property", () => {983	const res = runPatchTest(984		{985			x: 1986		},987		d => {988			d.x = 2989			delete d.x990		},991		[992			{993				op: "remove",994				path: ["x"]995			}996		]997	)998	test("valid result", () => {999		expect(res).toEqual({})1000	})1001})10021003test("replaying patches with interweaved replacements should work correctly", () => {1004	const patches = []1005	const s0 = {x: 1}10061007	const s1 = produce(1008		s0,1009		draft => {1010			draft.x = 21011		},1012		p => {1013			patches.push(...p)1014		}1015	)10161017	const s2 = produce(1018		s1,1019		draft => {1020			return {x: 0}1021		},1022		p => {1023			patches.push(...p)1024		}1025	)10261027	const s3 = produce(1028		s2,1029		draft => {1030			draft.x--1031		},1032		p => {1033			patches.push(...p)1034		}1035	)10361037	expect(s3).toEqual({x: -1}) // correct result1038	expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay10391040	// manual replay on a draft should also be correct1041	expect(1042		produce(s0, draft => {1043			return applyPatches(draft, patches)1044		})1045	).toEqual({x: -1})1046})10471048describe("#468", () => {1049	function run() {1050		const item = {id: 1}1051		const state = [item]1052		const [nextState, patches] = produceWithPatches(state, draft => {1053			draft[0].id = 21054			draft[1] = item1055		})10561057		expect(nextState).toEqual([{id: 2}, {id: 1}])1058		expect(patches).toEqual([1059			{1060				op: "replace",1061				path: [0, "id"],1062				value: 21063			},1064			{1065				op: "add",1066				path: [1],1067				value: {1068					id: 11069				}1070			}1071		])10721073		const final = applyPatches(state, patches)1074		expect(final).toEqual(nextState)1075	}10761077	test("es5", () => {1078		setUseProxies(false)1079		run()1080	})10811082	test("proxy", () => {1083		setUseProxies(true)1084		run()1085	})1086})10871088test("#521", () => {1089	const state = new Map()10901091	const [nextState, patches] = produceWithPatches(state, draft => {1092		draft.set("hello", new Set(["world"]))1093	})10941095	let patchedState = applyPatches(state, patches)1096	expect(patchedState).toEqual(nextState)10971098	const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1099		draft.get("hello").add("immer")1100	})11011102	expect(applyPatches(nextState, patchesV2)).toEqual(1103		new Map([["hello", new Set(["world", "immer"])]])1104	)1105})11061107test("#559 patches works in a nested reducer with proxies", () => {1108	setUseProxies(true)11091110	const state = {1111		x: 1,1112		sub: {1113			y: [{a: 0}, {a: 1}]1114		}1115	}11161117	const changes = []1118	const inverseChanges = []11191120	const newState = produce(state, draft => {1121		draft.sub = produce(1122			draft.sub,1123			draft => {1124				draft.y.pop()1125			},1126			(patches, inversePatches) => {1127				expect(isDraft(inversePatches[0].value)).toBeFalsy()1128				expect(inversePatches[0].value).toMatchObject({a: 1})1129				changes.push(...patches)1130				inverseChanges.push(...inversePatches)1131			}1132		)1133	})11341135	const reversedSubState = applyPatches(newState.sub, inverseChanges)11361137	expect(reversedSubState).toMatchObject(state.sub)1138})11391140describe("#588", () => {1141	const reference = {value: {num: 53}}11421143	class Base {1144		[immerable] = true1145		get nested() {1146			return reference.value1147		}1148		set nested(value) {}1149	}11501151	let base = new Base()11521153	runPatchTest(1154		base,1155		vdraft => {1156			reference.value = vdraft1157			produce(base, bdraft => {1158				bdraft.nested.num = 421159			})1160		},1161		[{op: "add", path: ["num"], value: 42}]1162	)1163})11641165test("#676 patching Date objects", () => {1166	class Test {1167		constructor() {1168			this.test = true1169		}1170		perform() {1171			return "tested!"1172		}1173	}11741175	const [nextState, patches] = produceWithPatches({}, function(draft) {1176		draft.date = new Date("2020-11-10T08:08:08.003Z")1177		draft.test = new Test()1178	})11791180	expect(nextState.date.toJSON()).toMatchInlineSnapshot(1181		`"2020-11-10T08:08:08.003Z"`1182	)1183	expect(nextState.test.perform()).toBe("tested!")11841185	const rebuilt = applyPatches({}, patches)1186	expect(rebuilt.date).toBeInstanceOf(Date)1187	expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1188		`"2020-11-10T08:08:08.003Z"`1189	)1190	expect(rebuilt.date).toEqual(new Date("2020-11-10T08:08:08.003Z"))1191})11921193test("do not allow __proto__ polution - 738", () => {1194	const obj = {}11951196	// @ts-ignore1197	expect(obj.polluted).toBe(undefined)1198	expect(() => {1199		applyPatches({}, [1200			{op: "add", path: ["__proto__", "polluted"], value: "yes"}1201		])1202	}).toThrow(1203		isProd1204			? "24"1205			: "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1206	)1207	// @ts-ignore1208	expect(obj.polluted).toBe(undefined)1209})12101211test("do not allow __proto__ polution using arrays - 738", () => {1212	const obj = {}1213	const ar = []12141215	// @ts-ignore1216	expect(obj.polluted).toBe(undefined)1217	// @ts-ignore1218	expect(ar.polluted).toBe(undefined)1219	expect(() => {1220		applyPatches(1221			[],1222			[{op: "add", path: ["__proto__", "polluted"], value: "yes"}]1223		)1224	}).toThrow(1225		isProd1226			? "24"1227			: "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1228	)1229	// @ts-ignore1230	expect(obj.polluted).toBe(undefined)1231	// @ts-ignore1232	expect(ar.polluted).toBe(undefined)1233})12341235test("do not allow prototype polution - 738", () => {1236	const obj = {}12371238	// @ts-ignore1239	expect(obj.polluted).toBe(undefined)1240	expect(() => {1241		applyPatches(Object, [1242			{op: "add", path: ["prototype", "polluted"], value: "yes"}1243		])1244	}).toThrow(1245		isProd1246			? "24"1247			: "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1248	)1249	// @ts-ignore1250	expect(obj.polluted).toBe(undefined)1251})12521253test("do not allow constructor polution - 738", () => {1254	const obj = {}12551256	// @ts-ignore1257	expect(obj.polluted).toBe(undefined)1258	const t = {}1259	applyPatches(t, [{op: "replace", path: ["constructor"], value: "yes"}])1260	expect(typeof t.constructor).toBe("function")1261	// @ts-ignore1262	expect(Object.polluted).toBe(undefined)1263})12641265test("do not allow constructor.prototype polution - 738", () => {1266	const obj = {}12671268	// @ts-ignore1269	expect(obj.polluted).toBe(undefined)1270	expect(() => {1271		applyPatches({}, [1272			{op: "add", path: ["constructor", "prototype", "polluted"], value: "yes"}1273		])1274	}).toThrow(1275		isProd1276			? "24"1277			: "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1278	)1279	// @ts-ignore1280	expect(Object.polluted).toBe(undefined)1281})12821283test("maps can store __proto__, prototype and constructor props", () => {1284	const obj = {}1285	const map = new Map()1286	map.set("__proto__", {})1287	map.set("constructor", {})1288	map.set("prototype", {})1289	const newMap = applyPatches(map, [1290		{op: "add", path: ["__proto__", "polluted"], value: "yes"},1291		{op: "add", path: ["constructor", "polluted"], value: "yes"},1292		{op: "add", path: ["prototype", "polluted"], value: "yes"}1293	])1294	expect(newMap.get("__proto__").polluted).toBe("yes")1295	expect(newMap.get("constructor").polluted).toBe("yes")1296	expect(newMap.get("prototype").polluted).toBe("yes")1297	expect(obj.polluted).toBe(undefined)1298})12991300test("CVE-2020-28477 (https://snyk.io/vuln/SNYK-JS-IMMER-1019369) follow up", () => {1301	const obj = {}13021303	// @ts-ignore1304	expect(obj.polluted).toBe(undefined)1305	expect(() => {1306		applyPatches({}, [1307			{op: "add", path: [["__proto__"], "polluted"], value: "yes"}1308		])1309	}).toThrow(1310		isProd1311			? "24"1312			: "Patching reserved attributes like __proto__, prototype and constructor is not allowed"1313	)1314	// @ts-ignore1315	expect(obj.polluted).toBe(undefined)1316})13171318test("#648 assigning object to itself should not change patches", () => {1319	const input = {1320		obj: {1321			value: 2001322		}1323	}13241325	const [nextState, patches] = produceWithPatches(input, draft => {1326		draft.obj.value = 11327		draft.obj = draft.obj1328	})13291330	expect(patches).toEqual([1331		{1332			op: "replace",1333			path: ["obj", "value"],1334			value: 11335		}1336	])1337})13381339test("#791 patch for  nothing is stored as undefined", () => {1340	const [newState, patches] = produceWithPatches({abc: 123}, draft => nothing)1341	expect(patches).toEqual([{op: "replace", path: [], value: undefined}])13421343	expect(applyPatches({}, patches)).toEqual(undefined)1344})13451346test("#876 Ensure empty patch set for atomic set+delete on Map", () => {1347	{1348		const [newState, patches] = produceWithPatches(1349			new Map([["foo", "baz"]]),1350			draft => {1351				draft.set("foo", "bar")1352				draft.delete("foo")1353			}1354		)1355		expect(patches).toEqual([{op: "remove", path: ["foo"]}])1356	}13571358	{1359		const [newState, patches] = produceWithPatches(new Map(), draft => {1360			draft.set("foo", "bar")1361			draft.delete("foo")1362		})1363		expect(patches).toEqual([])1364	}1365})13661367test("#888 patch to a primitive produces the primitive", () => {1368	{1369		const [res, patches] = produceWithPatches({abc: 123}, draft => nothing)1370		expect(res).toEqual(undefined)1371		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1372	}1373	{1374		const [res, patches] = produceWithPatches(null, draft => nothing)1375		expect(res).toEqual(undefined)1376		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1377	}1378	{1379		const [res, patches] = produceWithPatches(0, draft => nothing)1380		expect(res).toEqual(undefined)1381		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1382	}1383	{1384		const [res, patches] = produceWithPatches("foobar", draft => nothing)1385		expect(res).toEqual(undefined)1386		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1387	}1388	{1389		const [res, patches] = produceWithPatches([], draft => nothing)1390		expect(res).toEqual(undefined)1391		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1392	}1393	{1394		const [res, patches] = produceWithPatches(false, draft => nothing)1395		expect(res).toEqual(undefined)1396		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1397	}1398	{1399		const [res, patches] = produceWithPatches(1400			"foobar",1401			draft => "something else"1402		)1403		expect(res).toEqual("something else")1404		expect(patches).toEqual([1405			{op: "replace", path: [], value: "something else"}1406		])1407	}1408	{1409		const [res, patches] = produceWithPatches(false, draft => true)1410		expect(res).toEqual(true)1411		expect(patches).toEqual([{op: "replace", path: [], value: true}])1412	}1413})14141415describe("#879 delete item from array", () => {1416	runPatchTest(1417		[1, 2, 3],1418		draft => {1419			delete draft[1]1420		},1421		[{op: "replace", path: [1], value: undefined}],1422		[{op: "replace", path: [1], value: 2}],1423		[1, undefined, 3]1424	)1425})14261427describe("#879 delete item from array - 2", () => {1428	runPatchTest(1429		[1, 2, 3],1430		draft => {1431			delete draft[2]1432		},1433		[{op: "replace", path: [2], value: undefined}],1434		[{op: "replace", path: [2], value: 3}],1435		[1, 2, undefined]1436	)1437})14381439test("#897 appendPatch", () => {1440	const state0 = {a: []}1441	const state1 = applyPatches(state0, [{op: "add", path: ["a", "-"], value: 1}])1442	const state2 = applyPatches(state1, [{op: "add", path: ["a", "-"], value: 2}])1443	const state3 = applyPatches(state2, [{op: "add", path: ["a", "-"], value: 3}])1444	expect(state3).toEqual({1445		a: [1, 2, 3]1446	})1447})