-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtypes.subdocument.test.js
112 lines (90 loc) · 2.78 KB
/
types.subdocument.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
/**
* Module dependencies.
*/
'use strict';
const setDocumentTimestamps = require('../lib/helpers/timestamps/setDocumentTimestamps');
const start = require('./common');
const assert = require('assert');
const mongoose = start.mongoose;
const Schema = mongoose.Schema;
/**
* Test.
*/
describe('types.subdocument', function() {
let GrandChildSchema;
let ChildSchema;
let ParentSchema;
let db;
before(function() {
GrandChildSchema = new Schema({
name: String
});
ChildSchema = new Schema({
name: String,
child: GrandChildSchema
});
ParentSchema = new Schema({
name: String,
children: [ChildSchema]
});
mongoose.model('Parent-3589-Sub', ParentSchema);
db = start();
});
after(async function() {
await db.close();
});
it('returns a proper ownerDocument (gh-3589)', function() {
const Parent = mongoose.model('Parent-3589-Sub');
const p = new Parent({
name: 'Parent Parentson',
children: [
{
name: 'Child Parentson',
child: {
name: 'GrandChild Parentson'
}
}
]
});
assert.equal(p._id, p.children[0].child.ownerDocument()._id);
});
it('not setting timestamps in subdocuments', function() {
const Thing = db.model('Test', new Schema({
subArray: [{
testString: String
}]
}, {
timestamps: true
}));
const thingy = new Thing({
subArray: [{
testString: 'Test 1'
}]
});
const now = new Date();
setDocumentTimestamps(thingy, undefined, () => now, 'createdAt', 'updatedAt');
assert.equal(thingy.createdAt.valueOf(), now.valueOf());
assert.equal(thingy.updatedAt.valueOf(), now.valueOf());
assert.strictEqual(thingy.subArray[0].createdAt, undefined);
assert.strictEqual(thingy.subArray[0].updatedAt, undefined);
});
describe('#isModified', function() {
it('defers to parent isModified (gh-8223)', function() {
const childSchema = Schema({ id: Number, text: String });
const parentSchema = Schema({ child: childSchema });
const Model = db.model('Test1', parentSchema);
const doc = new Model({ child: { text: 'foo' } });
assert.ok(doc.isModified('child.id'));
assert.ok(doc.child.isModified('id'));
});
});
it('respects schematype-level minimize (gh-15313)', function() {
const MySubSchema = new Schema({}, { strict: false, _id: false });
const MySchema = new Schema({ myfield: { type: MySubSchema, minimize: false } });
const MyModel = db.model('MyModel', MySchema);
const doc = new MyModel({ myfield: {} });
assert.deepStrictEqual(doc.toObject().myfield, {});
const doc2 = new MyModel({ myfield: { empty: {} } });
assert.deepStrictEqual(doc2.toObject().myfield, { empty: {} });
});
});