File: test.js

package info (click to toggle)
node-is-accessor-descriptor 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 112 kB
  • sloc: makefile: 4; sh: 2
file content (117 lines) | stat: -rw-r--r-- 2,692 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';

require('mocha');
var assert = require('assert');
var isAccessor = require('./');
var noop = function() {};

describe('isAccessor', function() {
  it('should be false when not an object', function() {
    assert(!isAccessor('a'));
    assert(!isAccessor(null));
    assert(!isAccessor([]));
  });

  it('should be false when the property has data descriptor properties', function() {
    var obj = {
      foo: {
        writable: true,
        enumerable: true,
        configurable: true,
        get: noop,
        set: noop
      }
    };
    assert(!isAccessor(obj, 'foo'));
  });

  it('should be true when the property is a valid getter/setter', function() {
    const obj = {get foo() {}};
    assert(isAccessor(obj, 'foo'));
    assert(isAccessor(Object.getOwnPropertyDescriptor(obj, 'foo')));
  });

  it('should check ctor.prototype', function() {
    class Foo {
      get bar() {
        return 'baz';
      }
    }
    var obj = new Foo();
    assert(isAccessor(obj, 'bar'));
  });

  it('should not check ctor.prototype when disabled', function() {
    class Foo {
      get bar() {
        return 'baz';
      }
    }
    var obj = new Foo();
    assert(!isAccessor(obj, 'bar', false));
  });

  it('should be false when get or set are not functions', function() {
    assert(!isAccessor({
      configurable: true,
      enumerable: true,
      set: 'baz',
      get: noop
    }));

    assert(!isAccessor({
      configurable: true,
      enumerable: true,
      set: noop,
      get: 'foo'
    }));

    assert(!isAccessor({
      configurable: true,
      enumerable: true,
      bar: 'baz',
      get: 'foo'
    }));

    assert(!isAccessor({
      configurable: true,
      enumerable: true,
      set: 'baz',
      get: 'foo'
    }));

    assert(!isAccessor({
      get: 'foo',
      enumerable: true,
      configurable: true
    }));
  });

  it('should be false when the object lacks necessary properties', function() {
    assert(!isAccessor({ set: noop }));
    assert(!isAccessor({ get: noop, set: noop }));
    assert(!isAccessor({ get: noop }));
  });

  it('should be false when invalid properties are defined', function() {
    assert(!isAccessor({
      enumerable: true,
      configurable: true,
      get: noop,
      foo: true
    }));

    assert(!isAccessor({
      enumerable: true,
      configurable: true,
      get: noop,
      bar: true,
    }));
  });

  it('should be false when a value is not the correct type', function() {
    assert(!isAccessor({ get: noop, set: noop, enumerable: 'foo' }));
    assert(!isAccessor({ set: noop, configurable: 'foo' }));
    assert(!isAccessor({ get: noop, configurable: 'foo' }));
  });
});