Skip to content

Commit

Permalink
test(populate): repro #11538
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Apr 16, 2022
1 parent 73d2020 commit fcd9100
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10583,6 +10583,37 @@ describe('model: populate:', function() {
assert.equal(fromDb.author.name, 'John Smithe');
});

it('no-op when populating a single nested subdoc underneath a doc array with no ref (gh-11538) (gh-10856)', async function() {
const userSchema = Schema({ _id: Number, name: String, email: String, friends: [{ type: Number, ref: 'User' }] });
const blogPostSchema = Schema({
title: String,
people: [{
author: {
type: new Schema({ _id: Number, name: String, friends: [{ type: Number, ref: 'User' }] })
// ref: 'User'
}
}]
});

const User = db.model('User', userSchema);
const BlogPost = db.model('BlogPost', blogPostSchema);

await User.create({ _id: 2, name: 'Test', email: 'test@gmail.com' });
await User.create({ _id: 1, name: 'John Smith', email: 'test@gmail.com', friends: [2] });
await BlogPost.create({
title: 'Introduction to Mongoose',
people: [{
author: { _id: 1, name: 'John Smith', friends: [2] }
}]
});

const doc = await BlogPost.findOne().populate({
path: 'people',
populate: [{ path: 'author', populate: 'friends' }]
});
assert.strictEqual(doc.people[0].author.email, undefined);
});

it('supports ref on array containing subdocuments (gh-10856)', async function() {
const userSchema = Schema({ _id: Number, name: String, email: String });
const blogPostSchema = Schema({
Expand Down

0 comments on commit fcd9100

Please sign in to comment.