OwnPropertyKeys.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var test = require('tape');
  3. var hasSymbols = require('has-symbols')();
  4. var OwnPropertyKeys = require('../../helpers/OwnPropertyKeys');
  5. var defineProperty = require('./defineProperty');
  6. test('OwnPropertyKeys', function (t) {
  7. t.deepEqual(OwnPropertyKeys({ a: 1, b: 2 }).sort(), ['a', 'b'].sort(), 'returns own string keys');
  8. t.test('Symbols', { skip: !hasSymbols }, function (st) {
  9. var o = { a: 1 };
  10. var sym = Symbol();
  11. o[sym] = 2;
  12. st.deepEqual(OwnPropertyKeys(o), ['a', sym], 'returns own string and symbol keys');
  13. st.end();
  14. });
  15. t.test('non-enumerables', { skip: !defineProperty.oDP }, function (st) {
  16. var o = { a: 1, b: 42, c: NaN };
  17. defineProperty(o, 'b', { enumerable: false, value: 42 });
  18. defineProperty(o, 'c', { enumerable: false, get: function () { return NaN; } });
  19. if (hasSymbols) {
  20. defineProperty(o, 'd', { enumerable: false, value: true });
  21. defineProperty(o, 'e', { enumerable: false, get: function () { return true; } });
  22. }
  23. st.deepEqual(
  24. OwnPropertyKeys(o).sort(),
  25. (hasSymbols ? ['a', 'b', 'c', 'd', 'e'] : ['a', 'b', 'c']).sort(),
  26. 'returns non-enumerable own keys, including accessors and symbols if available'
  27. );
  28. st.end();
  29. });
  30. t.end();
  31. });