Blame view
app/bower_components/es5-shim/tests/spec/s-object.js
5.44 KB
f986e111b
|
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 |
describe('Object', function () { "use strict"; describe("Object.keys", function () { var obj = { "str": "boz", "obj": { }, "arr": [], "bool": true, "num": 42, "null": null, "undefined": undefined }; var loopedValues = []; for (var k in obj) { loopedValues.push(k); } var keys = Object.keys(obj); it('should have correct length', function () { expect(keys.length).toBe(7); }); it('should return an Array', function () { expect(Array.isArray(keys)).toBe(true); }); it('should return names which are own properties', function () { keys.forEach(function (name) { expect(obj.hasOwnProperty(name)).toBe(true); }); }); it('should return names which are enumerable', function () { keys.forEach(function (name) { expect(loopedValues.indexOf(name)).toNotBe(-1); }) }); it('should throw error for non object', function () { var e = {}; expect(function () { try { Object.keys(42) } catch (err) { throw e; } }).toThrow(e); }); }); describe("Object.isExtensible", function () { var obj = { }; it('should return true if object is extensible', function () { expect(Object.isExtensible(obj)).toBe(true); }); it('should return false if object is not extensible', function () { expect(Object.isExtensible(Object.preventExtensions(obj))).toBe(false); }); it('should return false if object is seal', function () { expect(Object.isExtensible(Object.seal(obj))).toBe(false); }); it('should return false if object is freeze', function () { expect(Object.isExtensible(Object.freeze(obj))).toBe(false); }); it('should throw error for non object', function () { var e1 = {}; expect(function () { try { Object.isExtensible(42) } catch (err) { throw e1; } }).toThrow(e1); }); }); describe("Object.defineProperty", function () { var obj; beforeEach(function() { obj = {}; Object.defineProperty(obj, 'name', { value : 'Testing', configurable: true, enumerable: true, writable: true }); }); it('should return the initial value', function () { expect(obj.hasOwnProperty('name')).toBeTruthy(); expect(obj.name).toBe('Testing'); }); it('should be setable', function () { obj.name = 'Other'; expect(obj.name).toBe('Other'); }); it('should return the parent initial value', function () { var child = Object.create(obj, {}); expect(child.name).toBe('Testing'); expect(child.hasOwnProperty('name')).toBeFalsy(); }); it('should not override the parent value', function () { var child = Object.create(obj, {}); Object.defineProperty(child, 'name', { value : 'Other' }); expect(obj.name).toBe('Testing'); expect(child.name).toBe('Other'); }); it('should throw error for non object', function () { expect(function () { Object.defineProperty(42, 'name', {}); }).toThrow(); }); }); describe("Object.getOwnPropertyDescriptor", function () { it('should return undefined because the object does not own the property', function () { var descr = Object.getOwnPropertyDescriptor({}, 'name'); expect(descr).toBeUndefined() }); it('should return a data descriptor', function () { var descr = Object.getOwnPropertyDescriptor({name: 'Testing'}, 'name'); expect(descr).not.toBeUndefined(); expect(descr.value).toBe('Testing'); expect(descr.writable).toBe(true); expect(descr.enumerable).toBe(true); expect(descr.configurable).toBe(true); }); it('should return undefined because the object does not own the property', function () { var descr = Object.getOwnPropertyDescriptor(Object.create({name: 'Testing'}, {}), 'name'); expect(descr).toBeUndefined() }); it('should return a data descriptor', function () { var obj = Object.create({}, { name: { value : 'Testing', configurable: true, enumerable: true, writable: true } }); var descr = Object.getOwnPropertyDescriptor(obj, 'name'); expect(descr).not.toBeUndefined(); expect(descr.value).toBe('Testing'); expect(descr.writable).toBe(true); expect(descr.enumerable).toBe(true); expect(descr.configurable).toBe(true); }); it('should throw error for non object', function () { expect(function () { Object.getOwnPropertyDescriptor(42, 'name'); }).toThrow(); }); }); }); |