File size: 10,444 Bytes
f9f0fec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
'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();
});