Skip to content

Commit

Permalink
test(document): add test case for #14418
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Mar 21, 2024
1 parent 3e63142 commit ed35573
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12476,6 +12476,68 @@ describe('document', function() {
doc.set({ nested: void 0 });
assert.strictEqual(doc.toObject().nested, void 0);
});

it('avoids depopulating populated subdocs underneath document arrays when copying to another document (gh-14418)', async function() {
const cartSchema = new mongoose.Schema({
products: [
{
product: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
},
quantity: Number
}
],
singleProduct: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
}
});
const purchaseSchema = new mongoose.Schema({
products: [
{
product: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
},
quantity: Number
}
],
singleProduct: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
}
});
const productSchema = new mongoose.Schema({
name: String
});

const Cart = db.model('Cart', cartSchema);
const Purchase = db.model('Purchase', purchaseSchema);
const Product = db.model('Product', productSchema);

const dbProduct = await Product.create({ name: 'Bug' });

const dbCart = await Cart.create({
products: [
{
product: dbProduct,
quantity: 2
}
],
singleProduct: dbProduct
});

const foundCart = await Cart.findById(dbCart._id).
populate('products.product singleProduct');

const purchaseFromDbCart = new Purchase({
products: foundCart.products,
singleProduct: foundCart.singleProduct
});
assert.equal(purchaseFromDbCart.products[0].product.name, 'Bug');
assert.equal(purchaseFromDbCart.singleProduct.name, 'Bug');
});
});

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

0 comments on commit ed35573

Please sign in to comment.