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

@ -132,7 +132,7 @@ describe("app.views.Publisher", function() {
}) })
}); });
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,33 +156,24 @@ 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._createCounter).toHaveBeenCalled(); expect(this.view.$('.counter').length).toBe(1);
}); });
it("removes the 'old' .counter span", function(){ it("removes any old counters", function(){
spyOn($.fn, "remove"); spyOn($.fn, "remove");
$(".service_icon").first().trigger('click'); $(".service_icon").first().trigger('click');
expect($.fn.remove).toHaveBeenCalled(); expect($.fn.remove).toHaveBeenCalled();
}); });
});
describe("#_toggleServiceField", function() {
it("gets called when you toggle service icons", function(){
spyOn(this.view, '_toggleServiceField');
$(".service_icon").first().trigger('click');
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() {
@ -192,16 +183,15 @@ describe("app.views.Publisher", function() {
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() {
@ -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(){
// inserts location to the DOM; it is the location's view element
setFixtures('<div id="location"></div>'); setFixtures('<div id="location"></div>');
this.view.view_locator = new app.views.Location({el: "#location"});
//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;
}) })
}); });