cleanup jasmine tests

and added changelog entry for bugfix #4005
This commit is contained in:
Florian Staudacher 2013-09-12 16:00:54 +02:00 committed by Jonne Haß
parent 5caa46cc83
commit 96f80c8cab
4 changed files with 85 additions and 45 deletions

View file

@ -19,12 +19,9 @@ app.collections.Comments = Backbone.Collection.extend({
success: function() { success: function() {
comment.set({author: app.currentUser.toJSON(), parent: self.post }) comment.set({author: app.currentUser.toJSON(), parent: self.post })
self.add(comment) self.add(comment)
}, }
error: function() { });
} return deferred;
})
return deferred
} }
}); });

View file

@ -82,7 +82,11 @@ app.models.Post.Interactions = Backbone.Model.extend({
var self = this; var self = this;
this.comments.make(text).fail(function () { 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() { }).done(function() {
self.trigger('change') //updates after sync self.trigger('change') //updates after sync
}); });

View file

@ -1,8 +1,8 @@
describe("app.views.CommentStream", function(){ describe("app.views.CommentStream", function(){
beforeEach(function(){ beforeEach(function(){
this.view = new app.views.CommentStream({model : factory.post()}) this.view = new app.views.CommentStream({model : factory.post()});
loginAs({}) loginAs({});
}) });
describe("binds", function() { describe("binds", function() {
it("re-renders on a commentsExpanded trigger", function(){ it("re-renders on a commentsExpanded trigger", function(){
@ -10,8 +10,8 @@ describe("app.views.CommentStream", function(){
this.view.setupBindings(); this.view.setupBindings();
this.view.model.trigger("commentsExpanded"); this.view.model.trigger("commentsExpanded");
expect(this.view.render).toHaveBeenCalled(); expect(this.view.render).toHaveBeenCalled();
}) });
}) });
describe("postRenderTemplate", function(){ describe("postRenderTemplate", function(){
it("applies infield labels", function(){ it("applies infield labels", function(){
@ -19,53 +19,65 @@ describe("app.views.CommentStream", function(){
this.view.postRenderTemplate() this.view.postRenderTemplate()
expect($.fn.placeholder).toHaveBeenCalled() expect($.fn.placeholder).toHaveBeenCalled()
expect($.fn.placeholder.mostRecentCall.object.selector).toBe("textarea") expect($.fn.placeholder.mostRecentCall.object.selector).toBe("textarea")
}) });
it("autoResizes the new comment textarea", function(){ it("autoResizes the new comment textarea", function(){
spyOn($.fn, "autoResize") spyOn($.fn, "autoResize")
this.view.postRenderTemplate() this.view.postRenderTemplate()
expect($.fn.autoResize).toHaveBeenCalled() expect($.fn.autoResize).toHaveBeenCalled()
expect($.fn.autoResize.mostRecentCall.object.selector).toBe("textarea") expect($.fn.autoResize.mostRecentCall.object.selector).toBe("textarea")
}) });
}) });
describe("createComment", function() { describe("createComment", function() {
beforeEach(function() { beforeEach(function() {
jasmine.Ajax.useMock(); jasmine.Ajax.useMock();
this.view.render(); this.view.render();
this.view.expandComments(); this.view.expandComments();
}) });
it("submits the new comment when comment text is not empty", function() { context("submission", function() {
this.view.$(".comment_box").val('a new comment'); beforeEach(function() {
this.view.createComment(); this.view.$(".comment_box").val('a new comment');
comments = mostRecentAjaxRequest(); this.view.createComment();
params = JSON.parse(comments.params)
expect(params.text).toEqual("a new comment");
})
it("comment doesn't get added if the submission fails", function(){ this.request = mostRecentAjaxRequest();
this.view.render(); });
var form = this.view.$("form")
var submitCallback = jasmine.createSpy().andReturn(false);form.submit(submitCallback); it("fires an AJAX request", function() {
var e = $.Event("keydown", { keyCode: 13 }); params = JSON.parse(this.request.params);
e.shiftKey = false; // TODO: use this, once jasmine-ajax is updated to latest version
this.view.keyDownOnCommentBox(e); //params = this.request.data();
expect(submitCallback).not.toHaveBeenCalled();
}) 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() { it("clears the comment box when there are only spaces", function() {
this.view.$(".comment_box").val(' '); this.view.$(".comment_box").val(' ');
this.view.createComment(); this.view.createComment();
expect(this.view.$(".comment_box").val()).toEqual(""); expect(this.view.$(".comment_box").val()).toEqual("");
}) });
it("resets comment box height", function() { it("resets comment box height", function() {
this.view.$(".comment_box").val('a new comment'); this.view.$(".comment_box").val('a new comment');
this.view.createComment(); this.view.createComment();
expect(this.view.$(".comment_box").attr("style")).not.toContain("height"); expect(this.view.$(".comment_box").attr("style")).not.toContain("height");
}) });
}) });
describe("appendComment", function(){ describe("appendComment", function(){
it("appends this.model as 'parent' to the comment", function(){ it("appends this.model as 'parent' to the comment", function(){
@ -74,8 +86,8 @@ describe("app.views.CommentStream", function(){
spyOn(comment, "set") spyOn(comment, "set")
this.view.appendComment(comment) this.view.appendComment(comment)
expect(comment.set).toHaveBeenCalled() expect(comment.set).toHaveBeenCalled()
}) });
}) });
describe("expandComments", function() { describe("expandComments", function() {
it("refills the comment textbox on success", function() { it("refills the comment textbox on success", function() {
@ -90,8 +102,8 @@ describe("app.views.CommentStream", function(){
mostRecentAjaxRequest().response({ comments : [] }); mostRecentAjaxRequest().response({ comments : [] });
expect(this.view.$("textarea").val()).toEqual("great post!"); expect(this.view.$("textarea").val()).toEqual("great post!");
}) });
}) });
describe("pressing a key when typing on the new comment box", function(){ describe("pressing a key when typing on the new comment box", function(){
it("should not submit the form when enter key is pressed", 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); this.view.keyDownOnCommentBox(e);
expect(submitCallback).not.toHaveBeenCalled(); expect(submitCallback).not.toHaveBeenCalled();
}) });
it("should submit the form when enter is pressed with ctrl", function(){ it("should submit the form when enter is pressed with ctrl", function(){
this.view.render(); this.view.render();
@ -117,7 +129,7 @@ describe("app.views.CommentStream", function(){
this.view.keyDownOnCommentBox(e); this.view.keyDownOnCommentBox(e);
expect(submitCallback).toHaveBeenCalled(); expect(submitCallback).toHaveBeenCalled();
}) });
}) });
}) });

View file

@ -25,7 +25,34 @@ beforeEach(function() {
$.extend(Page.prototype, Diaspora.EventBroker.extend(Diaspora.BaseWidget)); $.extend(Page.prototype, Diaspora.EventBroker.extend(Diaspora.BaseWidget));
Diaspora.page = new Page(); 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() { afterEach(function() {