forked from hagopj13/node-express-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for populate option in paginate
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |