UNPKG

@stenneepro/mongoose-soft-delete

Version:
216 lines (215 loc) 12.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const mocha_1 = require("mocha"); const setupModel_1 = __importDefault(require("./utils/setupModel")); const dropModel_1 = __importDefault(require("./utils/dropModel")); const chai_1 = require("chai"); const mongooseExpects_1 = require("./utils/mongooseExpects"); (0, mocha_1.describe)('fetch', function () { let TestModel; before(async function () { TestModel = (0, setupModel_1.default)('TestFetchDelete', { name: String }); }); beforeEach(async function () { await TestModel.create([ { name: 'Obi-Wan Kenobi', deleted: true }, { name: 'Darth Vader' }, { name: 'Luke Skywalker', deleted: true } ]); }); afterEach(async function () { await (0, dropModel_1.default)('TestFetchDelete'); }); (0, mocha_1.describe)('without deleted', function () { it('countDocuments() -> returns 1 document', async function () { const count = await TestModel.countDocuments(); (0, chai_1.expect)(count).to.equal(1); }); it('find() -> returns 1 document', async function () { const items = await TestModel.find(); (0, chai_1.expect)(items).to.have.lengthOf(1); }); it('findOne() -> return non-deleted document', async function () { const item = await TestModel.findOne({ name: 'Darth Vader' }); (0, chai_1.expect)(item).to.not.be.null; }); it('findOne() -> not return deleted document', async function () { const item = await TestModel.findOne({ name: 'Obi-Wan Kenobi' }); (0, chai_1.expect)(item).to.be.null; }); it('findById() -> return non-deleted document', async function () { const pre = await TestModel.findOne({ name: 'Darth Vader' }).orFail(); const item = await TestModel.findById(pre._id); (0, chai_1.expect)(item).to.not.be.null; }); it('findById() -> not return deleted document', async function () { const pre = await TestModel.findOne({ name: 'Obi-Wan Kenobi' }).withDeleted().orFail(); const item = await TestModel.findById(pre._id); (0, chai_1.expect)(item).to.be.null; }); it('findOneAndUpdate() -> find and update non-deleted document', async function () { const doc = await TestModel.findOneAndUpdate({ name: 'Darth Vader' }, { name: 'Darth Vader Test' }); (0, chai_1.expect)(doc).to.not.be.null; }); it('findOneAndUpdate() -> not find and update deleted document', async function () { const doc = await TestModel.findOneAndUpdate({ name: 'Obi-Wan Kenobi' }, { name: 'Obi-Wan Kenobi Test' }); (0, chai_1.expect)(doc).to.be.null; }); it('findByIdAndUpdate() -> find and update non-deleted document', async function () { const pre = await TestModel.findOne({ name: 'Darth Vader' }).orFail(); const doc = await TestModel.findByIdAndUpdate(pre._id, { name: 'Darth Vader Test' }); (0, chai_1.expect)(doc).to.not.be.null; }); it('findByIdAndUpdate() -> not find and update deleted document', async function () { const pre = await TestModel.findOne({ name: 'Obi-Wan Kenobi' }).withDeleted().orFail(); const doc = await TestModel.findByIdAndUpdate(pre._id, { name: 'Obi-Wan Kenobi Test' }); (0, chai_1.expect)(doc).to.be.null; }); it('updateOne() -> updates first non-deleted document', async function () { const result = await TestModel.updateOne({ name: 'Darth Vader' }, { name: 'Darth Vader Test' }); (0, mongooseExpects_1.expectOk)(result); (0, mongooseExpects_1.expectModifiedCount)(result, 1); }); it('updateOne() -> not to update first deleted document', async function () { const result = await TestModel.updateOne({ name: 'Obi-Wan Kenobi' }, { name: 'Obi-Wan Kenobi Test' }); (0, mongooseExpects_1.expectOk)(result); (0, mongooseExpects_1.expectModifiedCount)(result, 0); }); it('updateOne() -> insert new document when deleted', async function () { const result = await TestModel.updateOne({ name: 'Obi-Wan Kenobi' }, { name: 'Obi-Wan Kenobi Test' }, { upsert: true }); (0, mongooseExpects_1.expectOk)(result); (0, mongooseExpects_1.expectUpsertedCount)(result, 1); }); it('updateMany() -> updates non-deleted document', async function () { const result = await TestModel.updateMany({}, { name: 'Luke Skywalker Test' }); (0, mongooseExpects_1.expectOk)(result); (0, mongooseExpects_1.expectModifiedCount)(result, 1); }); }); (0, mocha_1.describe)('only deleted', function () { it('countDocuments() -> returns 2 document', async function () { const count = await TestModel.countDocuments().onlyDeleted(); (0, chai_1.expect)(count).to.equal(2); }); it('find() -> returns 2 document', async function () { const items = await TestModel.find().onlyDeleted(); (0, chai_1.expect)(items).to.have.lengthOf(2); }); it('findOne() -> returns deleted document', async function () { const item = await TestModel.findOne({ name: 'Obi-Wan Kenobi' }).onlyDeleted(); (0, chai_1.expect)(item).to.not.be.null; }); it('findOne() -> not return non-deleted document', async function () { const item = await TestModel.findOne({ name: 'Darth Vader' }).onlyDeleted(); (0, chai_1.expect)(item).to.be.null; }); it('findById() -> not return non-deleted document', async function () { const pre = await TestModel.findOne({ name: 'Darth Vader' }).orFail(); const item = await TestModel.findById(pre._id).onlyDeleted(); (0, chai_1.expect)(item).to.be.null; }); it('findById() -> return deleted document', async function () { const pre = await TestModel.findOne({ name: 'Obi-Wan Kenobi' }).withDeleted().orFail(); const item = await TestModel.findById(pre._id).onlyDeleted(); (0, chai_1.expect)(item).to.not.be.null; }); it('findOneAndUpdate() -> not find and update non-deleted document', async function () { const doc = await TestModel.findOneAndUpdate({ name: 'Darth Vader' }, { name: 'Darth Vader Test' }).onlyDeleted(); (0, chai_1.expect)(doc).to.be.null; }); it('findOneAndUpdate() -> find and update deleted document', async function () { const doc = await TestModel.findOneAndUpdate({ name: 'Obi-Wan Kenobi' }, { name: 'Obi-Wan Kenobi Test' }).onlyDeleted(); (0, chai_1.expect)(doc).to.not.be.null; }); it('findByIdAndUpdate() -> not find and update non-deleted document', async function () { const pre = await TestModel.findOne({ name: 'Darth Vader' }).orFail(); const doc = await TestModel.findByIdAndUpdate(pre._id, { name: 'Darth Vader Test' }).onlyDeleted(); (0, chai_1.expect)(doc).to.be.null; }); it('findByIdAndUpdate() -> find and update deleted document', async function () { const pre = await TestModel.findOne({ name: 'Obi-Wan Kenobi' }).withDeleted().orFail(); const doc = await TestModel.findByIdAndUpdate(pre._id, { name: 'Obi-Wan Kenobi Test' }).onlyDeleted(); (0, chai_1.expect)(doc).to.not.be.null; }); it('updateOne() -> not to update first non-deleted document', async function () { const result = await TestModel.updateOne({ name: 'Darth Vader', deleted: true }, { name: 'Darth Vader Test' }); (0, mongooseExpects_1.expectOk)(result); (0, mongooseExpects_1.expectModifiedCount)(result, 0); }); it('updateOne() -> updates first deleted document', async function () { const result = await TestModel.updateOne({ name: 'Obi-Wan Kenobi', deleted: true }, { name: 'Obi-Wan Kenobi Test' }); (0, mongooseExpects_1.expectOk)(result); (0, mongooseExpects_1.expectModifiedCount)(result, 1); }); it('updateMany() -> updates deleted document', async function () { const result = await TestModel.updateMany({ deleted: true }, { name: 'Luke Skywalker Test' }); (0, mongooseExpects_1.expectOk)(result); (0, mongooseExpects_1.expectModifiedCount)(result, 2); }); }); (0, mocha_1.describe)('with deleted', function () { it('countDocuments() -> return 3 document', async function () { const count = await TestModel.countDocuments().withDeleted(); (0, chai_1.expect)(count).to.equal(3); }); it('find() -> return 3 document', async function () { const items = await TestModel.find().withDeleted(); (0, chai_1.expect)(items.length).to.equal(3); }); it('findOne() -> return deleted document', async function () { const item = await TestModel.findOne({ name: 'Obi-Wan Kenobi' }).withDeleted().orFail(); (0, chai_1.expect)(item).to.not.be.null; (0, chai_1.expect)(item.name).to.exist; }); it('findOne() -> return non-deleted document', async function () { const item = await TestModel.findOne({ name: 'Darth Vader' }).withDeleted(); (0, chai_1.expect)(item).to.not.be.null; }); it('findById() -> return non-deleted document', async function () { const pre = await TestModel.findOne({ name: 'Darth Vader' }).orFail(); const item = await TestModel.findById(pre._id).withDeleted(); (0, chai_1.expect)(item).to.not.be.null; }); it('findById() -> return deleted document', async function () { const pre = await TestModel.findOne({ name: 'Obi-Wan Kenobi' }).withDeleted().orFail(); const item = await TestModel.findById(pre._id).withDeleted(); (0, chai_1.expect)(item).to.not.be.null; }); it('findOneAndUpdate() -> find and update non-deleted document', async function () { const doc = await TestModel.findOneAndUpdate({ name: 'Darth Vader' }, { name: 'Darth Vader Test' }).withDeleted(); (0, chai_1.expect)(doc).to.not.be.null; }); it('findOneAndUpdate() -> find and update deleted document', async function () { const doc = await TestModel.findOneAndUpdate({ name: 'Obi-Wan Kenobi' }, { name: 'Obi-Wan Kenobi Test' }).withDeleted(); (0, chai_1.expect)(doc).to.not.be.null; }); it('findByIdAndUpdate() -> find and update non-deleted document', async function () { const pre = await TestModel.findOne({ name: 'Darth Vader' }).orFail(); const doc = await TestModel.findByIdAndUpdate(pre._id, { name: 'Darth Vader Test' }).withDeleted(); (0, chai_1.expect)(doc).to.not.be.null; }); it('findByIdAndUpdate() -> find and update deleted document', async function () { const pre = await TestModel.findOne({ name: 'Obi-Wan Kenobi' }).withDeleted().orFail(); const doc = await TestModel.findByIdAndUpdate(pre._id, { name: 'Obi-Wan Kenobi Test' }).withDeleted(); (0, chai_1.expect)(doc).to.not.be.null; }); it('updateOne() -> updates first non-deleted document', async function () { const result = await TestModel.updateOne({ name: 'Darth Vader', deleted: { $in: [true, false] } }, { name: 'Darth Vader Test' }); (0, mongooseExpects_1.expectOk)(result); (0, mongooseExpects_1.expectModifiedCount)(result, 1); }); it('updateOne() -> updates first deleted document', async function () { const result = await TestModel.updateOne({ name: 'Obi-Wan Kenobi', deleted: { $in: [true, false] } }, { name: 'Obi-Wan Kenobi Test' }); (0, mongooseExpects_1.expectOk)(result); (0, mongooseExpects_1.expectModifiedCount)(result, 1); }); it('updateMany() -> updates all documents', async function () { const result = await TestModel.updateMany({ deleted: { $in: [true, false] } }, { name: 'Luke Skywalker Test' }); (0, mongooseExpects_1.expectOk)(result); (0, mongooseExpects_1.expectModifiedCount)(result, 3); }); }); });