/
test.js
54 lines (47 loc) · 1.58 KB
/
test.js
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
'use strict';
var assert = require('assert');
var makeObjConst = require('./index').makeObjConst;
var makePropConst = require('./index').makePropConst;
describe('makeObjConst()', function() {
it('Should make an object\'s properties constant.', function() {
var obj = {
a: 111,
b: '222',
c: { d: 333 }
};
// make the object properties constant
makeObjConst(obj);
try {
obj.a = 222; // this will throw
} catch (err) {
assert.ok(err.message === 'Cannot assign to read only property \'a\' of #<Object>');
}
assert.ok(obj.a === 111);
try {
obj.b = 'Hmm'; // this will throw
} catch (err) {
assert.ok(err.message === 'Cannot assign to read only property \'b\' of #<Object>');
}
assert.ok(obj.b === '222');
try {
obj.c.d = true;
} catch(err) {
assert.ok(err.message === 'Cannot assign to read only property \'d\' of #<Object>');
}
assert.ok(obj.c.d === 333);
});
});
describe('makePropConst()', function() {
it('Should make a single property constant.', function() {
var obj = { alpha: '0', beta: false };
makePropConst(obj, 'alpha');
try {
obj.alpha = 678;
} catch(err) {
assert.ok(err.message === 'Cannot assign to read only property \'alpha\' of #<Object>');
}
assert.ok(obj.alpha === '0');
obj.beta = true;
assert.ok(obj.beta === true);
});
});