Skip to content

Commit

Permalink
Maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
Xotic750 committed Aug 17, 2019
1 parent 5cc50ab commit 124889c
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 284 deletions.
18 changes: 15 additions & 3 deletions __tests__/__snapshots__/object-keys-x.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`objectKeys should throw for null or undefined 1`] = `"Cannot convert undefined or null to object"`;
exports[`objectKeys 0 should throw for null or undefined 1`] = `"Cannot call method on undefined"`;

exports[`objectKeys should throw for null or undefined 2`] = `"Cannot convert undefined or null to object"`;
exports[`objectKeys 0 should throw for null or undefined 2`] = `"Cannot call method on undefined"`;

exports[`objectKeys should throw for null or undefined 3`] = `"Cannot convert undefined or null to object"`;
exports[`objectKeys 0 should throw for null or undefined 3`] = `"Cannot call method on null"`;

exports[`objectKeys 1 should throw for null or undefined 1`] = `"Cannot call method on undefined"`;

exports[`objectKeys 1 should throw for null or undefined 2`] = `"Cannot call method on undefined"`;

exports[`objectKeys 1 should throw for null or undefined 3`] = `"Cannot call method on null"`;

exports[`objectKeys 2 should throw for null or undefined 1`] = `"Cannot convert undefined or null to object"`;

exports[`objectKeys 2 should throw for null or undefined 2`] = `"Cannot convert undefined or null to object"`;

exports[`objectKeys 2 should throw for null or undefined 3`] = `"Cannot convert undefined or null to object"`;
306 changes: 154 additions & 152 deletions __tests__/object-keys-x.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import objectKeys from '../src/object-keys-x';
import $A, {implementation as $I, patched as $P} from '../src/object-keys-x';

const has = Object.prototype.hasOwnProperty;
const supportsDescriptors =
Expand All @@ -21,190 +21,192 @@ const supportsDescriptors =

const ifWindowIt = typeof window === 'undefined' ? xit : it;

