From 3a7649064050c56fcfe7b238ee3f5333158dd9c2 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Sun, 22 Jan 2012 23:14:14 -0800 Subject: [PATCH] window.app.user() doesn't return attributes nested in a key; added a comment_view spec --- app/views/layouts/application.html.haml | 6 +-- app/views/templates/comment.jst | 2 +- public/javascripts/app/app.js | 5 +-- public/javascripts/app/views.js | 2 +- public/javascripts/app/views/comment_view.js | 9 +++++ public/javascripts/app/views/post_view.js | 2 +- spec/javascripts/app/app_spec.js | 2 +- .../app/views/comment_view_spec.js | 40 +++++++++++++++++++ .../javascripts/app/views/header_view_spec.js | 6 ++- spec/javascripts/helpers/SpecHelper.js | 5 ++- spec/javascripts/helpers/factory.js | 13 ++++++ 11 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 spec/javascripts/app/views/comment_view_spec.js diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 59d72bfe3..a9d3301cf 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -90,13 +90,13 @@ - if current_user :javascript - app.user({ - current_user: _.extend(#{current_user.person.as_api_response(:backbone).to_json}, { + app.user( + _.extend(#{current_user.person.as_api_response(:backbone).to_json}, { notifications_count : #{notification_count}, unread_messages_count : #{unread_message_count}, admin : #{current_user.admin?} }) - }); + ); = yield(:head) diff --git a/app/views/templates/comment.jst b/app/views/templates/comment.jst index 7397726dd..97318fa04 100644 --- a/app/views/templates/comment.jst +++ b/app/views/templates/comment.jst @@ -1,6 +1,6 @@
- <% if(author.id === current_user.id) { %> + <% if(ownComment) { %> Deletelabel diff --git a/public/javascripts/app/app.js b/public/javascripts/app/app.js index 1efcb82bc..5fcfc6017 100644 --- a/public/javascripts/app/app.js +++ b/public/javascripts/app/app.js @@ -5,9 +5,8 @@ var app = { views: {}, user: function(user) { - if(user) { return this._user = user; } - - return this._user || {current_user : false}; + if(user) { return this._user = user } + return this._user }, initialize: function() { diff --git a/public/javascripts/app/views.js b/public/javascripts/app/views.js index bd3c24301..e4c918194 100644 --- a/public/javascripts/app/views.js +++ b/public/javascripts/app/views.js @@ -10,7 +10,7 @@ app.views.Base = Backbone.View.extend({ defaultPresenter : function(){ var modelJson = this.model ? this.model.toJSON() : {} - return _.extend(modelJson, { current_user: app.user().current_user }); + return _.extend(modelJson, {current_user: app.user()}); }, render : function() { diff --git a/public/javascripts/app/views/comment_view.js b/public/javascripts/app/views/comment_view.js index e05fcfc6d..b551ab903 100644 --- a/public/javascripts/app/views/comment_view.js +++ b/public/javascripts/app/views/comment_view.js @@ -15,5 +15,14 @@ app.views.Comment = app.views.Content.extend({ $(this.el).attr("id", this.model.get("guid")); return this; + }, + + presenter : function() { + return _.extend(this.defaultPresenter(), {ownComment: this.ownComment()}) + }, + + ownComment: function() { + if(!app.user()){ return false } + return this.model.get("author").diaspora_id == app.user().diaspora_id } }); diff --git a/public/javascripts/app/views/post_view.js b/public/javascripts/app/views/post_view.js index de99742d5..1047382bc 100644 --- a/public/javascripts/app/views/post_view.js +++ b/public/javascripts/app/views/post_view.js @@ -38,7 +38,7 @@ app.views.Post = app.views.StreamObject.extend({ }, feedbackView : function(){ - if(!window.app.user().current_user ) { return null } + if(!window.app.user()) { return null } return new app.views.Feedback({model : this.model}); }, diff --git a/spec/javascripts/app/app_spec.js b/spec/javascripts/app/app_spec.js index 49d75124a..49c9b64a7 100644 --- a/spec/javascripts/app/app_spec.js +++ b/spec/javascripts/app/app_spec.js @@ -1,7 +1,7 @@ describe("app", function() { describe("user", function() { it("sets the user if given one and returns the current user", function() { - expect(app.user()).toEqual({current_user : false}); + expect(app.user()).toBeFalsy() app.user({name: "alice"}); diff --git a/spec/javascripts/app/views/comment_view_spec.js b/spec/javascripts/app/views/comment_view_spec.js new file mode 100644 index 000000000..dc90b74a3 --- /dev/null +++ b/spec/javascripts/app/views/comment_view_spec.js @@ -0,0 +1,40 @@ +describe("app.views.Comment", function(){ + beforeEach(function(){ + this.comment = factory.comment() + this.view = new app.views.Comment({model : this.comment}) + }) + + describe("render", function(){ + it("has a delete link if the author is the current user", function(){ + loginAs(this.comment.get("author")) + expect(this.view.render().$('.delete').length).toBe(1) + }) + + it("doesn't have a delete link if the author is not the current user", function(){ + loginAs(_.extend(this.comment.get("author"), {diaspora_id : "notbob@bob.com"}) + expect(this.view.render().$('.delete').length).toBe(0) + }) + + it("doesn't have a delete link if the user is logged out", function(){ + logout() + expect(this.view.render().$('.delete').length).toBe(0) + }) + }) + + describe("ownComment", function(){ + it("returns true if the author diaspora_id == the current user's diaspora_id", function(){ + loginAs(this.comment.get("author")) + expect(this.view.ownComment()).toBe(true) + }) + + it("returns false if the author diaspora_id != the current user's diaspora_id", function(){ + loginAs(_.extend(this.comment.get("author"), {diaspora_id : "notbob@bob.com"}) + expect(this.view.ownComment()).toBe(false); + }) + + it("returns false if the user is not logged in", function(){ + logout() + expect(this.view.ownComment()).toBe(false); + }) + }) +}) diff --git a/spec/javascripts/app/views/header_view_spec.js b/spec/javascripts/app/views/header_view_spec.js index 754ad9e31..95c1575ee 100644 --- a/spec/javascripts/app/views/header_view_spec.js +++ b/spec/javascripts/app/views/header_view_spec.js @@ -7,7 +7,11 @@ describe("app.views.Header", function() { spec.loadFixture("aspects_index"); this.view = new app.views.Header().render(); }); -describe("render", function(){ context("notifications badge", function(){ it("displays a count when the current user has a notification", function(){ loginAs(_.extend(this.userAttrs, {notifications_count : 1})) + + describe("render", function(){ + context("notifications badge", function(){ + it("displays a count when the current user has a notification", function(){ + loginAs(_.extend(this.userAttrs, {notifications_count : 1})) this.view.render(); expect(this.view.$("#notification_badge .badge_count").hasClass('hidden')).toBe(false); expect(this.view.$("#notification_badge .badge_count").text()).toContain("1"); diff --git a/spec/javascripts/helpers/SpecHelper.js b/spec/javascripts/helpers/SpecHelper.js index dd0cdc992..7e1e36087 100644 --- a/spec/javascripts/helpers/SpecHelper.js +++ b/spec/javascripts/helpers/SpecHelper.js @@ -52,11 +52,12 @@ window.stubView = function stubView(text){ } window.loginAs = function loginAs(attrs){ - return window.current_user = app.user({current_user: factory.userAttrs(attrs)}) + return window.current_user = app.user(factory.userAttrs(attrs)) } window.logout = function logout(){ - return window.current_user = app.user({current_user: null}) + this.app._user = undefined + return window.current_user = app.user() } spec.clearLiveEventBindings = function() { diff --git a/spec/javascripts/helpers/factory.js b/spec/javascripts/helpers/factory.js index 7bce3c5cc..4d63f970f 100644 --- a/spec/javascripts/helpers/factory.js +++ b/spec/javascripts/helpers/factory.js @@ -26,6 +26,7 @@ factory = { var defaultAttrs = { "name":"Awesome User" + id, "id": id, + "diaspora_id": "bob@bob.com", "avatar":{ "large":"http://localhost:3000/images/user/uma.jpg", "medium":"http://localhost:3000/images/user/uma.jpg", @@ -57,6 +58,18 @@ factory = { } return new app.models.Post(_.extend(defaultAttrs, overrides)) + }, + + comment: function(overrides) { + var defaultAttrs = { + "text" : "This is an awesome comment!", + "created_at" : "2012-01-03T19:53:13Z", + "author" : this.author(), + "guid" : this.guid(), + "id": this.id.next() + } + + return new app.models.Comment(_.extend(defaultAttrs, overrides)) } }