From ca4f52b29ce4cbca7c1f5e48e8e367eaf8cbd94a Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 23 Oct 2019 15:08:04 -0400 Subject: [PATCH] test(populate): repro #8230 --- test/model.populate.test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/model.populate.test.js b/test/model.populate.test.js index 0ab15770983..96f7425f2e9 100644 --- a/test/model.populate.test.js +++ b/test/model.populate.test.js @@ -8345,6 +8345,31 @@ describe('model: populate:', function() { }); }); + it('sets populate virtual to empty array if local field empty (gh-8230)', function() { + const GroupSchema = new Schema({ + roles: [{ + roleId: String + }] + }); + GroupSchema.virtual('roles$', { + ref: 'gh8230_Role', + localField: 'roles.roleId', + foreignField: '_id' + }); + + const RoleSchema = new Schema({}); + + const GroupModel = db.model('gh8230_Group', GroupSchema); + db.model('gh8230_Role', RoleSchema); + + return co(function*() { + yield GroupModel.create({ roles: [] }); + + const res = yield GroupModel.findOne({}).populate('roles$'); + assert.deepEqual(res.roles$, []); + }); + }); + it('sets populate virtual with count to 0 if local field empty (gh-7731)', function() { const GroupSchema = new Schema({ roles: [{