mongoose-delete-ts
Version:
Mongoose soft delete plugin
75 lines (62 loc) • 2.82 kB
text/typescript
import { Model, Schema, Types } from 'mongoose';
import { Deleted, DeletedMethods, DeletedQueryHelpers, DeletedStaticMethods } from '../source';
import { describe } from 'mocha';
import setupModel from './utils/setupModel';
import dropModel from './utils/dropModel';
import { expect } from 'chai';
type ParentTest = { name: string, child: Types.ObjectId } & Deleted;
type ParentTestQueryHelpers = DeletedQueryHelpers<ParentTest>;
type ParentTestModel = Model<ParentTest, ParentTestQueryHelpers, DeletedMethods> & DeletedStaticMethods<ParentTest, ParentTestQueryHelpers>;
type ChildTest = { name: string } & Deleted;
type ChildTestQueryHelpers = DeletedQueryHelpers<ChildTest>;
type ChildTestModel = Model<ChildTest, ChildTestQueryHelpers, DeletedMethods> & DeletedStaticMethods<ChildTest, ChildTestQueryHelpers>;
describe('population', function() {
let ParentTestModel: ParentTestModel;
let ChildTestModel: ChildTestModel;
before(async function() {
ChildTestModel = setupModel<ChildTest, ChildTestModel>('TestPopulationChildDelete', { name: String });
ParentTestModel = setupModel<ParentTest, ParentTestModel>('TestPopulationParentDelete', {
name: String,
child: { type: Schema.Types.ObjectId, ref: 'TestPopulationChildDelete' }
});
});
beforeEach(async function() {
await ChildTestModel.create(
[
{ name: 'Obi-Wan Kenobi', _id: new Types.ObjectId('53da93b16b4a6670076b16b1'), deleted: true },
{ name: 'Darth Vader', _id: new Types.ObjectId('53da93b16b4a6670076b16b2') },
{ name: 'Luke Skywalker', _id: new Types.ObjectId('53da93b16b4a6670076b16b3'), deleted: true }
]);
await ParentTestModel.create(
[
{ name: 'Student 1', child: new Types.ObjectId('53da93b16b4a6670076b16b1') },
{ name: 'Student 2', child: new Types.ObjectId('53da93b16b4a6670076b16b2') },
{ name: 'Student 3', child: new Types.ObjectId('53da93b16b4a6670076b16b3'), deleted: true }
]);
});
afterEach(async function() {
await dropModel('TestPopulationChildDelete');
await dropModel('TestPopulationParentDelete');
});
it('populate() -> not return deleted sub-document', async function() {
const document = await ParentTestModel
.findOne({ name: 'Student 1' })
.populate({ path: 'child' })
.orFail();
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();
expect(document.child).to.not.be.null;
});
it('populate() -> return deleted sub-document', async function() {
const document = await ParentTestModel
.findOne({ name: 'Student 1' })
.populate({ path: 'child', match: { deleted: { $in: [true, false] } } })
.orFail();
expect(document.child).to.not.be.null;
});
});