Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

979 lines · 16.5 KiB · JavaScript

Last changed by

chore: added test to confirm #468Michel Weststratebefore adoptionb2f23c4

History of this file

Take it

1"use strict"2import produce, {3	setUseProxies,4	applyPatches,5	produceWithPatches6} from "../src/index"78jest.setTimeout(1000)910function runPatchTest(base, producer, patches, inversePathes) {11	let resultProxies, resultEs51213	function runPatchTestHelper() {14		let recordedPatches15		let recordedInversePatches16		const res = produce(base, producer, (p, i) => {17			recordedPatches = p18			recordedInversePatches = i19		})2021		test("produces the correct patches", () => {22			expect(recordedPatches).toEqual(patches)23			if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)24		})2526		test("patches are replayable", () => {27			expect(applyPatches(base, recordedPatches)).toEqual(res)28		})2930		test("patches can be reversed", () => {31			expect(applyPatches(res, recordedInversePatches)).toEqual(base)32		})3334		return res35	}3637	describe(`proxy`, () => {38		setUseProxies(true)39		resultProxies = runPatchTestHelper()40	})4142	describe(`es5`, () => {43		setUseProxies(false)44		resultEs5 = runPatchTestHelper()45		test("ES5 and Proxy implementation yield same result", () => {46			expect(resultEs5).toEqual(resultProxies)47		})48	})4950	return resultProxies51}5253describe("applyPatches", () => {54	it("mutates the base state when it is a draft", () => {55		produce({a: 1}, draft => {56			const result = applyPatches(draft, [57				{op: "replace", path: ["a"], value: 2}58			])59			expect(result).toBe(draft)60			expect(draft.a).toBe(2)61		})62	})63	it("produces a copy of the base state when not a draft", () => {64		const base = {a: 1}65		const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])66		expect(result).not.toBe(base)67		expect(result.a).toBe(2)68		expect(base.a).toBe(1)69	})70	it('throws when `op` is not "add", "replace", nor "remove"', () => {71		expect(() => {72			const patch = {op: "copy", from: [0], path: [1]}73			applyPatches([2], [patch])74		}).toThrowErrorMatchingSnapshot()75	})76	it("throws when `path` cannot be resolved", () => {77		// missing parent78		expect(() => {79			const patch = {op: "add", path: ["a", "b"], value: 1}80			applyPatches({}, [patch])81		}).toThrowErrorMatchingSnapshot()8283		// missing grand-parent84		expect(() => {85			const patch = {op: "add", path: ["a", "b", "c"], value: 1}86			applyPatches({}, [patch])87		}).toThrowErrorMatchingSnapshot()88	})89	it("applied patches cannot be modified", () => {90		// see also: https://github.com/immerjs/immer/issues/41191		const s0 = {92			items: [1]93		}9495		const [s1, p1] = produceWithPatches(s0, draft => {96			draft.items = []97		})9899		const replaceValueBefore = p1[0].value.slice()100101		const [s2, p2] = produceWithPatches(s1, draft => {102			draft.items.push(2)103		})104105		applyPatches(s0, [...p1, ...p2])106107		const replaceValueAfter = p1[0].value.slice()108109		expect(replaceValueAfter).toStrictEqual(replaceValueBefore)110	})111})112113describe("simple assignment - 1", () => {114	runPatchTest(115		{x: 3},116		d => {117			d.x++118		},119		[{op: "replace", path: ["x"], value: 4}]120	)121})122123describe("simple assignment - 2", () => {124	runPatchTest(125		{x: {y: 4}},126		d => {127			d.x.y++128		},129		[{op: "replace", path: ["x", "y"], value: 5}]130	)131})132133describe("simple assignment - 3", () => {134	runPatchTest(135		{x: [{y: 4}]},136		d => {137			d.x[0].y++138		},139		[{op: "replace", path: ["x", 0, "y"], value: 5}]140	)141})142143describe("simple assignment - 4", () => {144	runPatchTest(145		new Map([["x", {y: 4}]]),146		d => {147			d.get("x").y++148		},149		[{op: "replace", path: ["x", "y"], value: 5}],150		[{op: "replace", path: ["x", "y"], value: 4}]151	)152})153154describe("simple assignment - 5", () => {155	runPatchTest(156		{x: new Map([["y", 4]])},157		d => {158			d.x.set("y", 5)159		},160		[{op: "replace", path: ["x", "y"], value: 5}],161		[{op: "replace", path: ["x", "y"], value: 4}]162	)163})164165describe("simple assignment - 6", () => {166	runPatchTest(167		new Map([["x", 1]]),168		d => {169			// Map.prototype.set should return the Map itself170			const res = d.set("x", 2)171			res.set("y", 3)172		},173		[174			{op: "replace", path: ["x"], value: 2},175			{op: "add", path: ["y"], value: 3}176		],177		[178			{op: "replace", path: ["x"], value: 1},179			{op: "remove", path: ["y"]}180		]181	)182})183184describe("simple assignment - 7", () => {185	const key1 = {prop: "val1"}186	const key2 = {prop: "val2"}187	runPatchTest(188		{x: new Map([[key1, 4]])},189		d => {190			d.x.set(key1, 5)191			d.x.set(key2, 6)192		},193		[194			{op: "replace", path: ["x", key1], value: 5},195			{op: "add", path: ["x", key2], value: 6}196		],197		[198			{op: "replace", path: ["x", key1], value: 4},199			{op: "remove", path: ["x", key2]}200		]201	)202})203204describe("delete 1", () => {205	runPatchTest(206		{x: {y: 4}},207		d => {208			delete d.x209		},210		[{op: "remove", path: ["x"]}]211	)212})213214describe("delete 2", () => {215	runPatchTest(216		new Map([["x", 1]]),217		d => {218			d.delete("x")219		},220		[{op: "remove", path: ["x"]}],221		[{op: "add", path: ["x"], value: 1}]222	)223})224225describe("delete 3", () => {226	runPatchTest(227		{x: new Map([["y", 1]])},228		d => {229			d.x.delete("y")230		},231		[{op: "remove", path: ["x", "y"]}],232		[{op: "add", path: ["x", "y"], value: 1}]233	)234})235236describe("delete 5", () => {237	const key1 = {prop: "val1"}238	const key2 = {prop: "val2"}239	runPatchTest(240		{241			x: new Map([242				[key1, 1],243				[key2, 2]244			])245		},246		d => {247			d.x.delete(key1)248			d.x.delete(key2)249		},250		[251			{op: "remove", path: ["x", key1]},252			{op: "remove", path: ["x", key2]}253		],254		[255			{op: "add", path: ["x", key1], value: 1},256			{op: "add", path: ["x", key2], value: 2}257		]258	)259})260261describe("delete 6", () => {262	runPatchTest(263		new Set(["x", 1]),264		d => {265			d.delete("x")266		},267		[{op: "remove", path: [0], value: "x"}],268		[{op: "add", path: [0], value: "x"}]269	)270})271272describe("delete 7", () => {273	runPatchTest(274		{x: new Set(["y", 1])},275		d => {276			d.x.delete("y")277		},278		[{op: "remove", path: ["x", 0], value: "y"}],279		[{op: "add", path: ["x", 0], value: "y"}]280	)281})282283describe("renaming properties", () => {284	describe("nested object (no changes)", () => {285		runPatchTest(286			{a: {b: 1}},287			d => {288				d.x = d.a289				delete d.a290			},291			[292				{op: "add", path: ["x"], value: {b: 1}},293				{op: "remove", path: ["a"]}294			]295		)296	})297298	describe("nested map (no changes)", () => {299		runPatchTest(300			new Map([["a", new Map([["b", 1]])]]),301			d => {302				d.set("x", d.get("a"))303				d.delete("a")304			},305			[306				{op: "add", path: ["x"], value: new Map([["b", 1]])},307				{op: "remove", path: ["a"]}308			],309			[310				{op: "remove", path: ["x"]},311				{op: "add", path: ["a"], value: new Map([["b", 1]])}312			]313		)314	})315316	describe("nested object (with changes)", () => {317		runPatchTest(318			{a: {b: 1, c: 1}},319			d => {320				let a = d.a321				a.b = 2 // change322				delete a.c // delete323				a.y = 2 // add324325				// rename326				d.x = a327				delete d.a328			},329			[330				{op: "add", path: ["x"], value: {b: 2, y: 2}},331				{op: "remove", path: ["a"]}332			]333		)334	})335336	describe("nested map (with changes)", () => {337		runPatchTest(338			new Map([339				[340					"a",341					new Map([342						["b", 1],343						["c", 1]344					])345				]346			]),347			d => {348				let a = d.get("a")349				a.set("b", 2) // change350				a.delete("c") // delete351				a.set("y", 2) // add352353				// rename354				d.set("x", a)355				d.delete("a")356			},357			[358				{359					op: "add",360					path: ["x"],361					value: new Map([362						["b", 2],363						["y", 2]364					])365				},366				{op: "remove", path: ["a"]}367			],368			[369				{op: "remove", path: ["x"]},370				{371					op: "add",372					path: ["a"],373					value: new Map([374						["b", 1],375						["c", 1]376					])377				}378			]379		)380	})381382	describe("deeply nested object (with changes)", () => {383		runPatchTest(384			{a: {b: {c: 1, d: 1}}},385			d => {386				let b = d.a.b387				b.c = 2 // change388				delete b.d // delete389				b.y = 2 // add390391				// rename392				d.a.x = b393				delete d.a.b394			},395			[396				{op: "add", path: ["a", "x"], value: {c: 2, y: 2}},397				{op: "remove", path: ["a", "b"]}398			]399		)400	})401402	describe("deeply nested map (with changes)", () => {403		runPatchTest(404			new Map([405				[406					"a",407					new Map([408						[409							"b",410							new Map([411								["c", 1],412								["d", 1]413							])414						]415					])416				]417			]),418			d => {419				let b = d.get("a").get("b")420				b.set("c", 2) // change421				b.delete("d") // delete422				b.set("y", 2) // add423424				// rename425				d.get("a").set("x", b)426				d.get("a").delete("b")427			},428			[429				{430					op: "add",431					path: ["a", "x"],432					value: new Map([433						["c", 2],434						["y", 2]435					])436				},437				{op: "remove", path: ["a", "b"]}438			],439			[440				{op: "remove", path: ["a", "x"]},441				{442					op: "add",443					path: ["a", "b"],444					value: new Map([445						["c", 1],446						["d", 1]447					])448				}449			]450		)451	})452})453454describe("minimum amount of changes", () => {455	runPatchTest(456		{x: 3, y: {a: 4}, z: 3},457		d => {458			d.y.a = 4459			d.y.b = 5460			Object.assign(d, {x: 4, y: {a: 2}})461		},462		[463			{op: "replace", path: ["x"], value: 4},464			{op: "replace", path: ["y"], value: {a: 2}}465		]466	)467})468469describe("arrays - prepend", () => {470	runPatchTest(471		{x: [1, 2, 3]},472		d => {473			d.x.unshift(4)474		},475		[{op: "add", path: ["x", 0], value: 4}]476	)477})478479describe("arrays - multiple prepend", () => {480	runPatchTest(481		{x: [1, 2, 3]},482		d => {483			d.x.unshift(4)484			d.x.unshift(5)485		},486		[487			{op: "add", path: ["x", 0], value: 5},488			{op: "add", path: ["x", 1], value: 4}489		]490	)491})492493describe("arrays - splice middle", () => {494	runPatchTest(495		{x: [1, 2, 3]},496		d => {497			d.x.splice(1, 1)498		},499		[{op: "remove", path: ["x", 1]}]500	)501})502503describe("arrays - multiple splice", () => {504	runPatchTest(505		[0, 1, 2, 3, 4, 5, 0],506		d => {507			d.splice(4, 2, 3)508			d.splice(1, 2, 3)509		},510		[511			{op: "replace", path: [1], value: 3},512			{op: "replace", path: [2], value: 3},513			{op: "remove", path: [5]},514			{op: "remove", path: [4]}515		]516	)517})518519describe("arrays - modify and shrink", () => {520	runPatchTest(521		{x: [1, 2, 3]},522		d => {523			d.x[0] = 4524			d.x.length = 2525		},526		[527			{op: "replace", path: ["x", 0], value: 4},528			{op: "remove", path: ["x", 2]}529		],530		[531			{op: "replace", path: ["x", 0], value: 1},532			{op: "add", path: ["x", 2], value: 3}533		]534	)535})536537describe("arrays - prepend then splice middle", () => {538	runPatchTest(539		{x: [1, 2, 3]},540		d => {541			d.x.unshift(4)542			d.x.splice(2, 1)543		},544		[545			{op: "replace", path: ["x", 0], value: 4},546			{op: "replace", path: ["x", 1], value: 1}547		]548	)549})550551describe("arrays - splice middle then prepend", () => {552	runPatchTest(553		{x: [1, 2, 3]},554		d => {555			d.x.splice(1, 1)556			d.x.unshift(4)557		},558		[559			{op: "replace", path: ["x", 0], value: 4},560			{op: "replace", path: ["x", 1], value: 1}561		]562	)563})564565describe("arrays - truncate", () => {566	runPatchTest(567		{x: [1, 2, 3]},568		d => {569			d.x.length -= 2570		},571		[572			{op: "remove", path: ["x", 2]},573			{op: "remove", path: ["x", 1]}574		],575		[576			{op: "add", path: ["x", 1], value: 2},577			{op: "add", path: ["x", 2], value: 3}578		]579	)580})581582describe("arrays - pop twice", () => {583	runPatchTest(584		{x: [1, 2, 3]},585		d => {586			d.x.pop()587			d.x.pop()588		},589		[590			{op: "remove", path: ["x", 2]},591			{op: "remove", path: ["x", 1]}592		]593	)594})595596describe("arrays - push multiple", () => {597	runPatchTest(598		{x: [1, 2, 3]},599		d => {600			d.x.push(4, 5)601		},602		[603			{op: "add", path: ["x", 3], value: 4},604			{op: "add", path: ["x", 4], value: 5}605		],606		[607			{op: "remove", path: ["x", 4]},608			{op: "remove", path: ["x", 3]}609		]610	)611})612613describe("arrays - splice (expand)", () => {614	runPatchTest(615		{x: [1, 2, 3]},616		d => {617			d.x.splice(1, 1, 4, 5, 6)618		},619		[620			{op: "replace", path: ["x", 1], value: 4},621			{op: "add", path: ["x", 2], value: 5},622			{op: "add", path: ["x", 3], value: 6}623		],624		[625			{op: "replace", path: ["x", 1], value: 2},626			{op: "remove", path: ["x", 3]},627			{op: "remove", path: ["x", 2]}628		]629	)630})631632describe("arrays - splice (shrink)", () => {633	runPatchTest(634		{x: [1, 2, 3, 4, 5]},635		d => {636			d.x.splice(1, 3, 6)637		},638		[639			{op: "replace", path: ["x", 1], value: 6},640			{op: "remove", path: ["x", 3]},641			{op: "remove", path: ["x", 2]}642		],643		[644			{op: "replace", path: ["x", 1], value: 2},645			{op: "add", path: ["x", 2], value: 3},646			{op: "add", path: ["x", 3], value: 4}647		]648	)649})650651describe("sets - add - 1", () => {652	runPatchTest(653		new Set([1]),654		d => {655			d.add(2)656		},657		[{op: "add", path: [1], value: 2}],658		[{op: "remove", path: [1], value: 2}]659	)660})661662describe("sets - add, delete, add - 1", () => {663	runPatchTest(664		new Set([1]),665		d => {666			d.add(2)667			d.delete(2)668			d.add(2)669		},670		[{op: "add", path: [1], value: 2}],671		[{op: "remove", path: [1], value: 2}]672	)673})674675describe("sets - add, delete, add - 2", () => {676	runPatchTest(677		new Set([2, 1]),678		d => {679			d.add(2)680			d.delete(2)681			d.add(2)682		},683		[],684		[]685	)686})687688describe("sets - mutate - 1", () => {689	const findById = (set, id) => {690		for (const item of set) {691			if (item.id === id) return item692		}693	}694	runPatchTest(695		new Set([696			{id: 1, val: "We"},697			{id: 2, val: "will"}698		]),699		d => {700			const obj1 = findById(d, 1)701			const obj2 = findById(d, 2)702			obj1.val = "rock"703			obj2.val = "you"704		},705		[706			{op: "remove", path: [0], value: {id: 1, val: "We"}},707			{op: "remove", path: [1], value: {id: 2, val: "will"}},708			{op: "add", path: [0], value: {id: 1, val: "rock"}},709			{op: "add", path: [1], value: {id: 2, val: "you"}}710		],711		[712			{op: "remove", path: [1], value: {id: 2, val: "you"}},713			{op: "remove", path: [0], value: {id: 1, val: "rock"}},714			{op: "add", path: [1], value: {id: 2, val: "will"}},715			{op: "add", path: [0], value: {id: 1, val: "We"}}716		]717	)718})719720describe("arrays - splice should should result in remove op.", () => {721	runPatchTest(722		[1, 2],723		d => {724			d.splice(1, 1)725		},726		[{op: "remove", path: [1]}],727		[{op: "add", path: [1], value: 2}]728	)729})730731describe("arrays - NESTED splice should should result in remove op.", () => {732	runPatchTest(733		{a: {b: {c: [1, 2]}}},734		d => {735			d.a.b.c.splice(1, 1)736		},737		[{op: "remove", path: ["a", "b", "c", 1]}],738		[{op: "add", path: ["a", "b", "c", 1], value: 2}]739	)740})741742describe("simple replacement", () => {743	runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])744})745746describe("same value replacement - 1", () => {747	runPatchTest(748		{x: {y: 3}},749		d => {750			const a = d.x751			d.x = a752		},753		[]754	)755})756757describe("same value replacement - 2", () => {758	runPatchTest(759		{x: {y: 3}},760		d => {761			const a = d.x762			d.x = 4763			d.x = a764		},765		[]766	)767})768769describe("same value replacement - 3", () => {770	runPatchTest(771		{x: 3},772		d => {773			d.x = 3774		},775		[]776	)777})778779describe("same value replacement - 4", () => {780	runPatchTest(781		{x: 3},782		d => {783			d.x = 4784			d.x = 3785		},786		[]787	)788})789790describe("same value replacement - 5", () => {791	runPatchTest(792		new Map([["x", 3]]),793		d => {794			d.set("x", 4)795			d.set("x", 3)796		},797		[],798		[]799	)800})801802describe("same value replacement - 6", () => {803	runPatchTest(804		new Set(["x", 3]),805		d => {806			d.delete("x")807			d.add("x")808		},809		[],810		[]811	)812})813814describe("simple delete", () => {815	runPatchTest(816		{x: 2},817		d => {818			delete d.x819		},820		[821			{822				op: "remove",823				path: ["x"]824			}825		]826	)827})828829describe("patch compressions yields correct results", () => {830	let p1, p2831	runPatchTest(832		{},833		d => {834			d.x = {test: true}835		},836		(p1 = [837			{838				op: "add",839				path: ["x"],840				value: {841					test: true842				}843			}844		])845	)846	runPatchTest(847		{x: {test: true}},848		d => {849			delete d.x850		},851		(p2 = [852			{853				op: "remove",854				path: ["x"]855			}856		])857	)858	const res = runPatchTest(859		{},860		d => {861			applyPatches(d, [...p1, ...p2])862		},863		[]864	)865866	expect(res).toEqual({})867})868869describe("change then delete property", () => {870	const res = runPatchTest(871		{872			x: 1873		},874		d => {875			d.x = 2876			delete d.x877		},878		[879			{880				op: "remove",881				path: ["x"]882			}883		]884	)885	test("valid result", () => {886		expect(res).toEqual({})887	})888})889890test("replaying patches with interweaved replacements should work correctly", () => {891	const patches = []892	const s0 = {x: 1}893894	const s1 = produce(895		s0,896		draft => {897			draft.x = 2898		},899		p => {900			patches.push(...p)901		}902	)903904	const s2 = produce(905		s1,906		draft => {907			return {x: 0}908		},909		p => {910			patches.push(...p)911		}912	)913914	const s3 = produce(915		s2,916		draft => {917			draft.x--918		},919		p => {920			patches.push(...p)921		}922	)923924	expect(s3).toEqual({x: -1}) // correct result925	expect(applyPatches(s0, patches)).toEqual({x: -1}) // correct replay926927	// manual replay on a draft should also be correct928	expect(929		produce(s0, draft => {930			return applyPatches(draft, patches)931		})932	).toEqual({x: -1})933})934935test.skip("#468", () => {936	const item = {id: 1}937938	const state = [item]939940	const [nextState, patches] = produceWithPatches(state, draft => {941		draft[0].id = 2942		draft[1] = item943	})944945	expect(nextState).toMatchInlineSnapshot(`946		Array [947		  Object {948		    "id": 2,949		  },950		  Object {951		    "id": 1,952		  },953		]954	`)955	expect(patches).toMatchInlineSnapshot(`956				Array [957				  Object {958				    "op": "replace",959				    "path": Array [960				      0,961				      "id",962				    ],963				    "value": 2,964				  },965				  Object {966				    "op": "add",967				    "path": Array [968				      0,969				    ],970				    "value": Object {971				      "id": 2,972				    },973				  },974				]975		`)976977	const final = applyPatches(state, patches)978	expect(final).toEqual(nextState)979})