Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 2, 2015
1 parent 301832d commit bd1ceae
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var entries = require('../');
var test = require('tape');

test('as a function', function (t) {
t.test('bad array/this value', function (st) {
st.throws(function () { entries(undefined); }, TypeError, 'undefined is not an object');
st.throws(function () { entries(null); }, TypeError, 'null is not an object');
st.end();
});

require('./tests')(entries, t);

t.end();
});
34 changes: 34 additions & 0 deletions test/shimmed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var entries = require('../');
entries.shim();

var test = require('tape');
var defineProperties = require('define-properties');
var isEnumerable = Object.prototype.propertyIsEnumerable;
var functionsHaveNames = function f() {}.name === 'f';

test('shimmed', function (t) {
t.equal(Object.entries.length, 1, 'Object.entries has a length of 1');
t.test('Function name', { skip: !functionsHaveNames }, function (st) {
st.equal(Object.entries.name, 'entries', 'Object.entries has name "entries"');
st.end();
});

t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
et.equal(false, isEnumerable.call(Object, 'entries'), 'Object.entries is not enumerable');
et.end();
});

var supportsStrictMode = (function () { return typeof this === 'undefined'; }());

t.test('bad object value', { skip: !supportsStrictMode }, function (st) {
st.throws(function () { return Object.entries(undefined); }, TypeError, 'undefined is not an object');
st.throws(function () { return Object.entries(null); }, TypeError, 'null is not an object');
st.end();
});

require('./tests')(Object.entries, t);

t.end();
});
56 changes: 56 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

var keys = require('object-keys');
var map = require('array-map');

var hasSymbols = typeof Symbol === 'function' && typeof Symbol() === 'symbol';

module.exports = function (entries, t) {
var a = {};
var b = {};
var c = {};
var obj = { a: a, b: b, c: c };

t.deepEqual(entries(obj), [['a', a], ['b', b], ['c', c]], 'basic support');
t.deepEqual(entries({ a: a, b: a, c: c }), [['a', a], ['b', a], ['c', c]], 'duplicate entries are included');

t.test('entries are in the same order as keys', function (st) {
var object = { a: a, b: b };
object[0] = 3;
object.c = c;
object[1] = 4;
delete object[0];
var objKeys = keys(object);
var objEntries = map(objKeys, function (key) {
return [key, object[key]];
});
st.deepEqual(entries(object), objEntries, 'entries match key order');
st.end();
});

t.test('non-enumerable properties are omitted', { skip: !Object.defineProperty }, function (st) {
var object = { a: a, b: b };
Object.defineProperty(object, 'c', { enumerable: false, value: c });
st.deepEqual(entries(object), [['a', a], ['b', b]], 'non-enumerable property‘s value is omitted');
st.end();
});

t.test('inherited properties are omitted', function (st) {
var F = function G() {};
F.prototype.a = a;
var f = new F();
f.b = b;
st.deepEqual(entries(f), [['b', b]], 'only own properties are included');
st.end();
});

t.test('Symbols are omitted', { skip: !hasSymbols }, function (st) {
var object = { a: a, b: b, c: c };
var enumSym = Symbol('enum');
var nonEnumSym = Symbol('non enum');
object[enumSym] = enumSym;
Object.defineProperty(object, nonEnumSym, { enumerable: false, value: nonEnumSym });
st.deepEqual(entries(object), [['a', a], ['b', b], ['c', c]], 'symbols are omitted');
st.end();
});
};

0 comments on commit bd1ceae

Please sign in to comment.