From ffc77a637d263adc2c0eb360786b79b9b3838377 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 4 Jan 2025 13:21:27 -0500 Subject: [PATCH] test: add test case for #15138 --- test/document.test.js | 46 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/test/document.test.js b/test/document.test.js index dc55d1f10d..4317795223 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -3090,6 +3090,51 @@ describe('document', function() { assert.strictEqual(nestedModel.isNew, false); }); + it('manual populattion with ref function (gh-15138)', async function() { + const userSchema = new mongoose.Schema({ + username: { type: String }, + phone: { type: mongoose.Schema.Types.ObjectId, ref: 'phones' } + }); + + const userSchemaWithFunc = new mongoose.Schema({ + username: { type: String }, + phone: { + type: mongoose.Schema.Types.ObjectId, + ref: function() { + return 'phones'; + } + } + }); + + const phoneSchema = new mongoose.Schema({ + phoneNumber: { type: String }, + extension: { type: String } + }); + + const User = db.model('users', userSchema); + const UserWithFunc = db.model('usersWithFunc', userSchemaWithFunc); + const Phone = db.model('phones', phoneSchema); + + const phone = await Phone.create({ + phoneNumber: '123456789', + extension: '123' + }); + + const user = await User.create({ + username: 'John Doe', + phone + }); + + const userWithFunc = await UserWithFunc.create({ + username: 'John Doe', + phone + }); + + assert.ok(user.populated('phone')); + assert.ok(userWithFunc.populated('phone')); + assert.equal(userWithFunc.phone.extension, '123'); + }); + it('manual population with refPath (gh-7070)', async function() { const ChildModelSchema = new mongoose.Schema({ name: String @@ -14190,4 +14235,3 @@ describe('Check if instance function that is supplied in schema option is availa assert.equal(TestDocument.instanceFn(), 'Returned from DocumentInstanceFn'); }); }); -