Skip to content

Commit

Permalink
test: add test case for #15138
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 4, 2025
1 parent 5dcca7e commit ffc77a6
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -14190,4 +14235,3 @@ describe('Check if instance function that is supplied in schema option is availa
assert.equal(TestDocument.instanceFn(), 'Returned from DocumentInstanceFn');
});
});

0 comments on commit ffc77a6

Please sign in to comment.