File: test.js

package info (click to toggle)
node-define-property 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 196 kB
  • sloc: makefile: 14; sh: 5
file content (113 lines) | stat: -rw-r--r-- 2,748 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
'use strict';

require('mocha');
var assert = require('assert');
var define = require('./');

describe('define', function() {
  it('should define a property and make it non-enumerable:', function() {
    var obj = {};
    define(obj, 'foo', function(val) {
      return val.toUpperCase();
    });

    assert.deepEqual(obj, {});
    assert.equal(obj.foo('bar'), 'BAR');
  });

  it('should allow any arbitrary value to be assigned:', function() {
    var obj = {};
    define(obj, 'foo', null);
    define(obj, 'bar');
    define(obj, 'baz', {});
    assert.equal(obj.foo, null);
    assert.equal(obj.bar, undefined);
    assert.deepEqual(obj.baz, {});
  });

  it('should define a property with accessor descriptors:', function() {
    var obj = {bar: 'baz'};
    define(obj, 'foo', {
      configurable: true,
      get: function() {
        return this._val;
      },
      set: function(key) {
        define(this, '_val', this[key]);
      }
    });
    obj.foo = 'bar';
    assert.equal(obj.foo, 'baz');
  });

  it('should define a property on a function', function() {
    function fixture() {}
    fixture.bar = 'baz';
    define(fixture, 'foo', {
      configurable: true,
      set: function(key) {
        define(this, '_val', this[key]);
      },
      get: function() {
        return this._val;
      }
    });
    fixture.foo = 'bar';
    assert.equal(fixture.foo, 'baz');
  });

  it('should define a property on an array', function() {
    var fixture = [];
    fixture.bar = 'baz';
    define(fixture, 'foo', {
      configurable: true,
      set: function(key) {
        define(this, '_val', this[key]);
      },
      get: function() {
        return this._val;
      }
    });
    fixture.foo = 'bar';
    assert.equal(fixture.foo, 'baz');
  });

  it('should define a property on an error', function() {
    var fixture = new Error('fixture error');
    fixture.bar = 'baz';
    define(fixture, 'foo', {
      configurable: true,
      set: function(key) {
        define(this, '_val', this[key]);
      },
      get: function() {
        return this._val;
      }
    });
    fixture.foo = 'bar';
    assert.equal(fixture.foo, 'baz');
  });

  it('should define a property with data descriptors:', function() {
    var obj = {};
    define(obj, 'foo', {
      writable: true,
      value: 'bar'
    });
    assert.equal(obj.foo, 'bar');
  });

  it('should throw an error when invalid args are passed:', function() {
    assert.throws(function() {
      define(null, 'foo');
    }, /expected an object, function, or array/);

    assert.throws(function() {
      define();
    }, /expected an object, function, or array/);

    assert.throws(function() {
      define({});
    }, /expected "key" to be a string/);
  });
});