deno.land / x / mongoose@6.7.5 / test / types.subdocument.test.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
/** * Module dependencies. */
'use strict';
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' }] }); let id; return thingy.save(). then(function() { id = thingy._id; }). then(function() { const thingy2 = { subArray: [{ testString: 'Test 2' }] }; return Thing.updateOne({ _id: id }, { $set: thingy2 }); }); });
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')); }); });});
Version Info