properties.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. import assert from 'assert';
  2. export function testMethodProperty(object, key, options) {
  3. let desc = Object.getOwnPropertyDescriptor(object, key);
  4. let { enumerable = false, configurable = false, writable = false, length } = options;
  5. assert.ok(desc, `Property ${ key.toString() } exists`);
  6. if (options.get || options.set) {
  7. if (options.get) {
  8. assert.equal(typeof desc.get, 'function', 'Getter is a function');
  9. assert.equal(desc.get.length, 0, 'Getter length is 0');
  10. } else {
  11. assert.equal(desc.get, undefined, 'Getter is undefined');
  12. }
  13. if (options.set) {
  14. assert.equal(typeof desc.set, 'function', 'Setter is a function');
  15. assert.equal(desc.set.length, 1, 'Setter length is 1');
  16. } else {
  17. assert.equal(desc.set, undefined, 'Setter is undefined');
  18. }
  19. } else {
  20. assert.equal(typeof desc.value, 'function', 'Value is a function');
  21. assert.equal(desc.value.length, length, `Function length is ${ length }`);
  22. assert.equal(desc.writable, writable, `Writable property is correct ${ writable }`);
  23. }
  24. assert.equal(desc.enumerable, enumerable, `Enumerable property is ${ enumerable }`);
  25. assert.equal(desc.configurable, configurable, `Configurable property is ${ configurable }`);
  26. }