fixed all but two jasmine specs...

This commit is contained in:
danielgrippi 2011-10-17 16:54:18 -07:00
parent 718cb2c2f4
commit b61246c168
3 changed files with 61 additions and 85 deletions

View file

@ -71,7 +71,7 @@ module AspectGlobalHelper
klass = checked ? "selected" : "" klass = checked ? "selected" : ""
str = <<LISTITEM str = <<LISTITEM
<li data-aspect_id=#{aspect.id} class='#{klass}'> <li data-aspect_id=#{aspect.id} class='#{klass} aspect_selector'>
#{aspect.name} #{aspect.name}
</li> </li>
LISTITEM LISTITEM

View file

@ -5,18 +5,21 @@
//TODO: make this a widget //TODO: make this a widget
var Publisher = { var Publisher = {
bookmarklet : false, bookmarklet : false,
close: function(){ close: function(){
Publisher.form().addClass('closed'); Publisher.form().addClass('closed');
Publisher.form().find("#publisher_textarea_wrapper").removeClass('active'); Publisher.form().find("#publisher_textarea_wrapper").removeClass('active');
Publisher.form().find("textarea.ac_input").css('min-height', ''); Publisher.form().find("textarea.ac_input").css('min-height', '');
}, },
open: function(){ open: function(){
Publisher.form().removeClass('closed'); Publisher.form().removeClass('closed');
Publisher.form().find("#publisher_textarea_wrapper").addClass('active'); Publisher.form().find("#publisher_textarea_wrapper").addClass('active');
Publisher.form().find("textarea.ac_input").css('min-height', '42px'); Publisher.form().find("textarea.ac_input").css('min-height', '42px');
Publisher.determineSubmitAvailability(); Publisher.determineSubmitAvailability();
}, },
cachedForm : false, cachedForm : false,
form: function(){ form: function(){
if(!Publisher.cachedForm){ if(!Publisher.cachedForm){
@ -24,6 +27,7 @@ var Publisher = {
} }
return Publisher.cachedForm; return Publisher.cachedForm;
}, },
cachedInput : false, cachedInput : false,
input: function(){ input: function(){
if(!Publisher.cachedInput){ if(!Publisher.cachedInput){
@ -237,8 +241,7 @@ var Publisher = {
}, },
searchTermFromValue: function(value, cursorIndex) searchTermFromValue: function(value, cursorIndex) {
{
var stringLoc = Publisher.autocompletion.findStringToReplace(value, cursorIndex); var stringLoc = Publisher.autocompletion.findStringToReplace(value, cursorIndex);
if(stringLoc[0] <= 2){ if(stringLoc[0] <= 2){
stringLoc[0] = 0; stringLoc[0] = 0;
@ -266,44 +269,32 @@ var Publisher = {
); );
} }
}, },
determineSubmitAvailability: function(){ determineSubmitAvailability: function(){
var onlyWhitespaces = (Publisher.input().val().trim() === ''); var onlyWhitespaces = (Publisher.input().val().trim() === ''),
var isSubmitDisabled = Publisher.submit().attr('disabled'); isSubmitDisabled = Publisher.submit().attr('disabled'),
var isPhotoAttached = ($("#photodropzone").children().length > 0); isPhotoAttached = ($("#photodropzone").children().length > 0);
if ((onlyWhitespaces && !isPhotoAttached) && !isSubmitDisabled) { if ((onlyWhitespaces && !isPhotoAttached) && !isSubmitDisabled) {
Publisher.submit().attr('disabled', true); Publisher.submit().attr('disabled', true);
} else if ((!onlyWhitespaces || isPhotoAttached) && isSubmitDisabled) { } else if ((!onlyWhitespaces || isPhotoAttached) && isSubmitDisabled) {
Publisher.submit().removeAttr('disabled'); Publisher.submit().removeAttr('disabled');
} }
}, },
clear: function(){ clear: function(){
this.autocompletion.mentionList.clear(); this.autocompletion.mentionList.clear();
$("#photodropzone").find('li').remove(); $("#photodropzone").find('li').remove();
$("#publisher textarea").removeClass("with_attachments").css('paddingBottom', ''); $("#publisher textarea").removeClass("with_attachments").css('paddingBottom', '');
}, },
bindServiceIcons: function(){ bindServiceIcons: function(){
$(".service_icon").bind("click", function(evt){ $(".service_icon").bind("click", function(evt){
$(this).toggleClass("dim"); $(this).toggleClass("dim");
Publisher.toggleServiceField($(this)); Publisher.toggleServiceField($(this));
}); });
}, },
bindPublicIcon: function(){
$(".public_icon").bind("click", function(evt){
$(this).toggleClass("dim");
var public_field = $("#publisher #status_message_public");
if (public_field.val() == 'false') {
public_field.val('true');
$(this).attr('title', Diaspora.I18n.t('publisher.public'));
} else {
public_field.val('false');
$(this).attr('title', Diaspora.I18n.t('publisher.limited'));
}
$(this).tipsy(true).fixTitle();
$(this).tipsy(true).show();
});
},
toggleServiceField: function(service){ toggleServiceField: function(service){
Publisher.createCounter(service); Publisher.createCounter(service);
@ -346,12 +337,11 @@ var Publisher = {
var aspectId = li.attr('data-aspect_id'), var aspectId = li.attr('data-aspect_id'),
hiddenFields = $('#publisher [name="aspect_ids[]"]'), hiddenFields = $('#publisher [name="aspect_ids[]"]'),
appendId = function(){ appendId = function(){
console.log(aspectId);
$("#publisher .content_creation form").append( $("#publisher .content_creation form").append(
'<input id="aspect_ids_" name="aspect_ids[]" type="hidden" value="'+aspectId+'">'); '<input id="aspect_ids_" name="aspect_ids[]" type="hidden" value="'+aspectId+'">');
}; };
console.log(aspectId); console.log(li);
if(li.hasClass('radio')){ if(li.hasClass('radio')){
$.each(hiddenFields, function(index, value){ $.each(hiddenFields, function(index, value){
@ -389,6 +379,7 @@ var Publisher = {
$('#status_message_fake_text').charCount({allowed: min, warning: min/10 }); $('#status_message_fake_text').charCount({allowed: min, warning: min/10 });
} }
}, },
bindAspectToggles: function() { bindAspectToggles: function() {
$('#publisher .dropdown .dropdown_list li').bind("click", function(evt){ $('#publisher .dropdown .dropdown_list li').bind("click", function(evt){
var li = $(this), var li = $(this),
@ -462,12 +453,12 @@ var Publisher = {
Publisher.form().bind('ajax:failure', Publisher.onFailure); Publisher.form().bind('ajax:failure', Publisher.onFailure);
Publisher.form().bind('ajax:success', Publisher.onSuccess); Publisher.form().bind('ajax:success', Publisher.onSuccess);
}, },
initialize: function() { initialize: function() {
Publisher.cachedForm = Publisher.cachedSubmit = Publisher.cachedForm = Publisher.cachedSubmit =
Publisher.cachedInput = Publisher.cachedHiddenInput = false; Publisher.cachedInput = Publisher.cachedHiddenInput = false;
Publisher.bindServiceIcons(); Publisher.bindServiceIcons();
Publisher.bindPublicIcon();
Publisher.bindAspectToggles(); Publisher.bindAspectToggles();
/* close text area */ /* close text area */
@ -477,7 +468,7 @@ var Publisher = {
}); });
Publisher.close(); Publisher.close();
}); });
Publisher.autocompletion.initialize(); Publisher.autocompletion.initialize();
Publisher.hiddenInput().val(Publisher.input().val()); Publisher.hiddenInput().val(Publisher.input().val());
Publisher.input().autoResize(); Publisher.input().autoResize();

View file

@ -43,28 +43,43 @@ describe("Publisher", function() {
Publisher.initialize(); Publisher.initialize();
expect(Publisher.bindAspectToggles).toHaveBeenCalled(); expect(Publisher.bindAspectToggles).toHaveBeenCalled();
}); });
it('toggles removed only on the clicked icon', function(){ it('correctly initializes an all_aspects state', function(){
Publisher.initialize(); Publisher.initialize();
expect($("#publisher .dropdown .dropdown_list li").first().hasClass("selected")).toBeTruthy(); expect($("#publisher .dropdown .dropdown_list li.radio").first().hasClass("selected")).toBeFalsy();
expect($("#publihser .dropdown .dropdown_list li").last().hasClass("selected")).toBeFalsy(); expect($("#publisher .dropdown .dropdown_list li.radio").last().hasClass("selected")).toBeTruthy();
Publisher.bindAspectToggles(); $.each($("#publihser .dropdown .dropdown_list li.aspect_selector"), function(index, element){
$("#publisher .dropdown .dropdown_list li").last().click(); expect($(element).hasClass("selected")).toBeFalsy();
});
expect($("#publisher .dropdown .dropdown_list li").first().hasClass("selected")).toBeTruthy();
expect($("#publisher .dropdown .dropdown_list li").last().hasClass("selected")).toBeTruthy();
}); });
it('binds to the services icons and toggles the hidden field', function(){ it('toggles removed only on the clicked icon', function(){
Publisher.initialize();
Publisher.bindAspectToggles();
$("#publisher .dropdown .dropdown_list li.aspect_selector").last().click();
$.each($("#publihser .dropdown .dropdown_list li.radio"), function(index, element){
expect($(element).hasClass("selected")).toBeFalsy();
});
expect($("#publisher .dropdown .dropdown_list li.aspect_selector").first().hasClass("selected")).toBeFalsy();
expect($("#publisher .dropdown .dropdown_list li.aspect_selector").last().hasClass("selected")).toBeTruthy();
});
it('is called with the correct element', function(){
spyOn(Publisher, 'toggleAspectIds'); spyOn(Publisher, 'toggleAspectIds');
Publisher.bindAspectToggles(); Publisher.bindAspectToggles();
var aspBadge = $("#publisher .dropdown .dropdown_list li").last(); var aspBadge = $("#publisher .dropdown .dropdown_list li").last();
var aspNum = aspBadge.attr('data-aspect_id');
console.log(aspBadge);
aspBadge.click(); aspBadge.click();
expect(Publisher.toggleAspectIds).toHaveBeenCalledWith(aspNum); expect(Publisher.toggleAspectIds).toHaveBeenCalledWith(aspBadge);
}); });
}); });
@ -72,68 +87,38 @@ describe("Publisher", function() {
describe('toggleAspectIds', function(){ describe('toggleAspectIds', function(){
beforeEach( function(){ beforeEach( function(){
spec.loadFixture('status_message_new'); spec.loadFixture('status_message_new');
li = $("<li data-aspect_id=42></li>");
}); });
it('adds a hidden field to the form if there is not one already', function(){ it('adds a hidden field to the form if there is not one already', function(){
expect($('#publisher [name="aspect_ids[]"]').length).toBe(2); expect($('#publisher [name="aspect_ids[]"]').length).toBe(1);
Publisher.toggleAspectIds(42); expect($('#publisher [name="aspect_ids[]"]').last().attr('value')).toBe('all_aspects');
expect($('#publisher [name="aspect_ids[]"]').length).toBe(3); Publisher.toggleAspectIds(li);
expect($('#publisher [name="aspect_ids[]"]').length).toBe(1);
expect($('#publisher [name="aspect_ids[]"]').last().attr('value')).toBe('42'); expect($('#publisher [name="aspect_ids[]"]').last().attr('value')).toBe('42');
}); });
it('removes the hidden field if its already there', function() { it('removes the hidden field if its already there', function() {
Publisher.toggleAspectIds(42); expect($('#publisher [name="aspect_ids[]"]').length).toBe(1);
expect($('#publisher [name="aspect_ids[]"]').length).toBe(3);
Publisher.toggleAspectIds(42); Publisher.toggleAspectIds(li);
expect($('#publisher [name="aspect_ids[]"]').length).toBe(2); expect($('#publisher [name="aspect_ids[]"]').length).toBe(1);
Publisher.toggleAspectIds(li);
expect($('#publisher [name="aspect_ids[]"]').length).toBe(0);
}); });
it('does not remove a hidden field with a different value', function() { it('does not remove a hidden field with a different value', function() {
Publisher.toggleAspectIds(42); var li2 = $("<li data-aspect_id=99></li>");
expect($('#publisher [name="aspect_ids[]"]').length).toBe(3);
Publisher.toggleAspectIds(99); Publisher.toggleAspectIds(li);
expect($('#publisher [name="aspect_ids[]"]').length).toBe(4); expect($('#publisher [name="aspect_ids[]"]').length).toBe(1);
Publisher.toggleAspectIds(li2);
expect($('#publisher [name="aspect_ids[]"]').length).toBe(2);
}); });
}); });
describe("bindPublicIcon", function() {
beforeEach( function(){
spec.loadFixture('aspects_index_services');
Diaspora.I18n.loadLocale(
{ 'publisher' :
{ 'public' : 'is public', 'limited' : 'is limited' } }, 'en');
});
it('gets called on initialize', function(){
spyOn(Publisher, 'bindPublicIcon');
Publisher.initialize();
expect(Publisher.bindPublicIcon).toHaveBeenCalled();
});
it('toggles dim only on the clicked icon', function(){
expect($(".public_icon").hasClass("dim")).toBeTruthy();
Publisher.bindPublicIcon();
$(".public_icon").click();
expect($(".public_icon").hasClass("dim")).toBeFalsy();
});
it('toggles the hidden field', function(){
Publisher.bindPublicIcon();
expect($('#publisher #status_message_public').val()).toBe('false');
$(".public_icon").click();
expect($('#publisher #status_message_public').val()).toBe('true');
});
it('toggles the tooltip on the clicked icon', function(){
Publisher.bindPublicIcon();
$(".public_icon").click();
expect($(".public_icon")).toHaveAttr('original-title', 'is public');
$(".public_icon").click();
expect($(".public_icon")).toHaveAttr('original-title', 'is limited');
});
});
describe("bindServiceIcons", function() { describe("bindServiceIcons", function() {
beforeEach( function(){ beforeEach( function(){
spec.loadFixture('aspects_index_services'); spec.loadFixture('aspects_index_services');