Skip to content

Commit ed35573

Browse files
committed
test(document): add test case for #14418
1 parent 3e63142 commit ed35573

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

test/document.test.js

+62
Original file line numberDiff line numberDiff line change
@@ -12476,6 +12476,68 @@ describe('document', function() {
1247612476
doc.set({ nested: void 0 });
1247712477
assert.strictEqual(doc.toObject().nested, void 0);
1247812478
});
12479+
12480+
it('avoids depopulating populated subdocs underneath document arrays when copying to another document (gh-14418)', async function() {
12481+
const cartSchema = new mongoose.Schema({
12482+
products: [
12483+
{
12484+
product: {
12485+
type: mongoose.Schema.Types.ObjectId,
12486+
ref: 'Product'
12487+
},
12488+
quantity: Number
12489+
}
12490+
],
12491+
singleProduct: {
12492+
type: mongoose.Schema.Types.ObjectId,
12493+
ref: 'Product'
12494+
}
12495+
});
12496+
const purchaseSchema = new mongoose.Schema({
12497+
products: [
12498+
{
12499+
product: {
12500+
type: mongoose.Schema.Types.ObjectId,
12501+
ref: 'Product'
12502+
},
12503+
quantity: Number
12504+
}
12505+
],
12506+
singleProduct: {
12507+
type: mongoose.Schema.Types.ObjectId,
12508+
ref: 'Product'
12509+
}
12510+
});
12511+
const productSchema = new mongoose.Schema({
12512+
name: String
12513+
});
12514+
12515+
const Cart = db.model('Cart', cartSchema);
12516+
const Purchase = db.model('Purchase', purchaseSchema);
12517+
const Product = db.model('Product', productSchema);
12518+
12519+
const dbProduct = await Product.create({ name: 'Bug' });
12520+
12521+
const dbCart = await Cart.create({
12522+
products: [
12523+
{
12524+
product: dbProduct,
12525+
quantity: 2
12526+
}
12527+
],
12528+
singleProduct: dbProduct
12529+
});
12530+
12531+
const foundCart = await Cart.findById(dbCart._id).
12532+
populate('products.product singleProduct');
12533+
12534+
const purchaseFromDbCart = new Purchase({
12535+
products: foundCart.products,
12536+
singleProduct: foundCart.singleProduct
12537+
});
12538+
assert.equal(purchaseFromDbCart.products[0].product.name, 'Bug');
12539+
assert.equal(purchaseFromDbCart.singleProduct.name, 'Bug');
12540+
});
1247912541
});
1248012542

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

0 commit comments

Comments
 (0)