UNPKG

2.76 kBJavaScriptView Raw
1'use strict';
2
3var keys = require('object-keys');
4var map = require('array-map');
5var define = require('define-properties');
6
7var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
8
9module.exports = function (entries, t) {
10 var a = {};
11 var b = {};
12 var c = {};
13 var obj = { a: a, b: b, c: c };
14
15 t.deepEqual(entries(obj), [['a', a], ['b', b], ['c', c]], 'basic support');
16 t.deepEqual(entries({ a: a, b: a, c: c }), [['a', a], ['b', a], ['c', c]], 'duplicate entries are included');
17
18 t.test('entries are in the same order as keys', function (st) {
19 var object = { a: a, b: b };
20 object[0] = 3;
21 object.c = c;
22 object[1] = 4;
23 delete object[0];
24 var objKeys = keys(object);
25 var objEntries = map(objKeys, function (key) {
26 return [key, object[key]];
27 });
28 st.deepEqual(entries(object), objEntries, 'entries match key order');
29 st.end();
30 });
31
32 t.test('non-enumerable properties are omitted', { skip: !Object.defineProperty }, function (st) {
33 var object = { a: a, b: b };
34 Object.defineProperty(object, 'c', { enumerable: false, value: c });
35 st.deepEqual(entries(object), [['a', a], ['b', b]], 'non-enumerable property‘s value is omitted');
36 st.end();
37 });
38
39 t.test('inherited properties are omitted', function (st) {
40 var F = function G() {};
41 F.prototype.a = a;
42 var f = new F();
43 f.b = b;
44 st.deepEqual(entries(f), [['b', b]], 'only own properties are included');
45 st.end();
46 });
47
48 t.test('Symbol properties are omitted', { skip: !hasSymbols }, function (st) {
49 var object = { a: a, b: b, c: c };
50 var enumSym = Symbol('enum');
51 var nonEnumSym = Symbol('non enum');
52 object[enumSym] = enumSym;
53 object.d = enumSym;
54 Object.defineProperty(object, nonEnumSym, { enumerable: false, value: nonEnumSym });
55 st.deepEqual(entries(object), [['a', a], ['b', b], ['c', c], ['d', enumSym]], 'symbol properties are omitted');
56 st.end();
57 });
58
59 t.test('not-yet-visited keys deleted on [[Get]] must not show up in output', { skip: !define.supportsDescriptors }, function (st) {
60 var o = { a: 1, b: 2, c: 3 };
61 Object.defineProperty(o, 'a', {
62 get: function () {
63 delete this.b;
64 return 1;
65 }
66 });
67 st.deepEqual(entries(o), [['a', 1], ['c', 3]], 'when "b" is deleted prior to being visited, it should not show up');
68 st.end();
69 });
70
71 t.test('not-yet-visited keys made non-enumerable on [[Get]] must not show up in output', { skip: !define.supportsDescriptors }, function (st) {
72 var o = { a: 'A', b: 'B' };
73 Object.defineProperty(o, 'a', {
74 get: function () {
75 Object.defineProperty(o, 'b', { enumerable: false });
76 return 'A';
77 }
78 });
79 st.deepEqual(entries(o), [['a', 'A']], 'when "b" is made non-enumerable prior to being visited, it should not show up');
80 st.end();
81 });
82};