deno.land / x / mongoose@6.7.5 / test / helpers / 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
'use strict';
const assert = require('assert');const clone = require('../../lib/helpers/clone');const symbols = require('../../lib/helpers/symbols');const ObjectId = require('../../lib/types/objectid');const Decimal = require('../../lib/types/decimal128');
describe('clone', () => { describe('falsy', () => { it('is null when null', () => { assert.deepStrictEqual(clone(null), null); });
it('is false when false', () => { assert.deepStrictEqual(clone(false), false); });
it('is undefined when undefined', () => { assert.deepStrictEqual(clone(undefined), undefined); });
it('is 0 when 0', () => { assert.deepStrictEqual(clone(0), 0); }); });
describe('Array', () => { it('clones first level', () => { const base = [1, 2]; const cloned = clone(base); assert.deepStrictEqual(cloned, base); cloned[0] = 2; assert.deepStrictEqual(base, [1, 2]); assert.deepStrictEqual(cloned, [2, 2]); });
it('clones deeper', () => { const base = [0, [1], { 2: 2 }]; const cloned = clone(base); assert.deepStrictEqual(cloned, base); cloned[0] = 1; cloned[1][0] = 2; cloned[2][2] = 3; assert.deepStrictEqual(cloned, [1, [2], { 2: 3 }]); assert.deepStrictEqual(base, [0, [1], { 2: 2 }]); }); });
describe('mongoose object', () => { it('use toObject', () => { const base = { $__: true, myAttr: 'myAttrVal', toObject() { const obj = JSON.parse(JSON.stringify(base)); obj.toObject = base.toObject; return obj; } }; const cloned = clone(base); assert.deepStrictEqual(cloned, base); cloned.myAttr = 'otherAttrVal'; assert.equal(base.myAttr, 'myAttrVal'); assert.equal(cloned.myAttr, 'otherAttrVal'); });
it('use toJSON', () => { const base = { $__: true, myAttr: 'myAttrVal', toJSON: () => JSON.stringify({ $__: true, myAttr: 'myAttrVal' }) }; const cloned = JSON.parse(clone(base, { json: true })); assert.equal(cloned.myAttr, 'myAttrVal'); cloned.myAttr = 'otherAttrVal'; assert.equal(base.myAttr, 'myAttrVal'); assert.equal(cloned.myAttr, 'otherAttrVal'); });
it('skipSingleNestedGetters', () => { const baseOpts = { _skipSingleNestedGetters: true, $isSingleNested: true }; const base = { $__: true, myAttr: 'myAttrVal', $isSingleNested: true, toObject(cloneOpts) { assert.deepStrictEqual( Object.assign({}, baseOpts, { getters: false }), cloneOpts ); const obj = JSON.parse(JSON.stringify(base)); obj.toObject = base.toObject; return obj; } }; const cloned = clone(base, baseOpts); assert.deepStrictEqual(cloned, base); cloned.myAttr = 'otherAttrVal'; assert.equal(base.myAttr, 'myAttrVal'); assert.equal(cloned.myAttr, 'otherAttrVal'); }); });
describe('global objects', () => { describe('constructor is Object', () => { it('!minimize || isArrayChild', () => { const base = { myAttr: 'myAttrVal' }; const cloned = clone(base); assert.deepStrictEqual(cloned, base); cloned.myAttr = 'otherAttrVal'; assert.equal(base.myAttr, 'myAttrVal'); assert.equal(cloned.myAttr, 'otherAttrVal'); });
it('!constructor && !minimize || isArrayChild', () => { const base = Object.create(null); base.myAttr = 'myAttrVal'; const cloned = clone(base); assert.equal(base.myAttr, cloned.myAttr); cloned.myAttr = 'otherAttrVal'; assert.equal(base.myAttr, 'myAttrVal'); assert.equal(cloned.myAttr, 'otherAttrVal'); });
it('minimize && !isArrayChild && hasKey', () => { const base = { myAttr: 'myAttrVal', otherAttr: undefined, prototype: 'p' }; const cloned = clone(base, { minimize: true }, true); assert.equal(base.myAttr, cloned.myAttr); assert.deepStrictEqual(Object.keys(base), ['myAttr', 'otherAttr', 'prototype']); assert.deepStrictEqual(Object.keys(cloned), ['myAttr']); });
it('minimize and !isArrayChild && !hasKey', () => { const base = { otherAttr: undefined, prototype: 'p' }; const cloned = clone(base, { minimize: true }, false); assert.equal(cloned, null); }); });
describe('constructor is Data', () => { it('return new equal date ', () => { const base = new Date(); const cloned = clone(base); assert.deepStrictEqual(base, cloned); }); });
describe('constructor is RegExp', () => { it('return new equal date ', () => { const base = new RegExp(/A-Z.*/g); base.lastIndex = 2; const cloned = clone(base); assert.deepStrictEqual(base, cloned); assert.ok(base.lastIndex === cloned.lastIndex); }); }); });
describe('mongo object', () => { it('is instance of ObjectId', () => { const base = new ObjectId(); const cloned = clone(base); assert.deepStrictEqual(base, cloned); }); });
describe('schema type', () => { it('have schemaTypeSymbol property', () => { const base = { myAttr: 'myAttrVal', [symbols.schemaTypeSymbol]: 'MyType', clone() { return { myAttr: this.myAttr }; } }; const cloned = clone(base); assert.deepStrictEqual(base.myAttr, cloned.myAttr); }); });
describe('bson', () => { it('Decimal128', () => { const base = { _bsontype: 'Decimal128', toString() { return '128'; } }; base.constructor = undefined; const cloned = clone(base); const expected = Decimal.fromString(base.toString()); assert.deepStrictEqual(cloned, expected); });
it('Decimal128 (flatternDecimal)', () => { const base = { _bsontype: 'Decimal128', toJSON() { return 128; } }; base.constructor = undefined; const cloned = clone(base, { flattenDecimals: true }); assert.deepStrictEqual(cloned, base.toJSON()); });
it('does nothing', () => { class BeeSon { constructor() { this.myAttr = 'myAttrVal'; } toBSON() {} } const base = new BeeSon(); const cloned = clone(base, { bson: true }); assert.equal(base.myAttr, cloned.myAttr); cloned.myAttr = 'otherAttrVal'; assert.equal(base.myAttr, 'otherAttrVal'); assert.equal(cloned.myAttr, 'otherAttrVal'); }); });
describe('wrapper', () => {
});
describe('any else', () => { it('valueOf', () => { let called = false; class Wrapper { constructor(myAttr) { this.myAttr = myAttr; } valueOf() { called = true; return new Wrapper(this.myAttr); } } const base = new Wrapper('myAttrVal'); const cloned = clone(base); assert.ok(called); assert.deepStrictEqual(cloned, base); cloned.myAttr = 'otherAttrVal'; assert.equal(base.myAttr, 'myAttrVal'); assert.equal(cloned.myAttr, 'otherAttrVal'); });
it('cloneObject', () => { class CloneMe { constructor(myAttr) { this.myAttr = myAttr; } } const base = new CloneMe('myAttrVal'); base.valueOf = undefined; const cloned = clone(base); assert.equal(base.myAttr, cloned.myAttr); cloned.myAttr = 'otherAttrVal'; assert.equal(base.myAttr, 'myAttrVal'); assert.equal(cloned.myAttr, 'otherAttrVal'); // I can't say it's expected behavior, but is how it's behave. assert.equal(typeof cloned, 'object'); assert.equal(cloned.constructor, Object); }); });});
mongoose

Version Info

Tagged at
a year ago