fix jasmine tests

This commit is contained in:
Florian Staudacher 2013-09-01 13:11:47 +02:00
parent 4da696253a
commit bedfacecfd
2 changed files with 49 additions and 97 deletions

View file

@ -9,8 +9,7 @@
//= require ./publisher/uploader //= require ./publisher/uploader
//= require jquery.textchange //= require jquery.textchange
app.views.Publisher = Backbone.View.extend(_.extend( app.views.Publisher = Backbone.View.extend({
app.views.PublisherGettingStarted, {
el : "#publisher", el : "#publisher",
@ -151,14 +150,15 @@ app.views.Publisher = Backbone.View.extend(_.extend(
showLocation: function(){ showLocation: function(){
if($('#location').length == 0){ if($('#location').length == 0){
$('#publisher_textarea_wrapper').after('<div id="location"></div>'); $('#publisher_textarea_wrapper').after('<div id="location"></div>');
app.views.location = new app.views.Location(); this.view_locator = new app.views.Location();
} }
}, },
// destroys the location // destroys the location
destroyLocation: function(){ destroyLocation: function(){
if(app.views.location){ if(this.view_locator){
app.views.location.remove(); this.view_locator.remove();
delete this.view_locator;
} }
}, },
@ -346,7 +346,7 @@ app.views.Publisher = Backbone.View.extend(_.extend(
}); });
} }
})); });
// jQuery helper for serializing a <form> into JSON // jQuery helper for serializing a <form> into JSON
$.fn.serializeObject = function() $.fn.serializeObject = function()

View file

