-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepFreeze.spec.js
More file actions
88 lines (77 loc) · 1.92 KB
/
deepFreeze.spec.js
File metadata and controls
88 lines (77 loc) · 1.92 KB
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
import { deepFreeze } from './index';
const expectFrozen = obj =>
expect(Object.isFrozen(obj)).toBe(true);
const expectNotFrozen = obj =>
expect(Object.isFrozen(obj)).toBe(false);
describe('deepFreeze:', () => {
test('should freeze an object', () => {
// when
const frozen = deepFreeze({ a: 'a' });
// then
expectFrozen(frozen);
});
test('should throw exepction on assignment', () => {
// when
const frozen = deepFreeze({ a: 'a' });
// then
expect(() => { frozen.a = 'b'; })
.toThrow(/Cannot assign to read only property 'a'/);
});
test('should create a copy of the argument', () => {
// given
const passedObj = { a: 'a' };
// when
const frozen = deepFreeze(passedObj);
// then
expect(frozen).not.toBe(passedObj);
});
test('should deeply freeze an object', () => {
// when
const frozen = deepFreeze({ a: { x: 'x' } });
// then
expectFrozen(frozen.a);
});
test('should freeze an array', () => {
// when
const frozen = deepFreeze(['a']);
// then
expectFrozen(frozen);
});
test('should deeply freeze an array', () => {
// when
const frozen = deepFreeze([{ a: 'a' }]);
// then
expectFrozen(frozen[0]);
});
test('should deeply freeze a complex object', () => {
// when
const frozen = deepFreeze({
a: [{
b: [
{ c: {} }
]
}]
});
// then
expectFrozen(frozen.a[0].b[0].c);
});
test('should not freeze instance of a class', () => {
// given
class SomeClass {}
const instance = new SomeClass();
// when
const frozen = deepFreeze(instance);
// then
expectNotFrozen(frozen);
expect(frozen).toBe(instance);
});
test('should not freeze freeze a date', () => {
// given
const date = new Date();
// when
const frozen = deepFreeze(date);
// then
expectNotFrozen(frozen);
expect(frozen).toBe(date);
});
});