Skip to content

Commit

Permalink
Add async param modify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
macalinao committed Aug 12, 2014
1 parent 40f032a commit fdd712f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ Model.prototype.applyModifiers = function(request, done) {
// Sync
if (modFn.length === 2) {
try {
req.query[mod.param] = modFn(req, val);
if (req.query[mod.param] === false) {
var ret = modFn(req, val);
if (ret === false) {
delete req.query[mod.param];
} else {
req.query[mod.param] = ret;
}
} catch (err) {
return next(err, null);
Expand All @@ -106,7 +108,12 @@ Model.prototype.applyModifiers = function(request, done) {
}

// Async
modFn(req, val, function(err) {
modFn(req, val, function(err, ret) {
if (ret === false) {
delete req.query[mod.param];
} else {
req.query[mod.param] = ret;
}
return next(err, req);
});
}
Expand Down
26 changes: 26 additions & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ describe('Model', function() {
expect(User.restricted).to.eql(['password']);
});

describe('modifiers', function() {
it('should synchronously modify params', function() {
User.modifyParam('name', function(req, value) {
return 'Tim';
});
User.applyModifiers({
query: {}
}, function(err, req) {
expect(req.query.name).to.equal('Tim');
});
});

it('should asynchronously modify params', function() {
User.modifyParam('name', function(req, value, next) {
async.times(1, function() {
next(null, 'Tim');
});
});
User.applyModifiers({
query: {}
}, function(err, req) {
expect(req.query.name).to.equal('Tim');
});
});
});

describe('transforms', function() {
it('should synchronously transform the document', function(done) {
User.transform(function(req, doc) {
Expand Down

0 comments on commit fdd712f

Please sign in to comment.