diff options
-rw-r--r-- | Gruntfile.js | 6 | ||||
-rw-r--r-- | dist/jquery-impromptu.js | 9 | ||||
-rw-r--r-- | dist/jquery-impromptu.min.js | 2 | ||||
-rw-r--r-- | src/jquery-impromptu.js | 9 | ||||
-rw-r--r-- | test/.jshintrc | 3 | ||||
-rw-r--r-- | test/SpecRunner.html | 1 | ||||
-rw-r--r-- | test/jquery-impromptu_spec.js | 756 | ||||
-rwxr-xr-x[-rw-r--r--] | test/lib/jasmine-jquery.js | 902 |
8 files changed, 1025 insertions, 663 deletions
diff --git a/Gruntfile.js b/Gruntfile.js index 4e9bf55..f98209b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -78,13 +78,11 @@ module.exports = function(grunt) { options: { specs: 'test/*_spec.js', vendor: [ - 'http://code.jquery.com/jquery-1.10.2.min.js' + 'http://code.jquery.com/jquery-1.10.2.min.js', + 'test/lib/jasmine-jquery.js' ] } }, - qunit: { - files: ['test/**/*.html'] - }, jshint: { gruntfile: { options: { diff --git a/dist/jquery-impromptu.js b/dist/jquery-impromptu.js index 24a0056..8ecae6b 100644 --- a/dist/jquery-impromptu.js +++ b/dist/jquery-impromptu.js @@ -497,9 +497,9 @@ if($state.length === 0){
return false;
}
-
+
// transition away from it before deleting
- if($state.is(':visible')){
+ if($state.css('display') !== 'none'){
if($state.next().length > 0){
$.prompt.nextState(rm);
}
@@ -658,9 +658,8 @@ if($.prompt.jqib){
$.prompt.jqib.fadeOut('fast',function(){
- if(callCallback) {
- $.prompt.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
- }
+ $.prompt.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
+
$.prompt.jqib.remove();
$(window).off('resize',$.prompt.position);
diff --git a/dist/jquery-impromptu.min.js b/dist/jquery-impromptu.min.js index 3146d7b..516e10f 100644 --- a/dist/jquery-impromptu.min.js +++ b/dist/jquery-impromptu.min.js @@ -1,4 +1,4 @@ /*! jQuery-Impromptu - v5.2.2 - 2013-12-15 * http://trentrichardson.com/Impromptu * Copyright (c) 2013 Trent Richardson; Licensed MIT */ -(function(t){"use strict";t.prompt=function(e,o){void 0!==o&&void 0!==o.classes&&"string"==typeof o.classes&&(o={box:o.classes}),t.prompt.options=t.extend({},t.prompt.defaults,o),t.prompt.currentPrefix=t.prompt.options.prefix,t.prompt.timeout&&clearTimeout(t.prompt.timeout),t.prompt.timeout=!1;var p=t.prompt.options,r=t(document.body),i=t(window),n='<div class="'+t.prompt.options.prefix+"box "+p.classes.box+'">';n+=p.useiframe&&t("object, applet").length>0?'<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+p.prefix+"fade "+p.classes.fade+'"></iframe>':'<div class="'+p.prefix+"fade "+p.classes.fade+'"></div>',n+='<div class="'+p.prefix+" "+p.classes.prompt+'">'+'<form action="javascript:false;" onsubmit="return false;" class="'+p.prefix+'form">'+'<div class="'+p.prefix+"close "+p.classes.close+'">'+p.closeText+"</div>"+'<div class="'+p.prefix+'states"></div>'+"</form>"+"</div>"+"</div>",t.prompt.jqib=t(n).appendTo(r),t.prompt.jqi=t.prompt.jqib.children("."+p.prefix),t.prompt.jqif=t.prompt.jqib.children("."+p.prefix+"fade"),e.constructor===String&&(e={state0:{title:p.title,html:e,buttons:p.buttons,position:p.position,focus:p.focus,defaultButton:p.defaultButton,submit:p.submit}}),t.prompt.options.states={};var s,a;for(s in e)a=t.extend({},t.prompt.defaults.state,{name:s},e[s]),t.prompt.addState(a.name,a),""===t.prompt.currentStateName&&(t.prompt.currentStateName=a.name);t.prompt.jqi.on("click","."+p.prefix+"buttons button",function(){var e=t(this),o=e.parents("."+p.prefix+"state"),r=t.prompt.options.states[o.data("jqi-name")],i=o.children("."+p.prefix+"message"),n=r.buttons[e.text()]||r.buttons[e.html()],s={};if(void 0===n)for(var a in r.buttons)(r.buttons[a].title===e.text()||r.buttons[a].title===e.html())&&(n=r.buttons[a].value);t.each(t.prompt.jqi.children("form").serializeArray(),function(t,e){void 0===s[e.name]?s[e.name]=e.value:typeof s[e.name]===Array||"object"==typeof s[e.name]?s[e.name].push(e.value):s[e.name]=[s[e.name],e.value]});var m=new t.Event("impromptu:submit");m.stateName=r.name,m.state=o,o.trigger(m,[n,i,s]),m.isDefaultPrevented()||t.prompt.close(!0,n,i,s)});var m=function(){if(p.persistent){var e=(""+p.top).indexOf("%")>=0?i.height()*(parseInt(p.top,10)/100):parseInt(p.top,10),o=parseInt(t.prompt.jqi.css("top").replace("px",""),10)-e;t("html,body").animate({scrollTop:o},"fast",function(){var e=0;t.prompt.jqib.addClass(p.prefix+"warning");var o=setInterval(function(){t.prompt.jqib.toggleClass(p.prefix+"warning"),e++>1&&(clearInterval(o),t.prompt.jqib.removeClass(p.prefix+"warning"))},100)})}else t.prompt.close(!0)},u=function(e){var o=window.event?event.keyCode:e.keyCode;if(27===o&&m(),13===o){var r=t.prompt.getCurrentState().find("."+p.prefix+"defaultbutton"),i=t(e.target);i.is("textarea,."+p.prefix+"button")===!1&&r.length>0&&(e.preventDefault(),r.click())}if(9===o){var n=t("input,select,textarea,button",t.prompt.getCurrentState()),s=!e.shiftKey&&e.target===n[n.length-1],a=e.shiftKey&&e.target===n[0];if(s||a)return setTimeout(function(){if(n){var t=n[a===!0?n.length-1:0];t&&t.focus()}},10),!1}};return t.prompt.position(),t.prompt.style(),t.prompt.jqif.click(m),i.resize({animate:!1},t.prompt.position),t.prompt.jqi.find("."+p.prefix+"close").click(t.prompt.close),t.prompt.jqib.on("keydown",u).on("impromptu:loaded",p.loaded).on("impromptu:close",p.close).on("impromptu:statechanging",p.statechanging).on("impromptu:statechanged",p.statechanged),t.prompt.jqif[p.show](p.overlayspeed),t.prompt.jqi[p.show](p.promptspeed,function(){var e=t.prompt.jqi.find("."+p.prefix+"states ."+p.prefix+"state").eq(0);t.prompt.goToState(e.data("jqi-name")),t.prompt.jqib.trigger("impromptu:loaded")}),p.timeout>0&&(t.prompt.timeout=setTimeout(function(){t.prompt.close(!0)},p.timeout)),t.prompt.jqib},t.prompt.defaults={prefix:"jqi",classes:{box:"",fade:"",prompt:"",close:"",title:"",message:"",buttons:"",button:"",defaultButton:""},title:"",closeText:"×",buttons:{Ok:!0},loaded:function(){},submit:function(){},close:function(){},statechanging:function(){},statechanged:function(){},opacity:.6,zIndex:999,overlayspeed:"slow",promptspeed:"fast",show:"fadeIn",focus:0,defaultButton:0,useiframe:!1,top:"15%",position:{container:null,x:null,y:null,arrow:null,width:null},persistent:!0,timeout:0,states:{},state:{name:null,title:"",html:"",buttons:{Ok:!0},focus:0,defaultButton:0,position:{container:null,x:null,y:null,arrow:null,width:null},submit:function(){return!0}}},t.prompt.currentPrefix=t.prompt.defaults.prefix,t.prompt.currentStateName="",t.prompt.setDefaults=function(e){t.prompt.defaults=t.extend({},t.prompt.defaults,e)},t.prompt.setStateDefaults=function(e){t.prompt.defaults.state=t.extend({},t.prompt.defaults.state,e)},t.prompt.position=function(e){var o=t.fx.off,p=t.prompt.getCurrentState(),r=t.prompt.options.states[p.data("jqi-name")],i=r?r.position:void 0,n=t(window),s=document.body.scrollHeight,a=t(window).height(),m=(t(document).height(),s>a?s:a),u=parseInt(n.scrollTop(),10)+((""+t.prompt.options.top).indexOf("%")>=0?a*(parseInt(t.prompt.options.top,10)/100):parseInt(t.prompt.options.top,10));if(void 0!==e&&e.data.animate===!1&&(t.fx.off=!0),t.prompt.jqib.css({position:"absolute",height:m,width:"100%",top:0,left:0,right:0,bottom:0}),t.prompt.jqif.css({position:"fixed",height:m,width:"100%",top:0,left:0,right:0,bottom:0}),i&&i.container){var f=t(i.container).offset();t.isPlainObject(f)&&void 0!==f.top&&(t.prompt.jqi.css({position:"absolute"}),t.prompt.jqi.animate({top:f.top+i.y,left:f.left+i.x,marginLeft:0,width:void 0!==i.width?i.width:null}),u=f.top+i.y-((""+t.prompt.options.top).indexOf("%")>=0?a*(parseInt(t.prompt.options.top,10)/100):parseInt(t.prompt.options.top,10)),t("html,body").animate({scrollTop:u},"slow","swing",function(){}))}else i&&i.width?(t.prompt.jqi.css({position:"absolute",left:"50%"}),t.prompt.jqi.animate({top:i.y||u,left:i.x||"50%",marginLeft:-1*(i.width/2),width:i.width})):t.prompt.jqi.css({position:"absolute",top:u,left:"50%",marginLeft:-1*(t.prompt.jqi.outerWidth(!1)/2)});void 0!==e&&e.data.animate===!1&&(t.fx.off=o)},t.prompt.style=function(){t.prompt.jqif.css({zIndex:t.prompt.options.zIndex,display:"none",opacity:t.prompt.options.opacity}),t.prompt.jqi.css({zIndex:t.prompt.options.zIndex+1,display:"none"}),t.prompt.jqib.css({zIndex:t.prompt.options.zIndex})},t.prompt.get=function(){return t("."+t.prompt.currentPrefix)},t.prompt.addState=function(e,o,p){var r,i,n,s="",a=null,m="",u="",f=t.prompt.options,l=t("."+t.prompt.currentPrefix+"states"),c=0;o=t.extend({},t.prompt.defaults.state,{name:e},o),null!==o.position.arrow&&(m='<div class="'+f.prefix+"arrow "+f.prefix+"arrow"+o.position.arrow+'"></div>'),o.title&&""!==o.title&&(u='<div class="lead '+f.prefix+"title "+f.classes.title+'">'+o.title+"</div>"),s+='<div id="'+f.prefix+"state_"+e+'" class="'+f.prefix+'state" data-jqi-name="'+e+'" style="display:none;">'+m+u+'<div class="'+f.prefix+"message "+f.classes.message+'">'+o.html+"</div>"+'<div class="'+f.prefix+"buttons "+f.classes.buttons+'"'+(t.isEmptyObject(o.buttons)?'style="display:none;"':"")+">";for(i in o.buttons)n=o.buttons[i],r=o.focus===c||isNaN(o.focus)&&o.defaultButton===c?t.prompt.currentPrefix+"defaultbutton "+f.classes.defaultButton:"","object"==typeof n?(s+='<button class="'+f.classes.button+" "+t.prompt.currentPrefix+"button "+r,n.classes!==void 0&&(s+=" "+(t.isArray(n.classes)?n.classes.join(" "):n.classes)+" "),s+='" name="'+f.prefix+"_"+e+"_button"+n.title.replace(/[^a-z0-9]+/gi,"")+'" id="'+f.prefix+"_"+e+"_button"+n.title.replace(/[^a-z0-9]+/gi,"")+'" value="'+n.value+'">'+n.title+"</button>"):s+='<button class="'+t.prompt.currentPrefix+"button "+f.classes.button+" "+r+'" name="'+f.prefix+"_"+e+"_button"+i.replace(/[^a-z0-9]+/gi,"")+'" id="'+f.prefix+"_"+e+"_button"+i.replace(/[^a-z0-9]+/gi,"")+'" value="'+n+'">'+i+"</button>",c++;return s+="</div></div>",a=t(s),a.on("impromptu:submit",o.submit),void 0!==p?l.find("#"+t.prompt.currentPrefix+"state_"+p).after(a):l.append(a),t.prompt.options.states[e]=o,a},t.prompt.removeState=function(e){var o=t.prompt.getState(e),p=function(){o.remove()};return 0===o.length?!1:(o.is(":visible")?o.next().length>0?t.prompt.nextState(p):t.prompt.prevState(p):o.slideUp("slow",p),!0)},t.prompt.getState=function(e){return t("#"+t.prompt.currentPrefix+"state_"+e)},t.prompt.getStateContent=function(e){return t.prompt.getState(e)},t.prompt.getCurrentState=function(){return t.prompt.getState(t.prompt.getCurrentStateName())},t.prompt.getCurrentStateName=function(){return t.prompt.currentStateName},t.prompt.goToState=function(e,o,p){var r=(t.prompt.get(),t.prompt.options),i=t.prompt.getState(e),n=r.states[i.data("jqi-name")],s=new t.Event("impromptu:statechanging");return"function"==typeof o&&(p=o,o=!1),t.prompt.jqib.trigger(s,[t.prompt.getCurrentStateName(),e]),!s.isDefaultPrevented()&&i.length>0&&(t.prompt.jqi.find("."+t.prompt.currentPrefix+"parentstate").removeClass(t.prompt.currentPrefix+"parentstate"),o?(t.prompt.jqi.find("."+t.prompt.currentPrefix+"substate").not(i).slideUp(r.promptspeed).removeClass("."+t.prompt.currentPrefix+"substate").find("."+t.prompt.currentPrefix+"arrow").hide(),t.prompt.jqi.find("."+t.prompt.currentPrefix+"state:visible").addClass(t.prompt.currentPrefix+"parentstate"),i.addClass(t.prompt.currentPrefix+"substate")):t.prompt.jqi.find("."+t.prompt.currentPrefix+"state").not(i).slideUp(r.promptspeed).find("."+t.prompt.currentPrefix+"arrow").hide(),t.prompt.currentStateName=n.name,i.slideDown(r.promptspeed,function(){var o=t(this);"string"==typeof n.focus?o.find(n.focus).eq(0).focus():o.find("."+t.prompt.currentPrefix+"defaultbutton").focus(),o.find("."+t.prompt.currentPrefix+"arrow").show(r.promptspeed),"function"==typeof p&&t.prompt.jqib.on("impromptu:statechanged",p),t.prompt.jqib.trigger("impromptu:statechanged",[e]),"function"==typeof p&&t.prompt.jqib.off("impromptu:statechanged",p)}),o||t.prompt.position()),i},t.prompt.nextState=function(e){var o=t("#"+t.prompt.currentPrefix+"state_"+t.prompt.getCurrentStateName()).next();return o.length>0&&t.prompt.goToState(o.attr("id").replace(t.prompt.currentPrefix+"state_",""),e),o},t.prompt.prevState=function(e){var o=t("#"+t.prompt.currentPrefix+"state_"+t.prompt.getCurrentStateName()).prev();return o.length>0&&t.prompt.goToState(o.attr("id").replace(t.prompt.currentPrefix+"state_",""),e),o},t.prompt.close=function(e,o,p,r){t.prompt.timeout&&(clearTimeout(t.prompt.timeout),t.prompt.timeout=!1),t.prompt.jqib&&t.prompt.jqib.fadeOut("fast",function(){e&&t.prompt.jqib.trigger("impromptu:close",[o,p,r]),t.prompt.jqib.remove(),t(window).off("resize",t.prompt.position)})},t.fn.prompt=function(e){void 0===e&&(e={}),void 0===e.withDataAndEvents&&(e.withDataAndEvents=!1),t.prompt(t(this).clone(e.withDataAndEvents).html(),e)}})(jQuery);
\ No newline at end of file +(function(t){"use strict";t.prompt=function(e,o){void 0!==o&&void 0!==o.classes&&"string"==typeof o.classes&&(o={box:o.classes}),t.prompt.options=t.extend({},t.prompt.defaults,o),t.prompt.currentPrefix=t.prompt.options.prefix,t.prompt.timeout&&clearTimeout(t.prompt.timeout),t.prompt.timeout=!1;var p=t.prompt.options,r=t(document.body),i=t(window),n='<div class="'+t.prompt.options.prefix+"box "+p.classes.box+'">';n+=p.useiframe&&t("object, applet").length>0?'<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+p.prefix+"fade "+p.classes.fade+'"></iframe>':'<div class="'+p.prefix+"fade "+p.classes.fade+'"></div>',n+='<div class="'+p.prefix+" "+p.classes.prompt+'">'+'<form action="javascript:false;" onsubmit="return false;" class="'+p.prefix+'form">'+'<div class="'+p.prefix+"close "+p.classes.close+'">'+p.closeText+"</div>"+'<div class="'+p.prefix+'states"></div>'+"</form>"+"</div>"+"</div>",t.prompt.jqib=t(n).appendTo(r),t.prompt.jqi=t.prompt.jqib.children("."+p.prefix),t.prompt.jqif=t.prompt.jqib.children("."+p.prefix+"fade"),e.constructor===String&&(e={state0:{title:p.title,html:e,buttons:p.buttons,position:p.position,focus:p.focus,defaultButton:p.defaultButton,submit:p.submit}}),t.prompt.options.states={};var s,a;for(s in e)a=t.extend({},t.prompt.defaults.state,{name:s},e[s]),t.prompt.addState(a.name,a),""===t.prompt.currentStateName&&(t.prompt.currentStateName=a.name);t.prompt.jqi.on("click","."+p.prefix+"buttons button",function(){var e=t(this),o=e.parents("."+p.prefix+"state"),r=t.prompt.options.states[o.data("jqi-name")],i=o.children("."+p.prefix+"message"),n=r.buttons[e.text()]||r.buttons[e.html()],s={};if(void 0===n)for(var a in r.buttons)(r.buttons[a].title===e.text()||r.buttons[a].title===e.html())&&(n=r.buttons[a].value);t.each(t.prompt.jqi.children("form").serializeArray(),function(t,e){void 0===s[e.name]?s[e.name]=e.value:typeof s[e.name]===Array||"object"==typeof s[e.name]?s[e.name].push(e.value):s[e.name]=[s[e.name],e.value]});var m=new t.Event("impromptu:submit");m.stateName=r.name,m.state=o,o.trigger(m,[n,i,s]),m.isDefaultPrevented()||t.prompt.close(!0,n,i,s)});var m=function(){if(p.persistent){var e=(""+p.top).indexOf("%")>=0?i.height()*(parseInt(p.top,10)/100):parseInt(p.top,10),o=parseInt(t.prompt.jqi.css("top").replace("px",""),10)-e;t("html,body").animate({scrollTop:o},"fast",function(){var e=0;t.prompt.jqib.addClass(p.prefix+"warning");var o=setInterval(function(){t.prompt.jqib.toggleClass(p.prefix+"warning"),e++>1&&(clearInterval(o),t.prompt.jqib.removeClass(p.prefix+"warning"))},100)})}else t.prompt.close(!0)},u=function(e){var o=window.event?event.keyCode:e.keyCode;if(27===o&&m(),13===o){var r=t.prompt.getCurrentState().find("."+p.prefix+"defaultbutton"),i=t(e.target);i.is("textarea,."+p.prefix+"button")===!1&&r.length>0&&(e.preventDefault(),r.click())}if(9===o){var n=t("input,select,textarea,button",t.prompt.getCurrentState()),s=!e.shiftKey&&e.target===n[n.length-1],a=e.shiftKey&&e.target===n[0];if(s||a)return setTimeout(function(){if(n){var t=n[a===!0?n.length-1:0];t&&t.focus()}},10),!1}};return t.prompt.position(),t.prompt.style(),t.prompt.jqif.click(m),i.resize({animate:!1},t.prompt.position),t.prompt.jqi.find("."+p.prefix+"close").click(t.prompt.close),t.prompt.jqib.on("keydown",u).on("impromptu:loaded",p.loaded).on("impromptu:close",p.close).on("impromptu:statechanging",p.statechanging).on("impromptu:statechanged",p.statechanged),t.prompt.jqif[p.show](p.overlayspeed),t.prompt.jqi[p.show](p.promptspeed,function(){var e=t.prompt.jqi.find("."+p.prefix+"states ."+p.prefix+"state").eq(0);t.prompt.goToState(e.data("jqi-name")),t.prompt.jqib.trigger("impromptu:loaded")}),p.timeout>0&&(t.prompt.timeout=setTimeout(function(){t.prompt.close(!0)},p.timeout)),t.prompt.jqib},t.prompt.defaults={prefix:"jqi",classes:{box:"",fade:"",prompt:"",close:"",title:"",message:"",buttons:"",button:"",defaultButton:""},title:"",closeText:"×",buttons:{Ok:!0},loaded:function(){},submit:function(){},close:function(){},statechanging:function(){},statechanged:function(){},opacity:.6,zIndex:999,overlayspeed:"slow",promptspeed:"fast",show:"fadeIn",focus:0,defaultButton:0,useiframe:!1,top:"15%",position:{container:null,x:null,y:null,arrow:null,width:null},persistent:!0,timeout:0,states:{},state:{name:null,title:"",html:"",buttons:{Ok:!0},focus:0,defaultButton:0,position:{container:null,x:null,y:null,arrow:null,width:null},submit:function(){return!0}}},t.prompt.currentPrefix=t.prompt.defaults.prefix,t.prompt.currentStateName="",t.prompt.setDefaults=function(e){t.prompt.defaults=t.extend({},t.prompt.defaults,e)},t.prompt.setStateDefaults=function(e){t.prompt.defaults.state=t.extend({},t.prompt.defaults.state,e)},t.prompt.position=function(e){var o=t.fx.off,p=t.prompt.getCurrentState(),r=t.prompt.options.states[p.data("jqi-name")],i=r?r.position:void 0,n=t(window),s=document.body.scrollHeight,a=t(window).height(),m=(t(document).height(),s>a?s:a),u=parseInt(n.scrollTop(),10)+((""+t.prompt.options.top).indexOf("%")>=0?a*(parseInt(t.prompt.options.top,10)/100):parseInt(t.prompt.options.top,10));if(void 0!==e&&e.data.animate===!1&&(t.fx.off=!0),t.prompt.jqib.css({position:"absolute",height:m,width:"100%",top:0,left:0,right:0,bottom:0}),t.prompt.jqif.css({position:"fixed",height:m,width:"100%",top:0,left:0,right:0,bottom:0}),i&&i.container){var f=t(i.container).offset();t.isPlainObject(f)&&void 0!==f.top&&(t.prompt.jqi.css({position:"absolute"}),t.prompt.jqi.animate({top:f.top+i.y,left:f.left+i.x,marginLeft:0,width:void 0!==i.width?i.width:null}),u=f.top+i.y-((""+t.prompt.options.top).indexOf("%")>=0?a*(parseInt(t.prompt.options.top,10)/100):parseInt(t.prompt.options.top,10)),t("html,body").animate({scrollTop:u},"slow","swing",function(){}))}else i&&i.width?(t.prompt.jqi.css({position:"absolute",left:"50%"}),t.prompt.jqi.animate({top:i.y||u,left:i.x||"50%",marginLeft:-1*(i.width/2),width:i.width})):t.prompt.jqi.css({position:"absolute",top:u,left:"50%",marginLeft:-1*(t.prompt.jqi.outerWidth(!1)/2)});void 0!==e&&e.data.animate===!1&&(t.fx.off=o)},t.prompt.style=function(){t.prompt.jqif.css({zIndex:t.prompt.options.zIndex,display:"none",opacity:t.prompt.options.opacity}),t.prompt.jqi.css({zIndex:t.prompt.options.zIndex+1,display:"none"}),t.prompt.jqib.css({zIndex:t.prompt.options.zIndex})},t.prompt.get=function(){return t("."+t.prompt.currentPrefix)},t.prompt.addState=function(e,o,p){var r,i,n,s="",a=null,m="",u="",f=t.prompt.options,l=t("."+t.prompt.currentPrefix+"states"),c=0;o=t.extend({},t.prompt.defaults.state,{name:e},o),null!==o.position.arrow&&(m='<div class="'+f.prefix+"arrow "+f.prefix+"arrow"+o.position.arrow+'"></div>'),o.title&&""!==o.title&&(u='<div class="lead '+f.prefix+"title "+f.classes.title+'">'+o.title+"</div>"),s+='<div id="'+f.prefix+"state_"+e+'" class="'+f.prefix+'state" data-jqi-name="'+e+'" style="display:none;">'+m+u+'<div class="'+f.prefix+"message "+f.classes.message+'">'+o.html+"</div>"+'<div class="'+f.prefix+"buttons "+f.classes.buttons+'"'+(t.isEmptyObject(o.buttons)?'style="display:none;"':"")+">";for(i in o.buttons)n=o.buttons[i],r=o.focus===c||isNaN(o.focus)&&o.defaultButton===c?t.prompt.currentPrefix+"defaultbutton "+f.classes.defaultButton:"","object"==typeof n?(s+='<button class="'+f.classes.button+" "+t.prompt.currentPrefix+"button "+r,n.classes!==void 0&&(s+=" "+(t.isArray(n.classes)?n.classes.join(" "):n.classes)+" "),s+='" name="'+f.prefix+"_"+e+"_button"+n.title.replace(/[^a-z0-9]+/gi,"")+'" id="'+f.prefix+"_"+e+"_button"+n.title.replace(/[^a-z0-9]+/gi,"")+'" value="'+n.value+'">'+n.title+"</button>"):s+='<button class="'+t.prompt.currentPrefix+"button "+f.classes.button+" "+r+'" name="'+f.prefix+"_"+e+"_button"+i.replace(/[^a-z0-9]+/gi,"")+'" id="'+f.prefix+"_"+e+"_button"+i.replace(/[^a-z0-9]+/gi,"")+'" value="'+n+'">'+i+"</button>",c++;return s+="</div></div>",a=t(s),a.on("impromptu:submit",o.submit),void 0!==p?l.find("#"+t.prompt.currentPrefix+"state_"+p).after(a):l.append(a),t.prompt.options.states[e]=o,a},t.prompt.removeState=function(e){var o=t.prompt.getState(e),p=function(){o.remove()};return 0===o.length?!1:("none"!==o.css("display")?o.next().length>0?t.prompt.nextState(p):t.prompt.prevState(p):o.slideUp("slow",p),!0)},t.prompt.getState=function(e){return t("#"+t.prompt.currentPrefix+"state_"+e)},t.prompt.getStateContent=function(e){return t.prompt.getState(e)},t.prompt.getCurrentState=function(){return t.prompt.getState(t.prompt.getCurrentStateName())},t.prompt.getCurrentStateName=function(){return t.prompt.currentStateName},t.prompt.goToState=function(e,o,p){var r=(t.prompt.get(),t.prompt.options),i=t.prompt.getState(e),n=r.states[i.data("jqi-name")],s=new t.Event("impromptu:statechanging");return"function"==typeof o&&(p=o,o=!1),t.prompt.jqib.trigger(s,[t.prompt.getCurrentStateName(),e]),!s.isDefaultPrevented()&&i.length>0&&(t.prompt.jqi.find("."+t.prompt.currentPrefix+"parentstate").removeClass(t.prompt.currentPrefix+"parentstate"),o?(t.prompt.jqi.find("."+t.prompt.currentPrefix+"substate").not(i).slideUp(r.promptspeed).removeClass("."+t.prompt.currentPrefix+"substate").find("."+t.prompt.currentPrefix+"arrow").hide(),t.prompt.jqi.find("."+t.prompt.currentPrefix+"state:visible").addClass(t.prompt.currentPrefix+"parentstate"),i.addClass(t.prompt.currentPrefix+"substate")):t.prompt.jqi.find("."+t.prompt.currentPrefix+"state").not(i).slideUp(r.promptspeed).find("."+t.prompt.currentPrefix+"arrow").hide(),t.prompt.currentStateName=n.name,i.slideDown(r.promptspeed,function(){var o=t(this);"string"==typeof n.focus?o.find(n.focus).eq(0).focus():o.find("."+t.prompt.currentPrefix+"defaultbutton").focus(),o.find("."+t.prompt.currentPrefix+"arrow").show(r.promptspeed),"function"==typeof p&&t.prompt.jqib.on("impromptu:statechanged",p),t.prompt.jqib.trigger("impromptu:statechanged",[e]),"function"==typeof p&&t.prompt.jqib.off("impromptu:statechanged",p)}),o||t.prompt.position()),i},t.prompt.nextState=function(e){var o=t("#"+t.prompt.currentPrefix+"state_"+t.prompt.getCurrentStateName()).next();return o.length>0&&t.prompt.goToState(o.attr("id").replace(t.prompt.currentPrefix+"state_",""),e),o},t.prompt.prevState=function(e){var o=t("#"+t.prompt.currentPrefix+"state_"+t.prompt.getCurrentStateName()).prev();return o.length>0&&t.prompt.goToState(o.attr("id").replace(t.prompt.currentPrefix+"state_",""),e),o},t.prompt.close=function(e,o,p,r){t.prompt.timeout&&(clearTimeout(t.prompt.timeout),t.prompt.timeout=!1),t.prompt.jqib&&t.prompt.jqib.fadeOut("fast",function(){t.prompt.jqib.trigger("impromptu:close",[o,p,r]),t.prompt.jqib.remove(),t(window).off("resize",t.prompt.position)})},t.fn.prompt=function(e){void 0===e&&(e={}),void 0===e.withDataAndEvents&&(e.withDataAndEvents=!1),t.prompt(t(this).clone(e.withDataAndEvents).html(),e)}})(jQuery);
\ No newline at end of file diff --git a/src/jquery-impromptu.js b/src/jquery-impromptu.js index f84cc01..6eb10ad 100644 --- a/src/jquery-impromptu.js +++ b/src/jquery-impromptu.js @@ -494,9 +494,9 @@ if($state.length === 0){
return false;
}
-
+
// transition away from it before deleting
- if($state.is(':visible')){
+ if($state.css('display') !== 'none'){
if($state.next().length > 0){
$.prompt.nextState(rm);
}
@@ -655,9 +655,8 @@ if($.prompt.jqib){
$.prompt.jqib.fadeOut('fast',function(){
- if(callCallback) {
- $.prompt.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
- }
+ $.prompt.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
+
$.prompt.jqib.remove();
$(window).off('resize',$.prompt.position);
diff --git a/test/.jshintrc b/test/.jshintrc index 744a6f2..c3bc914 100644 --- a/test/.jshintrc +++ b/test/.jshintrc @@ -34,6 +34,9 @@ "beforeEach", "afterEach", "spyOn", + "spyOnEvent", + "runs", + "waitsFor", "affix", "xdescribe", "xit" diff --git a/test/SpecRunner.html b/test/SpecRunner.html index ed8e9c4..300aeda 100644 --- a/test/SpecRunner.html +++ b/test/SpecRunner.html @@ -15,6 +15,7 @@ <script type="text/javascript" src="lib/jasmine-1.3.1/jasmine.js"></script> <script type="text/javascript" src="lib/jasmine-1.3.1/jasmine-html.js"></script> + <script type="text/javascript" src="lib/jasmine-jquery.js"></script> <script type="text/javascript" src="../src/jquery-impromptu.js"></script> diff --git a/test/jquery-impromptu_spec.js b/test/jquery-impromptu_spec.js index 15269b3..bdf308f 100644 --- a/test/jquery-impromptu_spec.js +++ b/test/jquery-impromptu_spec.js @@ -1,250 +1,257 @@ describe('jquery-impromptu', function() { + // ==================================================================================== // ==================================================================================== - describe('basic initialization', function() { + describe('base structure', function(){ - beforeEach(function() { - $.fx.off = true; // for our testing lets turn off fx - }); + // ==================================================================================== + // ==================================================================================== + describe('basic initialization', function() { - afterEach(function() { - $.prompt.close(); - }); - - it('should be defined', function() { - - expect($.prompt).not.toBeUndefined(); - }); - - it('should generate markup', function() { - var expectedTitle = 'This is a title', - expectedText = 'This is a test'; + beforeEach(function() { + $.fx.off = true; // for our testing lets turn off fx + }); - $.prompt(expectedText, { title: expectedTitle }); + afterEach(function() { + $.prompt.close(); + }); - expect($('.jqibox').length).toEqual(1); - expect($('.jqifade').length).toEqual(1); - expect($('.jqi').length).toEqual(1); - expect($('.jqi .jqititle').text()).toBe(expectedTitle); - expect($('.jqi .jqimessage').text()).toBe(expectedText); - }); + it('should be defined', function() { + + expect($.prompt).not.toBeUndefined(); + }); - }); + it('should generate markup', function() { + var expectedTitle = 'This is a title', + expectedText = 'This is a test'; + $.prompt(expectedText, { title: expectedTitle }); - // ==================================================================================== - // ==================================================================================== - describe('button creation', function() { + expect($('.jqibox')).toExist(); + expect($('.jqifade')).toExist(); + expect($('.jqi')).toExist(); + expect($('.jqi .jqititle')).toHaveText(expectedTitle); + expect($('.jqi .jqimessage')).toHaveText(expectedText); + }); - beforeEach(function() { - $.fx.off = true; // for our testing lets turn off fx }); - afterEach(function() { - $.prompt.close(); - }); + // ==================================================================================== + // ==================================================================================== + describe('button creation', function() { - it('should generate buttons from hash', function() { + beforeEach(function() { + $.fx.off = true; // for our testing lets turn off fx + }); - $.prompt('This is a test', { - buttons: { Ok:true, Cancel:false } + afterEach(function() { + $.prompt.close(); }); - var okBtn = $('#jqi_state0_buttonOk'), - cancelBtn = $('#jqi_state0_buttonCancel'); - expect($('.jqibutton').length).toBe(2); - expect(okBtn.length).toBe(1); - expect(cancelBtn.length).toBe(1); + it('should generate buttons from hash', function() { - expect(okBtn.text()).toBe('Ok'); - expect(cancelBtn.text()).toBe('Cancel'); + $.prompt('This is a test', { + buttons: { Ok:true, Cancel:false } + }); + var okBtn = $('#jqi_state0_buttonOk'), + cancelBtn = $('#jqi_state0_buttonCancel'); - expect(okBtn.val()).toBe('true'); - expect(cancelBtn.val()).toBe('false'); - }); + expect($('.jqibutton').length).toBe(2); + + expect(okBtn).toExist(); + expect(cancelBtn).toExist(); - it('should generate buttons from array', function() { + expect(okBtn).toHaveText('Ok'); + expect(cancelBtn).toHaveText('Cancel'); - $.prompt('This is a test', { - buttons: [ - { title: 'Ok', value: true }, - { title: 'Cancel', value: false } - ] + expect(okBtn).toHaveValue('true'); + expect(cancelBtn).toHaveValue('false'); }); - var okBtn = $('#jqi_state0_buttonOk'), - cancelBtn = $('#jqi_state0_buttonCancel'); - expect($('.jqibutton').length).toBe(2); + it('should generate buttons from array', function() { - expect(okBtn.length).toBe(1); - expect(cancelBtn.length).toBe(1); + $.prompt('This is a test', { + buttons: [ + { title: 'Ok', value: true }, + { title: 'Cancel', value: false } + ] + }); + var okBtn = $('#jqi_state0_buttonOk'), + cancelBtn = $('#jqi_state0_buttonCancel'); - expect(okBtn.text()).toBe('Ok'); - expect(cancelBtn.text()).toBe('Cancel'); + expect($('.jqibutton')).toHaveLength(2); - expect(okBtn.val()).toBe('true'); - expect(cancelBtn.val()).toBe('false'); - }); + expect(okBtn).toExist(); + expect(cancelBtn).toExist(); - it('should add classes to buttons', function() { + expect(okBtn).toHaveText('Ok'); + expect(cancelBtn).toHaveText('Cancel'); - $.prompt('This is a test', { - buttons: [ - { title: 'Ok', value: true, classes: ['ok1','ok2'] }, - { title: 'Cancel', value: false, classes: 'cancel1 cancel2' } - ] + expect(okBtn.val()).toBe('true'); + expect(cancelBtn.val()).toBe('false'); }); - var okBtn = $('#jqi_state0_buttonOk'), - cancelBtn = $('#jqi_state0_buttonCancel'); - expect(okBtn.hasClass('ok1')).toBe(true); - expect(okBtn.hasClass('ok2')).toBe(true); + it('should add classes to buttons', function() { - expect(cancelBtn.hasClass('cancel1')).toBe(true); - expect(cancelBtn.hasClass('cancel2')).toBe(true); - }); + $.prompt('This is a test', { + buttons: [ + { title: 'Ok', value: true, classes: ['ok1','ok2'] }, + { title: 'Cancel', value: false, classes: 'cancel1 cancel2' } + ] + }); + var okBtn = $('#jqi_state0_buttonOk'), + cancelBtn = $('#jqi_state0_buttonCancel'); - it('should add classes to buttons from classes obj', function() { + expect(okBtn).toHaveClass('ok1'); + expect(okBtn).toHaveClass('ok2'); - $.prompt('This is a test', { - buttons: [ - { title: 'Ok', value: true, classes: ['ok1','ok2'] }, - { title: 'Cancel', value: false, classes: 'cancel1 cancel2' } - ], - classes: { button: 'testclass' } + expect(cancelBtn).toHaveClass('cancel1'); + expect(cancelBtn).toHaveClass('cancel2'); }); - var okBtn = $('#jqi_state0_buttonOk'), - cancelBtn = $('#jqi_state0_buttonCancel'); - expect(okBtn.hasClass('testclass')).toBe(true); - expect(cancelBtn.hasClass('testclass')).toBe(true); - }); + it('should add classes to buttons from classes obj', function() { - it('should default correct button', function() { + $.prompt('This is a test', { + buttons: [ + { title: 'Ok', value: true, classes: ['ok1','ok2'] }, + { title: 'Cancel', value: false, classes: 'cancel1 cancel2' } + ], + classes: { button: 'testclass' } + }); + var okBtn = $('#jqi_state0_buttonOk'), + cancelBtn = $('#jqi_state0_buttonCancel'); - $.prompt('This is a test', { - buttons: [ - { title: 'Ok', value: 1 }, - { title: 'Cancel', value: 2 }, - { title: 'Another', value: 3 } - ], - focus: 1 + expect(okBtn).toHaveClass('testclass'); + expect(cancelBtn).toHaveClass('testclass'); }); - var okBtn = $('#jqi_state0_buttonOk'), - cancelBtn = $('#jqi_state0_buttonCancel'), - anotherBtn = $('#jqi_state0_buttonAnother'); - - expect(okBtn.hasClass('jqidefaultbutton')).toBe(false); - expect(cancelBtn.hasClass('jqidefaultbutton')).toBe(true); - expect(anotherBtn.hasClass('jqidefaultbutton')).toBe(false); - }); - it('should default correct button when focus on an input', function() { + it('should default correct button', function() { + + $.prompt('This is a test', { + buttons: [ + { title: 'Ok', value: 1 }, + { title: 'Cancel', value: 2 }, + { title: 'Another', value: 3 } + ], + focus: 1 + }); + var okBtn = $('#jqi_state0_buttonOk'), + cancelBtn = $('#jqi_state0_buttonCancel'), + anotherBtn = $('#jqi_state0_buttonAnother'); + + expect(okBtn).not.toHaveClass('jqidefaultbutton'); + expect(cancelBtn).toHaveClass('jqidefaultbutton'); + expect(anotherBtn).not.toHaveClass('jqidefaultbutton'); + }); - $.prompt('This is a test <input type="text" id="testInput" />', { - buttons: [ - { title: 'Ok', value: 1 }, - { title: 'Cancel', value: 2 }, - { title: 'Another', value: 3 } - ], - focus: '#testInput', - defaultButton: 1 + it('should default correct button when focus on an input', function() { + + $.prompt('This is a test <input type="text" id="testInput" />', { + buttons: [ + { title: 'Ok', value: 1 }, + { title: 'Cancel', value: 2 }, + { title: 'Another', value: 3 } + ], + focus: '#testInput', + defaultButton: 1 + }); + var okBtn = $('#jqi_state0_buttonOk'), + cancelBtn = $('#jqi_state0_buttonCancel'), + anotherBtn = $('#jqi_state0_buttonAnother'); + + expect(okBtn).not.toHaveClass('jqidefaultbutton'); + expect(cancelBtn).toHaveClass('jqidefaultbutton'); + expect(anotherBtn).not.toHaveClass('jqidefaultbutton'); }); - var okBtn = $('#jqi_state0_buttonOk'), - cancelBtn = $('#jqi_state0_buttonCancel'), - anotherBtn = $('#jqi_state0_buttonAnother'); - expect(okBtn.hasClass('jqidefaultbutton')).toBe(false); - expect(cancelBtn.hasClass('jqidefaultbutton')).toBe(true); - expect(anotherBtn.hasClass('jqidefaultbutton')).toBe(false); }); - }); - - // ==================================================================================== - // ==================================================================================== - describe('state creation', function() { + // ==================================================================================== + // ==================================================================================== + describe('state creation', function() { - beforeEach(function() { - $.fx.off = true; // for our testing lets turn off fx - }); + beforeEach(function() { + $.fx.off = true; // for our testing lets turn off fx + }); - afterEach(function() { - $.prompt.close(); - }); + afterEach(function() { + $.prompt.close(); + }); - it('should create a single state from string', function() { + it('should create a single state from string', function() { - $.prompt('This is a test'); - - expect($('.jqistate').length).toBe(1); - }); + $.prompt('This is a test'); + + expect($('.jqistate')).toExist(); + }); - it('should create states from hash', function() { - var states = { - s1: { html: 'state 1' }, - s2: { html: 'state 2' }, - s3: { html: 'state 3' } - }; + it('should create states from hash', function() { + var states = { + s1: { html: 'state 1' }, + s2: { html: 'state 2' }, + s3: { html: 'state 3' } + }; - $.prompt(states); - - expect($('.jqistate').length).toBe(3); + $.prompt(states); + + expect($('.jqistate')).toHaveLength(3); - expect($('#jqistate_s1 .jqimessage').text()).toBe(states.s1.html); - expect($('#jqistate_s2 .jqimessage').text()).toBe(states.s2.html); - expect($('#jqistate_s3 .jqimessage').text()).toBe(states.s3.html); - }); + expect($('#jqistate_s1 .jqimessage')).toHaveText(states.s1.html); + expect($('#jqistate_s2 .jqimessage')).toHaveText(states.s2.html); + expect($('#jqistate_s3 .jqimessage')).toHaveText(states.s3.html); + }); - it('should create states from array', function() { - var states = [ - { html: 'state 1' }, - { html: 'state 2' }, - { html: 'state 3' } - ]; + it('should create states from array', function() { + var states = [ + { html: 'state 1' }, + { html: 'state 2' }, + { html: 'state 3' } + ]; - $.prompt(states); - - expect($('.jqistate').length).toBe(3); + $.prompt(states); + + expect($('.jqistate')).toHaveLength(3); - expect($('#jqistate_0 .jqimessage').text()).toBe(states[0].html); - expect($('#jqistate_1 .jqimessage').text()).toBe(states[1].html); - expect($('#jqistate_2 .jqimessage').text()).toBe(states[2].html); - }); + expect($('#jqistate_0 .jqimessage')).toHaveText(states[0].html); + expect($('#jqistate_1 .jqimessage')).toHaveText(states[1].html); + expect($('#jqistate_2 .jqimessage')).toHaveText(states[2].html); + }); - it('should show the first state automatically', function() { + it('should show the first state automatically', function() { - // we can't reliably determine which entry is the first with a hash, js doesn't preserve order - var states = [ - { html: 'state 1' }, - { html: 'state 2' }, - { html: 'state 3' } - ]; + // we can't reliably determine which entry is the first with a hash, js doesn't preserve order + var states = [ + { html: 'state 1' }, + { html: 'state 2' }, + { html: 'state 3' } + ]; - $.prompt(states); + $.prompt(states); - expect($('#jqistate_0').css('display')).toBe('block'); - expect($('#jqistate_1').css('display')).toBe('none'); - expect($('#jqistate_2').css('display')).toBe('none'); - }); + expect($('#jqistate_0')).toHaveCss({display:'block'}); + expect($('#jqistate_1')).toHaveCss({display:'none'}); + expect($('#jqistate_2')).toHaveCss({display:'none'}); + }); - it('should name states properly when name specified', function() { - var states = [ - { name: 's1', html: 'state 1' }, - { name: 's2', html: 'state 2' }, - { name: 's3', html: 'state 3' } - ]; + it('should name states properly when name specified', function() { + var states = [ + { name: 's1', html: 'state 1' }, + { name: 's2', html: 'state 2' }, + { name: 's3', html: 'state 3' } + ]; - $.prompt(states); - - expect($('#jqistate_s1').length).toBe(1); - expect($('#jqistate_s2').length).toBe(1); - expect($('#jqistate_s3').length).toBe(1); + $.prompt(states); + + expect($('#jqistate_s1')).toExist(); + expect($('#jqistate_s2')).toExist(); + expect($('#jqistate_s3')).toExist(); + }); }); - }); + + }); // base structure // ==================================================================================== @@ -265,6 +272,43 @@ describe('jquery-impromptu', function() { $.prompt.close(); }); + + // ==================================================================================== + // ==================================================================================== + describe('$.prompt.setDefaults()', function() { + it('should change the default values', function() { + var origDefs = $.extend(true, {}, $.prompt.defaults), + overrides = { prefix: 'myjqi', classes: { box: 'boxclass' } }; + + $.prompt.setDefaults(overrides); + + expect($.prompt.defaults.prefix).toBe(overrides.prefix); + expect($.prompt.defaults.classes.box).toBe(overrides.classes.box); + expect($.prompt.defaults.speed).toBe(origDefs.speed); + + $.prompt.defaults = origDefs; + }); + }); + + // ==================================================================================== + // ==================================================================================== + describe('$.prompt.setStateDefaults()', function() { + it('should change the default state values', function() { + var origDefs = $.extend(true, {}, $.prompt.defaults), + overrides = { title: 'My Title', position: { width: 123 } }; + + $.prompt.setStateDefaults(overrides); + + expect($.prompt.defaults.state.title).toBe(overrides.title); + expect($.prompt.defaults.state.position.width).toBe(overrides.position.width); + expect($.prompt.defaults.state.focus).toBe(origDefs.state.focus); + + $.prompt.defaults = origDefs; + }); + }); + + // ==================================================================================== + // ==================================================================================== describe('$.prompt.get()', function() { it('should return the prompt jquery object', function() { @@ -277,6 +321,8 @@ describe('jquery-impromptu', function() { }); }); + // ==================================================================================== + // ==================================================================================== describe('$.prompt.getState()', function() { it('should return the state jquery object', function() { @@ -289,6 +335,56 @@ describe('jquery-impromptu', function() { }); }); + // ==================================================================================== + // ==================================================================================== + describe('$.prompt.getCurrentState()', function() { + it('should return the current state jquery object', function() { + + $.prompt(states); + + var actualResult = $.prompt.getCurrentState(), + expectedResult = $('#jqistate_s1'); + + expect(actualResult[0]).toBe(expectedResult[0]); + }); + + it('should return the current state jquery object after a state change', function() { + + $.prompt(states); + $.prompt.goToState('s2'); + var actualResult = $.prompt.getCurrentState(), + expectedResult = $('#jqistate_s2'); + + expect(actualResult[0]).toBe(expectedResult[0]); + }); + }); + + // ==================================================================================== + // ==================================================================================== + describe('$.prompt.getCurrentStateName()', function() { + it('should return the current state name', function() { + + $.prompt(states); + + var actualResult = $.prompt.getCurrentStateName(), + expectedResult = 's1'; + + expect(actualResult).toBe(expectedResult); + }); + + it('should return the current state name after a state change', function() { + + $.prompt(states); + $.prompt.goToState('s2'); + var actualResult = $.prompt.getCurrentStateName(), + expectedResult = 's2'; + + expect(actualResult).toBe(expectedResult); + }); + }); + + // ==================================================================================== + // ==================================================================================== describe('$.prompt.goToState()', function() { it('should make the requested state visible', function() { @@ -296,9 +392,9 @@ describe('jquery-impromptu', function() { $.prompt.goToState('s3'); - expect($('#jqistate_s1').css('display')).toBe('none'); - expect($('#jqistate_s2').css('display')).toBe('none'); - expect($('#jqistate_s3').css('display')).toBe('block'); + expect($('#jqistate_s1')).toHaveCss({display:'none'}); + expect($('#jqistate_s2')).toHaveCss({display:'none'}); + expect($('#jqistate_s3')).toHaveCss({display:'block'}); }); it('should do nothing if the state is not available', function() { @@ -307,9 +403,9 @@ describe('jquery-impromptu', function() { $.prompt.goToState('s4'); - expect($('#jqistate_s1').css('display')).toBe('block'); - expect($('#jqistate_s2').css('display')).toBe('none'); - expect($('#jqistate_s3').css('display')).toBe('none'); + expect($('#jqistate_s1')).toHaveCss({display:'block'}); + expect($('#jqistate_s2')).toHaveCss({display:'none'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); }); it('should handle substate option', function() { @@ -318,14 +414,16 @@ describe('jquery-impromptu', function() { $.prompt.goToState('s2',true); - expect($('#jqistate_s1').css('display')).toBe('block'); - expect($('#jqistate_s2').css('display')).toBe('block'); - expect($('#jqistate_s3').css('display')).toBe('none'); + expect($('#jqistate_s1')).toHaveCss({display:'block'}); + expect($('#jqistate_s2')).toHaveCss({display:'block'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); - expect($('#jqistate_s2').hasClass('jqisubstate')).toBe(true); + expect($('#jqistate_s2')).toHaveClass('jqisubstate'); }); }); + // ==================================================================================== + // ==================================================================================== describe('$.prompt.nextState()', function() { it('should make the next state visible', function() { @@ -333,9 +431,9 @@ describe('jquery-impromptu', function() { $.prompt.nextState(); - expect($('#jqistate_s1').css('display')).toBe('none'); - expect($('#jqistate_s2').css('display')).toBe('block'); - expect($('#jqistate_s3').css('display')).toBe('none'); + expect($('#jqistate_s1')).toHaveCss({display:'none'}); + expect($('#jqistate_s2')).toHaveCss({display:'block'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); }); it('should do nothing if the state is not available', function() { @@ -345,12 +443,14 @@ describe('jquery-impromptu', function() { $.prompt.goToState('s3'); $.prompt.nextState(); - expect($('#jqistate_s1').css('display')).toBe('none'); - expect($('#jqistate_s2').css('display')).toBe('none'); - expect($('#jqistate_s3').css('display')).toBe('block'); + expect($('#jqistate_s1')).toHaveCss({display:'none'}); + expect($('#jqistate_s2')).toHaveCss({display:'none'}); + expect($('#jqistate_s3')).toHaveCss({display:'block'}); }); }); + // ==================================================================================== + // ==================================================================================== describe('$.prompt.prevState()', function() { it('should make the previous state visible', function() { @@ -359,9 +459,9 @@ describe('jquery-impromptu', function() { $.prompt.goToState('s3'); $.prompt.prevState(); - expect($('#jqistate_s1').css('display')).toBe('none'); - expect($('#jqistate_s2').css('display')).toBe('block'); - expect($('#jqistate_s3').css('display')).toBe('none'); + expect($('#jqistate_s1')).toHaveCss({display:'none'}); + expect($('#jqistate_s2')).toHaveCss({display:'block'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); }); it('should do nothing if the state is not available', function() { @@ -370,12 +470,14 @@ describe('jquery-impromptu', function() { $.prompt.prevState(); - expect($('#jqistate_s1').css('display')).toBe('block'); - expect($('#jqistate_s2').css('display')).toBe('none'); - expect($('#jqistate_s3').css('display')).toBe('none'); + expect($('#jqistate_s1')).toHaveCss({display:'block'}); + expect($('#jqistate_s2')).toHaveCss({display:'none'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); }); }); + // ==================================================================================== + // ==================================================================================== describe('$.prompt.addState()', function() { it('should add a new state as the last state', function() { var newState = { @@ -390,19 +492,19 @@ describe('jquery-impromptu', function() { var $stateobj = $.prompt.addState(newState.name, newState); // element created? - expect($stateobj.length).toBe(1); + expect($stateobj).toExist(); // element in the right place? - expect($stateobj.prev().attr('id')).toBe('jqistate_s3'); + expect($stateobj.prev()).toHaveId('jqistate_s3'); // element visibility correct? - expect($('#jqistate_s1').css('display')).toBe('block'); - expect($stateobj.css('display')).toBe('none'); + expect($('#jqistate_s1')).toHaveCss({display:'block'}); + expect($stateobj).toHaveCss({display:'none'}); // content generated ok? - expect($stateobj.find('.jqimessage').text()).toBe(newState.html); - expect($stateobj.find('.jqititle').text()).toBe(newState.title); - expect($stateobj.find('.jqibutton').length).toBe(2); + expect($stateobj.find('.jqimessage')).toHaveText(newState.html); + expect($stateobj.find('.jqititle')).toHaveText(newState.title); + expect($stateobj.find('.jqibutton')).toHaveLength(2); }); it('should add a new state after specified state', function() { @@ -418,10 +520,12 @@ describe('jquery-impromptu', function() { var $stateobj = $.prompt.addState(newState.name, newState, afterState); - expect($stateobj.prev().attr('id')).toBe('jqistate_'+afterState); + expect($stateobj.prev()).toHaveId('jqistate_'+afterState); }); }); + // ==================================================================================== + // ==================================================================================== describe('$.prompt.removeState()', function() { it('should remove the specified state', function() { @@ -429,21 +533,233 @@ describe('jquery-impromptu', function() { $.prompt.removeState('s2'); - expect($('#jqistate_s2').length).toBe(0); + expect($('#jqistate_s2')).not.toExist(); }); - /* - it('should display next visible state', function() { + + it('should display next state', function() { $.prompt(states); $.prompt.removeState('s1'); - expect($('#jqistate_s2').css('display')).toBe('block'); - expect($('#jqistate_s3').css('display')).toBe('none'); + expect($('#jqistate_s2')).toHaveCss({display:'block'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); + }); + + it('should display previous state', function() { + + $.prompt(states); + $.prompt.goToState('s3'); + $.prompt.removeState('s3'); + + expect($('#jqistate_s1')).toHaveCss({display:'none'}); + expect($('#jqistate_s2')).toHaveCss({display:'block'}); + }); + + }); + + }); // end api methods + + // ==================================================================================== + // ==================================================================================== + describe('events', function() { + var states = [ + { name: 's1', html: 'state 1' }, + { name: 's2', html: 'state 2' }, + { name: 's3', html: 'state 3' } + ], + maxWaitTime = 100; + + beforeEach(function() { + $.fx.off = true; // for our testing lets turn off fx + + }); + + afterEach(function() { + $.prompt.close(); + }); + + // ==================================================================================== + // ==================================================================================== + describe('impromptu:loaded', function(){ + + it('should fire event', function(){ + var spyEventCalled = false; + + $('body').on('impromptu:loaded', '.jqibox', function(){ spyEventCalled = true; }); + $.prompt(states); + + waitsFor(function(){ + return spyEventCalled; + }, 'event should have been called',maxWaitTime); + + runs(function(){ + expect(spyEventCalled).toBe(true); + }); }); - */ + + it('should allow event function as option parameter', function(){ + var spyEventCalled = false; + + $.prompt(states, { loaded: function(){ spyEventCalled = true; } }); + + waitsFor(function(){ + return spyEventCalled; + }, 'event should have been called',maxWaitTime); + + runs(function(){ + expect(spyEventCalled).toBe(true); + }); + }); + + }); + + // ==================================================================================== + // ==================================================================================== + describe('impromptu:close', function(){ + + it('should fire event', function(){ + var spyEventCalled = false; + + $('body').on('impromptu:close', '.jqibox', function(){ spyEventCalled = true; }); + $.prompt(states, { + loaded: function(){ + $.prompt.close(); + } + }); + + waitsFor(function(){ + return spyEventCalled; + }, 'event should have been called',maxWaitTime); + + runs(function(){ + expect(spyEventCalled).toBe(true); + }); + }); + + it('should allow event function as option parameter', function(){ + var spyEventCalled = false; + + $.prompt(states, { + loaded: function(){ $.prompt.close(); }, + close: function(){ spyEventCalled = true; } + }); + + waitsFor(function(){ + return spyEventCalled; + }, 'event should have been called called',maxWaitTime); + + runs(function(){ + expect(spyEventCalled).toBe(true); + }); + }); + + }); + + // ==================================================================================== + // ==================================================================================== + describe('impromptu:statechanging', function(){ + + it('should fire event', function(){ + var spyEventCalled = false; + + $('body').on('impromptu:statechanging', '.jqibox', function(){ spyEventCalled = true; }); + $.prompt(states, { + loaded: function(){ + $.prompt.goToState('s2'); + } + }); + + waitsFor(function(){ + return spyEventCalled; + }, 'event should have been called',maxWaitTime); + + runs(function(){ + expect(spyEventCalled).toBe(true); + }); + }); + + it('should allow event function as option parameter', function(){ + var spyEventCalled = false; + + $.prompt(states, { + loaded: function(){ + $.prompt.goToState('s2'); + }, + statechanging: function(){ spyEventCalled = true; } + }); + + waitsFor(function(){ + return spyEventCalled; + }, 'event should have been called called',maxWaitTime); + + runs(function(){ + expect(spyEventCalled).toBe(true); + }); + }); + + it('should allow preventDefault', function(){ + var spyEvent = spyOnEvent('body', 'impromptu:statechanging'); + + $.prompt(states, { + loaded: function(){ + $.prompt.goToState('s2'); + }, + statechanging: function(e){ + e.preventDefault(); + } + }); + + expect(spyEvent).toHaveBeenTriggered(); + expect(spyEvent).toHaveBeenPrevented(); + }); + + }); + + // ==================================================================================== + // ==================================================================================== + describe('impromptu:statechanged', function(){ + + it('should fire event', function(){ + var spyEventCalled = false; + + $('body').on('impromptu:statechanged', '.jqibox', function(){ spyEventCalled = true; }); + $.prompt(states, { + loaded: function(){ + $.prompt.goToState('s2'); + } + }); + + waitsFor(function(){ + return spyEventCalled; + }, 'event should have been called',maxWaitTime); + + runs(function(){ + expect(spyEventCalled).toBe(true); + }); + }); + + it('should allow event function as option parameter', function(){ + var spyEventCalled = false; + + $.prompt(states, { + loaded: function(){ + $.prompt.goToState('s2'); + }, + statechanged: function(){ spyEventCalled = true; } + }); + + waitsFor(function(){ + return spyEventCalled; + }, 'event should have been called called',maxWaitTime); + + runs(function(){ + expect(spyEventCalled).toBe(true); + }); + }); + }); - }); + }); // end events }); diff --git a/test/lib/jasmine-jquery.js b/test/lib/jasmine-jquery.js index 3a5a028..a0f2860 100644..100755 --- a/test/lib/jasmine-jquery.js +++ b/test/lib/jasmine-jquery.js @@ -1,451 +1,400 @@ /*! - Jasmine-jQuery: a set of jQuery helpers for Jasmine tests. +Jasmine-jQuery: a set of jQuery helpers for Jasmine tests. - Version 1.5.2 +Version 1.7.0 - https://github.com/velesin/jasmine-jquery +https://github.com/velesin/jasmine-jquery - Copyright (c) 2010-2013 Wojciech Zawistowski, Travis Jeffery +Copyright (c) 2010-2013 Wojciech Zawistowski, Travis Jeffery - Permission is hereby granted, free of charge, to any person obtaining - a copy of this software and associated documentation files (the - "Software"), to deal in the Software without restriction, including - without limitation the rights to use, copy, modify, merge, publish, - distribute, sublicense, and/or sell copies of the Software, and to - permit persons to whom the Software is furnished to do so, subject to - the following conditions: +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: - The above copyright notice and this permission notice shall be - included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -var readFixtures = function() { - return jasmine.getFixtures().proxyCallTo_('read', arguments) -} - -var preloadFixtures = function() { - jasmine.getFixtures().proxyCallTo_('preload', arguments) -} -var loadFixtures = function() { - jasmine.getFixtures().proxyCallTo_('load', arguments) -} ++function (jasmine, $) { "use strict"; -var appendLoadFixtures = function() { - jasmine.getFixtures().proxyCallTo_('appendLoad', arguments) -} + jasmine.spiedEventsKey = function (selector, eventName) { + return [$(selector).selector, eventName].toString() + } -var setFixtures = function(html) { - jasmine.getFixtures().proxyCallTo_('set', arguments) -} + jasmine.getFixtures = function () { + return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures() + } -var appendSetFixtures = function() { - jasmine.getFixtures().proxyCallTo_('appendSet', arguments) -} + jasmine.getStyleFixtures = function () { + return jasmine.currentStyleFixtures_ = jasmine.currentStyleFixtures_ || new jasmine.StyleFixtures() + } -var sandbox = function(attributes) { - return jasmine.getFixtures().sandbox(attributes) -} - -var spyOnEvent = function(selector, eventName) { - return jasmine.JQuery.events.spyOn(selector, eventName) -} - -var preloadStyleFixtures = function() { - jasmine.getStyleFixtures().proxyCallTo_('preload', arguments) -} - -var loadStyleFixtures = function() { - jasmine.getStyleFixtures().proxyCallTo_('load', arguments) -} - -var appendLoadStyleFixtures = function() { - jasmine.getStyleFixtures().proxyCallTo_('appendLoad', arguments) -} - -var setStyleFixtures = function(html) { - jasmine.getStyleFixtures().proxyCallTo_('set', arguments) -} - -var appendSetStyleFixtures = function(html) { - jasmine.getStyleFixtures().proxyCallTo_('appendSet', arguments) -} - -var loadJSONFixtures = function() { - return jasmine.getJSONFixtures().proxyCallTo_('load', arguments) -} - -var getJSONFixture = function(url) { - return jasmine.getJSONFixtures().proxyCallTo_('read', arguments)[url] -} - -jasmine.spiedEventsKey = function (selector, eventName) { - return [$(selector).selector, eventName].toString() -} - -jasmine.getFixtures = function() { - return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures() -} - -jasmine.getStyleFixtures = function() { - return jasmine.currentStyleFixtures_ = jasmine.currentStyleFixtures_ || new jasmine.StyleFixtures() -} - -jasmine.Fixtures = function() { - this.containerId = 'jasmine-fixtures' - this.fixturesCache_ = {} - this.fixturesPath = 'spec/javascripts/fixtures' -} - -jasmine.Fixtures.prototype.set = function(html) { - this.cleanUp() - this.createContainer_(html) -} - -jasmine.Fixtures.prototype.appendSet= function(html) { - this.addToContainer_(html) -} - -jasmine.Fixtures.prototype.preload = function() { - this.read.apply(this, arguments) -} - -jasmine.Fixtures.prototype.load = function() { - this.cleanUp() - this.createContainer_(this.read.apply(this, arguments)) -} - -jasmine.Fixtures.prototype.appendLoad = function() { - this.addToContainer_(this.read.apply(this, arguments)) -} - -jasmine.Fixtures.prototype.read = function() { - var htmlChunks = [] - - var fixtureUrls = arguments - for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) { - htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex])) - } - - return htmlChunks.join('') -} - -jasmine.Fixtures.prototype.clearCache = function() { - this.fixturesCache_ = {} -} - -jasmine.Fixtures.prototype.cleanUp = function() { - $('#' + this.containerId).remove() -} - -jasmine.Fixtures.prototype.sandbox = function(attributes) { - var attributesToSet = attributes || {} - return $('<div id="sandbox" />').attr(attributesToSet) -} - -jasmine.Fixtures.prototype.createContainer_ = function(html) { - var container - if(html instanceof $) { - container = $('<div id="' + this.containerId + '" />') - container.html(html) - } else { - container = '<div id="' + this.containerId + '">' + html + '</div>' - } - $(document.body).append(container) -} - -jasmine.Fixtures.prototype.addToContainer_ = function(html){ - var container = $(document.body).find('#'+this.containerId).append(html) - if(!container.length){ - this.createContainer_(html) - } -} - -jasmine.Fixtures.prototype.getFixtureHtml_ = function(url) { - if (typeof this.fixturesCache_[url] === 'undefined') { - this.loadFixtureIntoCache_(url) - } - return this.fixturesCache_[url] -} - -jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) { - var url = this.makeFixtureUrl_(relativeUrl) - var request = $.ajax({ - type: "GET", - url: url + "?" + new Date().getTime(), - async: false - }) - this.fixturesCache_[relativeUrl] = request.responseText -} + jasmine.Fixtures = function () { + this.containerId = 'jasmine-fixtures' + this.fixturesCache_ = {} + this.fixturesPath = 'spec/javascripts/fixtures' + } -jasmine.Fixtures.prototype.makeFixtureUrl_ = function(relativeUrl){ - return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl -} + jasmine.Fixtures.prototype.set = function (html) { + this.cleanUp() + return this.createContainer_(html) + } -jasmine.Fixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) { - return this[methodName].apply(this, passedArguments) -} + jasmine.Fixtures.prototype.appendSet= function (html) { + this.addToContainer_(html) + } + jasmine.Fixtures.prototype.preload = function () { + this.read.apply(this, arguments) + } -jasmine.StyleFixtures = function() { - this.fixturesCache_ = {} - this.fixturesNodes_ = [] - this.fixturesPath = 'spec/javascripts/fixtures' -} + jasmine.Fixtures.prototype.load = function () { + this.cleanUp() + this.createContainer_(this.read.apply(this, arguments)) + } -jasmine.StyleFixtures.prototype.set = function(css) { - this.cleanUp() - this.createStyle_(css) -} + jasmine.Fixtures.prototype.appendLoad = function () { + this.addToContainer_(this.read.apply(this, arguments)) + } -jasmine.StyleFixtures.prototype.appendSet = function(css) { - this.createStyle_(css) -} + jasmine.Fixtures.prototype.read = function () { + var htmlChunks = [] + , fixtureUrls = arguments -jasmine.StyleFixtures.prototype.preload = function() { - this.read_.apply(this, arguments) -} + for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) { + htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex])) + } -jasmine.StyleFixtures.prototype.load = function() { - this.cleanUp() - this.createStyle_(this.read_.apply(this, arguments)) -} + return htmlChunks.join('') + } -jasmine.StyleFixtures.prototype.appendLoad = function() { - this.createStyle_(this.read_.apply(this, arguments)) -} + jasmine.Fixtures.prototype.clearCache = function () { + this.fixturesCache_ = {} + } -jasmine.StyleFixtures.prototype.cleanUp = function() { - while(this.fixturesNodes_.length) { - this.fixturesNodes_.pop().remove() + jasmine.Fixtures.prototype.cleanUp = function () { + $('#' + this.containerId).remove() } -} -jasmine.StyleFixtures.prototype.createStyle_ = function(html) { - var styleText = $('<div></div>').html(html).text(), - style = $('<style>' + styleText + '</style>') + jasmine.Fixtures.prototype.sandbox = function (attributes) { + var attributesToSet = attributes || {} + return $('<div id="sandbox" />').attr(attributesToSet) + } - this.fixturesNodes_.push(style) + jasmine.Fixtures.prototype.createContainer_ = function (html) { + var container = $('<div>') + .attr('id', this.containerId) + .html(html) - $('head').append(style) -} + $(document.body).append(container) + return container + } -jasmine.StyleFixtures.prototype.clearCache = jasmine.Fixtures.prototype.clearCache + jasmine.Fixtures.prototype.addToContainer_ = function (html){ + var container = $(document.body).find('#'+this.containerId).append(html) + if(!container.length){ + this.createContainer_(html) + } + } -jasmine.StyleFixtures.prototype.read_ = jasmine.Fixtures.prototype.read + jasmine.Fixtures.prototype.getFixtureHtml_ = function (url) { + if (typeof this.fixturesCache_[url] === 'undefined') { + this.loadFixtureIntoCache_(url) + } + return this.fixturesCache_[url] + } -jasmine.StyleFixtures.prototype.getFixtureHtml_ = jasmine.Fixtures.prototype.getFixtureHtml_ + jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) { + var self = this + , url = this.makeFixtureUrl_(relativeUrl) + , request = $.ajax({ + async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded + cache: false, + url: url, + success: function (data, status, $xhr) { + self.fixturesCache_[relativeUrl] = $xhr.responseText + }, + error: function (jqXHR, status, errorThrown) { + throw new Error('Fixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')') + } + }) + } -jasmine.StyleFixtures.prototype.loadFixtureIntoCache_ = jasmine.Fixtures.prototype.loadFixtureIntoCache_ + jasmine.Fixtures.prototype.makeFixtureUrl_ = function (relativeUrl){ + return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl + } -jasmine.StyleFixtures.prototype.makeFixtureUrl_ = jasmine.Fixtures.prototype.makeFixtureUrl_ + jasmine.Fixtures.prototype.proxyCallTo_ = function (methodName, passedArguments) { + return this[methodName].apply(this, passedArguments) + } -jasmine.StyleFixtures.prototype.proxyCallTo_ = jasmine.Fixtures.prototype.proxyCallTo_ -jasmine.getJSONFixtures = function() { - return jasmine.currentJSONFixtures_ = jasmine.currentJSONFixtures_ || new jasmine.JSONFixtures() -} + jasmine.StyleFixtures = function () { + this.fixturesCache_ = {} + this.fixturesNodes_ = [] + this.fixturesPath = 'spec/javascripts/fixtures' + } -jasmine.JSONFixtures = function() { - this.fixturesCache_ = {} - this.fixturesPath = 'spec/javascripts/fixtures/json' -} + jasmine.StyleFixtures.prototype.set = function (css) { + this.cleanUp() + this.createStyle_(css) + } -jasmine.JSONFixtures.prototype.load = function() { - this.read.apply(this, arguments) - return this.fixturesCache_ -} + jasmine.StyleFixtures.prototype.appendSet = function (css) { + this.createStyle_(css) + } -jasmine.JSONFixtures.prototype.read = function() { - var fixtureUrls = arguments - for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) { - this.getFixtureData_(fixtureUrls[urlIndex]) + jasmine.StyleFixtures.prototype.preload = function () { + this.read_.apply(this, arguments) } - return this.fixturesCache_ -} -jasmine.JSONFixtures.prototype.clearCache = function() { - this.fixturesCache_ = {} -} + jasmine.StyleFixtures.prototype.load = function () { + this.cleanUp() + this.createStyle_(this.read_.apply(this, arguments)) + } -jasmine.JSONFixtures.prototype.getFixtureData_ = function(url) { - this.loadFixtureIntoCache_(url) - return this.fixturesCache_[url] -} + jasmine.StyleFixtures.prototype.appendLoad = function () { + this.createStyle_(this.read_.apply(this, arguments)) + } -jasmine.JSONFixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) { - var self = this - var url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl - $.ajax({ - async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded - cache: false, - dataType: 'json', - url: url, - success: function(data) { - self.fixturesCache_[relativeUrl] = data - }, - error: function(jqXHR, status, errorThrown) { - throw Error('JSONFixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')') + jasmine.StyleFixtures.prototype.cleanUp = function () { + while(this.fixturesNodes_.length) { + this.fixturesNodes_.pop().remove() } - }) -} + } -jasmine.JSONFixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) { - return this[methodName].apply(this, passedArguments) -} + jasmine.StyleFixtures.prototype.createStyle_ = function (html) { + var styleText = $('<div></div>').html(html).text() + , style = $('<style>' + styleText + '</style>') -jasmine.JQuery = function() {} + this.fixturesNodes_.push(style) + $('head').append(style) + } -jasmine.JQuery.browserTagCaseIndependentHtml = function(html) { - return $('<div/>').append(html).html() -} + jasmine.StyleFixtures.prototype.clearCache = jasmine.Fixtures.prototype.clearCache + jasmine.StyleFixtures.prototype.read_ = jasmine.Fixtures.prototype.read + jasmine.StyleFixtures.prototype.getFixtureHtml_ = jasmine.Fixtures.prototype.getFixtureHtml_ + jasmine.StyleFixtures.prototype.loadFixtureIntoCache_ = jasmine.Fixtures.prototype.loadFixtureIntoCache_ + jasmine.StyleFixtures.prototype.makeFixtureUrl_ = jasmine.Fixtures.prototype.makeFixtureUrl_ + jasmine.StyleFixtures.prototype.proxyCallTo_ = jasmine.Fixtures.prototype.proxyCallTo_ -jasmine.JQuery.elementToString = function(element) { - var domEl = $(element).get(0) - if (domEl == undefined || domEl.cloneNode) - return $('<div />').append($(element).clone()).html() - else - return element.toString() -} + jasmine.getJSONFixtures = function () { + return jasmine.currentJSONFixtures_ = jasmine.currentJSONFixtures_ || new jasmine.JSONFixtures() + } + + jasmine.JSONFixtures = function () { + this.fixturesCache_ = {} + this.fixturesPath = 'spec/javascripts/fixtures/json' + } -jasmine.JQuery.matchersClass = {} + jasmine.JSONFixtures.prototype.load = function () { + this.read.apply(this, arguments) + return this.fixturesCache_ + } + + jasmine.JSONFixtures.prototype.read = function () { + var fixtureUrls = arguments + + for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) { + this.getFixtureData_(fixtureUrls[urlIndex]) + } + + return this.fixturesCache_ + } + + jasmine.JSONFixtures.prototype.clearCache = function () { + this.fixturesCache_ = {} + } + + jasmine.JSONFixtures.prototype.getFixtureData_ = function (url) { + if (!this.fixturesCache_[url]) this.loadFixtureIntoCache_(url) + return this.fixturesCache_[url] + } + + jasmine.JSONFixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) { + var self = this + , url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl + + $.ajax({ + async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded + cache: false, + dataType: 'json', + url: url, + success: function (data) { + self.fixturesCache_[relativeUrl] = data + }, + error: function (jqXHR, status, errorThrown) { + throw new Error('JSONFixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')') + } + }) + } + + jasmine.JSONFixtures.prototype.proxyCallTo_ = function (methodName, passedArguments) { + return this[methodName].apply(this, passedArguments) + } + + jasmine.jQuery = function () {} + + jasmine.jQuery.browserTagCaseIndependentHtml = function (html) { + return $('<div/>').append(html).html() + } + + jasmine.jQuery.elementToString = function (element) { + return $(element).map(function () { return this.outerHTML; }).toArray().join(', ') + } + + jasmine.jQuery.matchersClass = {} -!function(namespace) { var data = { - spiedEvents: {}, - handlers: [] + spiedEvents: {} + , handlers: [] } - namespace.events = { - spyOn: function(selector, eventName) { - var handler = function(e) { + jasmine.jQuery.events = { + spyOn: function (selector, eventName) { + var handler = function (e) { data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] = jasmine.util.argsToArray(arguments) } + $(selector).on(eventName, handler) data.handlers.push(handler) + return { selector: selector, eventName: eventName, handler: handler, - reset: function(){ + reset: function (){ delete data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] } } }, - args: function(selector, eventName) { - var actualArgs = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]; + args: function (selector, eventName) { + var actualArgs = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] if (!actualArgs) { - throw "There is no spy for " + eventName + " on " + selector.toString() + ". Make sure to create a spy using spyOnEvent."; + throw "There is no spy for " + eventName + " on " + selector.toString() + ". Make sure to create a spy using spyOnEvent." } - return actualArgs; + return actualArgs }, - wasTriggered: function(selector, eventName) { + wasTriggered: function (selector, eventName) { return !!(data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]) }, - wasTriggeredWith: function(selector, eventName, expectedArgs, env) { - var actualArgs = jasmine.JQuery.events.args(selector, eventName).slice(1); + wasTriggeredWith: function (selector, eventName, expectedArgs, env) { + var actualArgs = jasmine.jQuery.events.args(selector, eventName).slice(1) if (Object.prototype.toString.call(expectedArgs) !== '[object Array]') { - actualArgs = actualArgs[0]; + actualArgs = actualArgs[0] } - return env.equals_(expectedArgs, actualArgs); + return env.equals_(expectedArgs, actualArgs) }, - wasPrevented: function(selector, eventName) { - var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)], - e = args ? args[0] : undefined; + wasPrevented: function (selector, eventName) { + var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] + , e = args ? args[0] : undefined + return e && e.isDefaultPrevented() }, - wasStopped: function(selector, eventName) { - var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)], - e = args ? args[0] : undefined; + wasStopped: function (selector, eventName) { + var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] + , e = args ? args[0] : undefined return e && e.isPropagationStopped() }, - cleanUp: function() { + cleanUp: function () { data.spiedEvents = {} data.handlers = [] } } -}(jasmine.JQuery) -!function(){ var jQueryMatchers = { - toHaveClass: function(className) { + toHaveClass: function (className) { return this.actual.hasClass(className) }, - toHaveCss: function(css){ + toHaveCss: function (css){ for (var prop in css){ - if (this.actual.css(prop) !== css[prop]) return false + var value = css[prop] + // see issue #147 on gh + ;if (value === 'auto' && this.actual.get(0).style[prop] === 'auto') continue + if (this.actual.css(prop) !== value) return false } return true }, - toBeVisible: function() { + toBeVisible: function () { return this.actual.is(':visible') }, - toBeHidden: function() { + toBeHidden: function () { return this.actual.is(':hidden') }, - toBeSelected: function() { + toBeSelected: function () { return this.actual.is(':selected') }, - toBeChecked: function() { + toBeChecked: function () { return this.actual.is(':checked') }, - toBeEmpty: function() { + toBeEmpty: function () { return this.actual.is(':empty') }, - toExist: function() { - return $(document).find(this.actual).length + toBeInDOM: function () { + return $.contains(document.documentElement, this.actual[0]) + }, + + toExist: function () { + return this.actual.length }, - toHaveLength: function(length) { + toHaveLength: function (length) { return this.actual.length === length }, - toHaveAttr: function(attributeName, expectedAttributeValue) { + toHaveAttr: function (attributeName, expectedAttributeValue) { return hasProperty(this.actual.attr(attributeName), expectedAttributeValue) }, - toHaveProp: function(propertyName, expectedPropertyValue) { + toHaveProp: function (propertyName, expectedPropertyValue) { return hasProperty(this.actual.prop(propertyName), expectedPropertyValue) }, - toHaveId: function(id) { + toHaveId: function (id) { return this.actual.attr('id') == id }, - toHaveHtml: function(html) { - return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html) + toHaveHtml: function (html) { + return this.actual.html() == jasmine.jQuery.browserTagCaseIndependentHtml(html) }, - toContainHtml: function(html){ + toContainHtml: function (html){ var actualHtml = this.actual.html() - var expectedHtml = jasmine.JQuery.browserTagCaseIndependentHtml(html) + , expectedHtml = jasmine.jQuery.browserTagCaseIndependentHtml(html) + return (actualHtml.indexOf(expectedHtml) >= 0) }, - toHaveText: function(text) { + toHaveText: function (text) { var trimmedText = $.trim(this.actual.text()) + if (text && $.isFunction(text.test)) { return text.test(trimmedText) } else { @@ -453,41 +402,45 @@ jasmine.JQuery.matchersClass = {} } }, - toContainText: function(text) { + toContainText: function (text) { var trimmedText = $.trim(this.actual.text()) + if (text && $.isFunction(text.test)) { return text.test(trimmedText) } else { - return trimmedText.indexOf(text) != -1; + return trimmedText.indexOf(text) != -1 } }, - toHaveValue: function(value) { + toHaveValue: function (value) { return this.actual.val() === value }, - toHaveData: function(key, expectedValue) { + toHaveData: function (key, expectedValue) { return hasProperty(this.actual.data(key), expectedValue) }, - toBe: function(selector) { + toBe: function (selector) { return this.actual.is(selector) }, - toContain: function(selector) { + toContain: function (selector) { return this.actual.find(selector).length }, - toBeDisabled: function(selector){ + toBeMatchedBy: function (selector) { + return this.actual.filter(selector).length + }, + + toBeDisabled: function (selector){ return this.actual.is(':disabled') }, - toBeFocused: function(selector) { + toBeFocused: function (selector) { return this.actual[0] === this.actual[0].ownerDocument.activeElement }, - toHandle: function(event) { - + toHandle: function (event) { var events = $._data(this.actual.get(0), "events") if(!events || !event || typeof event !== "string") { @@ -495,13 +448,14 @@ jasmine.JQuery.matchersClass = {} } var namespaces = event.split(".") - var eventType = namespaces.shift() - var sortedNamespaces = namespaces.slice(0).sort() - var namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)") + , eventType = namespaces.shift() + , sortedNamespaces = namespaces.slice(0).sort() + , namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)") if(events[eventType] && namespaces.length) { for(var i = 0; i < events[eventType].length; i++) { var namespace = events[eventType][i].namespace + if(namespaceRegExp.test(namespace)) { return true } @@ -511,33 +465,38 @@ jasmine.JQuery.matchersClass = {} } }, - // tests the existence of a specific event binding + handler - toHandleWith: function(eventName, eventHandler) { - var stack = $._data(this.actual.get(0), "events")[eventName] + toHandleWith: function (eventName, eventHandler) { + var normalizedEventName = eventName.split('.')[0] + , stack = $._data(this.actual.get(0), "events")[normalizedEventName] + for (var i = 0; i < stack.length; i++) { if (stack[i].handler == eventHandler) return true } + return false } } - var hasProperty = function(actualValue, expectedValue) { + var hasProperty = function (actualValue, expectedValue) { if (expectedValue === undefined) return actualValue !== undefined - return actualValue == expectedValue + + return actualValue === expectedValue } - var bindMatcher = function(methodName) { + var bindMatcher = function (methodName) { var builtInMatcher = jasmine.Matchers.prototype[methodName] - jasmine.JQuery.matchersClass[methodName] = function() { + jasmine.jQuery.matchersClass[methodName] = function () { if (this.actual && (this.actual instanceof $ || jasmine.isDomNode(this.actual))) { this.actual = $(this.actual) var result = jQueryMatchers[methodName].apply(this, arguments) - var element + , element + if (this.actual.get && (element = this.actual.get()[0]) && !$.isWindow(element) && element.tagName !== "HTML") - this.actual = jasmine.JQuery.elementToString(this.actual) + this.actual = jasmine.jQuery.elementToString(this.actual) + return result } @@ -552,108 +511,195 @@ jasmine.JQuery.matchersClass = {} for(var methodName in jQueryMatchers) { bindMatcher(methodName) } -}() - -beforeEach(function() { - this.addMatchers(jasmine.JQuery.matchersClass) - this.addMatchers({ - toHaveBeenTriggeredOn: function(selector) { - this.message = function() { - return [ - "Expected event " + this.actual + " to have been triggered on " + selector, - "Expected event " + this.actual + " not to have been triggered on " + selector - ] + + beforeEach(function () { + this.addMatchers(jasmine.jQuery.matchersClass) + this.addMatchers({ + toHaveBeenTriggeredOn: function (selector) { + this.message = function () { + return [ + "Expected event " + this.actual + " to have been triggered on " + selector, + "Expected event " + this.actual + " not to have been triggered on " + selector + ] + } + return jasmine.jQuery.events.wasTriggered(selector, this.actual) } - return jasmine.JQuery.events.wasTriggered(selector, this.actual) - } - }) - this.addMatchers({ - toHaveBeenTriggered: function(){ - var eventName = this.actual.eventName, - selector = this.actual.selector - this.message = function() { - return [ - "Expected event " + eventName + " to have been triggered on " + selector, - "Expected event " + eventName + " not to have been triggered on " + selector - ] + }) + + this.addMatchers({ + toHaveBeenTriggered: function (){ + var eventName = this.actual.eventName + , selector = this.actual.selector + + this.message = function () { + return [ + "Expected event " + eventName + " to have been triggered on " + selector, + "Expected event " + eventName + " not to have been triggered on " + selector + ] + } + + return jasmine.jQuery.events.wasTriggered(selector, eventName) } - return jasmine.JQuery.events.wasTriggered(selector, eventName) - } - }) - this.addMatchers({ - toHaveBeenTriggeredOnAndWith: function() { - var selector = arguments[0], - expectedArgs = arguments[1], - wasTriggered = jasmine.JQuery.events.wasTriggered(selector, this.actual); - this.message = function() { - if (wasTriggered) { - var actualArgs = jasmine.JQuery.events.args(selector, this.actual, expectedArgs)[1]; + }) + + this.addMatchers({ + toHaveBeenTriggeredOnAndWith: function () { + var selector = arguments[0] + , expectedArgs = arguments[1] + , wasTriggered = jasmine.jQuery.events.wasTriggered(selector, this.actual) + + this.message = function () { + if (wasTriggered) { + var actualArgs = jasmine.jQuery.events.args(selector, this.actual, expectedArgs)[1] + return [ + "Expected event " + this.actual + " to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs), + "Expected event " + this.actual + " not to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs) + ] + } else { + return [ + "Expected event " + this.actual + " to have been triggered on " + selector, + "Expected event " + this.actual + " not to have been triggered on " + selector + ] + } + } + + return wasTriggered && jasmine.jQuery.events.wasTriggeredWith(selector, this.actual, expectedArgs, this.env) + } + }) + + this.addMatchers({ + toHaveBeenPreventedOn: function (selector) { + this.message = function () { return [ - "Expected event " + this.actual + " to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs), - "Expected event " + this.actual + " not to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs) + "Expected event " + this.actual + " to have been prevented on " + selector, + "Expected event " + this.actual + " not to have been prevented on " + selector ] - } else { + } + + return jasmine.jQuery.events.wasPrevented(selector, this.actual) + } + }) + + this.addMatchers({ + toHaveBeenPrevented: function () { + var eventName = this.actual.eventName + , selector = this.actual.selector + this.message = function () { return [ - "Expected event " + this.actual + " to have been triggered on " + selector, - "Expected event " + this.actual + " not to have been triggered on " + selector + "Expected event " + eventName + " to have been prevented on " + selector, + "Expected event " + eventName + " not to have been prevented on " + selector ] } + + return jasmine.jQuery.events.wasPrevented(selector, eventName) } - return wasTriggered && jasmine.JQuery.events.wasTriggeredWith(selector, this.actual, expectedArgs, this.env); - } - }) - this.addMatchers({ - toHaveBeenPreventedOn: function(selector) { - this.message = function() { - return [ - "Expected event " + this.actual + " to have been prevented on " + selector, - "Expected event " + this.actual + " not to have been prevented on " + selector - ] + }) + + this.addMatchers({ + toHaveBeenStoppedOn: function (selector) { + this.message = function () { + return [ + "Expected event " + this.actual + " to have been stopped on " + selector, + "Expected event " + this.actual + " not to have been stopped on " + selector + ] + } + + return jasmine.jQuery.events.wasStopped(selector, this.actual) } - return jasmine.JQuery.events.wasPrevented(selector, this.actual) - } - }) - this.addMatchers({ - toHaveBeenPrevented: function() { - var eventName = this.actual.eventName, - selector = this.actual.selector - this.message = function() { - return [ - "Expected event " + eventName + " to have been prevented on " + selector, - "Expected event " + eventName + " not to have been prevented on " + selector - ] + }) + + this.addMatchers({ + toHaveBeenStopped: function () { + var eventName = this.actual.eventName + , selector = this.actual.selector + this.message = function () { + return [ + "Expected event " + eventName + " to have been stopped on " + selector, + "Expected event " + eventName + " not to have been stopped on " + selector + ] + } + return jasmine.jQuery.events.wasStopped(selector, eventName) } - return jasmine.JQuery.events.wasPrevented(selector, eventName) - } - }) - this.addMatchers({ - toHaveBeenStoppedOn: function(selector) { - this.message = function() { - return [ - "Expected event " + this.actual + " to have been stopped on " + selector, - "Expected event " + this.actual + " not to have been stopped on " + selector - ] + }) + + jasmine.getEnv().addEqualityTester(function (a, b) { + if(a instanceof $ && b instanceof $) { + if(a.size() != b.size()) { + return jasmine.undefined + } + else if(a.is(b)) { + return true + } } - return jasmine.JQuery.events.wasStopped(selector, this.actual) - } + + return jasmine.undefined + }) }) - this.addMatchers({ - toHaveBeenStopped: function() { - var eventName = this.actual.eventName, - selector = this.actual.selector - this.message = function() { - return [ - "Expected event " + eventName + " to have been stopped on " + selector, - "Expected event " + eventName + " not to have been stopped on " + selector - ] - } - return jasmine.JQuery.events.wasStopped(selector, eventName) - } + + afterEach(function () { + jasmine.getFixtures().cleanUp() + jasmine.getStyleFixtures().cleanUp() + jasmine.jQuery.events.cleanUp() }) -}) -afterEach(function() { - jasmine.getFixtures().cleanUp() - jasmine.getStyleFixtures().cleanUp() - jasmine.JQuery.events.cleanUp() -}) + window.readFixtures = function () { + return jasmine.getFixtures().proxyCallTo_('read', arguments) + } + + window.preloadFixtures = function () { + jasmine.getFixtures().proxyCallTo_('preload', arguments) + } + + window.loadFixtures = function () { + jasmine.getFixtures().proxyCallTo_('load', arguments) + } + + window.appendLoadFixtures = function () { + jasmine.getFixtures().proxyCallTo_('appendLoad', arguments) + } + + window.setFixtures = function (html) { + return jasmine.getFixtures().proxyCallTo_('set', arguments) + } + + window.appendSetFixtures = function () { + jasmine.getFixtures().proxyCallTo_('appendSet', arguments) + } + + window.sandbox = function (attributes) { + return jasmine.getFixtures().sandbox(attributes) + } + + window.spyOnEvent = function (selector, eventName) { + return jasmine.jQuery.events.spyOn(selector, eventName) + } + + window.preloadStyleFixtures = function () { + jasmine.getStyleFixtures().proxyCallTo_('preload', arguments) + } + + window.loadStyleFixtures = function () { + jasmine.getStyleFixtures().proxyCallTo_('load', arguments) + } + + window.appendLoadStyleFixtures = function () { + jasmine.getStyleFixtures().proxyCallTo_('appendLoad', arguments) + } + + window.setStyleFixtures = function (html) { + jasmine.getStyleFixtures().proxyCallTo_('set', arguments) + } + + window.appendSetStyleFixtures = function (html) { + jasmine.getStyleFixtures().proxyCallTo_('appendSet', arguments) + } + + window.loadJSONFixtures = function () { + return jasmine.getJSONFixtures().proxyCallTo_('load', arguments) + } + + window.getJSONFixture = function (url) { + return jasmine.getJSONFixtures().proxyCallTo_('read', arguments)[url] + } +}(window.jasmine, window.jQuery); + |