Skip to content

Commit

Permalink
Added test case to validate populate functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
samkit5495 committed Dec 25, 2020
1 parent 9cf9535 commit 18e8f79
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ The `options` param can have the following (optional) fields:

```javascript
const options = {
populate: 'manager', //populate manager
sortBy: 'name:desc', // sort order
limit: 5, // maximum results per page
page: 2, // page number
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const createUser = catchAsync(async (req, res) => {

const getUsers = catchAsync(async (req, res) => {
const filter = pick(req.query, ['name', 'role']);
const options = pick(req.query, ['sortBy', 'limit', 'page']);
const options = pick(req.query, ['populate', 'sortBy', 'limit', 'page']);
const result = await userService.queryUsers(filter, options);
res.send(result);
});
Expand Down
4 changes: 4 additions & 0 deletions src/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const userSchema = mongoose.Schema(
enum: roles,
default: 'user',
},
manager: {
type: mongoose.SchemaTypes.ObjectId,
ref: 'User',
},
},
{
timestamps: true,
Expand Down
1 change: 1 addition & 0 deletions src/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const createUser = async (userBody) => {
* Query for users
* @param {Object} filter - Mongo filter
* @param {Object} options - Query options
* @param {string} [options.populate] - Populate data fields. Hierarchy of fields should be separated by (.). Multiple sorting criteria should be separated by commas (,)
* @param {string} [options.sortBy] - Sort option in the format: sortField:(desc|asc)
* @param {number} [options.limit] - Maximum number of results per page (default = 10)
* @param {number} [options.page] - Current page (default = 1)
Expand Down
1 change: 1 addition & 0 deletions src/validations/user.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const getUsers = {
query: Joi.object().keys({
name: Joi.string(),
role: Joi.string(),
populate: Joi.string(),
sortBy: Joi.string(),
limit: Joi.number().integer(),
page: Joi.number().integer(),
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,24 @@ describe('User routes', () => {
});
});

test('should return the populated managers', async () => {
await insertUsers([admin]);
await insertUsers([
{ ...userOne, manager: admin._id },
{ ...userTwo, manager: admin._id },
]);

const res = await request(app)
.get('/v1/users')
.set('Authorization', `Bearer ${adminAccessToken}`)
.query({ populate: 'manager' })
.send()
.expect(httpStatus.OK);

expect(res.body.results[1].manager.id).toBe(admin._id.toHexString());
expect(res.body.results[2].manager.id).toBe(admin._id.toHexString());
});

describe('GET /v1/users/:userId', () => {
test('should return 200 and the user object if data is ok', async () => {
await insertUsers([userOne]);
Expand Down

0 comments on commit 18e8f79

Please sign in to comment.