Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

1,050 lines · 17.9 KiB · JavaScript

Last changed by

removed `.only` specifier from testMichel Weststratebefore adoption2b54ac4

History of this file

Take it

1"use strict"2import produce, {3	setUseProxies,4	applyPatches,5	produceWithPatches,6	enableAllPlugins,7	isDraft8} from "../src/immer"910enableAllPlugins()1112jest.setTimeout(1000)1314function runPatchTest(base, producer, patches, inversePathes) {15	let resultProxies, resultEs51617	function runPatchTestHelper() {18		let recordedPatches19		let recordedInversePatches20		const res = produce(base, producer, (p, i) => {21			recordedPatches = p22			recordedInversePatches = i23		})2425		test("produces the correct patches", () => {26			expect(recordedPatches).toEqual(patches)27			if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)28		})2930		test("patches are replayable", () => {31			expect(applyPatches(base, recordedPatches)).toEqual(res)32		})3334		test("patches can be reversed", () => {35			expect(applyPatches(res, recordedInversePatches)).toEqual(base)36		})3738		return res39	}4041	describe(`proxy`, () => {42		setUseProxies(true)43		resultProxies = runPatchTestHelper()44	})4546	describe(`es5`, () => {47		setUseProxies(false)48		resultEs5 = runPatchTestHelper()49		test("ES5 and Proxy implementation yield same result", () => {50			expect(resultEs5).toEqual(resultProxies)51		})52	})5354	return resultProxies55}5657describe("applyPatches", () => {58	it("mutates the base state when it is a draft", () => {59		produce({a: 1}, draft => {60			const result = applyPatches(draft, [61				{op: "replace", path: ["a"], value: 2}62			])63			expect(result).toBe(draft)64			expect(draft.a).toBe(2)65		})66	})67	it("produces a copy of the base state when not a draft", () => {68		const base = {a: 1}69		const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])70		expect(result).not.toBe(base)71		expect(result.a).toBe(2)72		expect(base.a).toBe(1)73	})74	it('throws when `op` is not "add", "replace", nor "remove"', () => {75		expect(() => {76			const patch = {op: "copy", from: [0], path: [1]}77			applyPatches([2], [patch])78		}).toThrowErrorMatchingSnapshot()79	})80	it("throws when `path` cannot be resolved", () => {81		// missing parent82		expect(() => {83			const patch = {op: "add", path: ["a", "b"], value: 1}84			applyPatches({}, [patch])85		}).toThrowErrorMatchingSnapshot()8687		// missing grand-parent88		expect(() => {89			const patch = {op: "add", path: ["a", "b", "c"], value: 1}90			applyPatches({}, [patch])91		}).toThrowErrorMatchingSnapshot()92	})93	it("applied patches cannot be modified", () => {94		// see also: https://github.com/immerjs/immer/issues/41195		const s0 = {96			items: [1]97		}9899		const [s1, p1] = produceWithPatches(s0, draft => {100			draft.items = []101		})102103		const replaceValueBefore = p1[0].value.slice()104105		const [s2, p2] = produceWithPatches(s1, draft => {106			draft.items.push(2)107		})108109		applyPatches(s0, [...p1, ...p2])110111		const replaceValueAfter = p1[0].value.slice()112113		expect(replaceValueAfter).toStrictEqual(replaceValueBefore)114	})115})116117describe("simple assignment - 1", () => {118	runPatchTest(119		{x: 3},120		d => {121			d.x++122		},123		[{op: "replace", path: ["x"], value: 4}]124	)125})126127describe("simple assignment - 2", () => {128	runPatchTest(129		{x: {y: 4}},130		d => {131			d.x.y++132		},133		[{op: "replace", path: ["x", "y"], value: 5}]134	)135})136137describe("simple assignment - 3", () => {138	runPatchTest(139		{x: [{y: 4}]},140		d => {141			d.x[0].y++142		},143		[{op: "replace", path: ["x", 0, "y"], value: 5}]144	)145})146147describe("simple assignment - 4", () => {148	runPatchTest(149		new Map([["x", {y: 4}]]),150		d => {151			d.get("x").y++152		},153		[{op: "replace", path: ["x", "y"], value: 5}],154		[{op: "replace", path: ["x", "y"], value: 4}]155	)156})157158describe("simple assignment - 5", () => {159	runPatchTest(160		{x: new Map([["y", 4]])},161		d => {162			d.x.set("y", 5)163		},164		[{op: "replace", path: ["x", "y"], value: 5}],165		[{op: "replace", path: ["x", "y"], value: 4}]166	)167})168169describe("simple assignment - 6", () => {170	runPatchTest(171		new Map([["x", 1]]),172		d => {173			// Map.prototype.set should return the Map itself174			const res = d.set("x", 2)175			res.set("y", 3)176		},177		[178			{op: "replace", path: ["x"], value: 2},179			{op: "add", path: ["y"], value: 3}180		],181		[182			{op: "replace", path: ["x"], value: 1},183			{op: "remove", path: ["y"]}184		]185	)186})187188describe("simple assignment - 7", () => {189	const key1 = {prop: "val1"}190	const key2 = {prop: "val2"}191	runPatchTest(192		{x: new Map([[key1, 4]])},193		d => {194			d.x.set(key1, 5)195			d.x.set(key2, 6)196		},197		[198			{op: "replace", path: ["x", key1], value: 5},199			{op: "add", path: ["x", key2], value: 6}200		],201		[202			{op: "replace", path: ["x", key1], value: 4},203			{op: "remove", path: ["x", key2]}204		]205	)206})207208describe("delete 1", () => {209	runPatchTest(210		{x: {y: 4}},211		d => {212			delete d.x213		},214		[{op: "remove", path: ["x"]}]215	)216})217218describe("delete 2", () => {219	runPatchTest(220		new Map([["x", 1]]),221		d => {222			d.delete("x")223		},224		[{op: "remove", path: ["x"]}],225		[{op: "add", path: ["x"], value: 1}]226	)227})228229describe("delete 3", () => {230	runPatchTest(231		{x: new Map([["y", 1]])},232		d => {233			d.x.delete("y")234		},235		[{op: "remove", path: ["x", "y"]}],236		[{op: "add", path: ["x", "y"], value: 1}]237	)238})239240describe("delete 5", () => {241	const key1 = {prop: "val1"}242	const key2 = {prop: "val2"}243	runPatchTest(244		{245			x: new Map([246				[key1, 1],247				[key2, 2]248			])249		},250		d => {251			d.x.delete(key1)252			d.x.delete(key2)253		},254		[255			{op: "remove", path: ["x", key1]},256			{op: "remove", path: ["x", key2]}257		],258		[259			{op: "add", path: ["x", key1], value: 1},260			{op: "add", path: ["x", key2], value: 2}261		]262	)263})264265describe("delete 6", () => {266	runPatchTest(267		new Set(["x", 1]),268		d => {269			d.delete("x")270		},271		[{op: "remove", path: [0], value: "x"}],272		[{op: "add", path: [0], value: "x"}]273	)274})275276describe("delete 7", () => {277	runPatchTest(278		{x: new Set(["y", 1])},279		d => {280			d.x.delete("y")281		},282		[{op: "remove", path: ["x", 0], value: "y"}],283		[{op: "add", path: ["x", 0], value: "y"}]284	)285})286287describe("renaming properties", () => {288	describe("nested object (no changes)", () => {289		runPatchTest(290			{a: {b: 1}},291			d => {292				d.x = d.a293				delete d.a294			},295			[296				{op: "add", path: ["x"], value: {b: 1}},297				{op: "remove", path: ["a"]}298			]299		)300	})301302	describe("nested map (no changes)", () => {303		runPatchTest(304			new Map([["a", new Map([["b", 1]])]]),305			d => {306				d.set("x", d.get("a"))307				d.delete("a")308			},309			[310				{op: "add", path: ["x"], value: new Map([["b", 1]])},311				{op: "remove", path: ["a"]}312			],313			[314				{op: "remove", path: ["x"]},315				{op: "add", path: ["a"], value: new Map([["b", 1]])}316			]317		)318	})319320	describe("nested object (with changes)", () => {321		runPatchTest(322			{a: {b: 1, c: 1}},323			d => {324				let a = d.a325				a.b = 2 // change326				delete a.c // delete327				a.y = 2 // add328329				// rename330				d.x = a331				delete d.a332			},333			[334				{op: "add", path: ["x"], value: {b: 2, y: 2}},335				{op: "remove", path: ["a"]}336			]337		)338	})339340	describe("nested map (with changes)", () => {341		runPatchTest(342			new Map([343				[344					"a",345					new Map([346						["b", 1],347						["c", 1]348					])349				]350			]),351			d => {352				let a = d.get("a")353				a.set("b", 2) // change354				a.delete("c") // delete355				a.set("y", 2) // add356357				// rename358				d.set("x", a)359				d.delete("a")360			},361			[362				{363					op: "add",364					path: ["x"],365					value: new Map([366						["b", 2],367						["y", 2]368					])369				},370				{op: "remove", path: ["a"]}371			],372			[373				{op: "remove", path: ["x"]},374				{375					op: "add",376					path: ["a"],377					value: new Map([378						["b", 1],379						["c", 1]380					])381				}382			]383		)384	})385386	describe("deeply nested object (with changes)", () => {387		runPatchTest(388			{a: {b: {c: 1, d: 1}}},389			d => {390				let b = d.a.b391				b.c = 2 // change392				delete b.d // delete393				b.y = 2 // add394395				// rename396				d.a.x = b397				delete d.a.b398			},399			[400				{op: "add", path: ["a", "x"], value: {c: 2, y: 2}},401				{op: "remove", path: ["a", "b"]}402			]403		)404	})405406	describe("deeply nested map (with changes)", () => {407		runPatchTest(408			new Map([409				[410					"a",411					new Map([412						[413							"b",414							new Map([415								["c", 1],416								["d", 1]417							])418						]419					])420				]421			]),422			d => {423				let b = d.get("a").get("b")424				b.set("c", 2) // change425				b.delete("d") // delete426				b.set("y", 2) // add427428				// rename429				d.get("a").set("x", b)430				d.get("a").delete("b")431			},432			[433				{434					op: "add",435					path: ["a", "x"],436					value: new Map([437						["c", 2],438						["y", 2]439					])440				},441				{op: "remove", path: ["a", "b"]}442			],443			[444				{op: "remove", path: ["a", "x"]},445				{446					op: "add",447					path: ["a", "b"],448					value: new Map([449						["c", 1],450						["d", 1]451					])452				}453			]454		)455	})456})457458describe("minimum amount of changes", () => {459	runPatchTest(460		{x: 3, y: {a: 4}, z: 3},461		d => {462			d.y.a = 4463			d.y.b = 5464			Object.assign(d, {x: 4, y: {a: 2}})465		},466		[467			{op: "replace", path: ["x"], value: 4},468			{op: "replace", path: ["y"], value: {a: 2}}469		]470	)471})472473describe("arrays - prepend", () => {474	runPatchTest(475		{x: [1, 2, 3]},476		d => {477			d.x.unshift(4)478		},479		[{op: "add", path: ["x", 0], value: 4}]480	)481})482483describe("arrays - multiple prepend", () => {484	runPatchTest(485		{x: [1, 2, 3]},486		d => {487			d.x.unshift(4)488			d.x.unshift(5)489		},490		[491			{op: "add", path: ["x", 0], value: 5},492			{op: "add", path: ["x", 1], value: 4}493		]494	)495})496497describe("arrays - splice middle", () => {498	runPatchTest(499		{x: [1, 2, 3]},500		d => {501			d.x.splice(1, 1)502		},503		[{op: "remove", path: ["x", 1]}]504	)505})506507describe("arrays - multiple splice", () => {508	runPatchTest(509		[0, 1, 2, 3, 4, 5, 0],510		d => {511			d.splice(4, 2, 3)512			d.splice(1, 2, 3)513		},514		[515			{op: "replace", path: [1], value: 3},516			{op: "replace", path: [2], value: 3},517			{op: "remove", path: [5]},518			{op: "remove", path: [4]}519		]520	)521})522523describe("arrays - modify and shrink", () => {524	runPatchTest(525		{x: [1, 2, 3]},526		d => {527			d.x[0] = 4528			d.x.length = 2529		},530		[531			{op: "replace", path: ["x", 0], value: 4},532			{op: "remove", path: ["x", 2]}533		],534		[535			{op: "replace", path: ["x", 0], value: 1},536			{op: "add", path: ["x", 2], value: 3}537		]538	)539})540541describe("arrays - prepend then splice middle", () => {542	runPatchTest(543		{x: [1, 2, 3]},544		d => {545			d.x.unshift(4)546			d.x.splice(2, 1)547		},548		[549			{op: "replace", path: ["x", 0], value: 4},550			{op: "replace", path: ["x", 1], value: 1}551		]552	)553})554555describe("arrays - splice middle then prepend", () => {556	runPatchTest(557		{x: [1, 2, 3]},558		d => {559			d.x.splice(1, 1)560			d.x.unshift(4)561		},562		[563			{op: "replace", path: ["x", 0], value: 4},564			{op: "replace", path: ["x", 1], value: 1}565		]566	)567})568569describe("arrays - truncate", () => {570	runPatchTest(571		{x: [1, 2, 3]},572		d => {573			d.x.length -= 2574		},575		[576			{op: "remove", path: ["x", 2]},577			{op: "remove", path: ["x", 1]}578		],579		[580			{op: "add", path: ["x", 1], value: 2},581			{op: "add", path: ["x", 2], value: 3}582		]583	)584})585586describe("arrays - pop twice", () => {587	runPatchTest(588		{x: [1, 2, 3]},589		d => {590			d.x.pop()591			d.x.pop()592		},593		[594			{op: "remove", path: ["x", 2]},595			{op: "remove", path: ["x", 1]}596		]597	)598})599600describe("arrays - push multiple", () => {601	runPatchTest(602		{x: [1, 2, 3]},603		d => {604			d.x.push(4, 5)605		},606		[607			{op: "add", path: ["x", 3], value: 4},608			{op: "add", path: ["x", 4], value: 5}609		],610		[611			{op: "remove", path: ["x", 4]},612			{op: "remove", path: ["x", 3]}613		]614	)615})616617describe("arrays - splice (expand)", () => {618	runPatchTest(619		{x: [1, 2, 3]},620		d => {621			d.x.splice(1, 1, 4, 5, 6)622		},623		[624			{op: "replace", path: ["x", 1], value: 4},625			{op: "add", path: ["x", 2], value: 5},626			{op: "add", path: ["x", 3], value: 6}627		],628		[629			{op: "replace", path: ["x", 1], value: 2},630			{op: "remove", path: ["x", 3]},631			{op: "remove", path: ["x", 2]}632		]633	)634})635636describe("arrays - splice (shrink)", () => {637	runPatchTest(638		{x: [1, 2, 3, 4, 5]},639		d => {640			d.x.splice(1, 3, 6)641		},642		[643			{op: "replace", path: ["x", 1], value: 6},644			{op: "remove", path: ["x", 3]},645			{op: "remove", path: ["x", 2]}646		],647		[648			{op: "replace", path: ["x", 1], value: 2},649			{op: "add", path: ["x", 2], value: 3},650			{op: "add", path: ["x", 3], value: 4}651		]652	)653})654655describe("arrays - delete", () => {656	runPatchTest(657		{658			x: [659				{a: 1, b: 2},660				{c: 3, d: 4}661			]662		},663		d => {664			delete d.x[1].c665		},666		[{op: "remove", path: ["x", 1, "c"]}]667	)668})669670describe("sets - add - 1", () => {671	runPatchTest(672		new Set([1]),673		d => {674			d.add(2)675		},676		[{op: "add", path: [1], value: 2}],677		[{op: "remove", path: [1], value: 2}]678	)679})680681describe("sets - add, delete, add - 1", () => {682	runPatchTest(683		new Set([1]),684		d => {685			d.add(2)686			d.delete(2)687			d.add(2)688		},689		[{op: "add", path: [1], value: 2}],690		[{op: "remove", path: [1], value: 2}]691	)692})693694describe("sets - add, delete, add - 2", () => {695	runPatchTest(696		new Set([2, 1]),697		d => {698			d.add(2)699			d.delete(2)700			d.add(2)701		},702		[],703		[]704	)705})706707describe("sets - mutate - 1", () => {708	const findById = (set, id) => {709		for (const item of set) {710			if (item.id === id) return item711		}712	}713	runPatchTest(714		new Set([715			{id: 1, val: "We"},716			{id: 2, val: "will"}717		]),718		d => {719			const obj1 = findById(d, 1)720			const obj2 = findById(d, 2)721			obj1.val = "rock"722			obj2.val = "you"723		},724		[725			{op: "remove", path: [0], value: {id: 1, val: "We"}},726			{op: "remove", path: [1], value: {id: 2, val: "will"}},727			{op: "add", path: [0], value: {id: 1, val: "rock"}},728			{op: "add", path: [1], value: {id: 2, val: "you"}}729		],730		[731			{op: "remove", path: [1], value: {id: 2, val: "you"}},732			{op: "remove", path: [0], value: {id: 1, val: "rock"}},733			{op: "add", path: [1], value: {id: 2, val: "will"}},734			{op: "add", path: [0], value: {id: 1, val: "We"}}735		]736	)737})738739describe("arrays - splice should should result in remove op.", () => {740	runPatchTest(741		[1, 2],742		d => {743			d.splice(1, 1)744		},745		[{op: "remove", path: [1]}],746		[{op: "add", path: [1], value: 2}]747	)748})749750describe("arrays - NESTED splice should should result in remove op.", () => {751	runPatchTest(752		{a: {b: {c: [1, 2]}}},753		d => {754			d.a.b.c.splice(1, 1)755		},756		[{op: "remove", path: ["a", "b", "c", 1]}],757		[{op: "add", path: ["a", "b", "c", 1], value: 2}]758	)759})760761describe("simple replacement", () => {762	runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])763})764765describe("same value replacement - 1", () => {766	runPatchTest(767		{x: {y: 3}},768		d => {769			const a = d.x770			d.x = a771		},772		[]773	)774})775776describe("same value replacement - 2", () => {777	runPatchTest(778		{x: {y: 3}},779		d => {780			const a = d.x781			d.x = 4782			d.x = a783		},784		[]785	)786})787788describe("same value replacement - 3", () => {789	runPatchTest(790		{x: 3},791		d => {792			d.x = 3793		},794		[]795	)796})797798describe("same value replacement - 4", () => {799	runPatchTest(800		{x: 3},801		d => {802			d.x = 4803			d.x = 3804		},805		[]806	)807})808809describe("same value replacement - 5", () => {810	runPatchTest(811		new Map([["x", 3]]),812		d => {813			d.set("x", 4)814			d.set("x", 3)815		},816		[],817		[]818	)819})820821describe("same value replacement - 6", () => {822	runPatchTest(823		new Set(["x", 3]),824		d => {825			d.delete("x")826			d.add("x")827		},828		[],829		[]830	)831})832833describe("simple delete", () => {834	runPatchTest(835		{x: 2},836		d => {837			delete d.x838		},839		[840			{841				op: "remove",842				path: ["x"]843			}844		]845	)846})847848describe("patch compressions yields correct results", () => {849	let p1, p2850	runPatchTest(851		{},852		d => {853			d.x = {test: true}854		},855		(p1 = [856			{857				op: "add",858				path: ["x"],859				value: {860					test: true861				}862			}863		])864	)865	runPatchTest(866		{x: {test: true}},867		d => {868			delete d.x869		},870		(p2 = [871			{872				op: "remove",873				path: ["x"]874			}875		])876	)877	const res = runPatchTest(878		{},879		d => {880			applyPatches(d, [...p1, ...p2])881		},882		[]883	)884885	expect(res).toEqual({})886})887888describe("change then delete property", () => {889	const res = runPatchTest(890		{891			x: 1892		},893		d => {894			d.x = 2895			delete d.x896		},897		[898			{899				op: "remove",900				path: ["x"]901			}902		]903	)904	test("valid result", () => {905		expect(res).toEqual({})906	})907})908909test("replaying patches with interweaved replacements should work correctly", () => {910	const patches = []911	const s0 = {x: 1}912913	const s1 = produce(914		s0,915		draft => {916			draft.x = 2917		},918		p => {919			patches.push(...p)920		}921	)922923	const s2 = produce(924		s1,925		draft => {926			return {x: 0}927		},928		p => {929			patches.push(...p)930		}931	)932933	const s3 = produce(934		s2,935		draft => {936			draft.x--937		},938		p => {939			patches.push(...p)940		}941	)942943	expect(s3).toEqual({x: -1}) // correct result944	expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay945946	// manual replay on a draft should also be correct947	expect(948		produce(s0, draft => {949			return applyPatches(draft, patches)950		})951	).toEqual({x: -1})952})953954test.skip("#468", () => {955	const item = {id: 1}956957	const state = [item]958959	const [nextState, patches] = produceWithPatches(state, draft => {960		draft[0].id = 2961		draft[1] = item962	})963964	expect(nextState).toMatchInlineSnapshot(`965		Array [966		  Object {967		    "id": 2,968		  },969		  Object {970		    "id": 1,971		  },972		]973	`)974	expect(patches).toMatchInlineSnapshot(`975				Array [976				  Object {977				    "op": "replace",978				    "path": Array [979				      0,980				      "id",981				    ],982				    "value": 2,983				  },984				  Object {985				    "op": "add",986				    "path": Array [987				      0,988				    ],989				    "value": Object {990				      "id": 2,991				    },992				  },993				]994		`)995996	const final = applyPatches(state, patches)997	expect(final).toEqual(nextState)998})9991000test("#521", () => {1001	const state = new Map()10021003	const [nextState, patches] = produceWithPatches(state, draft => {1004		draft.set("hello", new Set(["world"]))1005	})10061007	let patchedState = applyPatches(state, patches)1008	expect(patchedState).toEqual(nextState)10091010	const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {1011		draft.get("hello").add("immer")1012	})10131014	expect(applyPatches(nextState, patchesV2)).toEqual(1015		new Map([["hello", new Set(["world", "immer"])]])1016	)1017})10181019test("#559 patches works in a nested reducer with proxies", () => {1020	setUseProxies(true)10211022	const state = {1023		x: 1,1024		sub: {1025			y: [{a: 0}, {a: 1}]1026		}1027	}10281029	const changes = []1030	const inverseChanges = []10311032	const newState = produce(state, draft => {1033		draft.sub = produce(1034			draft.sub,1035			draft => {1036				draft.y.pop()1037			},1038			(patches, inversePatches) => {1039				expect(isDraft(inversePatches[0].value)).toBeFalsy()1040				expect(inversePatches[0].value).toMatchObject({a: 1})1041				changes.push(...patches)1042				inverseChanges.push(...inversePatches)1043			}1044		)1045	})10461047	const reversedSubState = applyPatches(newState.sub, inverseChanges)10481049	expect(reversedSubState).toMatchObject(state.sub)1050})