From 0a4b4fb34432bc48ff4ad87024aeb7e7df1131c3 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Wed, 28 Dec 2011 01:03:11 -0500 Subject: [PATCH] remove duplication in jasmine tests (Post view / Feedback view); one correclty failing jasmine spec for Feedback view; created a StatusMessage model for the publisher --- app/views/templates/reshare.ujs | 10 ++++---- public/javascripts/app/models/post.js | 12 ++++++++-- public/javascripts/app/models/reshare.js | 4 ++-- .../javascripts/app/models/status_message.js | 3 +++ public/javascripts/app/views/feedback_view.js | 10 +++++--- .../javascripts/app/views/publisher_view.js | 8 ++++++- .../app/views/stream_object_view.js | 2 +- spec/javascripts/app/models/post-spec.js | 22 ++++++++++++++--- .../app/views/feedback_view_spec.js | 24 +++++++++++++------ spec/javascripts/app/views/post_view_spec.js | 20 ---------------- 10 files changed, 71 insertions(+), 44 deletions(-) create mode 100644 public/javascripts/app/models/status_message.js diff --git a/app/views/templates/reshare.ujs b/app/views/templates/reshare.ujs index aef86c767..7167a8773 100644 --- a/app/views/templates/reshare.ujs +++ b/app/views/templates/reshare.ujs @@ -43,9 +43,9 @@ - <% } else { %> -

- Original post deleted by author. -

- <% } %> + <% } else { %> +

+ Original post deleted by author. +

+ <% } %> diff --git a/public/javascripts/app/models/post.js b/public/javascripts/app/models/post.js index c4e43ada1..581289b95 100644 --- a/public/javascripts/app/models/post.js +++ b/public/javascripts/app/models/post.js @@ -11,7 +11,7 @@ app.models.Post = Backbone.Model.extend({ if(this.id) { return "/posts/" + this.id; } else { - return "/status_messages" + return "/posts" } }, @@ -19,11 +19,19 @@ app.models.Post = Backbone.Model.extend({ return +new Date(this.get("created_at")) / 1000; }, - rootGuid : function() { + baseGuid : function() { if(this.get("root")){ return this.get("root").guid; } else { return this.get("guid"); } + }, + + baseAuthor : function() { + if(this.get("root")){ + return this.get("root").author; + } else { + return this.get("author"); + } } }); diff --git a/public/javascripts/app/models/reshare.js b/public/javascripts/app/models/reshare.js index bf09bf17c..826274aba 100644 --- a/public/javascripts/app/models/reshare.js +++ b/public/javascripts/app/models/reshare.js @@ -1,3 +1,3 @@ -app.models.Reshare = Backbone.Model.extend({ - urlRoot: "/reshares" +app.models.Reshare = app.models.Post.extend({ + url : function() { return "/reshares"; } }); diff --git a/public/javascripts/app/models/status_message.js b/public/javascripts/app/models/status_message.js new file mode 100644 index 000000000..fb73daad4 --- /dev/null +++ b/public/javascripts/app/models/status_message.js @@ -0,0 +1,3 @@ +app.models.StatusMessage = app.models.Post.extend({ + url : function() { return "/status_messages"; } +}); diff --git a/public/javascripts/app/views/feedback_view.js b/public/javascripts/app/views/feedback_view.js index 9b7bd5b6d..b1dbc5eb7 100644 --- a/public/javascripts/app/views/feedback_view.js +++ b/public/javascripts/app/views/feedback_view.js @@ -36,9 +36,13 @@ app.views.Feedback = app.views.StreamObject.extend({ resharePost : function(evt){ if(evt) { evt.preventDefault(); } - if(window.confirm("Reshare " + this.model.get("author").name + "'s post?")) { - var reshare = new app.models.Reshare({root_guid : this.model.rootGuid()}); - reshare.save(); + if(window.confirm("Reshare " + this.model.baseAuthor().name + "'s post?")) { + var reshare = new app.models.Reshare(); + reshare.save({root_guid : this.model.baseGuid()}, { + success : $.proxy(function(data){ + app.stream.collection.add(this); + }, reshare) + }); return reshare; } } diff --git a/public/javascripts/app/views/publisher_view.js b/public/javascripts/app/views/publisher_view.js index 8f645214f..e4943f5d0 100644 --- a/public/javascripts/app/views/publisher_view.js +++ b/public/javascripts/app/views/publisher_view.js @@ -18,12 +18,18 @@ app.views.Publisher = Backbone.View.extend({ var serializedForm = $(evt.target).closest("form").serializeObject(); - this.collection.create({ + // save status message + var statusMessage = new app.models.StatusMessage(); + statusMessage.save({ "status_message" : { "text" : serializedForm["status_message[text]"] }, "aspect_ids" : serializedForm["aspect_ids[]"], "photos" : serializedForm["photos[]"] + }, { + success : $.proxy(function(data) { + app.stream.collection.add(this); + }, statusMessage) }); // clear state diff --git a/public/javascripts/app/views/stream_object_view.js b/public/javascripts/app/views/stream_object_view.js index f22990505..76e08708b 100644 --- a/public/javascripts/app/views/stream_object_view.js +++ b/public/javascripts/app/views/stream_object_view.js @@ -11,7 +11,7 @@ app.views.StreamObject = app.views.Base.extend({ this.model.destroy(); $(this.el).slideUp(400, function(){ - this.remove(); + $(this).remove(); }); } }); diff --git a/spec/javascripts/app/models/post-spec.js b/spec/javascripts/app/models/post-spec.js index 7991e4b23..970b5852b 100644 --- a/spec/javascripts/app/models/post-spec.js +++ b/spec/javascripts/app/models/post-spec.js @@ -13,19 +13,35 @@ describe("app.models.Post", function() { }); }); - describe("rootGuid", function(){ + describe("baseGuid", function(){ it("returns the post's guid if the post does not have a root", function() { this.post.attributes.root = null; this.post.attributes.guid = "abcd"; - expect(this.post.rootGuid()).toBe("abcd") + expect(this.post.baseGuid()).toBe("abcd") }) it("returns the post's root guid if the post has a root", function() { this.post.attributes.root = {guid : "1234"} this.post.attributes.guid = "abcd"; - expect(this.post.rootGuid()).toBe("1234") + expect(this.post.baseGuid()).toBe("1234") + }) + }) + + describe("baseAuthor", function(){ + it("returns the post's guid if the post does not have a root", function() { + this.post.attributes.root = null; + this.post.attributes.author = "abcd"; + + expect(this.post.baseAuthor()).toBe("abcd") + }) + + it("returns the post's root guid if the post has a root", function() { + this.post.attributes.root = {author : "1234"} + this.post.attributes.author = "abcd"; + + expect(this.post.baseAuthor()).toBe("1234") }) }) }); diff --git a/spec/javascripts/app/views/feedback_view_spec.js b/spec/javascripts/app/views/feedback_view_spec.js index 7521e2653..b79a75aa2 100644 --- a/spec/javascripts/app/views/feedback_view_spec.js +++ b/spec/javascripts/app/views/feedback_view_spec.js @@ -1,6 +1,6 @@ describe("app.views.Feedback", function(){ beforeEach(function(){ - window.current_user = app.user({name: "alice", avatar : {small : "http://avatar.com/photo.jpg"}}); + window.current_user = app.user({id : 1, name: "alice", avatar : {small : "http://avatar.com/photo.jpg"}}); var posts = $.parseJSON(spec.readFixture("multi_stream_json"))["posts"]; @@ -93,34 +93,43 @@ describe("app.views.Feedback", function(){ context("when the post is public", function(){ beforeEach(function(){ - this.post.attributes.public = true + this.post.attributes.public = true; this.view.render(); }) it("shows a reshare_action link", function(){ expect($(this.view.el).html()).toContain('reshare_action') }); + + it("does not show a reshare_action link if the original post has been deleted", function(){ + this.post.attributes.root = null + this.view.render(); + + expect($(this.view.el).html()).not.toContain('reshare_action'); + }) }) context("when the post is not public", function(){ beforeEach(function(){ - this.post.attributes.public = false + this.post.attributes.public = false; + this.post.attributes.root = {author : {name : "susan"}}; this.view.render(); }) - it("shows a reshare_action link", function(){ - expect($(this.view.el).html()).not.toContain('reshare_action') + it("does not show a reshare_action link", function(){ + expect($(this.view.el).html()).not.toContain('reshare_action'); }); }) context("when the current user owns the post", function(){ beforeEach(function(){ - this.post.attributes.author = window.current_user - this.post.attributes.public = true + this.post.attributes.author = window.current_user; this.view.render(); }) it("does not display a reshare_action link", function(){ + this.post.attributes.public = false + this.view.render(); expect($(this.view.el).html()).not.toContain('reshare_action') }) }) @@ -128,6 +137,7 @@ describe("app.views.Feedback", function(){ context("reshares", function(){ beforeEach(function(){ this.post.attributes.public = true + this.post.attributes.root = {author : {name : "susan"}}; this.view.render(); }) diff --git a/spec/javascripts/app/views/post_view_spec.js b/spec/javascripts/app/views/post_view_spec.js index f0e70e6c3..68120b351 100644 --- a/spec/javascripts/app/views/post_view_spec.js +++ b/spec/javascripts/app/views/post_view_spec.js @@ -37,25 +37,5 @@ describe("app.views.Post", function(){ expect(statusElement.find(".shield").html()).toNotBe(null); }) }) - - context("Reshare link", function(){ - it("is present if the post is public", function(){ - var view = new app.views.Post({model : this.statusMessage}).render(); - this.statusMessage.set({"public" : true}); - - var statusElement = $(view.el) - - expect(statusElement.find(".reshare_action")).toNotBe(null); - }) - - it("is not present if the post is not public", function(){ - this.statusMessage.set({"public" : false}); - - var view = new app.views.Post({model : this.statusMessage}).render(); - var statusElement = $(view.el) - - expect(statusElement.find(".reshare_action").html()).toBeNull(); - }) - }) }) })