Skip to content

Commit

Permalink
Merge pull request #25 from ilovepixelart/feature/test-for-set
Browse files Browse the repository at this point in the history
$setOnInsert
  • Loading branch information
ilovepixelart authored Apr 21, 2023
2 parents e322cd3 + e18d15e commit 21a3418
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 24 deletions.
21 changes: 12 additions & 9 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,16 @@ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: I
isNew: options.upsert && count === 0
}

const keys = _.keys(update).filter((key) => key.startsWith('$'))
if (update && !_.isEmpty(keys)) {
_.forEach(keys, (key) => {
commands.push({ [key]: update[key] })
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete update[key]
})
if (update) {
delete update.$setOnInsert
const keys = _.keys(update).filter((key) => key.startsWith('$'))
if (!_.isEmpty(keys)) {
_.forEach(keys, (key) => {
commands.push({ [key]: update[key] })
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete update[key]
})
}
}

const cursor = this.model.find<HydratedDocument<T>>(filter).cursor()
Expand All @@ -135,7 +138,7 @@ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: I

schema.post(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>, _, next) {
const options = this.getOptions()
if (options.ignoreHook) return
if (options.ignoreHook) return next()

const update = this.getUpdate()

Expand Down Expand Up @@ -204,7 +207,7 @@ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: I

schema.post(deleteMethods as MongooseQueryMiddleware[], options, async function (this: IHookContext<T>, _, next) {
const options = this.getOptions()
if (options.ignoreHook) return
if (options.ignoreHook) return next()

await deletePatch(opts, this._context)

Expand Down
71 changes: 70 additions & 1 deletion tests/plugin-event-created.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ describe('plugin - event created & patch history disabled', () => {
updatedAt: john.updatedAt
})
})

// Check if the document is saved
const found = await User.findOne({})
expect(found).not.toBeNull()
expect(found?.name).toBe('John')
expect(found?.role).toBe('user')
})

it('should create() and emit one create event', async () => {
Expand All @@ -72,14 +78,20 @@ describe('plugin - event created & patch history disabled', () => {
updatedAt: user.updatedAt
})
})

// Check if the document is saved
const found = await User.findOne({})
expect(found).not.toBeNull()
expect(found?.name).toBe('John')
expect(found?.role).toBe('user')
})

it('should insertMany() and emit three create events', async () => {
const users = await User.insertMany([
{ name: 'John', role: 'user' },
{ name: 'Alice', role: 'user' },
{ name: 'Bob', role: 'user' }
])
], { ordered: true })

const [john, alice, bob] = users

Expand Down Expand Up @@ -114,6 +126,21 @@ describe('plugin - event created & patch history disabled', () => {
updatedAt: bob.updatedAt
})
})

// Check if the documents are saved
const found = await User.find({})
expect(found).toHaveLength(3)

const [foundJohn, foundAlice, foundBob] = found

expect(foundJohn.name).toBe('John')
expect(foundJohn.role).toBe('user')

expect(foundAlice.name).toBe('Alice')
expect(foundAlice.role).toBe('user')

expect(foundBob.name).toBe('Bob')
expect(foundBob.role).toBe('user')
})
})

Expand All @@ -140,6 +167,12 @@ describe('plugin - event created & patch history disabled', () => {
// Upsert does not set createdAt and updatedAt
})
})

// Check if the document is saved
const found = await User.findOne({})
expect(found).not.toBeNull()
expect(found?.name).toBe('John')
expect(found?.role).toBe('admin')
})

it('should updateOne() + upsert and emit one create event', async () => {
Expand All @@ -164,6 +197,12 @@ describe('plugin - event created & patch history disabled', () => {
// Upsert does not set createdAt and updatedAt
})
})

// Check if the document is saved
const found = await User.findOne({})
expect(found).not.toBeNull()
expect(found?.name).toBe('John')
expect(found?.role).toBe('admin')
})

