index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. var test = require('tape');
  3. var isDescriptor = require('../');
  4. var noop = function () {};
  5. test('isDescriptor', function (t) {
  6. t.test('is false when not an object:', function (st) {
  7. st.notOk(isDescriptor('a'));
  8. st.notOk(isDescriptor(null));
  9. st.notOk(isDescriptor([]));
  10. st.end();
  11. });
  12. t.test('returns true if the property exists', function (st) {
  13. var obj = { foo: null };
  14. Object.defineProperty(obj, 'bar', {
  15. value: 'xyz'
  16. });
  17. Object.defineProperty(obj, 'baz', {
  18. get: function () {
  19. return 'aaa';
  20. }
  21. });
  22. st.ok(isDescriptor(obj, 'foo'));
  23. st.ok(isDescriptor(obj, 'bar'));
  24. st.ok(isDescriptor(obj, 'baz'));
  25. st.end();
  26. });
  27. t.test('data descriptor:', function (st) {
  28. st.test('is false when the object has invalid properties:', function (s2t) {
  29. s2t.notOk(isDescriptor({ value: 'foo', get: noop }));
  30. s2t.notOk(isDescriptor({ get: noop, value: noop }));
  31. s2t.end();
  32. });
  33. st.test('is not false when the object has unrecognize properties:', function (s2t) {
  34. s2t.ok(isDescriptor({ value: 'foo', bar: 'baz' }));
  35. s2t.ok(isDescriptor({ value: 'foo', bar: 'baz' }));
  36. s2t.end();
  37. });
  38. st.test('is true when the object has valid properties:', function (s2t) {
  39. s2t.ok(isDescriptor({ value: 'foo' }));
  40. s2t.ok(isDescriptor({ value: noop }));
  41. s2t.end();
  42. });
  43. st.test('is false when a value is not the correct type:', function (s2t) {
  44. s2t.notOk(isDescriptor({ value: 'foo', enumerable: 'foo' }));
  45. s2t.notOk(isDescriptor({ value: 'foo', configurable: 'foo' }));
  46. s2t.notOk(isDescriptor({ value: 'foo', writable: 'foo' }));
  47. s2t.end();
  48. });
  49. st.end();
  50. });
  51. t.test('accessor descriptor:', function (st) {
  52. st.test('should be false when the object has invalid properties:', function (s2t) {
  53. s2t.ok(!isDescriptor({ get: noop, writable: true }));
  54. s2t.ok(!isDescriptor({ get: noop, value: true }));
  55. s2t.end();
  56. });
  57. st.test('is not false when the object has unrecognize properties:', function (s2t) {
  58. s2t.ok(isDescriptor({ get: noop, set: noop, bar: 'baz' }));
  59. s2t.end();
  60. });
  61. st.test('is false when an accessor is not a function:', function (s2t) {
  62. s2t.notOk(isDescriptor({ get: noop, set: 'baz' }));
  63. s2t.notOk(isDescriptor({ get: 'foo', set: noop }));
  64. s2t.notOk(isDescriptor({ get: 'foo', bar: 'baz' }));
  65. s2t.notOk(isDescriptor({ get: 'foo', set: 'baz' }));
  66. s2t.end();
  67. });
  68. st.test('is false when "get" or "set" is not a function', function (s2t) {
  69. s2t.notOk(isDescriptor({ set: 'foo' }));
  70. s2t.notOk(isDescriptor({ get: 'foo' }));
  71. s2t.end();
  72. });
  73. st.test('is true when the object has valid properties:', function (s2t) {
  74. s2t.ok(isDescriptor({ get: noop, set: noop }));
  75. s2t.ok(isDescriptor({ get: noop }));
  76. s2t.end();
  77. });
  78. st.test('is false when a value is not the correct type:', function (s2t) {
  79. s2t.notOk(isDescriptor({ get: noop, set: noop, enumerable: 'foo' }));
  80. s2t.notOk(isDescriptor({ set: noop, configurable: 'foo' }));
  81. s2t.notOk(isDescriptor({ get: noop, configurable: 'foo' }));
  82. s2t.end();
  83. });
  84. st.end();
  85. });
  86. });