Skip to content

Commit

Permalink
test: clone with lean returns lean subdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanHafez committed Apr 4, 2020
1 parent 08592a5 commit d9f2a08
Showing 1 changed file with 48 additions and 17 deletions.
65 changes: 48 additions & 17 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8143,30 +8143,61 @@ describe('model: populate:', function() {
});
});

it('can use clone with lean (gh-8760)', function() {
const blogPostSchema = new Schema({
commentsIds: [{ type: Schema.ObjectId, ref: 'Comment' }]
describe('gh-8760', function() {
it('can use clone with lean', function() {
const blogPostSchema = new Schema({
commentsIds: [{ type: Schema.ObjectId, ref: 'Comment' }]
});

const commentSchema = new Schema({ content: String });

const BlogPost = db.model('BlogPost', blogPostSchema);
const Comment = db.model('Comment', commentSchema);

const comment = new Comment({ content: 'Cool post.' });
const blogPost = new BlogPost({ commentsIds: [comment._id] });

return co(function*() {
yield Promise.all([
blogPost.save(),
comment.save()
]);

const foundBlogPost = yield BlogPost.findOne({ _id: blogPost._id })
.populate({ path: 'commentsIds', options: { clone: true } })
.lean();

assert.equal(foundBlogPost.commentsIds[0].content, 'Cool post.');
});
});

const commentSchema = new Schema({ content: String });
it('clone with populate and lean correctly makes child lean', function() {
const isLean = v => v != null && !(v instanceof mongoose.Document);

const BlogPost = db.model('BlogPost', blogPostSchema);
const Comment = db.model('Comment', commentSchema);
const blogPostSchema = new Schema({
commentsIds: [{ type: Schema.ObjectId, ref: 'Comment' }]
});

const comment = new Comment({ content: 'Cool post.' });
const blogPost = new BlogPost({ commentsIds: [comment._id] });
const commentSchema = new Schema({ content: String });

return co(function*() {
yield Promise.all([
blogPost.save(),
comment.save()
]);
const BlogPost = db.model('BlogPost', blogPostSchema);
const Comment = db.model('Comment', commentSchema);

const comment = new Comment({ content: 'Cool post.' });
const blogPost = new BlogPost({ commentsIds: [comment._id] });

return co(function*() {
yield Promise.all([
blogPost.save(),
comment.save()
]);

const foundBlogPost = yield BlogPost.findOne({ _id: blogPost._id })
.populate({ path: 'commentsIds', options: { clone: true } })
.lean();
const foundBlogPost = yield BlogPost.findOne({ _id: blogPost._id })
.populate({ path: 'commentsIds', options: { clone: true } })
.lean();

assert.equal(foundBlogPost.commentsIds[0].content, 'Cool post.');
assert.ok(isLean(foundBlogPost.commentsIds[0]));
});
});
});

Expand Down

0 comments on commit d9f2a08

Please sign in to comment.