shimmed.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. var assign = require('../');
  3. assign.shim();
  4. var test = require('tape');
  5. var defineProperties = require('define-properties');
  6. var isEnumerable = Object.prototype.propertyIsEnumerable;
  7. var functionsHaveNames = require('functions-have-names')();
  8. var runTests = require('./tests');
  9. test('shimmed', function (t) {
  10. t.equal(Object.assign.length, 2, 'Object.assign has a length of 2');
  11. t.test('Function name', { skip: !functionsHaveNames }, function (st) {
  12. st.equal(Object.assign.name, 'assign', 'Object.assign has name "assign"');
  13. st.end();
  14. });
  15. t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
  16. et.equal(false, isEnumerable.call(Object, 'assign'), 'Object.assign is not enumerable');
  17. et.end();
  18. });
  19. var supportsStrictMode = (function () { return typeof this === 'undefined'; }());
  20. t.test('bad object value', { skip: !supportsStrictMode }, function (st) {
  21. st['throws'](function () { return Object.assign(undefined); }, TypeError, 'undefined is not an object');
  22. st['throws'](function () { return Object.assign(null); }, TypeError, 'null is not an object');
  23. st.end();
  24. });
  25. // v8 in node 0.8 and 0.10 have non-enumerable string properties
  26. var stringCharsAreEnumerable = isEnumerable.call('xy', 0);
  27. t.test('when Object.assign is present and has pending exceptions', { skip: !stringCharsAreEnumerable || !Object.preventExtensions }, function (st) {
  28. /*
  29. * Firefox 37 still has "pending exception" logic in its Object.assign implementation,
  30. * which is 72% slower than our shim, and Firefox 40's native implementation.
  31. */
  32. var thrower = Object.preventExtensions({ 1: '2' });
  33. var error;
  34. try { Object.assign(thrower, 'xy'); } catch (e) { error = e; }
  35. st.equal(error instanceof TypeError, true, 'error is TypeError');
  36. st.equal(thrower[1], '2', 'thrower[1] === "2"');
  37. st.end();
  38. });
  39. runTests(Object.assign, t);
  40. t.end();
  41. });