Skip to content

Commit 8ddfc0a

Browse files
committed
test(populate): repro #8247
1 parent df31656 commit 8ddfc0a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

test/model.populate.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -8667,4 +8667,27 @@ describe('model: populate:', function() {
86678667
assert.equal(foo2.children[0].bar.name, 'bar');
86688668
});
86698669
});
8670+
8671+
it('checking `populated()` on a document array element (gh-8247)', function() {
8672+
const authorSchema = Schema({ name: String });
8673+
const subSchema = Schema({
8674+
author: { type: Schema.Types.ObjectId, ref: 'gh8247_Author' },
8675+
comment: String
8676+
});
8677+
const pageSchema = Schema({ title: String, comments: [subSchema] });
8678+
const Author = db.model('gh8247_Author', authorSchema);
8679+
const Page = db.model('gh8247_Page', pageSchema);
8680+
8681+
return co(function*() {
8682+
const doc = yield Author.create({ name: 'test author' });
8683+
yield Page.create({ comments: [{ author: doc._id }] });
8684+
8685+
const fromDb = yield Page.findOne().populate('comments.author');
8686+
assert.ok(Array.isArray(fromDb.populated('comments.author')));
8687+
assert.equal(fromDb.populated('comments.author').length, 1);
8688+
assert.equal(fromDb.comments[0].author.name, 'test author');
8689+
8690+
assert.ok(fromDb.comments[0].populated('author'));
8691+
});
8692+
});
86708693
});

0 commit comments

Comments
 (0)