mongoose-crudl
Version:
CRUDL operations with Mongoose. (Create, Read, Update, Delete, List)
120 lines (96 loc) • 3.37 kB
JavaScript
import { describe, test, expect, beforeAll, afterAll, afterEach } from 'vitest'
import mongoose from 'mongoose'
import createMongooseMemoryServer from 'mongoose-memory'
import { NotFoundError } from 'standard-api-errors'
import { readOne } from './index.js'
const mongooseMemoryServer = createMongooseMemoryServer(mongoose)
const Test2Model = mongoose.model('Test2', new mongoose.Schema({
name: { type: String, required: true, unique: true }
}, { timestamps: true }))
const TestModel = mongoose.model('Test', new mongoose.Schema({
name: { type: String, required: true },
refId: { type: mongoose.Types.ObjectId, ref: 'Test2', required: false }
}, { timestamps: true }))
describe('readOne', () => {
beforeAll(async () => {
await mongooseMemoryServer.start()
await mongooseMemoryServer.connect('test-db')
})
afterEach(async () => {
await mongooseMemoryServer.purge()
})
afterAll(async () => {
await mongooseMemoryServer.disconnect()
await mongooseMemoryServer.stop()
})
test('Error: Not found by id', async () => {
const test = new TestModel({ name: 'test' })
await test.save()
await TestModel.deleteOne({ _id: test._id })
await expect(readOne(TestModel, { id: test._id }))
.rejects
.toThrow(new NotFoundError(`Test with {"_id":"${test._id.toString()}"} is not found.`))
})
test('Error: Not found by params', async () => {
const refId = new mongoose.Types.ObjectId()
const test = new TestModel({ name: 'test' })
await test.save()
await expect(readOne(TestModel, { refId, id: test._id }))
.rejects
.toThrow(new NotFoundError(`Test with {"_id":"${test._id.toString()}","refId":"${refId}"} is not found.`))
})
test('Success by id', async () => {
const test = new TestModel({ name: 'test' })
await test.save()
const res = await readOne(TestModel, { id: test._id })
expect(res).toEqual({
status: 200,
result: {
__v: 0,
_id: test._id,
name: test.name,
createdAt: test.createdAt,
updatedAt: test.updatedAt
}
})
})
test('Success with populate', async () => {
const test1 = new Test2Model({ name: 'test2' })
await test1.save()
const test = new TestModel({ name: 'test', refId: test1._id })
await test.save()
const res = await readOne(TestModel, { id: test._id }, undefined, 'refId')
expect(res.status).toBe(200)
expect(res.result.refId.name).toBe('test2')
})
test('Success by params', async () => {
const refId = new mongoose.Types.ObjectId()
const test = new TestModel({ name: 'test', refId })
await test.save()
const res = await readOne(TestModel, { refId, id: test._id })
expect(res).toEqual({
status: 200,
result: {
__v: 0,
_id: test._id,
refId: refId,
name: test.name,
createdAt: test.createdAt,
updatedAt: test.updatedAt
}
})
})
test('Success by params with select', async () => {
const refId = new mongoose.Types.ObjectId()
const test = new TestModel({ name: 'test', refId })
await test.save()
const res = await readOne(TestModel, { refId, id: test._id }, { select: { updatedAt: 1 } })
expect(res).toEqual({
status: 200,
result: {
_id: test._id,
updatedAt: test.updatedAt
}
})
})
})