| 1 |
"use strict"; |
| 2 |
|
| 3 |
Object.defineProperty(Object.prototype, 'NOSET', { |
| 4 |
get(value) { |
| 5 |
// noop |
| 6 |
}, |
| 7 |
}); |
| 8 |
|
| 9 |
Object.defineProperty(Object.prototype, 'NOWRITE', { |
| 10 |
writable: false, |
| 11 |
value: 'abc', |
| 12 |
}); |
| 13 |
|
| 14 |
const obj = { 'NOSET': 123 }; |
| 15 |
// this won't work as expected if transformed as Object.assign (or equivalent) |
| 16 |
// because those trigger object setters (spread don't) |
| 17 |
expect(() => { |
| 18 |
const objSpread = { ...obj }; |
| 19 |
}).toThrow(); |
| 20 |
|
| 21 |
const obj2 = { 'NOWRITE': 456 }; |
| 22 |
// this throws `TypeError: Cannot assign to read only property 'NOWRITE'` |
| 23 |
// if transformed as Object.assign (or equivalent) because those use *assignment* for creating properties |
| 24 |
// (spread defines them) |
| 25 |
expect(() => { |
| 26 |
const obj2Spread = { ...obj2 }; |
| 27 |
}).toThrow(); |
| 28 |
|
| 29 |
const KEY = Symbol('key'); |
| 30 |
const obj3Spread = { ...{ get foo () { return 'bar' } }, [KEY]: 'symbol' }; |
| 31 |
expect(Object.getOwnPropertyDescriptor(obj3Spread, 'foo').value).toBe('bar'); |
| 32 |
expect(Object.getOwnPropertyDescriptor(obj3Spread, KEY).value).toBe('symbol'); |
| 33 |
|
| 34 |
const obj4Spread = { ...Object.prototype }; |
| 35 |
expect(Object.getOwnPropertyDescriptor(obj4Spread, 'hasOwnProperty')).toBeUndefined(); |
| 36 |
|
| 37 |
expect(() => ({ ...null, ...undefined })).not.toThrow(); |
| 38 |
|
| 39 |
const o = Object.create(null); |
| 40 |
o.a = 'foo'; |
| 41 |
o.__proto__ = []; |
| 42 |
const o2 = { ...o }; |
| 43 |
// Loose will do o2.__proto__ = [] |
| 44 |
expect(Array.isArray(Object.getPrototypeOf(o2))).toBe(true); |