-
Notifications
You must be signed in to change notification settings - Fork 671
Expand file tree
/
Copy pathutils.object.tests.js
More file actions
223 lines (181 loc) · 7.43 KB
/
Copy pathutils.object.tests.js
File metadata and controls
223 lines (181 loc) · 7.43 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const objectUtils = require('core/utils/object');
QUnit.test('orderEach', function(assert) {
const checkOrderEach = function(mapKeys, keys) {
let i;
const map = {};
for(i = 0; i < mapKeys.length; i++) {
map[mapKeys[i]] = i;
}
mapKeys = [];
objectUtils.orderEach(map, function(key, value) {
mapKeys.push(key);
assert.strictEqual(map[key], value, 'key value');
});
assert.deepEqual(mapKeys, keys, 'keys order');
};
checkOrderEach(['', 1, '100', 12, -5, 'test', 3, undefined, null], ['-5', '1', '3', '12', '100', '', 'null', 'test', 'undefined']);
});
// T396670
QUnit.test('orderEach when there is custom method in prototype of the array', function(assert) {
/* eslint-disable no-extend-native */
// arrange
const array = [1, 2, 3];
const keys = [];
Array.prototype.add = function(item) {
this[this.length] = item;
};
// act
objectUtils.orderEach(array, function(key, value) {
keys.push(key);
});
// assert
assert.deepEqual(keys, ['0', '1', '2'], 'keys order');
delete Array.prototype.add;
});
QUnit.module('Object cloning', {
beforeEach: function() {
this.SomeClass = function(a, b) {
this.a = a;
this.b = b;
this.changeB = function(b) { this.b = b; };
};
this.source = new this.SomeClass('a', 'b');
}
});
QUnit.test('Prototypical cloning', function(assert) {
// act
const clone = objectUtils.clone(this.source);
// assert
assert.ok(clone);
assert.ok(clone instanceof this.SomeClass);
assert.notEqual(clone, this.source);
assert.equal(clone.a, 'a');
assert.equal(clone.b, 'b');
});
QUnit.test('External source changes affect clone', function(assert) {
// arrange
const clone = objectUtils.clone(this.source);
// act
this.source.a = 'aa';
// assert
assert.equal(this.source.a, 'aa');
assert.equal(clone.a, 'aa');
});
QUnit.test('External clone changes don\'t affect source', function(assert) {
// arrange
const clone = objectUtils.clone(this.source);
// act
clone.a = 'aa';
// assert
assert.equal(this.source.a, 'a');
assert.equal(clone.a, 'aa');
});
QUnit.test('Internal source changes affect clone', function(assert) {
// arrange
const clone = objectUtils.clone(this.source);
// act
this.source.changeB(15);
// assert
assert.equal(this.source.b, 15);
assert.equal(clone.b, 15);
});
QUnit.test('Internal clone changes don\'t affect source', function(assert) {
// arrange
const clone = objectUtils.clone(this.source);
// act
clone.changeB([]);
// assert
assert.equal(this.source.b, 'b');
assert.deepEqual(clone.b, []);
});
QUnit.module('deepExtendArraySafe utility', {
beforeEach: function() {
this.SomeClass = function(simpleProp, toChange) {
this.simpleProp = simpleProp;
this.toChange = toChange;
};
}
});
QUnit.test('deepExtendArraySafe utility does not change complex \'object\' to plain \'object\' by default', function(assert) {
const target = {
deepProp: new this.SomeClass('simple value', 'value to be changed')
};
const changes = {
deepProp: { toChange: 'changed value' }
};
const result = objectUtils.deepExtendArraySafe(target, changes);
assert.equal(result.deepProp.simpleProp, undefined);
assert.equal(result.deepProp.toChange, 'changed value');
});
QUnit.test('deepExtendArraySafe utility can extend complex \'object\' by plain \'object\' (T482160)', function(assert) {
const target = {
deepProp: new this.SomeClass('simple value', 'value to be changed')
};
const changes = {
deepProp: { toChange: 'changed value' }
};
const result = objectUtils.deepExtendArraySafe(target, changes, true);
assert.equal(result.deepProp.simpleProp, 'simple value');
assert.equal(result.deepProp.toChange, 'changed value');
});
QUnit.test('deepExtendArraySafe utility could not extend complex \'object\' by another complex \'object\' ', function(assert) {
const oldValue = {
deepProp: new this.SomeClass('some value', 'missed value')
};
const newValue = {
deepProp: new this.SomeClass('new value')
};
const result = objectUtils.deepExtendArraySafe(oldValue, newValue, true);
assert.equal(result.deepProp.simpleProp, 'new value');
assert.notOk(!!result.deepProp.toChange);
});
QUnit.test('deepExtendArraySafe utility does not throw an error with \'null\' deep property', function(assert) {
const oldValue = {
deepProp: null
};
const newValue = {
deepProp: { toChange: 'changed value' }
};
const result = objectUtils.deepExtendArraySafe(oldValue, newValue, true);
assert.equal(result.deepProp.toChange, 'changed value');
});
QUnit.test('deepExtendArraySafe sets undefined', function(assert) {
const objWithValue = { time: { duration: 50 } };
const objNoValue = {};
objectUtils.deepExtendArraySafe(objWithValue, { time: { duration: undefined } }, true);
objectUtils.deepExtendArraySafe(objNoValue, { time: { duration: undefined } }, true);
assert.equal(objWithValue.time.duration, 50);
assert.notOk(Object.prototype.hasOwnProperty.call(objNoValue.time, 'duration'));
objectUtils.deepExtendArraySafe(objWithValue, { time: { duration: undefined } }, true, false, true);
objectUtils.deepExtendArraySafe(objNoValue, { time: { duration: undefined } }, true, false, true);
assert.equal(objWithValue.time.duration, undefined);
assert.ok(Object.prototype.hasOwnProperty.call(objWithValue.time, 'duration'));
assert.ok(Object.prototype.hasOwnProperty.call(objNoValue.time, 'duration'));
});
QUnit.test('deepExtendArraySafe doesn\'t set undefined if shouldCopyUndefined == false', function(assert) {
const objWithValue = { time: { duration: 50 } };
const objNoValue = {};
objectUtils.deepExtendArraySafe(objWithValue, { time: { duration: undefined } }, true, false, false);
objectUtils.deepExtendArraySafe(objNoValue, { time: { duration: undefined } }, true, false, false);
assert.equal(objWithValue.time.duration, 50);
assert.notOk(Object.prototype.hasOwnProperty.call(objNoValue.time, 'duration'));
});
QUnit.test('deepExtendArraySafe copies array into object property deeply', function(assert) {
const objWithValue = { time: undefined };
const complexTime = { complexTime: 2 };
const timeArray = [1, complexTime, 3];
objectUtils.deepExtendArraySafe(objWithValue, { time: timeArray }, true, false, false, objectUtils.newAssign);
assert.deepEqual(objWithValue.time, timeArray);
assert.notStrictEqual(objWithValue.time, timeArray);
assert.notStrictEqual(objWithValue.time[1], complexTime);
timeArray[0] = 5;
assert.deepEqual(objWithValue.time, [1, complexTime, 3]);
});
QUnit.test('deepExtendArraySafe preserves custom object instances in arrays', function(assert) {
const CustomClass = function(value) { this.value = value; };
const target = { items: [new CustomClass(1), new CustomClass(2)] };
const changes = { items: [new CustomClass(3), new CustomClass(4)] };
const result = objectUtils.deepExtendArraySafe(target, changes, true, false, false, objectUtils.newAssign);
assert.ok(result.items[0] instanceof CustomClass, 'First item preserves custom type');
assert.ok(result.items[1] instanceof CustomClass, 'Second item preserves custom type');
});