From b42d0f5d5642fcdbb0e96581a60b3e93ef28b192 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 24 Sep 2019 15:38:40 -0700 Subject: [PATCH] test(populate): repro #8173 #6488 --- test/model.populate.test.js | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/test/model.populate.test.js b/test/model.populate.test.js index b137d5b8b16..8256c2600c6 100644 --- a/test/model.populate.test.js +++ b/test/model.populate.test.js @@ -8627,4 +8627,50 @@ describe('model: populate:', function() { assert.equal(team.developers[2].ticketCount, 0); }); }); + + it('handles virtual populate underneath embedded discriminator nested path (gh-6488) (gh-8173)', function() { + return co(function*() { + const UserModel = db.model('gh6488_User', Schema({ + employeeId: Number, + name: String + })); + + const eventSchema = Schema({ message: String }, { discriminatorKey: 'kind' }); + const batchSchema = Schema({ nested: { events: [eventSchema] } }); + + const nestedLayerSchema = Schema({ users: [ Number ] }); + nestedLayerSchema.virtual('users_$', { + ref: 'gh6488_User', + localField: 'users', + foreignField: 'employeeId' + }); + + const docArray = batchSchema.path('nested.events'); + const Clicked = docArray. + discriminator('gh6488_Clicked', Schema({ nestedLayer: nestedLayerSchema })); + const Purchased = docArray. + discriminator('gh6488_Purchased', Schema({ purchased: String })); + + const Batch = db.model('gh6488', batchSchema); + + yield UserModel.create({ employeeId: 1, name: 'test' }); + yield Batch.create({ + nested: { + events: [ + { kind: 'gh6488_Clicked', nestedLayer: { users: [1] } }, + { kind: 'gh6488_Purchased', purchased: 'test' } + ] + } + }); + + let res = yield Batch.findOne(). + populate('nested.events.nestedLayer.users_$'); + assert.equal(res.nested.events[0].nestedLayer.users_$.length, 1); + assert.equal(res.nested.events[0].nestedLayer.users_$[0].name, 'test'); + + res = res.toObject({ virtuals: true }); + assert.equal(res.nested.events[0].nestedLayer.users_$.length, 1); + assert.equal(res.nested.events[0].nestedLayer.users_$[0].name, 'test'); + }); + }); });