-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclone.test.js
186 lines (155 loc) · 4.46 KB
/
clone.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
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
import { clone, cloneDeep } from './clone'
describe('Test clone', () => {
it('clone should be defined', () => {
expect(clone).toBeDefined()
})
it('clone undefined', () => {
expect(clone(undefined)).toBeUndefined()
})
it('clone null', () => {
expect(clone(null)).toBeNull()
})
it('clone boolean', () => {
expect(clone(true)).toBeTruthy()
expect(clone(new Boolean(true)).valueOf()).toBeTruthy()
})
it('clone number', () => {
expect(clone(1)).toBe(1)
expect(clone(new Number(1)).valueOf()).toBe(1)
expect(clone(NaN)).toBeNaN()
expect(clone(Infinity)).toBe(Infinity)
})
it('clone string', () => {
expect(clone('foo')).toBe('foo')
expect(clone(new String('foo')).valueOf()).toBe('foo')
})
it('clone NaN', () => {
expect(clone(NaN)).toBeNaN()
})
it('clone Infinity', () => {
expect(clone(Infinity)).toBe(Infinity)
})
it('clone array', () => {
const o = [1, '2', { foo: 3 }]
const cloned = clone(o)
expect(cloned !== o).toBeTruthy()
expect(cloned[0] === o[0]).toBeTruthy()
expect(cloned[1] === o[1]).toBeTruthy()
expect(cloned[2] === o[2]).toBeTruthy()
})
it('clone arguments', () => {
function cloneArguments() {
return clone(arguments)
}
expect(cloneArguments()).toStrictEqual([])
expect(cloneArguments(1, 2, 3)).toStrictEqual([1, 2, 3])
})
describe('clone object', () => {
function staticExpect() {
expect(cloned).not.toBe(o)
expect(cloned.a === o.a).toBeTruthy()
expect(cloned.b === o.b).toBeTruthy()
expect(cloned.c === o.c).toBeTruthy()
}
let o, cloned
beforeEach(() => {
o = { a: 1, b: { foo: 'bar' }, c: [1, 2, 3] }
cloned = null
})
it('clone object with [[proto]]', () => {
cloned = clone(o)
staticExpect()
})
it('clone object without [[proto]]', () => {
const npo = Object.create(null)
npo.a = o.a
npo.b = o.b
npo.c = o.c
cloned = clone(npo)
staticExpect()
})
})
it('clone Class instance', () => {
function Foo() {}
const o = new Foo()
const cloned = clone(o)
expect(cloned !== o).toBeTruthy()
expect(cloned instanceof Foo).toBeTruthy()
})
it('clone Date', () => {
const o = new Date()
expect(clone(o) !== o).toBeTruthy()
expect(clone(o).getTime() === o.getTime()).toBeTruthy()
})
it('clone Set', () => {
const obj = { a: 1 }
const o = new Set([1, '2', obj])
const cloned = clone(o)
expect(cloned !== o).toBeTruthy()
expect(cloned.has(1)).toBeTruthy()
expect(cloned.has('2')).toBeTruthy()
expect(cloned.has(obj)).toBeTruthy()
})
it('clone Map', () => {
const obj = { a: 1 }
const o = new Map()
o.set('foo', 'bar')
o.set('obj', obj)
const cloned = clone(o)
expect(cloned !== o).toBeTruthy()
expect(cloned.get('foo')).toBe('bar')
expect(cloned.get('obj')).toBe(obj)
})
it('clone function', () => {
function foo() {}
expect(clone(foo)).toBe(foo)
})
it('cloneDeep string', () => {
expect(cloneDeep('foo')).toBe('foo')
expect(cloneDeep(new String('foo')).valueOf()).toBe('foo')
})
it('cloneDeep array', () => {
const o = [1, '2', { foo: 3 }]
const cloned = cloneDeep(o)
expect(cloned !== o).toBeTruthy()
expect(cloned[0] === o[0]).toBeTruthy()
expect(cloned[1] === o[1]).toBeTruthy()
expect(cloned[2] === o[2]).toBeTruthy()
})
describe('cloneDeep object', () => {
function staticExpect() {
expect(cloned).not.toBe(o)
expect(cloned.a === o.a).toBeTruthy()
expect(cloned.b !== o.b).toBeTruthy()
expect(cloned.b.obj !== o.b.obj).toBeTruthy()
expect(cloned.b.obj).toStrictEqual(o.b.obj)
expect(cloned.c !== o.c).toBeTruthy()
expect(cloned.c).toStrictEqual(o.c)
}
let o, cloned
beforeEach(() => {
o = { a: 1, b: { obj: { bar: 'baz' } }, c: [1, 2, 3] }
cloned = null
})
it('cloneDeep object with [[proto]]', () => {
cloned = cloneDeep(o)
staticExpect()
})
it('cloneDepp object without [[proto]]', () => {
const npo = Object.create(null)
npo.a = o.a
npo.b = o.b
npo.c = o.c
cloned = cloneDeep(npo)
staticExpect()
})
it('cloneDeep object with circular ref', () => {
o.b.arr = o.c
o.d = o.b.obj
cloned = cloneDeep(o)
staticExpect()
expect(cloned.b.arr === cloned.c).toBeTruthy()
expect(cloned.d === cloned.b.obj).toBeTruthy()
})
})
})