From 4d3874cc2ed05a0f292051519659232d3ae02758 Mon Sep 17 00:00:00 2001 From: Florian Staudacher <florian_staudacher@yahoo.de> Date: Fri, 7 Mar 2014 16:22:41 +0100 Subject: [PATCH] port more specs, add aspect factory --- spec/javascripts/app/router_spec.js | 4 +- .../app/views/aspect_membership_view_spec.js | 71 +++++++++++-------- .../javascripts/app/views/aspect_view_spec.js | 6 +- .../app/views/aspects_list_view_spec.js | 4 +- .../app/views/comment_stream_view_spec.js | 27 ++++--- .../app/views/comment_view_spec.js | 12 ++-- spec/javascripts/helpers/factory.js | 14 ++++ 7 files changed, 81 insertions(+), 57 deletions(-) diff --git a/spec/javascripts/app/router_spec.js b/spec/javascripts/app/router_spec.js index 74afd91900..3beac1f075 100644 --- a/spec/javascripts/app/router_spec.js +++ b/spec/javascripts/app/router_spec.js @@ -39,8 +39,8 @@ describe('app.Router', function () { it('hides the aspects list', function(){ setFixtures('<div id="aspects_list" />'); aspects = new app.collections.Aspects([ - { name: 'Work', selected: true }, - { name: 'Fun', selected: false } + factory.aspectAttrs({selected:true}), + factory.aspectAttrs() ]); var aspectsListView = new app.views.AspectsList({collection: aspects}).render(); router.aspects_list = aspectsListView; diff --git a/spec/javascripts/app/views/aspect_membership_view_spec.js b/spec/javascripts/app/views/aspect_membership_view_spec.js index 5f5abe9bb7..b04f57f590 100644 --- a/spec/javascripts/app/views/aspect_membership_view_spec.js +++ b/spec/javascripts/app/views/aspect_membership_view_spec.js @@ -1,9 +1,20 @@ describe("app.views.AspectMembership", function(){ + var resp_success = {status: 200, responseText: '{}'}; + var resp_fail = {status: 400}; + beforeEach(function() { // mock a dummy aspect dropdown spec.loadFixture("aspect_membership_dropdown_bootstrap"); this.view = new app.views.AspectMembership({el: $('.aspect_membership_dropdown')}); this.person_id = $('.dropdown-menu').data('person_id'); + Diaspora.I18n.load({ + aspect_dropdown: { + started_sharing_with: 'you started sharing with <%= name %>', + stopped_sharing_with: 'you stopped sharing with <%= name %>', + error: 'unable to add <%= name %>', + error_remove: 'unable to remove <%= name %>' + } + }); }); context('adding to aspects', function() { @@ -12,29 +23,30 @@ describe("app.views.AspectMembership", function(){ this.newAspectId = this.newAspect.data('aspect_id'); }); - it('calls "addMembership"', function() { - spyOn(this.view, "addMembership"); - this.newAspect.trigger('click'); + it('marks the aspect as selected', function() { + this.newAspect.trigger('click'); + jasmine.Ajax.requests.mostRecent().response(resp_success); - expect(this.view.addMembership).toHaveBeenCalledWith(this.person_id, this.newAspectId); + expect(this.newAspect.attr('class')).toContain('selected'); }); - it('tries to create a new AspectMembership', function() { - spyOn(app.models.AspectMembership.prototype, "save"); - this.view.addMembership(1, 2); + it('displays flash message when added to first aspect', function() { + spec.content().find('li').removeClass('selected'); + this.newAspect.trigger('click'); + jasmine.Ajax.requests.mostRecent().response(resp_success); - expect(app.models.AspectMembership.prototype.save).toHaveBeenCalled(); + expect($('[id^="flash"]')).toBeSuccessFlashMessage( + Diaspora.I18n.t('aspect_dropdown.started_sharing_with', {name: this.person.name}) + ); }); it('displays an error when it fails', function() { - spyOn(this.view, "_displayError"); - spyOn(app.models.AspectMembership.prototype, "save").andCallFake(function() { - this.trigger('error'); - }); - - this.view.addMembership(1, 2); + this.newAspect.trigger('click'); + jasmine.Ajax.requests.mostRecent().response(resp_fail); - expect(this.view._displayError).toHaveBeenCalledWith('aspect_dropdown.error'); + expect($('[id^="flash"]')).toBeErrorFlashMessage( + Diaspora.I18n.t('aspect_dropdown.error', {name: this.person.name}) + ); }); }); @@ -44,29 +56,30 @@ describe("app.views.AspectMembership", function(){ this.oldMembershipId = this.oldAspect.data('membership_id'); }); - it('calls "removeMembership"', function(){ - spyOn(this.view, "removeMembership"); + it('marks the aspect as unselected', function(){ this.oldAspect.trigger('click'); + jasmine.Ajax.requests.mostRecent().response(resp_success); - expect(this.view.removeMembership).toHaveBeenCalledWith(this.oldMembershipId); + expect(this.oldAspect.attr('class')).not.toContain('selected'); }); - it('tries to destroy an AspectMembership', function() { - spyOn(app.models.AspectMembership.prototype, "destroy"); - this.view.removeMembership(1); + it('displays a flash message when removed from last aspect', function() { + spec.content().find('li.selected:last').removeClass('selected'); + this.oldAspect.trigger('click'); + jasmine.Ajax.requests.mostRecent().response(resp_success); - expect(app.models.AspectMembership.prototype.destroy).toHaveBeenCalled(); + expect($('[id^="flash"]')).toBeSuccessFlashMessage( + Diaspora.I18n.t('aspect_dropdown.stopped_sharing_with', {name: this.person.name}) + ); }); it('displays an error when it fails', function() { - spyOn(this.view, "_displayError"); - spyOn(app.models.AspectMembership.prototype, "destroy").andCallFake(function() { - this.trigger('error'); - }); - - this.view.removeMembership(1); + this.oldAspect.trigger('click'); + jasmine.Ajax.requests.mostRecent().response(resp_fail); - expect(this.view._displayError).toHaveBeenCalledWith('aspect_dropdown.error_remove'); + expect($('[id^="flash"]')).toBeErrorFlashMessage( + Diaspora.I18n.t('aspect_dropdown.error_remove', {name: this.person.name}) + ); }); }); diff --git a/spec/javascripts/app/views/aspect_view_spec.js b/spec/javascripts/app/views/aspect_view_spec.js index 40839c9225..1247d6ad64 100644 --- a/spec/javascripts/app/views/aspect_view_spec.js +++ b/spec/javascripts/app/views/aspect_view_spec.js @@ -1,6 +1,6 @@ describe("app.views.Aspect", function(){ beforeEach(function(){ - this.aspect = new app.models.Aspect({ name: 'Acquaintances', selected: true }); + this.aspect = factory.aspect({selected:true}); this.view = new app.views.Aspect({ model: this.aspect }); }); @@ -14,14 +14,14 @@ describe("app.views.Aspect", function(){ }); it('should show the name of the aspect', function(){ - expect(this.view.$el.children('a.selectable').text()).toMatch('Acquaintances'); + expect(this.view.$el.children('a.selectable').text()).toMatch(this.aspect.get('name')); }); describe('selecting aspects', function(){ beforeEach(function(){ app.router = new app.Router(); spyOn(app.router, 'aspects_stream'); - spyOn(this.view, 'toggleAspect').andCallThrough(); + spyOn(this.view, 'toggleAspect').and.callThrough(); this.view.delegateEvents(); }); diff --git a/spec/javascripts/app/views/aspects_list_view_spec.js b/spec/javascripts/app/views/aspects_list_view_spec.js index 81bc7b19ff..e6bd915ec3 100644 --- a/spec/javascripts/app/views/aspects_list_view_spec.js +++ b/spec/javascripts/app/views/aspects_list_view_spec.js @@ -40,8 +40,8 @@ describe("app.views.AspectsList", function(){ beforeEach(function(){ app.router = new app.Router(); spyOn(app.router, 'aspects_stream'); - spyOn(this.view, 'toggleAll').andCallThrough(); - spyOn(this.view, 'toggleSelector').andCallThrough(); + spyOn(this.view, 'toggleAll').and.callThrough(); + spyOn(this.view, 'toggleSelector').and.callThrough(); this.view.delegateEvents(); this.view.$('.toggle_selector').click(); }); diff --git a/spec/javascripts/app/views/comment_stream_view_spec.js b/spec/javascripts/app/views/comment_stream_view_spec.js index 38948d2944..816ce35a21 100644 --- a/spec/javascripts/app/views/comment_stream_view_spec.js +++ b/spec/javascripts/app/views/comment_stream_view_spec.js @@ -18,20 +18,19 @@ describe("app.views.CommentStream", function(){ spyOn($.fn, "placeholder") this.view.postRenderTemplate() expect($.fn.placeholder).toHaveBeenCalled() - expect($.fn.placeholder.mostRecentCall.object.selector).toBe("textarea") + expect($.fn.placeholder.calls.mostRecent().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") + expect($.fn.autoResize.calls.mostRecent().object.selector).toBe("textarea") }); }); describe("createComment", function() { beforeEach(function() { - jasmine.Ajax.useMock(); this.view.render(); this.view.expandComments(); }); @@ -41,7 +40,7 @@ describe("app.views.CommentStream", function(){ this.view.$(".comment_box").val('a new comment'); this.view.createComment(); - this.request = mostRecentAjaxRequest(); + this.request = jasmine.Ajax.requests.mostRecent(); }); it("fires an AJAX request", function() { @@ -90,28 +89,27 @@ describe("app.views.CommentStream", function(){ describe("expandComments", function() { it("refills the comment textbox on success", function() { - jasmine.Ajax.useMock(); - this.view.render(); - this.view.$("textarea").val("great post!"); - this.view.expandComments(); - mostRecentAjaxRequest().response({ - status: 200, - responseText: JSON.stringify([factory.comment()]) - }); + jasmine.Ajax.requests.mostRecent().response({ comments : [] }); expect(this.view.$("textarea").val()).toEqual("great post!"); }); }); describe("pressing a key when typing on the new comment box", function(){ + var submitCallback; + + beforeEach(function() { + submitCallback = jasmine.createSpy().and.returnValue(false); + }); + it("should not submit the form when enter key is pressed", function(){ this.view.render(); var form = this.view.$("form") - var submitCallback = jasmine.createSpy().andReturn(false);form.submit(submitCallback); + form.submit(submitCallback); var e = $.Event("keydown", { keyCode: 13 }); e.shiftKey = false; @@ -122,8 +120,7 @@ describe("app.views.CommentStream", function(){ it("should submit the form when enter is pressed with ctrl", function(){ this.view.render(); - var form = this.view.$("form") - var submitCallback = jasmine.createSpy().andReturn(false); + var form = this.view.$("form"); form.submit(submitCallback); var e = $.Event("keydown", { keyCode: 13 }); diff --git a/spec/javascripts/app/views/comment_view_spec.js b/spec/javascripts/app/views/comment_view_spec.js index 2b1676b9d2..4ba3f708b4 100644 --- a/spec/javascripts/app/views/comment_view_spec.js +++ b/spec/javascripts/app/views/comment_view_spec.js @@ -49,15 +49,15 @@ describe("app.views.Comment", function(){ describe("canRemove", function(){ context("is truthy", function(){ it("when ownComment is true", function(){ - spyOn(this.view, "ownComment").andReturn(true) - spyOn(this.view, "postOwner").andReturn(false) + spyOn(this.view, "ownComment").and.returnValue(true) + spyOn(this.view, "postOwner").and.returnValue(false) expect(this.view.canRemove()).toBe(true) }) it("when postOwner is true", function(){ - spyOn(this.view, "postOwner").andReturn(true) - spyOn(this.view, "ownComment").andReturn(false) + spyOn(this.view, "postOwner").and.returnValue(true) + spyOn(this.view, "ownComment").and.returnValue(false) expect(this.view.canRemove()).toBe(true) }) @@ -65,8 +65,8 @@ describe("app.views.Comment", function(){ context("is falsy", function(){ it("when postOwner and ownComment are both false", function(){ - spyOn(this.view, "postOwner").andReturn(false) - spyOn(this.view, "ownComment").andReturn(false) + spyOn(this.view, "postOwner").and.returnValue(false) + spyOn(this.view, "ownComment").and.returnValue(false) expect(this.view.canRemove()).toBe(false) }) diff --git a/spec/javascripts/helpers/factory.js b/spec/javascripts/helpers/factory.js index 1f1ae657c1..b8e5d360b4 100644 --- a/spec/javascripts/helpers/factory.js +++ b/spec/javascripts/helpers/factory.js @@ -161,6 +161,20 @@ factory = { return new app.models.Comment(_.extend(defaultAttrs, overrides)) }, + aspectAttrs: function(overrides) { + var names = ['Work','School','Family','Friends','Just following','People','Interesting']; + var defaultAttrs = { + name: names[Math.floor(Math.random()*names.length)]+' '+Math.floor(Math.random()*100), + selected: false + }; + + return _.extend({}, defaultAttrs, overrides); + }, + + aspect: function(overrides) { + return new app.models.Aspect(this.aspectAttrs(overrides)); + }, + preloads: function(overrides) { var defaults = { aspect_ids: [] -- GitLab