-
Notifications
You must be signed in to change notification settings - Fork 1
/
object_immutable.test.js
113 lines (98 loc) · 3.58 KB
/
object_immutable.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
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
import mutate from '../index';
describe('mutate', () => {
describe( 'should return the same object', () => {
test('when changes are empty array', () => {
const obj = { a: 1 };
const changes = [];
const result = mutate(obj, changes);
expect(result).toBe(obj);
});
test('when changes are empty object', () => {
const obj = { a: 1 };
const changes = {};
const result = mutate(obj, changes);
expect(result).toBe(obj);
});
test('when new value is equal of current value', () => {
const obj = { a: 1 };
const changes = { a: 1 };
const result = mutate(obj, changes);
expect(result).toBe(obj);
});
test('when changes contains instruction to remove undefined elements', () => {
const obj = { a: 1 };
const changes = { b: undefined };
const result = mutate(obj, changes);
expect(result).toBe(obj);
});
test('when new value is equal of current value', () => {
const obj = { a: 1 };
const changes = { a: 1, b: undefined };
const result = mutate(obj, changes);
expect(result).toBe(obj);
});
test('when new value is equal of current value in deep path', () => {
const obj = { a: { aa: [1,2,3] } };
const changes = { 'a.aa.[2]': 3 };
const result = mutate(obj, changes);
expect(result).toBe(obj);
});
test('when new value is equal of current value in deep path', () => {
const obj = { a: { aa: { aaa: 35 } } };
const changes = { 'a.aa.aaa': 35 };
const result = mutate(obj, changes);
expect(result).toBe(obj);
});
});
describe( 'should return new object', () => {
test('when changes contains new value as array', () => {
const obj = { a: 1 };
const changes = [['a', 15]];
const result = mutate(obj, changes);
expect(result).not.toBe(obj);
expect(result).toEqual({ a: 15 });
});
test('when changes contains new value as object', () => {
const obj = { a: 1 };
const changes = { a: 30 };
const result = mutate(obj, changes);
expect(result).not.toBe(obj);
expect(result).toEqual({ a: 30 });
});
test('when changes contains new value', () => {
const obj = { a: 1 };
const changes = { a: 1, b: 7 };
const result = mutate(obj, changes);
expect(result).not.toBe(obj);
expect(result).toEqual({ a: 1, b: 7 });
});
test('when changes contains instruction to remove defined prop', () => {
const obj = { a: 1 };
const changes = { a: undefined };
const result = mutate(obj, changes);
expect(result).not.toBe(obj);
expect(result).toEqual({ });
});
test('when changes contains instruction to remove defined prop', () => {
const obj = { a: { aa: { aaa: 35 } } };
const changes = { 'a.aa.aaa': undefined };
const result = mutate(obj, changes);
expect(result).not.toBe(obj);
expect(result).toEqual({ a: { aa: { } } });
});
test('when new value is not equal of current value in deep path', () => {
const obj = { a: { aa: [1,2,3] } };
const changes = { 'a.aa.[2]': 7 };
const result = mutate(obj, changes);
expect(result).not.toBe(obj);
expect(result).toEqual({ a: { aa: [1,2,7] } });
});
test('when new value is not equal of current value in deep path', () => {
const obj = { a: { aa: { aaa: 35 } } };
const changes = { 'a.aa.aaa': 99 };
const result = mutate(obj, changes);
expect(result).not.toBe(obj);
expect(result).toEqual({ a: { aa: { aaa: 99 } } });
});
});
});