/
deepCyclicCopyReplaceable.test.ts
141 lines (118 loc) · 3.88 KB
/
deepCyclicCopyReplaceable.test.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
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import deepCyclicCopyReplaceable from '../deepCyclicCopyReplaceable';
test('returns the same value for primitive or function values', () => {
const fn = () => {};
expect(deepCyclicCopyReplaceable(undefined)).toBe(undefined);
expect(deepCyclicCopyReplaceable(null)).toBe(null);
expect(deepCyclicCopyReplaceable(true)).toBe(true);
expect(deepCyclicCopyReplaceable(42)).toBe(42);
expect(Number.isNaN(deepCyclicCopyReplaceable(NaN))).toBe(true);
expect(deepCyclicCopyReplaceable('foo')).toBe('foo');
expect(deepCyclicCopyReplaceable(fn)).toBe(fn);
});
test('convert accessor descriptor into value descriptor', () => {
const obj = {
set foo(_) {},
get foo() {
return 'bar';
},
};
expect(Object.getOwnPropertyDescriptor(obj, 'foo')).toEqual({
configurable: true,
enumerable: true,
get: expect.any(Function),
set: expect.any(Function),
});
const copy = deepCyclicCopyReplaceable(obj);
expect(Object.getOwnPropertyDescriptor(copy, 'foo')).toEqual({
configurable: true,
enumerable: true,
value: 'bar',
writable: true,
});
});
test('shuold not skips non-enumerables', () => {
const obj = {};
Object.defineProperty(obj, 'foo', {enumerable: false, value: 'bar'});
const copy = deepCyclicCopyReplaceable(obj);
expect(Object.getOwnPropertyDescriptors(copy)).toEqual({
foo: {
configurable: true,
enumerable: false,
value: 'bar',
writable: true,
},
});
});
test('copies symbols', () => {
const symbol = Symbol('foo');
const obj = {[symbol]: 42};
expect(deepCyclicCopyReplaceable(obj)[symbol]).toBe(42);
});
test('copies arrays as array objects', () => {
const array = [null, 42, 'foo', 'bar', [], {}];
expect(deepCyclicCopyReplaceable(array)).toEqual(array);
expect(Array.isArray(deepCyclicCopyReplaceable(array))).toBe(true);
});
test('handles cyclic dependencies', () => {
const cyclic: any = {a: 42, subcycle: {}};
cyclic.subcycle.baz = cyclic;
cyclic.bar = cyclic;
expect(() => deepCyclicCopyReplaceable(cyclic)).not.toThrow();
const copy = deepCyclicCopyReplaceable(cyclic);
expect(copy.a).toBe(42);
expect(copy.bar).toEqual(copy);
expect(copy.subcycle.baz).toEqual(copy);
});
test('Copy Map', () => {
const map = new Map([
['a', 1],
['b', 2],
]);
const copy = deepCyclicCopyReplaceable(map);
expect(copy).toEqual(map);
expect(copy.constructor).toBe(Map);
});
test('Copy cyclic Map', () => {
const map: Map<any, any> = new Map([
['a', 1],
['b', 2],
]);
map.set('map', map);
expect(deepCyclicCopyReplaceable(map)).toEqual(map);
});
test('return same value for built-in object type except array, map and object', () => {
const date = new Date();
const buffer = Buffer.from('jest');
const numberArray = new Uint8Array([1, 2, 3]);
const regexp = /jest/;
const set = new Set(['foo', 'bar']);
expect(deepCyclicCopyReplaceable(date)).toBe(date);
expect(deepCyclicCopyReplaceable(buffer)).toBe(buffer);
expect(deepCyclicCopyReplaceable(numberArray)).toBe(numberArray);
expect(deepCyclicCopyReplaceable(regexp)).toBe(regexp);
expect(deepCyclicCopyReplaceable(set)).toBe(set);
});
test('should copy object symbol key property', () => {
const symbolKey = Symbol.for('key');
expect(deepCyclicCopyReplaceable({[symbolKey]: 1})).toEqual({[symbolKey]: 1});
});
test('should set writable, configurable to true', () => {
const a = {};
Object.defineProperty(a, 'key', {
configurable: false,
enumerable: true,
value: 1,
writable: false,
});
const copied = deepCyclicCopyReplaceable(a);
expect(Object.getOwnPropertyDescriptors(copied)).toEqual({
key: {configurable: true, enumerable: true, value: 1, writable: true},
});
});