From c6e41729269e8d59bb4ca7cca13836212d2cdcf3 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Thu, 15 Dec 2011 22:15:40 -0800 Subject: [PATCH] fixed likes_controller; half of the failing jasmine tests --- public/javascripts/app/views/post_view.js | 22 ++++++++++++++---- .../app/views/stream_object_view.js | 1 + spec/controllers/likes_controller_spec.rb | 14 ++++------- spec/controllers/people_controller_spec.rb | 6 ----- spec/javascripts/app/views/post_view_spec.js | 23 ++++++++++--------- 5 files changed, 36 insertions(+), 30 deletions(-) diff --git a/public/javascripts/app/views/post_view.js b/public/javascripts/app/views/post_view.js index 4a41071f8..eae8d6abf 100644 --- a/public/javascripts/app/views/post_view.js +++ b/public/javascripts/app/views/post_view.js @@ -63,14 +63,29 @@ App.Views.Post = App.Views.StreamObject.extend({ if(evt) { evt.preventDefault(); } var link = $(evt.target); + var post = this.model; 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 { - 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){ @@ -98,7 +113,6 @@ App.Views.Post = App.Views.StreamObject.extend({ }, appendLike: function(model){ - console.log(model.get('author')); $(this.el).append("", { href : "/person/" + model.get("author")["id"] }).html($("", { diff --git a/public/javascripts/app/views/stream_object_view.js b/public/javascripts/app/views/stream_object_view.js index 270f895db..c94ba6959 100644 --- a/public/javascripts/app/views/stream_object_view.js +++ b/public/javascripts/app/views/stream_object_view.js @@ -2,6 +2,7 @@ App.Views.StreamObject = Backbone.View.extend({ initialize: function(options) { this.model = options.model; this.model.bind('remove', this.remove, this); + this.model.bind('change', this.render, this); }, destroyModel: function(evt){ diff --git a/spec/controllers/likes_controller_spec.rb b/spec/controllers/likes_controller_spec.rb index f85e009bb..6832a4c89 100644 --- a/spec/controllers/likes_controller_spec.rb +++ b/spec/controllers/likes_controller_spec.rb @@ -29,14 +29,10 @@ describe LikesController 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.comment "hey", :post => @target if class_const == Comment - end - - it 'responds to format js' do - post :create, like_hash.merge(:format => 'js') + post :create, like_hash.merge(:format => :json) response.code.should == '201' end end @@ -119,9 +115,9 @@ describe LikesController do it 'lets a user destroy their like' do 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) - response.status.should == 200 + response.status.should == 204 end it 'does not let a user destroy other likes' do @@ -129,7 +125,7 @@ describe LikesController do like2.save 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) response.status.should == 403 diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index bdf3389ec..db3c4f0c0 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -211,12 +211,6 @@ describe PeopleController do get :show, :id => @user.person.id response.should be_success 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 context "with no user signed in" do diff --git a/spec/javascripts/app/views/post_view_spec.js b/spec/javascripts/app/views/post_view_spec.js index bdfae07c4..2ae8eb60b 100644 --- a/spec/javascripts/app/views/post_view_spec.js +++ b/spec/javascripts/app/views/post_view_spec.js @@ -55,26 +55,27 @@ describe("App.views.Post", function(){ context("Like link", function(){ beforeEach(function(){ this.view = new App.Views.Post({model : this.statusMessage}) + this.link = function(){ return this.view.$(".like_action"); } }) it("clicking 'Like' toggles appropriately", function(){ this.statusMessage.set({user_like : null}); - this.view.render() - var link = this.view.$(".like_action"); + this.view.render(); - expect(link.text()).toContain('Like'); - link.click(); - expect(link.text()).toContain('Unlike'); + expect(this.link().text()).toContain('Like'); + this.link().click(); + 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.view.render() - var link = this.view.$(".like_action"); + this.view.render(); - expect(link.text()).toContain('Unlike'); - link.click(); - expect(link.text()).toContain('Like'); + expect(this.link().text()).toContain('Unlike'); + this.link().click(); + expect(this.link().text()).toContain('Like'); + expect($(this.view.el).html()).toNotContain('1 Like'); }) }) })