From 8ddfc0a563049370ee8e3adb5a8ea3841037da28 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 20 Oct 2019 10:54:06 -0400 Subject: [PATCH] test(populate): repro #8247 --- test/model.populate.test.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/model.populate.test.js b/test/model.populate.test.js index 699973d53f..57c7821de2 100644 --- a/test/model.populate.test.js +++ b/test/model.populate.test.js @@ -8667,4 +8667,27 @@ describe('model: populate:', function() { assert.equal(foo2.children[0].bar.name, 'bar'); }); }); + + it('checking `populated()` on a document array element (gh-8247)', function() { + const authorSchema = Schema({ name: String }); + const subSchema = Schema({ + author: { type: Schema.Types.ObjectId, ref: 'gh8247_Author' }, + comment: String + }); + const pageSchema = Schema({ title: String, comments: [subSchema] }); + const Author = db.model('gh8247_Author', authorSchema); + const Page = db.model('gh8247_Page', pageSchema); + + return co(function*() { + const doc = yield Author.create({ name: 'test author' }); + yield Page.create({ comments: [{ author: doc._id }] }); + + const fromDb = yield Page.findOne().populate('comments.author'); + assert.ok(Array.isArray(fromDb.populated('comments.author'))); + assert.equal(fromDb.populated('comments.author').length, 1); + assert.equal(fromDb.comments[0].author.name, 'test author'); + + assert.ok(fromDb.comments[0].populated('author')); + }); + }); });