add tests for improvement categories

This commit is contained in:
Phillip Thelen
2016-03-06 17:56:51 +01:00
parent a1e1ac211f
commit 36790592c6
3 changed files with 33 additions and 2 deletions

View File

@@ -175,4 +175,27 @@ describe('PUT /user', () => {
});
});
});
context('Improvement Categories', () => {
it('sets valid categories', async () => {
await user.put('/user', {
'preferences.improvementCategories': ['work', 'school'],
});
await user.sync();
expect(user.preferences.improvementCategories).to.eql(['work', 'school']);
});
it('discards invalid categories', async () => {
await expect(user.put('/user', {
'preferences.improvementCategories': ['work', 'procrastination', 'school'],
})).to.eventually.be.rejected.and.eql({
code: 400,
text: [
'Validator failed for path `preferences.improvementCategories` with value `work,procrastination,school`',
],
});
});
});
});

View File

@@ -355,7 +355,15 @@ api.update = (req, res, next) => {
user.save((err) => {
if (!_.isEmpty(errors)) return res.json(401, {err: errors});
if (err) return next(err);
if (err) {
if (err.name == 'ValidationError') {
let errorMessages = _.map(_.values(err.errors), (error) => {
return error.message;
});
return res.json(400, {err: errorMessages});
}
return next(err);
}
res.json(200, user);
user = errors = null;

View File

@@ -395,7 +395,7 @@ var UserSchema = new Schema({
type: Array,
validate: (categories) => {
const validCategories = ['work', 'exercise', 'healthWellness', 'school', 'teams', 'chores', 'creativity'];
let isValidCategory = categories.every(category => validValues.indexOf(category) !== -1);
let isValidCategory = categories.every(category => validCategories.indexOf(category) !== -1);
return isValidCategory;
}}
},