index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. var test = require('tape');
  3. var inspect = require('object-inspect');
  4. var v = require('es-value-fixtures');
  5. var forEach = require('for-each');
  6. var has = require('has');
  7. var shimUnscopables = require('../');
  8. var sortSymbols = function (a, b) {
  9. return inspect(a).localeCompare(inspect(b));
  10. };
  11. test('shimUnscopables', function (t) {
  12. t.equal(typeof shimUnscopables, 'function', 'is a function');
  13. forEach(v.nonStrings, function (notNonEmptyString) {
  14. t['throws'](
  15. function () { shimUnscopables(notNonEmptyString); },
  16. TypeError,
  17. inspect(notNonEmptyString) + ' is not a non-empty String'
  18. );
  19. });
  20. t['throws'](
  21. function () { shimUnscopables('x'); },
  22. TypeError,
  23. inspect('x') + ' is not on Array.prototype'
  24. );
  25. t.test('no symbols', { skip: typeof Symbol === 'function' }, function (st) {
  26. st.doesNotThrow(function () { shimUnscopables('forEach'); });
  27. st.end();
  28. });
  29. t.test('symbols, no unscopables', { skip: typeof Symbol !== 'function' || Symbol.unscopables }, function (st) {
  30. st.deepEqual(Object.getOwnPropertySymbols(Array.prototype), [Symbol.iterator]);
  31. shimUnscopables('forEach');
  32. st.deepEqual(Object.getOwnPropertySymbols(Array.prototype), [Symbol.iterator]);
  33. st.end();
  34. });
  35. t.test('Symbol.unscopables', { skip: typeof Symbol !== 'function' || !Symbol.unscopables }, function (st) {
  36. st.deepEqual(
  37. Object.getOwnPropertySymbols(Array.prototype).sort(sortSymbols),
  38. [Symbol.iterator, Symbol.unscopables]
  39. );
  40. st.notOk(has(Array.prototype[Symbol.unscopables], 'forEach'), 'unscopables map lacks forEach');
  41. shimUnscopables('forEach');
  42. st.deepEqual(
  43. Object.getOwnPropertySymbols(Array.prototype).sort(sortSymbols),
  44. [Symbol.iterator, Symbol.unscopables]
  45. );
  46. st.equal(Array.prototype[Symbol.unscopables].forEach, true, 'unscopables map has forEach');
  47. st.end();
  48. });
  49. t.end();
  50. });