| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 | 'use strict';var test = require('tape');var v = require('es-value-fixtures');var forEach = require('for-each');var inspect = require('object-inspect');var has = require('has');var hasPropertyDescriptors = require('has-property-descriptors')();var getOwnPropertyDescriptors = require('object.getownpropertydescriptors');var ownKeys = require('reflect.ownkeys');var defineDataProperty = require('../');test('defineDataProperty', function (t) {	t.test('argument validation', function (st) {		forEach(v.primitives, function (nonObject) {			st['throws'](				// @ts-expect-error				function () { defineDataProperty(nonObject, 'key', 'value'); },				TypeError,				'throws on non-object input: ' + inspect(nonObject)			);		});		forEach(v.nonPropertyKeys, function (nonPropertyKey) {			st['throws'](				// @ts-expect-error				function () { defineDataProperty({}, nonPropertyKey, 'value'); },				TypeError,				'throws on non-PropertyKey input: ' + inspect(nonPropertyKey)			);		});		forEach(v.nonBooleans, function (nonBoolean) {			if (nonBoolean !== null) {				st['throws'](				// @ts-expect-error					function () { defineDataProperty({}, 'key', 'value', nonBoolean); },					TypeError,					'throws on non-boolean nonEnumerable: ' + inspect(nonBoolean)				);				st['throws'](				// @ts-expect-error					function () { defineDataProperty({}, 'key', 'value', false, nonBoolean); },					TypeError,					'throws on non-boolean nonWritable: ' + inspect(nonBoolean)				);				st['throws'](				// @ts-expect-error					function () { defineDataProperty({}, 'key', 'value', false, false, nonBoolean); },					TypeError,					'throws on non-boolean nonConfigurable: ' + inspect(nonBoolean)				);			}		});		st.end();	});	t.test('normal data property', function (st) {		/** @type {Record<PropertyKey, string>} */		var obj = { existing: 'existing property' };		st.ok(has(obj, 'existing'), 'has initial own property');		st.equal(obj.existing, 'existing property', 'has expected initial value');		var res = defineDataProperty(obj, 'added', 'added property');		st.equal(res, void undefined, 'returns `undefined`');		st.ok(has(obj, 'added'), 'has expected own property');		st.equal(obj.added, 'added property', 'has expected value');		defineDataProperty(obj, 'existing', 'new value');		st.ok(has(obj, 'existing'), 'still has expected own property');		st.equal(obj.existing, 'new value', 'has new expected value');		defineDataProperty(obj, 'explicit1', 'new value', false);		st.ok(has(obj, 'explicit1'), 'has expected own property (explicit enumerable)');		st.equal(obj.explicit1, 'new value', 'has new expected value (explicit enumerable)');		defineDataProperty(obj, 'explicit2', 'new value', false, false);		st.ok(has(obj, 'explicit2'), 'has expected own property (explicit writable)');		st.equal(obj.explicit2, 'new value', 'has new expected value (explicit writable)');		defineDataProperty(obj, 'explicit3', 'new value', false, false, false);		st.ok(has(obj, 'explicit3'), 'has expected own property (explicit configurable)');		st.equal(obj.explicit3, 'new value', 'has new expected value (explicit configurable)');		st.end();	});	t.test('loose mode', { skip: !hasPropertyDescriptors }, function (st) {		var obj = { existing: 'existing property' };		defineDataProperty(obj, 'added', 'added value 1', true, null, null, true);		st.deepEqual(			getOwnPropertyDescriptors(obj),			{				existing: {					configurable: true,					enumerable: true,					value: 'existing property',					writable: true				},				added: {					configurable: true,					enumerable: !hasPropertyDescriptors,					value: 'added value 1',					writable: true				}			},			'in loose mode, obj still adds property 1'		);		defineDataProperty(obj, 'added', 'added value 2', false, true, null, true);		st.deepEqual(			getOwnPropertyDescriptors(obj),			{				existing: {					configurable: true,					enumerable: true,					value: 'existing property',					writable: true				},				added: {					configurable: true,					enumerable: true,					value: 'added value 2',					writable: !hasPropertyDescriptors				}			},			'in loose mode, obj still adds property 2'		);		defineDataProperty(obj, 'added', 'added value 3', false, false, true, true);		st.deepEqual(			getOwnPropertyDescriptors(obj),			{				existing: {					configurable: true,					enumerable: true,					value: 'existing property',					writable: true				},				added: {					configurable: !hasPropertyDescriptors,					enumerable: true,					value: 'added value 3',					writable: true				}			},			'in loose mode, obj still adds property 3'		);		st.end();	});	t.test('non-normal data property, ES3', { skip: hasPropertyDescriptors }, function (st) {		/** @type {Record<PropertyKey, string>} */		var obj = { existing: 'existing property' };		st['throws'](			function () { defineDataProperty(obj, 'added', 'added value', true); },			SyntaxError,			'nonEnumerable throws a Syntax Error'		);		st['throws'](			function () { defineDataProperty(obj, 'added', 'added value', false, true); },			SyntaxError,			'nonWritable throws a Syntax Error'		);		st['throws'](			function () { defineDataProperty(obj, 'added', 'added value', false, false, true); },			SyntaxError,			'nonWritable throws a Syntax Error'		);		st.deepEqual(			ownKeys(obj),			['existing'],			'obj still has expected keys'		);		st.equal(obj.existing, 'existing property', 'obj still has expected values');		st.end();	});	t.test('new non-normal data property, ES5+', { skip: !hasPropertyDescriptors }, function (st) {		/** @type {Record<PropertyKey, string>} */		var obj = { existing: 'existing property' };		defineDataProperty(obj, 'nonEnum', null, true);		defineDataProperty(obj, 'nonWrit', null, false, true);		defineDataProperty(obj, 'nonConf', null, false, false, true);		st.deepEqual(			getOwnPropertyDescriptors(obj),			{				existing: {					configurable: true,					enumerable: true,					value: 'existing property',					writable: true				},				nonEnum: {					configurable: true,					enumerable: false,					value: null,					writable: true				},				nonWrit: {					configurable: true,					enumerable: true,					value: null,					writable: false				},				nonConf: {					configurable: false,					enumerable: true,					value: null,					writable: true				}			},			'obj has expected property descriptors'		);		st.end();	});	t.test('existing non-normal data property, ES5+', { skip: !hasPropertyDescriptors }, function (st) {		// test case changing an existing non-normal property		/** @type {Record<string, null | string>} */		var obj = {};		Object.defineProperty(obj, 'nonEnum', { configurable: true, enumerable: false, value: null, writable: true });		Object.defineProperty(obj, 'nonWrit', { configurable: true, enumerable: true, value: null, writable: false });		Object.defineProperty(obj, 'nonConf', { configurable: false, enumerable: true, value: null, writable: true });		st.deepEqual(			getOwnPropertyDescriptors(obj),			{				nonEnum: {					configurable: true,					enumerable: false,					value: null,					writable: true				},				nonWrit: {					configurable: true,					enumerable: true,					value: null,					writable: false				},				nonConf: {					configurable: false,					enumerable: true,					value: null,					writable: true				}			},			'obj initially has expected property descriptors'		);		defineDataProperty(obj, 'nonEnum', 'new value', false);		defineDataProperty(obj, 'nonWrit', 'new value', false, false);		st['throws'](			function () { defineDataProperty(obj, 'nonConf', 'new value', false, false, false); },			TypeError,			'can not alter a nonconfigurable property'		);		st.deepEqual(			getOwnPropertyDescriptors(obj),			{				nonEnum: {					configurable: true,					enumerable: true,					value: 'new value',					writable: true				},				nonWrit: {					configurable: true,					enumerable: true,					value: 'new value',					writable: true				},				nonConf: {					configurable: false,					enumerable: true,					value: null,					writable: true				}			},			'obj ends up with expected property descriptors'		);		st.end();	});	t.test('frozen object, ES5+', { skip: !hasPropertyDescriptors }, function (st) {		var frozen = Object.freeze({ existing: true });		st['throws'](			function () { defineDataProperty(frozen, 'existing', 'new value'); },			TypeError,			'frozen object can not modify an existing property'		);		st['throws'](			function () { defineDataProperty(frozen, 'new', 'new property'); },			TypeError,			'frozen object can not add a new property'		);		st.end();	});	t.test('sealed object, ES5+', { skip: !hasPropertyDescriptors }, function (st) {		var sealed = Object.seal({ existing: true });		st.deepEqual(			Object.getOwnPropertyDescriptor(sealed, 'existing'),			{				configurable: false,				enumerable: true,				value: true,				writable: true			},			'existing value on sealed object has expected descriptor'		);		defineDataProperty(sealed, 'existing', 'new value');		st.deepEqual(			Object.getOwnPropertyDescriptor(sealed, 'existing'),			{				configurable: false,				enumerable: true,				value: 'new value',				writable: true			},			'existing value on sealed object has changed descriptor'		);		st['throws'](			function () { defineDataProperty(sealed, 'new', 'new property'); },			TypeError,			'sealed object can not add a new property'		);		st.end();	});	t.test('nonextensible object, ES5+', { skip: !hasPropertyDescriptors }, function (st) {		var nonExt = Object.preventExtensions({ existing: true });		st.deepEqual(			Object.getOwnPropertyDescriptor(nonExt, 'existing'),			{				configurable: true,				enumerable: true,				value: true,				writable: true			},			'existing value on non-extensible object has expected descriptor'		);		defineDataProperty(nonExt, 'existing', 'new value', true);		st.deepEqual(			Object.getOwnPropertyDescriptor(nonExt, 'existing'),			{				configurable: true,				enumerable: false,				value: 'new value',				writable: true			},			'existing value on non-extensible object has changed descriptor'		);		st['throws'](			function () { defineDataProperty(nonExt, 'new', 'new property'); },			TypeError,			'non-extensible object can not add a new property'		);		st.end();	});	t.end();});
 |