UNPKG

@stenneepro/mongoose-soft-delete

Version:
59 lines (58 loc) 2.83 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const mongoose_1 = require("mongoose"); const mocha_1 = require("mocha"); const setupModel_1 = __importDefault(require("./utils/setupModel")); const dropModel_1 = __importDefault(require("./utils/dropModel")); const chai_1 = require("chai"); (0, mocha_1.describe)('population', function () { let ParentTestModel; let ChildTestModel; before(async function () { ChildTestModel = (0, setupModel_1.default)('TestPopulationChildDelete', { name: String }); ParentTestModel = (0, setupModel_1.default)('TestPopulationParentDelete', { name: String, child: { type: mongoose_1.Schema.Types.ObjectId, ref: 'TestPopulationChildDelete' } }); }); beforeEach(async function () { await ChildTestModel.create([ { name: 'Obi-Wan Kenobi', _id: new mongoose_1.Types.ObjectId('53da93b16b4a6670076b16b1'), deleted: true }, { name: 'Darth Vader', _id: new mongoose_1.Types.ObjectId('53da93b16b4a6670076b16b2') }, { name: 'Luke Skywalker', _id: new mongoose_1.Types.ObjectId('53da93b16b4a6670076b16b3'), deleted: true } ]); await ParentTestModel.create([ { name: 'Student 1', child: new mongoose_1.Types.ObjectId('53da93b16b4a6670076b16b1') }, { name: 'Student 2', child: new mongoose_1.Types.ObjectId('53da93b16b4a6670076b16b2') }, { name: 'Student 3', child: new mongoose_1.Types.ObjectId('53da93b16b4a6670076b16b3'), deleted: true } ]); }); afterEach(async function () { await (0, dropModel_1.default)('TestPopulationChildDelete'); await (0, dropModel_1.default)('TestPopulationParentDelete'); }); it('populate() -> not return deleted sub-document', async function () { const document = await ParentTestModel .findOne({ name: 'Student 1' }) .populate({ path: 'child' }) .orFail(); (0, chai_1.expect)(document.child).to.be.null; }); it('populate() -> return non-deleted sub-document', async function () { const document = await ParentTestModel .findOne({ name: 'Student 2' }) .populate({ path: 'child' }) .orFail(); (0, chai_1.expect)(document.child).to.not.be.null; }); it('populate() -> return deleted sub-document withDeleted=true', async function () { const document = await ParentTestModel .findOne({ name: 'Student 1' }) .populate({ path: 'child', options: { ignoreDeleted: true } }) .orFail(); (0, chai_1.expect)(document.child).to.not.be.null; }); });