fixed likes_controller; half of the failing jasmine tests
This commit is contained in:
parent
a7627fabea
commit
c6e4172926
5 changed files with 36 additions and 30 deletions
|
|
@ -63,14 +63,29 @@ App.Views.Post = App.Views.StreamObject.extend({
|
||||||
if(evt) { evt.preventDefault(); }
|
if(evt) { evt.preventDefault(); }
|
||||||
|
|
||||||
var link = $(evt.target);
|
var link = $(evt.target);
|
||||||
|
var post = this.model;
|
||||||
|
|
||||||
if(link.hasClass('like')) {
|
if(link.hasClass('like')) {
|
||||||
this.model.likes.create();
|
var like = this.model.likes.create();
|
||||||
|
if(like) {
|
||||||
|
console.log(like);
|
||||||
|
this.model.set({
|
||||||
|
user_like : like,
|
||||||
|
likes_count : post.get("likes_count") + 1
|
||||||
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.model.likes.get(link.data("id")).destroy();
|
this.model.likes.get(link.data("id")).destroy({
|
||||||
|
success : function(){
|
||||||
|
post.set({
|
||||||
|
user_like : null,
|
||||||
|
likes_count : post.get("likes_count") - 1
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return this
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
expandLikes: function(evt){
|
expandLikes: function(evt){
|
||||||
|
|
@ -98,7 +113,6 @@ App.Views.Post = App.Views.StreamObject.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
appendLike: function(model){
|
appendLike: function(model){
|
||||||
console.log(model.get('author'));
|
|
||||||
$(this.el).append("<a>", {
|
$(this.el).append("<a>", {
|
||||||
href : "/person/" + model.get("author")["id"]
|
href : "/person/" + model.get("author")["id"]
|
||||||
}).html($("<img>", {
|
}).html($("<img>", {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ App.Views.StreamObject = Backbone.View.extend({
|
||||||
initialize: function(options) {
|
initialize: function(options) {
|
||||||
this.model = options.model;
|
this.model = options.model;
|
||||||
this.model.bind('remove', this.remove, this);
|
this.model.bind('remove', this.remove, this);
|
||||||
|
this.model.bind('change', this.render, this);
|
||||||
},
|
},
|
||||||
|
|
||||||
destroyModel: function(evt){
|
destroyModel: function(evt){
|
||||||
|
|
|
||||||
|
|
@ -29,14 +29,10 @@ describe LikesController do
|
||||||
}
|
}
|
||||||
|
|
||||||
context "on my own post" do
|
context "on my own post" do
|
||||||
before do
|
it 'succeeds' do
|
||||||
@target = alice.post :status_message, :text => "AWESOME", :to => @alices_aspect.id
|
@target = alice.post :status_message, :text => "AWESOME", :to => @alices_aspect.id
|
||||||
|
|
||||||
@target = alice.comment "hey", :post => @target if class_const == Comment
|
@target = alice.comment "hey", :post => @target if class_const == Comment
|
||||||
end
|
post :create, like_hash.merge(:format => :json)
|
||||||
|
|
||||||
it 'responds to format js' do
|
|
||||||
post :create, like_hash.merge(:format => 'js')
|
|
||||||
response.code.should == '201'
|
response.code.should == '201'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -119,9 +115,9 @@ describe LikesController do
|
||||||
|
|
||||||
it 'lets a user destroy their like' do
|
it 'lets a user destroy their like' do
|
||||||
expect {
|
expect {
|
||||||
delete :destroy, :format => "js", id_field => @like.target_id, :id => @like.id
|
delete :destroy, :format => :json, id_field => @like.target_id, :id => @like.id
|
||||||
}.should change(Like, :count).by(-1)
|
}.should change(Like, :count).by(-1)
|
||||||
response.status.should == 200
|
response.status.should == 204
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does not let a user destroy other likes' do
|
it 'does not let a user destroy other likes' do
|
||||||
|
|
@ -129,7 +125,7 @@ describe LikesController do
|
||||||
like2.save
|
like2.save
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
delete :destroy, :format => "js", id_field => like2.target_id, :id => like2.id
|
delete :destroy, :format => :json, id_field => like2.target_id, :id => like2.id
|
||||||
}.should_not change(Like, :count)
|
}.should_not change(Like, :count)
|
||||||
|
|
||||||
response.status.should == 403
|
response.status.should == 403
|
||||||
|
|
|
||||||
|
|
@ -211,12 +211,6 @@ describe PeopleController do
|
||||||
get :show, :id => @user.person.id
|
get :show, :id => @user.person.id
|
||||||
response.should be_success
|
response.should be_success
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'passes through the includes option for json requests' do
|
|
||||||
json = @user.person.as_json
|
|
||||||
Person.any_instance.should_receive(:as_json).with(:includes => "horses").and_return(json)
|
|
||||||
get :show, :format => :json, :id => @user.person.id, :includes => "horses"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with no user signed in" do
|
context "with no user signed in" do
|
||||||
|
|
|
||||||
|
|
@ -55,26 +55,27 @@ describe("App.views.Post", function(){
|
||||||
context("Like link", function(){
|
context("Like link", function(){
|
||||||
beforeEach(function(){
|
beforeEach(function(){
|
||||||
this.view = new App.Views.Post({model : this.statusMessage})
|
this.view = new App.Views.Post({model : this.statusMessage})
|
||||||
|
this.link = function(){ return this.view.$(".like_action"); }
|
||||||
})
|
})
|
||||||
|
|
||||||
it("clicking 'Like' toggles appropriately", function(){
|
it("clicking 'Like' toggles appropriately", function(){
|
||||||
this.statusMessage.set({user_like : null});
|
this.statusMessage.set({user_like : null});
|
||||||
this.view.render()
|
this.view.render();
|
||||||
var link = this.view.$(".like_action");
|
|
||||||
|
|
||||||
expect(link.text()).toContain('Like');
|
expect(this.link().text()).toContain('Like');
|
||||||
link.click();
|
this.link().click();
|
||||||
expect(link.text()).toContain('Unlike');
|
expect(this.link().text()).toContain('Unlike');
|
||||||
|
expect($(this.view.el).html()).toContain('1 like');
|
||||||
})
|
})
|
||||||
|
|
||||||
it("clicking 'UnLike' toggles appropriately", function(){
|
it("clicking 'Unlike' toggles appropriately", function(){
|
||||||
this.statusMessage.set({user_like : { id : 1 }});
|
this.statusMessage.set({user_like : { id : 1 }});
|
||||||
this.view.render()
|
this.view.render();
|
||||||
var link = this.view.$(".like_action");
|
|
||||||
|
|
||||||
expect(link.text()).toContain('Unlike');
|
expect(this.link().text()).toContain('Unlike');
|
||||||
link.click();
|
this.link().click();
|
||||||
expect(link.text()).toContain('Like');
|
expect(this.link().text()).toContain('Like');
|
||||||
|
expect($(this.view.el).html()).toNotContain('1 Like');
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue