added jasmine test for poll view
This commit is contained in:
parent
dde39aa1bb
commit
2d0abbae23
3 changed files with 63 additions and 1 deletions
|
|
@ -76,7 +76,7 @@ app.views.Poll = app.views.Base.extend({
|
|||
var parent = this;
|
||||
pollParticipation.save({
|
||||
"poll_answer_id" : result,
|
||||
"poll_id" : this.poll.poll_id,
|
||||
"poll_id" : this.poll.poll_id
|
||||
},{
|
||||
url : "/posts/"+this.poll.post_id+"/poll_participations",
|
||||
success : function(model, response) {
|
||||
|
|
@ -1,3 +1,45 @@
|
|||
describe("app.views.Poll", function(){
|
||||
|
||||
beforeEach(function() {
|
||||
loginAs({name: "alice", avatar : {small : "http://avatar.com/photo.jpg"}});
|
||||
this.view = new app.views.Poll({ "model" : factory.postWithPoll()});
|
||||
this.view.render();
|
||||
});
|
||||
|
||||
describe("setProgressBar", function(){
|
||||
it("sets the progress bar according to the voting result", function(){
|
||||
var percentage = (this.view.poll.poll_answers[0].vote_count / this.view.poll.participation_count)*100;
|
||||
expect(this.view.$('.poll_progress_bar:first').css('width')).toBe(this.view.progressBarFactor * percentage+"px");
|
||||
expect(this.view.$(".percentage:first").text()).toBe(" - " + percentage + "%");
|
||||
})
|
||||
});
|
||||
|
||||
describe("toggleResult", function(){
|
||||
it("toggles the progress bar and result", function(){
|
||||
expect(this.view.$('.poll_progress_bar_wrapper:first').css('display')).toBe("none");
|
||||
this.view.toggleResult(null);
|
||||
expect(this.view.$('.poll_progress_bar_wrapper:first').css('display')).toBe("block");
|
||||
})
|
||||
});
|
||||
|
||||
describe("updateCounter", function(){
|
||||
it("updates the counter after a vote", function(){
|
||||
var pc = this.view.poll.participation_count;
|
||||
var answerCount = this.view.poll.poll_answers[0].vote_count;
|
||||
this.view.updateCounter(1);
|
||||
expect(this.view.poll.participation_count).toBe(pc+1);
|
||||
expect(this.view.poll.poll_answers[0].vote_count).toBe(answerCount+1);
|
||||
})
|
||||
});
|
||||
|
||||
describe("vote", function(){
|
||||
it("checks the ajax call for voting", function(){
|
||||
spyOn($, "ajax");
|
||||
var radio = this.view.$('input[name="vote"]:first');
|
||||
radio.attr('checked', true);
|
||||
this.view.vote({'target' : radio});
|
||||
var obj = JSON.parse($.ajax.mostRecentCall.args[0].data);
|
||||
expect(obj.poll_id).toBe(this.view.poll.poll_id);
|
||||
expect(obj.poll_answer_id).toBe(this.view.poll.poll_answers[0].id);
|
||||
})
|
||||
})
|
||||
});
|
||||
|
|
|
|||
|
|
@ -124,11 +124,31 @@ factory = {
|
|||
return new app.models.Post(_.extend(defaultAttrs, overrides))
|
||||
},
|
||||
|
||||
postWithPoll : function(overrides) {
|
||||
defaultAttrs = _.extend(factory.postAttrs(), {"author" : this.author()});
|
||||
defaultAttrs = _.extend(defaultAttrs, {"already_participated_in_poll" : false});
|
||||
defaultAttrs = _.extend(defaultAttrs, {"poll" : factory.poll()});
|
||||
return new app.models.Post(_.extend(defaultAttrs, overrides));
|
||||
},
|
||||
|
||||
statusMessage : function(overrides){
|
||||
//intentionally doesn't have an author to mirror creation process, maybe we should change the creation process
|
||||
return new app.models.StatusMessage(_.extend(factory.postAttrs(), overrides))
|
||||
},
|
||||
|
||||
poll: function(overrides){
|
||||
return {
|
||||
"question" : "This is an awesome question",
|
||||
"created_at" : "2012-01-03T19:53:13Z",
|
||||
"author" : this.author(),
|
||||
"post_id" : 1,
|
||||
"poll_answers" : [{"answer" : "yes", "id" : 1, "vote_count" : 9}, {"answer" : "no", "id" : 2, "vote_count" : 1}],
|
||||
"guid" : this.guid(),
|
||||
"poll_id": this.id.next(),
|
||||
"participation_count" : 10
|
||||
}
|
||||
},
|
||||
|
||||
comment: function(overrides) {
|
||||
var defaultAttrs = {
|
||||
"text" : "This is an awesome comment!",
|
||||
|
|
|
|||
Loading…
Reference in a new issue