forked from apollographql/apollo-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloneDeep.ts
56 lines (47 loc) · 1.88 KB
/
cloneDeep.ts
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
import { cloneDeep } from '../src/util/cloneDeep';
import { assert } from 'chai';
describe('cloneDeep', () => {
it('will clone primitive values', () => {
assert.equal(cloneDeep(undefined), undefined);
assert.equal(cloneDeep(null), null);
assert.equal(cloneDeep(true), true);
assert.equal(cloneDeep(false), false);
assert.equal(cloneDeep(-1), -1);
assert.equal(cloneDeep(+1), +1);
assert.equal(cloneDeep(0.5), 0.5);
assert.equal(cloneDeep('hello'), 'hello');
assert.equal(cloneDeep('world'), 'world');
});
it('will clone objects', () => {
const value1 = {};
const value2 = { a: 1, b: 2, c: 3 };
const value3 = { x: { a: 1, b: 2, c: 3 }, y: { a: 1, b: 2, c: 3 } };
const clonedValue1 = cloneDeep(value1);
const clonedValue2 = cloneDeep(value2);
const clonedValue3 = cloneDeep(value3);
assert.deepEqual(clonedValue1, value1);
assert.deepEqual(clonedValue2, value2);
assert.deepEqual(clonedValue3, value3);
assert.notStrictEqual(clonedValue1, value1);
assert.notStrictEqual(clonedValue2, value2);
assert.notStrictEqual(clonedValue3, value3);
assert.notStrictEqual(clonedValue3.x, value3.x);
assert.notStrictEqual(clonedValue3.y, value3.y);
});
it('will clone arrays', () => {
const value1: Array<number> = [];
const value2 = [1, 2, 3];
const value3 = [[1, 2, 3], [1, 2, 3]];
const clonedValue1 = cloneDeep(value1);
const clonedValue2 = cloneDeep(value2);
const clonedValue3 = cloneDeep(value3);
assert.deepEqual(clonedValue1, value1);
assert.deepEqual(clonedValue2, value2);
assert.deepEqual(clonedValue3, value3);
assert.notStrictEqual(clonedValue1, value1);
assert.notStrictEqual(clonedValue2, value2);
assert.notStrictEqual(clonedValue3, value3);
assert.notStrictEqual(clonedValue3[0], value3[0]);
assert.notStrictEqual(clonedValue3[1], value3[1]);
});
});