UNPKG

mongoose-smart-delete

Version:

A Mongoose plugin for implementing soft delete functionality, allowing documents to be marked as deleted without being removed from the database.

54 lines (45 loc) 2.26 kB
const createModel = require('./setup/createModel') const modes = require('./setup/modes') modes.forEach((mode) => { describe(`SoftDelete - findOneAndReplace (mode: ${mode})`, () => { let Model, Document beforeAll(async () => { Model = createModel({name: String}, {mode: mode}) }) beforeEach(async () => { Document = await Model.create({name: 'TestDocument'}) }) afterEach(async () => { await Model.deleteMany({}, {softDelete: false, withDeleted: true}) }) it('Should not update deleted document', async () => { await Document.deleteOne() await Model.findOneAndReplace({_id: Document._id}, {name: 'UpdatedDocument'}) const Updated = await Model.findOne({_id: Document._id}, null, {withDeleted: true}) expect(Updated.name).toBe('TestDocument') }) it('Should update deleted document when using withDeleted option', async () => { await Document.deleteOne() await Model.findOneAndReplace({_id: Document._id}, {name: 'UpdatedDocument'}, {withDeleted: true}) const Updated = await Model.findOne({_id: Document._id}, null, {withDeleted: true}) expect(Updated.name).toBe('UpdatedDocument') }) it('Should update deleted document when chaining withDeleted(true)', async () => { await Document.deleteOne() await Model.findOneAndReplace({_id: Document._id}, {name: 'UpdatedDocument'}).withDeleted(true) const Updated = await Model.findOne({_id: Document._id}, null, {withDeleted: true}) expect(Updated.name).toBe('UpdatedDocument') }) it('Should update deleted document when using onlyDeleted option', async () => { await Document.deleteOne() await Model.findOneAndReplace({_id: Document._id}, {name: 'UpdatedDocument'}, {onlyDeleted: true}) const Updated = await Model.findOne({_id: Document._id}).withDeleted() expect(Updated.name).toBe('UpdatedDocument') }) it('Should not update not deleted document when using onlyDeleted option', async () => { await Model.findOneAndReplace({_id: Document._id}, {name: 'UpdatedDocument'}, {onlyDeleted: true}) const Updated = await Model.findOne({_id: Document._id}) expect(Updated.name).toBe('TestDocument') }) }) })