green jasmine, yo
This commit is contained in:
parent
9d24b518f5
commit
b582f45452
6 changed files with 80 additions and 60 deletions
|
|
@ -19,7 +19,7 @@ class LikesController < ApplicationController
|
|||
respond_to do |format|
|
||||
format.html { render :nothing => true, :status => 201 }
|
||||
format.mobile { redirect_to post_path(@like.post_id) }
|
||||
format.json{ render :json => @like.as_api_response(:backbone), :status => 201 }
|
||||
format.json{ render :json => @like.parent.as_api_response(:backbone), :status => 201 }
|
||||
end
|
||||
else
|
||||
render :nothing => true, :status => 422
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
</a>
|
||||
·
|
||||
|
||||
<% if(public && author.id != current_user.id) { %>
|
||||
<% if(public && author.id != current_user.id && (post_type == "Reshare" ? root : true)) { %>
|
||||
<a href="#" class="reshare_action" rel='nofollow'>
|
||||
Reshare
|
||||
</a>
|
||||
|
|
|
|||
|
|
@ -15,6 +15,15 @@ app.models.Post = Backbone.Model.extend({
|
|||
}
|
||||
},
|
||||
|
||||
toggleLike : function() {
|
||||
var userLike = this.get("user_like")
|
||||
if(userLike) {
|
||||
this.unlike()
|
||||
} else {
|
||||
this.like()
|
||||
}
|
||||
},
|
||||
|
||||
createdAt : function() {
|
||||
return +new Date(this.get("created_at")) / 1000;
|
||||
},
|
||||
|
|
@ -33,5 +42,14 @@ app.models.Post = Backbone.Model.extend({
|
|||
} else {
|
||||
return this.get("author");
|
||||
}
|
||||
},
|
||||
|
||||
unlike : function() {
|
||||
this.get("user_like").destroy();
|
||||
this.set({ user_like : null });
|
||||
},
|
||||
|
||||
like : function() {
|
||||
this.set({ user_like : this.likes.create() });
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -10,35 +10,19 @@ app.views.Feedback = app.views.StreamObject.extend({
|
|||
|
||||
toggleLike: function(evt) {
|
||||
if(evt) { evt.preventDefault(); }
|
||||
|
||||
var userLike = this.model.get("user_like");
|
||||
|
||||
if(userLike) {
|
||||
this.model.likes.get(userLike.id).destroy({
|
||||
success : $.proxy(function() {
|
||||
this.model.set({user_like : null, likes_count : this.model.get("likes_count") - 1});
|
||||
}, this)
|
||||
});
|
||||
} else {
|
||||
this.model.likes.create({}, {
|
||||
success : $.proxy(function(like) {
|
||||
this.model.set({user_like : like, likes_count : this.model.get("likes_count") + 1}); // this should be in a callback...
|
||||
}, this)
|
||||
});
|
||||
}
|
||||
this.model.toggleLike();
|
||||
},
|
||||
|
||||
resharePost : function(evt){
|
||||
if(evt) { evt.preventDefault(); }
|
||||
if(!window.confirm("Reshare " + this.model.baseAuthor().name + "'s post?")) { return }
|
||||
|
||||
if(window.confirm("Reshare " + this.model.baseAuthor().name + "'s post?")) {
|
||||
var reshare = new app.models.Reshare();
|
||||
reshare.save({root_guid : this.model.baseGuid()}, {
|
||||
success : function(){
|
||||
app.stream.collection.add(reshare.toJSON());
|
||||
}
|
||||
});
|
||||
return reshare;
|
||||
}
|
||||
var reshare = new app.models.Reshare();
|
||||
reshare.save({root_guid : this.model.baseGuid()}, {
|
||||
success : function(){
|
||||
app.stream.collection.add(reshare.toJSON());
|
||||
}
|
||||
});
|
||||
return reshare;
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -29,6 +29,45 @@ describe("app.models.Post", function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe("toggleLike", function(){
|
||||
it("calls unliked when the user_like exists", function(){
|
||||
this.post.set({user_like : "123"});
|
||||
spyOn(this.post, "unlike").andReturn(true);
|
||||
|
||||
this.post.toggleLike();
|
||||
expect(this.post.unlike).toHaveBeenCalled();
|
||||
})
|
||||
|
||||
it("calls liked when the user_like does not exist", function(){
|
||||
this.post.set({user_like : null});
|
||||
spyOn(this.post, "like").andReturn(true);
|
||||
|
||||
this.post.toggleLike();
|
||||
expect(this.post.like).toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
describe("like", function(){
|
||||
it("calls create on the likes collection", function(){
|
||||
spyOn(this.post.likes, "create");
|
||||
|
||||
this.post.like();
|
||||
expect(this.post.likes.create).toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
describe("unlike", function(){
|
||||
it("calls destroy on the likes collection", function(){
|
||||
var like = new app.models.Like();
|
||||
this.post.set({user_like : like})
|
||||
|
||||
spyOn(like, "destroy");
|
||||
|
||||
this.post.unlike();
|
||||
expect(like.destroy).toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
describe("baseAuthor", function(){
|
||||
it("returns the post's guid if the post does not have a root", function() {
|
||||
this.post.attributes.root = null;
|
||||
|
|
|
|||
|
|
@ -11,63 +11,42 @@ describe("app.views.Feedback", function(){
|
|||
describe(".render", function(){
|
||||
beforeEach(function(){
|
||||
this.link = function(){ return this.view.$(".like_action"); }
|
||||
this.view.render();
|
||||
})
|
||||
|
||||
context("likes", function(){
|
||||
context("when the user likes the post", function(){
|
||||
beforeEach(function(){
|
||||
this.view.render();
|
||||
})
|
||||
it("calls 'toggleLike' on the target post", function(){
|
||||
this.view.render();
|
||||
spyOn(this.post, "toggleLike");
|
||||
|
||||
this.link().click();
|
||||
expect(this.post.toggleLike).toHaveBeenCalled();
|
||||
})
|
||||
|
||||
context("when the user likes the post", function(){
|
||||
it("the like action should be 'Unlike'", function(){
|
||||
expect(this.link().text()).toContain('Unlike');
|
||||
})
|
||||
|
||||
it("removes like when Unlike is clicked", function() {
|
||||
var likeModel = new app.models.Like(this.view.model.get("user_like"));
|
||||
spyOn(this.view.model.likes, "get").andReturn(likeModel);
|
||||
spyOn(likeModel, "destroy");
|
||||
|
||||
this.link().click();
|
||||
expect(likeModel.destroy).toHaveBeenCalled();
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
context("when the user doesn't yet like the post", function(){
|
||||
beforeEach(function(){
|
||||
this.view.model.set({user_like : null});
|
||||
this.view.render();
|
||||
})
|
||||
|
||||
it("contains a .like_action", function(){
|
||||
expect($(this.view.el).html()).toContain("like_action");
|
||||
})
|
||||
|
||||
it("the like action should be 'Like'", function(){
|
||||
expect(this.link().text()).toContain('Like');
|
||||
})
|
||||
|
||||
it("allows for unliking a just-liked post", function(){
|
||||
var like = new app.models.Like({id : 2});
|
||||
|
||||
spyOn(this.post.likes, "create").andReturn(like);
|
||||
|
||||
expect(this.link().text()).toContain('Like');
|
||||
this.link().click();
|
||||
|
||||
this.view.render();
|
||||
this.link().click();
|
||||
expect(this.link().text()).toContain('Unlike');
|
||||
|
||||
// spying + stubbing for destroy
|
||||
var likeModel = new app.models.Like(this.view.model.get("user_like"));
|
||||
spyOn(this.view.model.likes, "get").andReturn(likeModel);
|
||||
spyOn(likeModel, "destroy").andReturn(function(){
|
||||
this.view.model.set({user_like : null})
|
||||
});
|
||||
|
||||
this.link().click();
|
||||
|
||||
this.view.render();
|
||||
expect(this.link().text()).toContain('Like');
|
||||
})
|
||||
})
|
||||
|
|
@ -88,7 +67,7 @@ describe("app.views.Feedback", function(){
|
|||
});
|
||||
|
||||
it("does not show a reshare_action link if the original post has been deleted", function(){
|
||||
this.post.attributes.root = null
|
||||
this.post.set({post_type : "Reshare", root : null})
|
||||
this.view.render();
|
||||
|
||||
expect($(this.view.el).html()).not.toContain('reshare_action');
|
||||
|
|
|
|||
Loading…
Reference in a new issue