@ -18,7 +18,7 @@ describe("app.views.Publisher", function() {
it("hides the close button in standalone mode", function() { it("hides the close button in standalone mode", function() {
expect(this.view.$('#hide_publisher').is(':visible')).toBeFalsy(); expect(this.view.$('#hide_publisher').is(':visible')).toBeFalsy();
}); });
it("hides the post preview button in standalone mode", function() { it("hides the post preview button in standalone mode", function() {
expect(this.view.$('.post_preview_button').is(':visible')).toBeFalsy(); expect(this.view.$('.post_preview_button').is(':visible')).toBeFalsy();
}); });
@ -65,7 +65,7 @@ describe("app.views.Publisher", function() {
this.view.clear($.Event()); this.view.clear($.Event());
expect(this.view.close).toHaveBeenCalled(); expect(this.view.close).toHaveBeenCalled();
}) })
it("calls removePostPreview", function(){ it("calls removePostPreview", function(){
spyOn(this.view, "removePostPreview"); spyOn(this.view, "removePostPreview");
@ -121,18 +121,18 @@ describe("app.views.Publisher", function() {
var form = this.view.$("form") var form = this.view.$("form")
var submitCallback = jasmine.createSpy().andReturn(false); var submitCallback = jasmine.createSpy().andReturn(false);
form.submit(submitCallback); form.submit(submitCallback);
var e = $.Event("keydown", { keyCode: 13 }); var e = $.Event("keydown", { keyCode: 13 });
e.ctrlKey = true; e.ctrlKey = true;
this.view.keyDown(e); this.view.keyDown(e);
expect(submitCallback).toHaveBeenCalled(); expect(submitCallback).toHaveBeenCalled();
expect($(this.view.el)).not.toHaveClass("closed"); expect($(this.view.el)).not.toHaveClass("closed");
}) })
}) })
}); });
context("#toggleService", function(){ context("services", function(){
beforeEach( function(){ beforeEach( function(){
spec.loadFixture('aspects_index_services'); spec.loadFixture('aspects_index_services');
this.view = new app.views.Publisher(); this.view = new app.views.Publisher();
@ -156,52 +156,42 @@ describe("app.views.Publisher", function() {
expect(second.hasClass('dim')).toBeTruthy(); expect(second.hasClass('dim')).toBeTruthy();
}); });
describe("#_createCounter", function() { it("creates a counter element", function(){
it("gets called in when you toggle service icons", function(){ expect(this.view.$('.counter').length).toBe(0);
spyOn(this.view, '_createCounter'); $(".service_icon").first().trigger('click');
$(".service_icon").first().trigger('click'); expect(this.view.$('.counter').length).toBe(1);
expect(this.view._createCounter).toHaveBeenCalled();
});
it("removes the 'old' .counter span", function(){
spyOn($.fn, "remove");
$(".service_icon").first().trigger('click');
expect($.fn.remove).toHaveBeenCalled();
});
}); });
describe("#_toggleServiceField", function() { it("removes any old counters", function(){
it("gets called when you toggle service icons", function(){ spyOn($.fn, "remove");
spyOn(this.view, '_toggleServiceField'); $(".service_icon").first().trigger('click');
$(".service_icon").first().trigger('click'); expect($.fn.remove).toHaveBeenCalled();
expect(this.view._toggleServiceField).toHaveBeenCalled(); });
});
it("toggles the hidden input field", function(){ it("toggles the hidden input field", function(){
expect($('input[name="services[]"]').length).toBe(0); expect(this.view.$('input[name="services[]"]').length).toBe(0);
$(".service_icon").first().trigger('click'); $(".service_icon").first().trigger('click');
expect($('input[name="services[]"]').length).toBe(1); expect(this.view.$('input[name="services[]"]').length).toBe(1);
$(".service_icon").first().trigger('click'); $(".service_icon").first().trigger('click');
expect($('input[name="services[]"]').length).toBe(0); expect(this.view.$('input[name="services[]"]').length).toBe(0);
}); });
it("toggles the correct input", function() { it("toggles the correct input", function() {
var first = $(".service_icon").eq(0); var first = $(".service_icon").eq(0);
var second = $(".service_icon").eq(1); var second = $(".service_icon").eq(1);
first.trigger('click'); first.trigger('click');
second.trigger('click'); second.trigger('click');
expect($('input[name="services[]"]').length).toBe(2); expect(this.view.$('input[name="services[]"]').length).toBe(2);
first.trigger('click'); first.trigger('click');
var prov1 = first.attr('id'); var prov1 = first.attr('id');
var prov2 = second.attr('id'); var prov2 = second.attr('id');
expect($('input[name="services[]"][value="'+prov1+'"]').length).toBe(0); expect(this.view.$('input[name="services[]"][value="'+prov1+'"]').length).toBe(0);
expect($('input[name="services[]"][value="'+prov2+'"]').length).toBe(1); expect(this.view.$('input[name="services[]"][value="'+prov2+'"]').length).toBe(1);
});
}); });
}); });
@ -236,18 +226,12 @@ describe("app.views.Publisher", function() {
expect(this.check_els.last().hasClass('selected')).toBeTruthy(); expect(this.check_els.last().hasClass('selected')).toBeTruthy();
}); });
describe("#_updateSelectedAspectIds", function(){ describe("hidden form elements", function(){
beforeEach(function(){ beforeEach(function(){
this.li = $('<li data-aspect_id="42" />'); this.li = $('<li data-aspect_id="42" />');
this.view.$('.dropdown_list').append(this.li); this.view.$('.dropdown_list').append(this.li);
}); });
it("gets called when aspects are selected", function(){
spyOn(this.view, "_updateSelectedAspectIds");
this.check_els.last().trigger('click');
expect(this.view._updateSelectedAspectIds).toHaveBeenCalled();
});
it("removes a previous selection and inserts the current one", function() { it("removes a previous selection and inserts the current one", function() {
var selected = this.view.$('input[name="aspect_ids[]"]'); var selected = this.view.$('input[name="aspect_ids[]"]');
expect(selected.length).toBe(1); expect(selected.length).toBe(1);
@ -261,13 +245,13 @@ describe("app.views.Publisher", function() {
}); });
it("toggles the same item", function() { it("toggles the same item", function() {
expect(this.view.$('input[name="aspect_ids[]"]').length).toBe(1); expect(this.view.$('input[name="aspect_ids[]"][value="42"]').length).toBe(0);
this.li.trigger('click'); this.li.trigger('click');
expect(this.view.$('input[name="aspect_ids[]"]').length).toBe(1); expect(this.view.$('input[name="aspect_ids[]"][value="42"]').length).toBe(1);
this.li.trigger('click'); this.li.trigger('click');
expect(this.view.$('input[name="aspect_ids[]"]').length).toBe(0); expect(this.view.$('input[name="aspect_ids[]"][value="42"]').length).toBe(0);
}); });
it("keeps other fields with different values", function() { it("keeps other fields with different values", function() {
@ -275,30 +259,13 @@ describe("app.views.Publisher", function() {
this.view.$('.dropdown_list').append(li2); this.view.$('.dropdown_list').append(li2);
this.li.trigger('click'); this.li.trigger('click');
expect(this.view.$('input[name="aspect_ids[]"]').length).toBe(1);
li2.trigger('click'); li2.trigger('click');
expect(this.view.$('input[name="aspect_ids[]"]').length).toBe(2);
expect(this.view.$('input[name="aspect_ids[]"][value="42"]').length).toBe(1);
expect(this.view.$('input[name="aspect_ids[]"][value="99"]').length).toBe(1);
}); });
}); });
describe("#_addHiddenAspectInput", function(){
it("gets called when aspects are selected", function(){
spyOn(this.view, "_addHiddenAspectInput");
this.check_els.last().trigger('click');
expect(this.view._addHiddenAspectInput).toHaveBeenCalled();
});
it("adds a hidden input to the form", function(){
var id = 42;
this.view._addHiddenAspectInput(id);
var input = this.view.$('input[name="aspect_ids[]"][value="'+id+'"]');
expect(input.length).toBe(1);
expect(input.val()).toBe('42');
});
});
}); });
context("locator", function() { context("locator", function() {
@ -314,9 +281,9 @@ describe("app.views.Publisher", function() {
it("Show location", function(){ it("Show location", function(){
// inserts location to the DOM; it is the location's view element // inserts location to the DOM; it is the location's view element
setFixtures('<div id="publisher_textarea_wrapper"></div>'); setFixtures('<div id="publisher_textarea_wrapper"></div>');
// creates a fake Locator // creates a fake Locator
OSM = {}; OSM = {};
OSM.Locator = function(){return { getAddress:function(){}}}; OSM.Locator = function(){return { getAddress:function(){}}};
@ -333,26 +300,11 @@ describe("app.views.Publisher", function() {
describe('#destroyLocation', function(){ describe('#destroyLocation', function(){
it("Destroy location if exists", function(){ it("Destroy location if exists", function(){
setFixtures('<div id="location"></div>');
// inserts location to the DOM; it is the location's view element this.view.view_locator = new app.views.Location({el: "#location"});
setFixtures('<div id="location"></div>');
//Backup original view
var original_location = app.views.Location;
// creates a new Location view with the #location element
app.views.Location = new Backbone.View({el:"#location"});
// creates the mock
app.views.location = sinon.mock(app.views.Location).object;
// calls the destroy function and test the expected result
this.view.destroyLocation(); this.view.destroyLocation();
expect($("#location").length).toBe(0); expect($("#location").length).toBe(0);
//Restore view
app.views.Location = original_location;
}) })
}); });