Skip to content

Commit 7512100

Browse files
committed
test(model): repro #6840
1 parent d78357b commit 7512100

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

test/model.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4852,6 +4852,29 @@ describe('Model', function() {
48524852
});
48534853
});
48544854

4855+
it('run default function with correct this scope in DocumentArray (gh-6840)', function() {
4856+
var schema = new Schema({
4857+
title: String,
4858+
actors: {
4859+
type: [{ name: String, character: String }],
4860+
default: function() {
4861+
// `this` should be root document and has initial data
4862+
if (this.title === 'Passenger') {
4863+
return [
4864+
{ name: 'Jennifer Lawrence', character: 'Aurora Lane' },
4865+
{ name: 'Chris Pratt', character: 'Jim Preston' }
4866+
];
4867+
}
4868+
return [];
4869+
}
4870+
}
4871+
});
4872+
4873+
var Movie = db.model('gh6840', schema);
4874+
var movie = new Movie({ title: 'Passenger'});
4875+
assert.equal(movie.actors.length, 2);
4876+
});
4877+
48554878
describe('3.6 features', function() {
48564879
before(function(done) {
48574880
start.mongodVersion((err, version) => {

0 commit comments

Comments
 (0)