it('should replaceOne() + upsert and emit one create event', async () => {
Expand All @@ -188,6 +227,12 @@ describe('plugin - event created & patch history disabled', () => {
// Upsert does not set createdAt and updatedAt
})
})

// Check if the document is saved
const found = await User.findOne({})
expect(found).not.toBeNull()
expect(found?.name).toBe('John')
expect(found?.role).toBe('admin')
})

it('should updateMany() + upsert and emit one create event', async () => {
Expand All @@ -211,6 +256,12 @@ describe('plugin - event created & patch history disabled', () => {
// Upsert does not set createdAt and updatedAt
})
})

// Check if the document is saved
const found = await User.findById(users?._id)
expect(found).not.toBeNull()
expect(found?.name).toBe('Steve')
expect(found?.role).toBe('admin')
})

it('should findOneAndUpdate() + upsert and emit one create event', async () => {
Expand All @@ -235,6 +286,12 @@ describe('plugin - event created & patch history disabled', () => {
// Upsert does not set createdAt and updatedAt
})
})

// Check if the document is saved
const found = await User.findOne({})
expect(found).not.toBeNull()
expect(found?.name).toBe('John')
expect(found?.role).toBe('admin')
})

it('should findOneAndReplace() + upsert and emit one create event', async () => {
Expand All @@ -259,6 +316,12 @@ describe('plugin - event created & patch history disabled', () => {
// Upsert does not set createdAt and updatedAt
})
})

// Check if the document is saved
const found = await User.findOne({})
expect(found).not.toBeNull()
expect(found?.name).toBe('John')
expect(found?.role).toBe('admin')
})

it('should findByIdAndUpdate() + upsert and emit one create event', async () => {
Expand All @@ -279,6 +342,12 @@ describe('plugin - event created & patch history disabled', () => {
role: user?.role
})
})

// Check if the document is saved
const found = await User.findOne({})
expect(found).not.toBeNull()
expect(found?.name).toBe('John')
expect(found?.role).toBe('admin')
})
})
})
105 changes: 91 additions & 14 deletions tests/plugin-event-deleted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ describe('plugin - event delete & patch history disabled', () => {
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
oldDoc: expect.objectContaining(john.toObject(toObjectOptions))
})

// Check if data is deleted
const user = await User.findById(john._id)
expect(user).toBeNull()
})

