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

1"use strict"2import {vi} from "vitest"3import {4	produce,5	applyPatches,6	produceWithPatches,7	isDraft,8	immerable,9	nothing,10	enablePatches,11	enableMapSet12} from "../src/immer"1314enablePatches()15enableMapSet()1617vi.setConfig({18	testTimeout: 100019})2021const isProd = process.env.NODE_ENV === "production"2223function 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) {74			test("produced the correct result", () => {75				expect(testData.result).toEqual(expectedResult)76			})77		}7879		test("produces the correct patches", () => {80			expect(testData.patches).toEqual(expectedPatches)81			if (expectedInversePatches) {82				expect(testData.inversePatches).toEqual(expectedInversePatches)83			}84		})8586		test("patches are replayable", () => {87			expect(applyPatches(base, testData.patches)).toEqual(testData.result)88		})8990		test("patches can be reversed", () => {91			if (expectedInversePatches) {92				expect(applyPatches(testData.result, testData.inversePatches)).toEqual(93					base94				)95			}96		})97	})9899	return testData100}101102// Convenience functions for common use cases103runPatchTests.only = function (104	testName,105	base,106	producer,107	expectedPatches,108	expectedInversePatches,109	expectedResult110) {111	return runPatchTests(112		testName,113		base,114		producer,115		expectedPatches,116		expectedInversePatches,117		expectedResult,118		{only: true}119	)120}121122runPatchTests.skip = function (123	testName,124	base,125	producer,126	expectedPatches,127	expectedInversePatches,128	expectedResult129) {130	return runPatchTests(131		testName,132		base,133		producer,134		expectedPatches,135		expectedInversePatches,136		expectedResult,137		{skip: true}138	)139}140141describe("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}146			])147			expect(result).toBe(draft)148			expect(draft.a).toBe(2)149		})150	})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)157	})158	it('throws when `op` is not "add", "replace", nor "remove"', () => {159		expect(() => {160			const patch = {op: "copy", from: [0], path: [1]}161			applyPatches([2], [patch])162		}).toThrowErrorMatchingSnapshot()163	})164	it("throws when `path` cannot be resolved", () => {165		// missing parent166		expect(() => {167			const patch = {op: "add", path: ["a", "b"], value: 1}168			applyPatches({}, [patch])169		}).toThrowErrorMatchingSnapshot()170171		// missing grand-parent172		expect(() => {173			const patch = {op: "add", path: ["a", "b", "c"], value: 1}174			applyPatches({}, [patch])175		}).toThrowErrorMatchingSnapshot()176	})177	it("applied patches cannot be modified", () => {178		// see also: https://github.com/immerjs/immer/issues/411179		const s0 = {180			items: [1]181		}182183		const [s1, p1] = produceWithPatches(s0, draft => {184			draft.items = []185		})186187		const replaceValueBefore = p1[0].value.slice()188189		const [s2, p2] = produceWithPatches(s1, draft => {190			draft.items.push(2)191		})192193		applyPatches(s0, [...p1, ...p2])194195		const replaceValueAfter = p1[0].value.slice()196197		expect(replaceValueAfter).toStrictEqual(replaceValueBefore)198	})199})200201describe("#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})263264// New macro-style test265runPatchTests(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 describe343describe("simple assignment - 7", () => {344	const key1 = {prop: "val1"}345	const key2 = {prop: "val2"}346347	runPatchTests(348		"",349		{x: new Map([[key1, 4]])},350		d => {351			d.x.set(key1, 5)352			d.x.set(key2, 6)353		},354		[355			{op: "replace", path: ["x", key1], value: 5},356			{op: "add", path: ["x", key2], value: 6}357		],358		[359			{op: "replace", path: ["x", key1], value: 4},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)410411describe("delete 5", () => {412	const key1 = {prop: "val1"}413	const key2 = {prop: "val2"}414415	runPatchTests(416		"",417		{418			x: new Map([419				[key1, 1],420				[key2, 2]421			])422		},423		d => {424			d.x.delete(key1)425			d.x.delete(key2)426		},427		[428			{op: "remove", path: ["x", key1]},429			{op: "remove", path: ["x", key2]}430		],431		[432			{op: "add", path: ["x", key1], value: 1},433			{op: "add", path: ["x", key2], value: 2}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)457458describe("renaming properties", () => {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",564				path: ["x"],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",1089		{x: {test: true}},1090		d => {1091			delete d.x1092		},1093		p21094	)1095	const testData = runPatchTests(1096		"add and delete together cancel",1097		{},1098		d => {1099			applyPatches(d, [...p1, ...p2])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)11231124test("replaying patches with interweaved replacements should work correctly", () => {1125	const patches = []1126	const s0 = {x: 1}11271128	const s1 = produce(1129		s0,1130		draft => {1131			draft.x = 21132		},1133		p => {1134			patches.push(...p)1135		}1136	)11371138	const s2 = produce(1139		s1,1140		draft => {1141			return {x: 0}1142		},1143		p => {1144			patches.push(...p)1145		}1146	)11471148	const s3 = produce(1149		s2,1150		draft => {1151			draft.x--1152		},1153		p => {1154			patches.push(...p)1155		}1156	)11571158	expect(s3).toEqual({x: -1}) // correct result1159	expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay11601161	// manual replay on a draft should also be correct1162	expect(1163		produce(s0, draft => {1164			return applyPatches(draft, patches)1165		})1166	).toEqual({x: -1})1167})11681169describe("#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] = item1176		})11771178		expect(nextState).toEqual([{id: 2}, {id: 1}])1179		expect(patches).toEqual([1180			{1181				op: "replace",1182				path: [0, "id"],1183				value: 21184			},1185			{1186				op: "add",1187				path: [1],1188				value: {1189					id: 11190				}1191			}1192		])11931194		const final = applyPatches(state, patches)1195		expect(final).toEqual(nextState)1196	}11971198	test("proxy", () => {1199		run()1200	})1201})12021203test("#521", () => {1204	const state = new Map()12051206	const [nextState, patches] = produceWithPatches(state, draft => {1207		draft.set("hello", new Set(["world"]))1208	})12091210	let patchedState = applyPatches(state, patches)1211	expect(patchedState).toEqual(nextState)12121213	const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1214		draft.get("hello").add("immer")1215	})12161217	expect(applyPatches(nextState, patchesV2)).toEqual(1218		new Map([["hello", new Set(["world", "immer"])]])1219	)1220})12211222test("#559 patches works in a nested reducer with proxies", () => {1223	const state = {1224		x: 1,1225		sub: {1226			y: [{a: 0}, {a: 1}]1227		}1228	}12291230	const changes = []1231	const inverseChanges = []12321233	const newState = produce(state, draft => {1234		draft.sub = produce(1235			draft.sub,1236			draft => {1237				draft.y.pop()1238			},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)1244			}1245		)1246	})12471248	const reversedSubState = applyPatches(newState.sub, inverseChanges)12491250	expect(reversedSubState).toMatchObject(state.sub)1251})12521253describe("#588", () => {1254	const reference = {value: {num: 53}}12551256	class Base {1257		[immerable] = true1258		get nested() {1259			return reference.value1260		}1261		set nested(value) {}1262	}12631264	let base = new Base()12651266	runPatchTests(1267		"",1268		base,1269		vdraft => {1270			reference.value = vdraft1271			produce(base, bdraft => {1272				bdraft.nested.num = 421273			})1274		},1275		[{op: "add", path: ["num"], value: 42}]1276	)1277})12781279test("#676 patching Date objects", () => {1280	class Test {1281		constructor() {1282			this.test = true1283		}1284		perform() {1285			return "tested!"1286		}1287	}12881289	const [nextState, patches] = produceWithPatches({}, function (draft) {1290		draft.date = new Date("2020-11-10T08:08:08.003Z")1291		draft.test = new Test()1292	})12931294	expect(nextState.date.toJSON()).toMatchInlineSnapshot(1295		`"2020-11-10T08:08:08.003Z"`1296	)1297	expect(nextState.test.perform()).toBe("tested!")12981299	const rebuilt = applyPatches({}, patches)1300	expect(rebuilt.date).toBeInstanceOf(Date)1301	expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1302		`"2020-11-10T08:08:08.003Z"`1303	)1304	expect(rebuilt.date).toEqual(new Date("2020-11-10T08:08:08.003Z"))1305})13061307test("do not allow __proto__ polution - 738", () => {1308	const obj = {}13091310	// @ts-ignore1311	expect(obj.polluted).toBe(undefined)1312	expect(() => {1313		applyPatches({}, [1314			{op: "add", path: ["__proto__", "polluted"], value: "yes"}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})13241325test("do not allow __proto__ polution using arrays - 738", () => {1326	const obj = {}1327	const ar = []13281329	// @ts-ignore1330	expect(obj.polluted).toBe(undefined)1331	// @ts-ignore1332	expect(ar.polluted).toBe(undefined)1333	expect(() => {1334		applyPatches(1335			[],1336			[{op: "add", path: ["__proto__", "polluted"], value: "yes"}]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})13481349test("do not allow prototype polution - 738", () => {1350	const obj = {}13511352	// @ts-ignore1353	expect(obj.polluted).toBe(undefined)1354	expect(() => {1355		applyPatches(Object, [1356			{op: "add", path: ["prototype", "polluted"], value: "yes"}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})13661367test("do not allow constructor polution - 738", () => {1368	const obj = {}13691370	// @ts-ignore1371	expect(obj.polluted).toBe(undefined)1372	const t = {}1373	applyPatches(t, [{op: "replace", path: ["constructor"], value: "yes"}])1374	expect(typeof t.constructor).toBe("function")1375	// @ts-ignore1376	expect(Object.polluted).toBe(undefined)1377})13781379test("do not allow constructor.prototype polution - 738", () => {1380	const obj = {}13811382	// @ts-ignore1383	expect(obj.polluted).toBe(undefined)1384	expect(() => {1385		applyPatches({}, [1386			{op: "add", path: ["constructor", "prototype", "polluted"], value: "yes"}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})13961397test("maps can store __proto__, prototype and constructor props", () => {1398	const obj = {}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"}1407	])1408	expect(newMap.get("__proto__").polluted).toBe("yes")1409	expect(newMap.get("constructor").polluted).toBe("yes")1410	expect(newMap.get("prototype").polluted).toBe("yes")1411	expect(obj.polluted).toBe(undefined)1412})14131414test("CVE-2020-28477 (https://snyk.io/vuln/SNYK-JS-IMMER-1019369) follow up", () => {1415	const obj = {}14161417	// @ts-ignore1418	expect(obj.polluted).toBe(undefined)1419	expect(() => {1420		applyPatches({}, [1421			{op: "add", path: [["__proto__"], "polluted"], value: "yes"}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})14311432test("#648 assigning object to itself should not change patches", () => {1433	const input = {1434		obj: {1435			value: 2001436		}1437	}14381439	const [nextState, patches] = produceWithPatches(input, draft => {1440		draft.obj.value = 11441		draft.obj = draft.obj1442	})14431444	expect(patches).toEqual([1445		{1446			op: "replace",1447			path: ["obj", "value"],1448			value: 11449		}1450	])1451})14521453test("#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}])14561457	expect(applyPatches({}, patches)).toEqual(undefined)1458})14591460test("#876 Ensure empty patch set for atomic set+delete on Map", () => {1461	{1462		const [newState, patches] = produceWithPatches(1463			new Map([["foo", "baz"]]),1464			draft => {1465				draft.set("foo", "bar")1466				draft.delete("foo")1467			}1468		)1469		expect(patches).toEqual([{op: "remove", path: ["foo"]}])1470	}14711472	{1473		const [newState, patches] = produceWithPatches(new Map(), draft => {1474			draft.set("foo", "bar")1475			draft.delete("foo")1476		})1477		expect(patches).toEqual([])1478	}1479})14801481test("#888 patch to a primitive produces the primitive", () => {1482	{1483		const [res, patches] = produceWithPatches({abc: 123}, draft => nothing)1484		expect(res).toEqual(undefined)1485		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1486	}1487	{1488		const [res, patches] = produceWithPatches(null, draft => nothing)1489		expect(res).toEqual(undefined)1490		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1491	}1492	{1493		const [res, patches] = produceWithPatches(0, draft => nothing)1494		expect(res).toEqual(undefined)1495		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1496	}1497	{1498		const [res, patches] = produceWithPatches("foobar", draft => nothing)1499		expect(res).toEqual(undefined)1500		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1501	}1502	{1503		const [res, patches] = produceWithPatches([], draft => nothing)1504		expect(res).toEqual(undefined)1505		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1506	}1507	{1508		const [res, patches] = produceWithPatches(false, draft => nothing)1509		expect(res).toEqual(undefined)1510		expect(patches).toEqual([{op: "replace", path: [], value: undefined}])1511	}1512	{1513		const [res, patches] = produceWithPatches(1514			"foobar",1515			draft => "something else"1516		)1517		expect(res).toEqual("something else")1518		expect(patches).toEqual([1519			{op: "replace", path: [], value: "something else"}1520		])1521	}1522	{1523		const [res, patches] = produceWithPatches(false, draft => true)1524		expect(res).toEqual(true)1525		expect(patches).toEqual([{op: "replace", path: [], value: true}])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)15501551test("#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})15601561// 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})