Skip to content

Commit 4e900eb

Browse files
committed
test(populate): repro #8247
1 parent d69ab22 commit 4e900eb

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed

test/model.populate.test.js

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8668,26 +8668,70 @@ describe('model: populate:', function() {
86688668
});
86698669
});
86708670

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
8671+
describe('gh-8247', function() {
8672+
let Author;
8673+
let Page;
8674+
8675+
before(function() {
8676+
const authorSchema = Schema({ name: String });
8677+
const subSchema = Schema({
8678+
author: { type: Schema.Types.ObjectId, ref: 'gh8247_Author' },
8679+
comment: String
8680+
});
8681+
const pageSchema = Schema({ title: String, comments: [subSchema] });
8682+
Author = db.model('gh8247_Author', authorSchema);
8683+
Page = db.model('gh8247_Page', pageSchema);
86768684
});
8677-
const pageSchema = Schema({ title: String, comments: [subSchema] });
8678-
const Author = db.model('gh8247_Author', authorSchema);
8679-
const Page = db.model('gh8247_Page', pageSchema);
86808685

8681-
return co(function*() {
8682-
const doc = yield Author.create({ name: 'test author' });
8683-
yield Page.create({ comments: [{ author: doc._id }] });
8686+
this.beforeEach(() => co(function*() {
8687+
yield Author.deleteMany({});
8688+
yield Page.deleteMany({});
8689+
}));
8690+
8691+
it('checking `populated()` on a document array element (gh-8247)', function() {
8692+
return co(function*() {
8693+
const doc = yield Author.create({ name: 'test author' });
8694+
yield Page.create({ comments: [{ author: doc._id }] });
8695+
8696+
const fromDb = yield Page.findOne().populate('comments.author');
8697+
assert.ok(Array.isArray(fromDb.populated('comments.author')));
8698+
assert.equal(fromDb.populated('comments.author').length, 1);
8699+
assert.equal(fromDb.comments[0].author.name, 'test author');
86848700

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');
8701+
assert.ok(fromDb.comments[0].populated('author'));
8702+
});
8703+
});
86898704

8690-
assert.ok(fromDb.comments[0].populated('author'));
8705+
it('updates top-level populated() when pushing elements onto a document array with single populated path (gh-8247)', function() {
8706+
return co(function*() {
8707+
const docs = yield Author.create([
8708+
{ name: 'test1' },
8709+
{ name: 'test2' }
8710+
]);
8711+
yield Page.create({ comments: [{ author: docs[0]._id }] });
8712+
8713+
// Try setting to non-manually populated path...
8714+
let fromDb = yield Page.findOne().populate('comments.author');
8715+
assert.ok(Array.isArray(fromDb.populated('comments.author')));
8716+
assert.equal(fromDb.populated('comments.author').length, 1);
8717+
assert.equal(fromDb.comments[0].author.name, 'test1');
8718+
8719+
fromDb.comments.push({ author: docs[1]._id });
8720+
let pop = fromDb.populated('comments.author');
8721+
assert.equal(pop.length, 2);
8722+
assert.equal(pop[0].toHexString(), docs[0]._id.toHexString());
8723+
assert.equal(pop[1], null);
8724+
8725+
// And try setting to populated path
8726+
fromDb = yield Page.findOne().populate('comments.author');
8727+
assert.ok(Array.isArray(fromDb.populated('comments.author')));
8728+
assert.equal(fromDb.populated('comments.author').length, 1);
8729+
assert.equal(fromDb.comments[0].author.name, 'test1');
8730+
8731+
fromDb.comments.push({ author: docs[1] });
8732+
pop = fromDb.populated('comments.author');
8733+
assert.equal(pop.length, 2);
8734+
});
86918735
});
86928736
});
86938737
});

0 commit comments

Comments
 (0)