Skip to content

Commit

Permalink
Add new unvote method to cancel votes
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiandouce committed Jun 10, 2014
1 parent 7786990 commit 714038e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ function voting (schema, options) {
};
};

schema.methods.unvote = function unvote(user, fn) {
this.vote.negative.pull(user);
this.vote.positive.pull(user);

// If callback fn, save and return
if (2 === arguments.length) {
this.save(fn);
};
}

schema.methods.upvoted = function upvoted(user) {
if (user._id) {
return schema.methods.upvoted.call(this, user._id);
Expand Down
42 changes: 42 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,48 @@ describe('voting', function () {

});

describe.only('unvote', function() {
it('should unvote', function(done) {
var author2 = new User({ name: 'Jorge' });

comment.upvote(author);
comment.downvote(author2);

assert.equal(author.id, comment.vote.positive[0]);
assert.equal(author2.id, comment.vote.negative[0]);

comment.unvote(author);
assert.equal(0, comment.vote.positive.length);
assert.equal(1, comment.vote.negative.length);

comment.unvote(author2);
assert.equal(0, comment.vote.positive.length);
assert.equal(0, comment.vote.negative.length);

done();
});

it('should save document when callback fn is provided', function(done) {
// give mongo a little extra time.
this.timeout(5000);

comment.upvote(author, function (err, doc) {
assert.equal(comment, doc);

comment.unvote(author, function(err, doc) {
if (err) {
return done(err)
};

assert.equal(doc, comment);

done();
});

});
});
});

describe('upvoted', function() {
it('should success if voted positive', function(done) {
comment.upvote(author);
Expand Down

0 comments on commit 714038e

Please sign in to comment.