From 96f80c8cabcb743d908586fd048e66fc6ab9c866 Mon Sep 17 00:00:00 2001 From: Florian Staudacher <florian_staudacher@yahoo.de> Date: Thu, 12 Sep 2013 16:00:54 +0200 Subject: [PATCH] cleanup jasmine tests and added changelog entry for bugfix #4005 --- .../javascripts/app/collections/comments.js | 11 +-- .../app/models/post/interactions.js | 6 +- .../app/views/comment_stream_view_spec.js | 84 +++++++++++-------- spec/javascripts/helpers/SpecHelper.js | 29 ++++++- 4 files changed, 85 insertions(+), 45 deletions(-) diff --git a/app/assets/javascripts/app/collections/comments.js b/app/assets/javascripts/app/collections/comments.js index 482b525989..d25d851282 100644 --- a/app/assets/javascripts/app/collections/comments.js +++ b/app/assets/javascripts/app/collections/comments.js @@ -19,12 +19,9 @@ app.collections.Comments = Backbone.Collection.extend({ success: function() { comment.set({author: app.currentUser.toJSON(), parent: self.post }) self.add(comment) - }, - error: function() { + } + }); - } - }) - - return deferred + return deferred; } -}); \ No newline at end of file +}); diff --git a/app/assets/javascripts/app/models/post/interactions.js b/app/assets/javascripts/app/models/post/interactions.js index 1d78da1c91..f4740be815 100644 --- a/app/assets/javascripts/app/models/post/interactions.js +++ b/app/assets/javascripts/app/models/post/interactions.js @@ -82,7 +82,11 @@ app.models.Post.Interactions = Backbone.Model.extend({ var self = this; this.comments.make(text).fail(function () { - alert(Diaspora.I18n.t("failed_to_post_message")); + flash = new Diaspora.Widgets.FlashMessages; + flash.render({ + success: false, + notice: Diaspora.I18n.t("failed_to_post_message") + }); }).done(function() { self.trigger('change') //updates after sync }); diff --git a/spec/javascripts/app/views/comment_stream_view_spec.js b/spec/javascripts/app/views/comment_stream_view_spec.js index bf8e344e6e..c5c3ba430e 100644 --- a/spec/javascripts/app/views/comment_stream_view_spec.js +++ b/spec/javascripts/app/views/comment_stream_view_spec.js @@ -1,8 +1,8 @@ describe("app.views.CommentStream", function(){ beforeEach(function(){ - this.view = new app.views.CommentStream({model : factory.post()}) - loginAs({}) - }) + this.view = new app.views.CommentStream({model : factory.post()}); + loginAs({}); + }); describe("binds", function() { it("re-renders on a commentsExpanded trigger", function(){ @@ -10,8 +10,8 @@ describe("app.views.CommentStream", function(){ this.view.setupBindings(); this.view.model.trigger("commentsExpanded"); expect(this.view.render).toHaveBeenCalled(); - }) - }) + }); + }); describe("postRenderTemplate", function(){ it("applies infield labels", function(){ @@ -19,53 +19,65 @@ describe("app.views.CommentStream", function(){ this.view.postRenderTemplate() expect($.fn.placeholder).toHaveBeenCalled() expect($.fn.placeholder.mostRecentCall.object.selector).toBe("textarea") - }) + }); it("autoResizes the new comment textarea", function(){ spyOn($.fn, "autoResize") this.view.postRenderTemplate() expect($.fn.autoResize).toHaveBeenCalled() expect($.fn.autoResize.mostRecentCall.object.selector).toBe("textarea") - }) - }) + }); + }); describe("createComment", function() { beforeEach(function() { jasmine.Ajax.useMock(); this.view.render(); this.view.expandComments(); - }) + }); - it("submits the new comment when comment text is not empty", function() { - this.view.$(".comment_box").val('a new comment'); - this.view.createComment(); - comments = mostRecentAjaxRequest(); - params = JSON.parse(comments.params) - expect(params.text).toEqual("a new comment"); - }) + context("submission", function() { + beforeEach(function() { + this.view.$(".comment_box").val('a new comment'); + this.view.createComment(); - it("comment doesn't get added if the submission fails", function(){ - this.view.render(); - var form = this.view.$("form") - var submitCallback = jasmine.createSpy().andReturn(false);form.submit(submitCallback); - var e = $.Event("keydown", { keyCode: 13 }); - e.shiftKey = false; - this.view.keyDownOnCommentBox(e); - expect(submitCallback).not.toHaveBeenCalled(); - }) + this.request = mostRecentAjaxRequest(); + }); + + it("fires an AJAX request", function() { + params = JSON.parse(this.request.params); + // TODO: use this, once jasmine-ajax is updated to latest version + //params = this.request.data(); + + expect(params.text).toEqual("a new comment"); + }); + + it("adds the comment to the view", function() { + this.request.response({status: 200}); + expect(this.view.$(".comment-content p").text()).toEqual("a new comment"); + }); + + it("doesn't add the comment to the view, when the request fails", function(){ + Diaspora.I18n.loadLocale({failed_to_post_message: "posting failed!"}); + this.request.response({status: 500}); + + expect(this.view.$(".comment-content p").text()).not.toEqual("a new comment"); + expect($('*[id^="flash"]')).toBeErrorFlashMessage("posting failed!"); + }); + }); it("clears the comment box when there are only spaces", function() { this.view.$(".comment_box").val(' '); this.view.createComment(); expect(this.view.$(".comment_box").val()).toEqual(""); - }) + }); it("resets comment box height", function() { this.view.$(".comment_box").val('a new comment'); this.view.createComment(); expect(this.view.$(".comment_box").attr("style")).not.toContain("height"); - }) - }) + }); + }); describe("appendComment", function(){ it("appends this.model as 'parent' to the comment", function(){ @@ -74,8 +86,8 @@ describe("app.views.CommentStream", function(){ spyOn(comment, "set") this.view.appendComment(comment) expect(comment.set).toHaveBeenCalled() - }) - }) + }); + }); describe("expandComments", function() { it("refills the comment textbox on success", function() { @@ -90,8 +102,8 @@ describe("app.views.CommentStream", function(){ mostRecentAjaxRequest().response({ comments : [] }); expect(this.view.$("textarea").val()).toEqual("great post!"); - }) - }) + }); + }); describe("pressing a key when typing on the new comment box", function(){ it("should not submit the form when enter key is pressed", function(){ @@ -104,7 +116,7 @@ describe("app.views.CommentStream", function(){ this.view.keyDownOnCommentBox(e); expect(submitCallback).not.toHaveBeenCalled(); - }) + }); it("should submit the form when enter is pressed with ctrl", function(){ this.view.render(); @@ -117,7 +129,7 @@ describe("app.views.CommentStream", function(){ this.view.keyDownOnCommentBox(e); expect(submitCallback).toHaveBeenCalled(); - }) - }) + }); + }); -}) \ No newline at end of file +}); diff --git a/spec/javascripts/helpers/SpecHelper.js b/spec/javascripts/helpers/SpecHelper.js index 56c54e2c91..19cb1dfdd4 100644 --- a/spec/javascripts/helpers/SpecHelper.js +++ b/spec/javascripts/helpers/SpecHelper.js @@ -25,7 +25,34 @@ beforeEach(function() { $.extend(Page.prototype, Diaspora.EventBroker.extend(Diaspora.BaseWidget)); Diaspora.page = new Page(); - Diaspora.page.publish("page/ready", [$(document.body)]) + Diaspora.page.publish("page/ready", [$(document.body)]); + + + // matches flash messages with success/error and contained text + var flashMatcher = function(flash, id, text) { + textContained = true; + if( text ) { + textContained = (flash.text().indexOf(text) !== -1); + } + + return flash.is(id) && + flash.hasClass('expose') && + textContained; + }; + + // add custom matchers for flash messages + this.addMatchers({ + toBeSuccessFlashMessage: function(containedText) { + var flash = this.actual; + return flashMatcher(flash, '#flash_notice', containedText); + }, + + toBeErrorFlashMessage: function(containedText) { + var flash = this.actual; + return flashMatcher(flash, '#flash_error', containedText); + } + }); + }); afterEach(function() { -- GitLab