forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.create.null.test.js
143 lines (113 loc) · 2.72 KB
/
object.create.null.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
/**
* Test dependencies.
*/
'use strict';
const start = require('./common');
const assert = require('assert');
const mongoose = start.mongoose;
const Schema = mongoose.Schema;
let schema;
describe('is compatible with object created using Object.create(null) (gh-1484)', function() {
let db;
let M;
before(function() {
schema = new Schema({
a: String,
b: {
c: Number,
d: [{ e: String }]
},
f: { g: Date },
h: {}
});
});
before(function() {
db = start();
M = db.model('Test', schema);
});
after(function(done) {
db.close(done);
});
it('during construction', function(done) {
assert.doesNotThrow(function() {
new M(Object.create(null));
});
assert.doesNotThrow(function() {
const o = Object.create(null);
o.b = Object.create(null);
new M(o);
});
assert.doesNotThrow(function() {
const o = Object.create(null);
o.b = Object.create(null);
o.b.c = 9;
const e = Object.create(null);
e.e = 'hi i am a string';
o.b.d = [e];
const date = new Date;
const f = Object.create(null);
f.g = date;
o.f = f;
const h = Object.create(null);
h.ad = 1;
h.hoc = 2;
h.obj = Object.create(null);
o.h = h;
const m = new M(o);
assert.equal(m.b.c, 9);
assert.equal(m.b.d[0].e, 'hi i am a string');
assert.equal(date, m.f.g);
assert.equal(m.h.ad, 1);
assert.equal(m.h.hoc, 2);
assert.deepEqual({}, m.h.obj);
});
done();
});
it('with .set(path, obj)', function(done) {
const m = new M;
const b = Object.create(null);
b.c = 9;
m.set('b', b);
const ee = Object.create(null);
ee.e = 'hi i am a string';
const e = [ee];
m.set('b.d', e);
const date = new Date;
const f = Object.create(null);
f.g = date;
m.set('f', f);
const thing = Object.create(null);
thing.h = 'yes';
m.set('h.obj.thing', thing);
assert.equal(m.b.c, 9);
assert.equal(m.b.d[0].e, 'hi i am a string');
assert.equal(date, m.f.g);
assert.deepEqual('yes', m.h.obj.thing.h);
done();
});
it('with schema', function(done) {
const o = Object.create(null);
o.name = String;
o.created = Date;
o.nested = Object.create(null);
o.nested.n = Number;
void function() {
new Schema(o);
}();
void function() {
const s = new Schema;
const o = Object.create(null);
o.yay = Number;
s.path('works', o);
}();
void function() {
const s = new Schema;
let o = Object.create(null);
o = {};
o.name = String;
const x = { type: [o] };
s.path('works', x);
}();
done();
});
});