Skip to content

Commit 6356c5b

Browse files
committed
test: add test case for #14840
1 parent f87c811 commit 6356c5b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

test/document.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13843,6 +13843,36 @@ describe('document', function() {
1384313843
assert.strictEqual(requiredCalls[0], doc.config.prop);
1384413844
assert.strictEqual(requiredCalls[1], doc.config.prop);
1384513845
});
13846+
13847+
it('applies toObject() getters to 3 level deep subdocuments (gh-14840) (gh-14835)', async function() {
13848+
// Define nested schemas
13849+
const Level3Schema = new mongoose.Schema({
13850+
property: {
13851+
type: String,
13852+
get: (value) => value ? value.toUpperCase() : value
13853+
}
13854+
});
13855+
13856+
const Level2Schema = new mongoose.Schema({ level3: Level3Schema });
13857+
const Level1Schema = new mongoose.Schema({ level2: Level2Schema });
13858+
const MainSchema = new mongoose.Schema({ level1: Level1Schema });
13859+
const MainModel = db.model('Test', MainSchema);
13860+
13861+
const doc = await MainModel.create({
13862+
level1: {
13863+
level2: {
13864+
level3: {
13865+
property: 'testValue'
13866+
}
13867+
}
13868+
}
13869+
});
13870+
13871+
// Fetch and convert the document to an object with getters applied
13872+
const result = await MainModel.findById(doc._id);
13873+
const objectWithGetters = result.toObject({ getters: true, virtuals: false });
13874+
assert.strictEqual(objectWithGetters.level1.level2.level3.property, 'TESTVALUE');
13875+
});
1384613876
});
1384713877

1384813878
describe('Check if instance function that is supplied in schema option is available', function() {

0 commit comments

Comments
 (0)