it('should remove() and emit two delete events', async () => {
Expand All @@ -67,10 +71,6 @@ describe('plugin - event delete & patch history disabled', () => {

await User.remove({ role: 'user' }).exec()

const remaining = await User.find({})

expect(remaining).toHaveLength(1)

const history = await History.find({})
expect(history).toHaveLength(0)

Expand All @@ -81,6 +81,16 @@ describe('plugin - event delete & patch history disabled', () => {
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
oldDoc: expect.objectContaining(alice.toObject(toObjectOptions))
})

// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()

const deletedAlice = await User.findById(alice._id)
expect(deletedAlice).toBeNull()

const remaining = await User.find({})
expect(remaining).toHaveLength(1)
})

it('should remove() and emit one delete event { single: true }', async () => {
Expand All @@ -101,6 +111,13 @@ describe('plugin - event delete & patch history disabled', () => {
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
oldDoc: expect.objectContaining(john.toObject(toObjectOptions))
})

// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()

const remaining = await User.find({})
expect(remaining).toHaveLength(2)
})

it('should findOneAndDelete() and emit one delete event', async () => {
Expand All @@ -121,6 +138,13 @@ describe('plugin - event delete & patch history disabled', () => {
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
oldDoc: expect.objectContaining(john.toObject(toObjectOptions))
})

// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()

const remaining = await User.find({})
expect(remaining).toHaveLength(2)
})

it('should findOneAndRemove() and emit one delete event', async () => {
Expand All @@ -141,6 +165,13 @@ describe('plugin - event delete & patch history disabled', () => {
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
oldDoc: expect.objectContaining(john.toObject(toObjectOptions))
})

// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()

const remaining = await User.find({ name: { $in: ['Alice', 'Bob'] } })
expect(remaining).toHaveLength(2)
})

it('should findByIdAndDelete() and emit one delete event', async () => {
Expand All @@ -161,6 +192,13 @@ describe('plugin - event delete & patch history disabled', () => {
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
oldDoc: expect.objectContaining(john.toObject(toObjectOptions))
})

// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()

const remaining = await User.find({ name: { $in: ['Alice', 'Bob'] } })
expect(remaining).toHaveLength(2)
})

it('should findByIdAndRemove() and emit one delete event', async () => {
Expand All @@ -181,6 +219,13 @@ describe('plugin - event delete & patch history disabled', () => {
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
oldDoc: expect.objectContaining(john.toObject(toObjectOptions))
})

// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()

const remaining = await User.find({ name: { $in: ['Alice', 'Bob'] } })
expect(remaining).toHaveLength(2)
})

it('should deleteOne() and emit one delete event', async () => {
Expand All @@ -201,6 +246,13 @@ describe('plugin - event delete & patch history disabled', () => {
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
oldDoc: expect.objectContaining(john.toObject(toObjectOptions))
})

// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()

const remaining = await User.find({ name: { $in: ['Alice', 'Bob'] } })
expect(remaining).toHaveLength(2)
})

it('should deleteMany() and emit two delete events', async () => {
Expand All @@ -224,6 +276,16 @@ describe('plugin - event delete & patch history disabled', () => {
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
oldDoc: expect.objectContaining(alice.toObject(toObjectOptions))
})

// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()

const deletedAlice = await User.findById(alice._id)
expect(deletedAlice).toBeNull()

const remaining = await User.find({})
expect(remaining).toHaveLength(1)
})

it('should deleteMany() and emit one delete event { single: true }', async () => {
Expand All @@ -244,6 +306,13 @@ describe('plugin - event delete & patch history disabled', () => {
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
oldDoc: expect.objectContaining(john.toObject(toObjectOptions))
})

// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()

const remaining = await User.find({})
expect(remaining).toHaveLength(2)
})

it('should create then delete and emit one delete event', async () => {
Expand All @@ -257,35 +326,43 @@ describe('plugin - event delete & patch history disabled', () => {
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
oldDoc: expect.objectContaining(john.toObject(toObjectOptions))
})

// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()

const remaining = await User.find({})
expect(remaining).toHaveLength(0)
})

it('should ignoreHook option on deleteMany', async () => {
await User.create({ name: 'John', role: 'user' })
const john = await User.create({ name: 'John', role: 'user' })
await User.deleteMany({ role: 'user' }, { ignoreHook: true }).exec()

const history = await History.find({})
expect(history).toHaveLength(0)

expect(em.emit).toHaveBeenCalledTimes(0)

// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()
})

it('should ignoreHook option on deleteOne', async () => {
await User.create({ name: 'John', role: 'user' })
const john = await User.create({ name: 'John', role: 'user' })
await User.deleteOne({ role: 'user' }, { ignoreHook: true }).exec()

const history = await History.find({})
expect(history).toHaveLength(0)

expect(em.emit).toHaveBeenCalledTimes(0)
})

it('should ignoreHook option on updateMany', async () => {
await User.create({ name: 'John', role: 'user' })
await User.updateMany({ role: 'user' }, { role: 'admin' }, { ignoreHook: true }).exec()
// Check if data is deleted
const deletedJohn = await User.findById(john._id)
expect(deletedJohn).toBeNull()

const history = await History.find({})
expect(history).toHaveLength(0)

expect(em.emit).toHaveBeenCalledTimes(0)
const remaining = await User.find({})
expect(remaining).toHaveLength(0)
})
})
Loading

0 comments on commit 21a3418

Please sign in to comment.