remove duplication in jasmine tests (Post view / Feedback view); one correclty failing jasmine spec for Feedback view; created a StatusMessage model for the publisher

This commit is contained in:
danielgrippi 2011-12-28 01:03:11 -05:00 committed by Dennis Collinson
parent 02ca7a32f0
commit 0a4b4fb344
10 changed files with 71 additions and 44 deletions

View file

@ -11,7 +11,7 @@ app.models.Post = Backbone.Model.extend({
if(this.id) { if(this.id) {
return "/posts/" + this.id; return "/posts/" + this.id;
} else { } else {
return "/status_messages" return "/posts"
} }
}, },
@ -19,11 +19,19 @@ app.models.Post = Backbone.Model.extend({
return +new Date(this.get("created_at")) / 1000; return +new Date(this.get("created_at")) / 1000;
}, },
rootGuid : function() { baseGuid : function() {
if(this.get("root")){ if(this.get("root")){
return this.get("root").guid; return this.get("root").guid;
} else { } else {
return this.get("guid"); return this.get("guid");
} }
},
baseAuthor : function() {
if(this.get("root")){
return this.get("root").author;
} else {
return this.get("author");
}
} }
}); });

View file

@ -1,3 +1,3 @@
app.models.Reshare = Backbone.Model.extend({ app.models.Reshare = app.models.Post.extend({
urlRoot: "/reshares" url : function() { return "/reshares"; }
}); });

View file

@ -0,0 +1,3 @@
app.models.StatusMessage = app.models.Post.extend({
url : function() { return "/status_messages"; }
});

View file

@ -36,9 +36,13 @@ app.views.Feedback = app.views.StreamObject.extend({
resharePost : function(evt){ resharePost : function(evt){
if(evt) { evt.preventDefault(); } if(evt) { evt.preventDefault(); }
if(window.confirm("Reshare " + this.model.get("author").name + "'s post?")) { if(window.confirm("Reshare " + this.model.baseAuthor().name + "'s post?")) {
var reshare = new app.models.Reshare({root_guid : this.model.rootGuid()}); var reshare = new app.models.Reshare();
reshare.save(); reshare.save({root_guid : this.model.baseGuid()}, {
success : $.proxy(function(data){
app.stream.collection.add(this);
}, reshare)
});
return reshare; return reshare;
} }
} }

View file

@ -18,12 +18,18 @@ app.views.Publisher = Backbone.View.extend({
var serializedForm = $(evt.target).closest("form").serializeObject(); var serializedForm = $(evt.target).closest("form").serializeObject();
this.collection.create({ // save status message
var statusMessage = new app.models.StatusMessage();
statusMessage.save({
"status_message" : { "status_message" : {
"text" : serializedForm["status_message[text]"] "text" : serializedForm["status_message[text]"]
}, },
"aspect_ids" : serializedForm["aspect_ids[]"], "aspect_ids" : serializedForm["aspect_ids[]"],
"photos" : serializedForm["photos[]"] "photos" : serializedForm["photos[]"]
}, {
success : $.proxy(function(data) {
app.stream.collection.add(this);
}, statusMessage)
}); });
// clear state // clear state

View file

@ -11,7 +11,7 @@ app.views.StreamObject = app.views.Base.extend({
this.model.destroy(); this.model.destroy();
$(this.el).slideUp(400, function(){ $(this.el).slideUp(400, function(){
this.remove(); $(this).remove();
}); });
} }
}); });

View file

@ -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() { it("returns the post's guid if the post does not have a root", function() {
this.post.attributes.root = null; this.post.attributes.root = null;
this.post.attributes.guid = "abcd"; 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() { it("returns the post's root guid if the post has a root", function() {
this.post.attributes.root = {guid : "1234"} this.post.attributes.root = {guid : "1234"}
this.post.attributes.guid = "abcd"; 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")
}) })
}) })
}); });

View file

@ -1,6 +1,6 @@
describe("app.views.Feedback", function(){ describe("app.views.Feedback", function(){
beforeEach(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"]; 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(){ context("when the post is public", function(){
beforeEach(function(){ beforeEach(function(){
this.post.attributes.public = true this.post.attributes.public = true;
this.view.render(); this.view.render();
}) })
it("shows a reshare_action link", function(){ it("shows a reshare_action link", function(){
expect($(this.view.el).html()).toContain('reshare_action') 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(){ context("when the post is not public", function(){
beforeEach(function(){ beforeEach(function(){
this.post.attributes.public = false this.post.attributes.public = false;
this.post.attributes.root = {author : {name : "susan"}};
this.view.render(); this.view.render();
}) })
it("shows a reshare_action link", function(){ it("does not show a reshare_action link", function(){
expect($(this.view.el).html()).not.toContain('reshare_action') expect($(this.view.el).html()).not.toContain('reshare_action');
}); });
}) })
context("when the current user owns the post", function(){ context("when the current user owns the post", function(){
beforeEach(function(){ beforeEach(function(){
this.post.attributes.author = window.current_user this.post.attributes.author = window.current_user;
this.post.attributes.public = true
this.view.render(); this.view.render();
}) })
it("does not display a reshare_action link", function(){ 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') expect($(this.view.el).html()).not.toContain('reshare_action')
}) })
}) })
@ -128,6 +137,7 @@ describe("app.views.Feedback", function(){
context("reshares", function(){ context("reshares", function(){
beforeEach(function(){ beforeEach(function(){
this.post.attributes.public = true this.post.attributes.public = true
this.post.attributes.root = {author : {name : "susan"}};
this.view.render(); this.view.render();
}) })

View file

@ -37,25 +37,5 @@ describe("app.views.Post", function(){
expect(statusElement.find(".shield").html()).toNotBe(null); 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();
})
})
}) })
}) })