init
This commit is contained in:
61
tests/unit/models/plugins/paginate.plugin.test.js
Normal file
61
tests/unit/models/plugins/paginate.plugin.test.js
Normal file
@@ -0,0 +1,61 @@
|
||||
const mongoose = require('mongoose');
|
||||
const setupTestDB = require('../../../utils/setupTestDB');
|
||||
const paginate = require('../../../../src/models/plugins/paginate.plugin');
|
||||
|
||||
const projectSchema = mongoose.Schema({
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
});
|
||||
|
||||
projectSchema.virtual('tasks', {
|
||||
ref: 'Task',
|
||||
localField: '_id',
|
||||
foreignField: 'project',
|
||||
});
|
||||
|
||||
projectSchema.plugin(paginate);
|
||||
const Project = mongoose.model('Project', projectSchema);
|
||||
|
||||
const taskSchema = mongoose.Schema({
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
project: {
|
||||
type: mongoose.SchemaTypes.ObjectId,
|
||||
ref: 'Project',
|
||||
required: true,
|
||||
},
|
||||
});
|
||||
|
||||
taskSchema.plugin(paginate);
|
||||
const Task = mongoose.model('Task', taskSchema);
|
||||
|
||||
setupTestDB();
|
||||
|
||||
describe('paginate plugin', () => {
|
||||
describe('populate option', () => {
|
||||
test('should populate the specified data fields', async () => {
|
||||
const project = await Project.create({ name: 'Project One' });
|
||||
const task = await Task.create({ name: 'Task One', project: project._id });
|
||||
|
||||
const taskPages = await Task.paginate({ _id: task._id }, { populate: 'project' });
|
||||
|
||||
expect(taskPages.results[0].project).toHaveProperty('_id', project._id);
|
||||
});
|
||||
|
||||
test('should populate nested fields', async () => {
|
||||
const project = await Project.create({ name: 'Project One' });
|
||||
const task = await Task.create({ name: 'Task One', project: project._id });
|
||||
|
||||
const projectPages = await Project.paginate({ _id: project._id }, { populate: 'tasks.project' });
|
||||
const { tasks } = projectPages.results[0];
|
||||
|
||||
expect(tasks).toHaveLength(1);
|
||||
expect(tasks[0]).toHaveProperty('_id', task._id);
|
||||
expect(tasks[0].project).toHaveProperty('_id', project._id);
|
||||
});
|
||||
});
|
||||
});
|
||||
89
tests/unit/models/plugins/toJSON.plugin.test.js
Normal file
89
tests/unit/models/plugins/toJSON.plugin.test.js
Normal file
@@ -0,0 +1,89 @@
|
||||
const mongoose = require('mongoose');
|
||||
const { toJSON } = require('../../../../src/models/plugins');
|
||||
|
||||
describe('toJSON plugin', () => {
|
||||
let connection;
|
||||
|
||||
beforeEach(() => {
|
||||
connection = mongoose.createConnection();
|
||||
});
|
||||
|
||||
it('should replace _id with id', () => {
|
||||
const schema = mongoose.Schema();
|
||||
schema.plugin(toJSON);
|
||||
const Model = connection.model('Model', schema);
|
||||
const doc = new Model();
|
||||
expect(doc.toJSON()).not.toHaveProperty('_id');
|
||||
expect(doc.toJSON()).toHaveProperty('id', doc._id.toString());
|
||||
});
|
||||
|
||||
it('should remove __v', () => {
|
||||
const schema = mongoose.Schema();
|
||||
schema.plugin(toJSON);
|
||||
const Model = connection.model('Model', schema);
|
||||
const doc = new Model();
|
||||
expect(doc.toJSON()).not.toHaveProperty('__v');
|
||||
});
|
||||
|
||||
it('should remove createdAt and updatedAt', () => {
|
||||
const schema = mongoose.Schema({}, { timestamps: true });
|
||||
schema.plugin(toJSON);
|
||||
const Model = connection.model('Model', schema);
|
||||
const doc = new Model();
|
||||
expect(doc.toJSON()).not.toHaveProperty('createdAt');
|
||||
expect(doc.toJSON()).not.toHaveProperty('updatedAt');
|
||||
});
|
||||
|
||||
it('should remove any path set as private', () => {
|
||||
const schema = mongoose.Schema({
|
||||
public: { type: String },
|
||||
private: { type: String, private: true },
|
||||
});
|
||||
schema.plugin(toJSON);
|
||||
const Model = connection.model('Model', schema);
|
||||
const doc = new Model({ public: 'some public value', private: 'some private value' });
|
||||
expect(doc.toJSON()).not.toHaveProperty('private');
|
||||
expect(doc.toJSON()).toHaveProperty('public');
|
||||
});
|
||||
|
||||
it('should remove any nested paths set as private', () => {
|
||||
const schema = mongoose.Schema({
|
||||
public: { type: String },
|
||||
nested: {
|
||||
private: { type: String, private: true },
|
||||
},
|
||||
});
|
||||
schema.plugin(toJSON);
|
||||
const Model = connection.model('Model', schema);
|
||||
const doc = new Model({
|
||||
public: 'some public value',
|
||||
nested: {
|
||||
private: 'some nested private value',
|
||||
},
|
||||
});
|
||||
expect(doc.toJSON()).not.toHaveProperty('nested.private');
|
||||
expect(doc.toJSON()).toHaveProperty('public');
|
||||
});
|
||||
|
||||
it('should also call the schema toJSON transform function', () => {
|
||||
const schema = mongoose.Schema(
|
||||
{
|
||||
public: { type: String },
|
||||
private: { type: String },
|
||||
},
|
||||
{
|
||||
toJSON: {
|
||||
transform: (doc, ret) => {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
delete ret.private;
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
schema.plugin(toJSON);
|
||||
const Model = connection.model('Model', schema);
|
||||
const doc = new Model({ public: 'some public value', private: 'some private value' });
|
||||
expect(doc.toJSON()).not.toHaveProperty('private');
|
||||
expect(doc.toJSON()).toHaveProperty('public');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user