describe('objectKeys', function() {
const obj = {
arr: [],
bool: true,
null: null,
num: 42,
obj: {},
str: 'boz',

undefined: void 0,
};

const loopedValues = [];
/* eslint-disable-next-line no-restricted-syntax,guard-for-in */
for (const key in obj) {
loopedValues.push(key);
}

const keys = objectKeys(obj);
it('should throw for null or undefined', function() {
expect.assertions(3);
expect(function() {
objectKeys();
}).toThrowErrorMatchingSnapshot();

expect(function() {
objectKeys(void 0);
}).toThrowErrorMatchingSnapshot();

expect(function() {
objectKeys(null);
}).toThrowErrorMatchingSnapshot();
});

it('should not throw for non-objects', function() {
expect.assertions(3);
expect(objectKeys(1)).toStrictEqual([]);
expect(objectKeys(true)).toStrictEqual([]);
expect(objectKeys('')).toStrictEqual([]);
});

it('should have correct length', function() {
expect.assertions(1);
expect(keys).toHaveLength(7);
});
[$I, $P, $A].forEach((objectKeys, testNum) => {
describe(`objectKeys ${testNum}`, function() {
const obj = {
arr: [],
bool: true,
null: null,
num: 42,
obj: {},
str: 'boz',

undefined: void 0,
};

const loopedValues = [];
/* eslint-disable-next-line no-restricted-syntax,guard-for-in */
for (const key in obj) {
loopedValues.push(key);
}

describe('arguments objects', function() {
it('works with an arguments object', function() {
const keys = objectKeys(obj);
it('should throw for null or undefined', function() {
expect.assertions(3);
(function() {
expect(arguments).toHaveLength(3);
expect(function() {
objectKeys();
}).toThrowErrorMatchingSnapshot();

expect(objectKeys(arguments)).toHaveLength(arguments.length);
expect(function() {
objectKeys(void 0);
}).toThrowErrorMatchingSnapshot();

expect(objectKeys(arguments)).toStrictEqual(['0', '1', '2']);
})(1, 2, 3);
expect(function() {
objectKeys(null);
}).toThrowErrorMatchingSnapshot();
});

it('works with a legacy arguments object', function() {
it('should not throw for non-objects', function() {
expect.assertions(3);
expect(objectKeys(1)).toStrictEqual([]);
expect(objectKeys(true)).toStrictEqual([]);
expect(objectKeys('')).toStrictEqual([]);
});

it('should have correct length', function() {
expect.assertions(1);
const FakeArguments = function(args) {
args.forEach(
function(arg, i) {
this[i] = arg;
}.bind(this),
);
};
expect(keys).toHaveLength(7);
});

describe('arguments objects', function() {
it('works with an arguments object', function() {
expect.assertions(3);
(function() {
expect(arguments).toHaveLength(3);

FakeArguments.prototype.length = 3;
expect(objectKeys(arguments)).toHaveLength(arguments.length);

FakeArguments.prototype.callee = function() {};
expect(objectKeys(arguments)).toStrictEqual(['0', '1', '2']);
})(1, 2, 3);
});

const fakeOldArguments = new FakeArguments(['a', 'b', 'c']);
expect(objectKeys(fakeOldArguments)).toStrictEqual(['0', '1', '2']);
it('works with a legacy arguments object', function() {
expect.assertions(1);
const FakeArguments = function(args) {
args.forEach(
function(arg, i) {
this[i] = arg;
}.bind(this),
);
};

FakeArguments.prototype.length = 3;

FakeArguments.prototype.callee = function() {};

const fakeOldArguments = new FakeArguments(['a', 'b', 'c']);
expect(objectKeys(fakeOldArguments)).toStrictEqual(['0', '1', '2']);
});
});
});

it('should return an Array', function() {
expect.assertions(1);
expect(Array.isArray(keys)).toBe(true);
});
it('should return an Array', function() {
expect.assertions(1);
expect(Array.isArray(keys)).toBe(true);
});

it('should return names which are own properties', function() {
expect.assertions(7);
keys.forEach(function(name) {
expect(has.call(obj, name)).toBe(true);
it('should return names which are own properties', function() {
expect.assertions(7);
keys.forEach(function(name) {
expect(has.call(obj, name)).toBe(true);
});
});
});

it('should return names which are enumerable', function() {
expect.assertions(7);
keys.forEach(function(name) {
expect(loopedValues.indexOf(name)).not.toBe(-1);
it('should return names which are enumerable', function() {
expect.assertions(7);
keys.forEach(function(name) {
expect(loopedValues.indexOf(name)).not.toBe(-1);
});
});
});

describe('enumerating over non-enumerable properties', function() {
it('has no enumerable keys on a Function', function() {
expect.assertions(1);
describe('enumerating over non-enumerable properties', function() {
it('has no enumerable keys on a Function', function() {
expect.assertions(1);

const Foo = function() {};

const Foo = function() {};
expect(objectKeys(Foo.prototype)).toStrictEqual([]);
});

it('has no enumerable keys on a boolean', function() {
expect.assertions(1);
expect(objectKeys(Boolean.prototype)).toStrictEqual([]);
});

expect(objectKeys(Foo.prototype)).toStrictEqual([]);
it('has no enumerable keys on an object', function() {
expect.assertions(1);
expect(objectKeys(Object.prototype)).toStrictEqual([]);
});
});

it('has no enumerable keys on a boolean', function() {
it('works with boxed primitives', function() {
expect.assertions(1);
expect(objectKeys(Boolean.prototype)).toStrictEqual([]);
expect(objectKeys(Object('hello'))).toStrictEqual(['0', '1', '2', '3', '4']);
});

it('has no enumerable keys on an object', function() {
it('works with boxed primitives with extra properties', function() {
expect.assertions(1);
expect(objectKeys(Object.prototype)).toStrictEqual([]);
const x = Object('x');
x.y = 1;
const actual = objectKeys(x);
const expected = ['0', 'y'];
actual.sort();
expected.sort();
expect(actual).toStrictEqual(expected);
});
});

it('works with boxed primitives', function() {
expect.assertions(1);
expect(objectKeys(Object('hello'))).toStrictEqual(['0', '1', '2', '3', '4']);
});

it('works with boxed primitives with extra properties', function() {
expect.assertions(1);
const x = Object('x');
x.y = 1;
const actual = objectKeys(x);
const expected = ['0', 'y'];
actual.sort();
expected.sort();
expect(actual).toStrictEqual(expected);
});
it('works with regexs', function() {
expect.assertions(1);
const x = /a/g;
const actual = objectKeys(x).sort();
expect(actual).toStrictEqual([]);
});

it('works with regexs', function() {
expect.assertions(1);
const x = /a/g;
const actual = objectKeys(x).sort();
expect(actual).toStrictEqual([]);
});
ifWindowIt('can serialize all objects on the `window`', function() {
expect.assertions(62);
let windowItemKeys;
let exception;
const excludedKeys = [
'window',
'console',
'parent',
'self',
'frame',
'frames',
'frameElement',
'external',
'height',
'width',
];

if (supportsDescriptors) {
Object.defineProperty(window, 'thrower', {
configurable: true,
get() {
throw new RangeError('thrower!');
},
});
}

ifWindowIt('can serialize all objects on the `window`', function() {
expect.assertions(62);
let windowItemKeys;
let exception;
const excludedKeys = [
'window',
'console',
'parent',
'self',
'frame',
'frames',
'frameElement',
'external',
'height',
'width',
];

if (supportsDescriptors) {
Object.defineProperty(window, 'thrower', {
configurable: true,
get() {
throw new RangeError('thrower!');
},
});
}
/* eslint-disable-next-line guard-for-in,no-restricted-syntax */
for (const k in window) {
exception = void 0;
windowItemKeys = exception;

/* eslint-disable-next-line guard-for-in,no-restricted-syntax */
for (const k in window) {
exception = void 0;
windowItemKeys = exception;
if (excludedKeys.indexOf(k) === -1 && has.call(window, k) && window[k] !== null && typeof window[k] === 'object') {
try {
windowItemKeys = objectKeys(window[k]);
} catch (e) {
exception = e;
}

if (excludedKeys.indexOf(k) === -1 && has.call(window, k) && window[k] !== null && typeof window[k] === 'object') {
try {
windowItemKeys = objectKeys(window[k]);
} catch (e) {
exception = e;
expect(Array.isArray(windowItemKeys)).toBe(true);
expect(exception).toBeUndefined();
}

expect(Array.isArray(windowItemKeys)).toBe(true);
expect(exception).toBeUndefined();
}
}

if (supportsDescriptors) {
delete window.thrower;
}
if (supportsDescriptors) {
delete window.thrower;
}
});
});
});
Loading

0 comments on commit 124889c

Please sign in to comment.