port more specs, add aspect factory

This commit is contained in:
Florian Staudacher 2014-03-07 16:22:41 +01:00 committed by Jonne Haß
parent adf7aa98dd
commit 4d3874cc2e
7 changed files with 81 additions and 57 deletions

View file

@ -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;

View file

@ -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.newAspect.trigger('click');
jasmine.Ajax.requests.mostRecent().response(resp_fail);
this.view.addMembership(1, 2);
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.oldAspect.trigger('click');
jasmine.Ajax.requests.mostRecent().response(resp_fail);
this.view.removeMembership(1);
expect(this.view._displayError).toHaveBeenCalledWith('aspect_dropdown.error_remove');
expect($('[id^="flash"]')).toBeErrorFlashMessage(
Diaspora.I18n.t('aspect_dropdown.error_remove', {name: this.person.name})
);
});
});

View file

@ -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();
});

View file

@ -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();
});

View file

@ -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 });

View file

@ -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)
})

View file

@ -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: []