-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_spec.js
59 lines (47 loc) · 1.6 KB
/
index_spec.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
55
56
57
58
59
// @flow
'use strict';
var simpleDeepFreeze = require('./index');
describe('simpleDeepFreeze', function() {
it('deep freezes a simple object structure and returns it', function() {
var object = {
someBoolean: true,
someNumber: 10,
someString: 'hello!',
someObject: { foo: 'bar', anotherObject: { nested: 'inObject' } },
someArray: [false, 20, 'bye!', { nested: 'inArray' }],
};
expect(simpleDeepFreeze(object)).toEqual(object);
expect(Object.isFrozen(object)).toEqual(true);
expect(Object.isFrozen(object.someObject)).toEqual(true);
expect(Object.isFrozen(object.someObject.anotherObject)).toEqual(true);
expect(Object.isFrozen(object.someArray)).toEqual(true);
object.someArray.forEach(function(value) {
expect(Object.isFrozen(value)).toEqual(true);
});
});
it('does not let you mutate objects deep down', function() {
var object = {
someObject: { foo: 'bar' },
};
expect(simpleDeepFreeze(object)).toEqual(object);
expect(function() {
object.someObject.foo = 'baz';
}).toThrow();
});
it('does not recurse down when an object is already frozen', function() {
var object = {
someObject: { nested: 'inObject' },
};
Object.freeze(object);
simpleDeepFreeze(object);
expect(Object.isFrozen(object.someObject)).toEqual(false);
});
it('errors for objects that are not plain old Javascript objects', function() {
expect(function() {
simpleDeepFreeze(Buffer);
}).toThrow();
expect(function() {
simpleDeepFreeze({ someBuffer: Buffer });
}).toThrow();
});
});