added immediate function to prevent scoping issues in core unit tests

This commit is contained in:
John Bender 2011-01-22 22:39:14 -08:00
parent b671753039
commit e30ece11ac
2 changed files with 193 additions and 189 deletions

View file

@ -2,148 +2,148 @@
* mobile core unit tests * mobile core unit tests
*/ */
var libName = "jquery.mobile.core.js", (function($){
setGradeA = function(value) { $.support.mediaquery = value; }, var libName = "jquery.mobile.core.js",
extendFn = $.extend; setGradeA = function(value) { $.support.mediaquery = value; },
extendFn = $.extend;
module(libName, { module(libName, {
setup: function(){ setup: function(){
// NOTE reset for gradeA tests // NOTE reset for gradeA tests
$('html').removeClass('ui-mobile'); $('html').removeClass('ui-mobile');
// NOTE reset for pageLoading tests // NOTE reset for pageLoading tests
$('.ui-loader').remove(); $('.ui-loader').remove();
}, },
teardown: function(){ teardown: function(){
$.extend = extendFn; $.extend = extendFn;
} }
});
$.testHelper.excludeFileProtocol(function(){
test( "grade A browser support media queries", function(){
setGradeA(false);
$.testHelper.reloadLib(libName);
ok(!$.mobile.gradeA());
setGradeA(true);
$.testHelper.reloadLib(libName);
ok($.mobile.gradeA());
}); });
test( "loading the core library triggers mobilinit on the document", function(){ $.testHelper.excludeFileProtocol(function(){
expect( 1 ); test( "grade A browser support media queries", function(){
setGradeA(false);
$.testHelper.reloadLib(libName);
ok(!$.mobile.gradeA());
$(window.document).bind('mobileinit', function(event){ setGradeA(true);
ok(true); $.testHelper.reloadLib(libName);
ok($.mobile.gradeA());
}); });
$.testHelper.reloadLib(libName); test( "loading the core library triggers mobilinit on the document", function(){
}); expect( 1 );
test( "enhancments are skipped when the browser is not grade A", function(){ $(window.document).bind('mobileinit', function(event){
setGradeA(false); ok(true);
$.testHelper.reloadLib(libName); });
//NOTE easiest way to check for enhancements, not the most obvious $.testHelper.reloadLib(libName);
ok(!$("html").hasClass("ui-mobile"));
});
test( "enhancments are added when the browser is grade A", function(){
setGradeA(true);
$.testHelper.reloadLib(libName);
ok($("html").hasClass("ui-mobile"));
});
//TODO lots of duplication
test( "pageLoading doesn't add the dialog to the page when loading message is false", function(){
$.testHelper.alterExtend({loadingMessage: false});
$.testHelper.reloadLib(libName);
$.mobile.pageLoading(false);
ok(!$(".ui-loader").length);
});
test( "pageLoading doesn't add the dialog to the page when done is passed as true", function(){
$.testHelper.alterExtend({loadingMessage: true});
$.testHelper.reloadLib(libName);
// TODO add post reload callback
$('.ui-loader').remove();
$.mobile.pageLoading(true);
ok(!$(".ui-loader").length);
});
test( "pageLoading adds the dialog to the page when done is true", function(){
$.testHelper.alterExtend({loadingMessage: true});
$.testHelper.reloadLib(libName);
$.mobile.pageLoading(false);
ok($(".ui-loader").length);
});
var metaViewportSelector = "head meta[name=viewport]",
setViewPortContent = function(value){
$(metaViewportSelector).remove();
$.testHelper.alterExtend({metaViewportContent: value});
$.testHelper.reloadLib(libName);
};
test( "meta view port element is added to head when defined on mobile", function(){
setViewPortContent("width=device-width");
same($(metaViewportSelector).length, 1);
});
test( "meta view port element not added to head when not defined on mobile", function(){
setViewPortContent(false);
same($(metaViewportSelector).length, 0);
});
var findFirstPage = function() {
return $("[data-role='page']").first();
};
test( "active page and start page should be set to the fist page in the selected set", function(){
var firstPage = findFirstPage();
$.testHelper.reloadLib(libName);
same($.mobile.startPage, firstPage);
same($.mobile.activePage, firstPage);
});
test( "mobile viewport class is defined on the first page's parent", function(){
var firstPage = findFirstPage();
$.testHelper.reloadLib(libName);
ok(firstPage.parent().hasClass('ui-mobile-viewport'));
});
test( "mobile page container is the first page's parent", function(){
var firstPage = findFirstPage();
$.testHelper.reloadLib(libName);
same($.mobile.pageContainer, firstPage.parent());
});
test( "page loading is called on document ready", function(){
expect( 2 );
$.testHelper.alterExtend({ pageLoading: function(){
ok("called");
}});
$.testHelper.reloadLib(libName);
});
test( "hashchange triggered on document ready with single argument: true", function(){
expect( 2 );
$(window).bind("hashchange", function(ev, arg){
same(arg, true);
}); });
$.testHelper.reloadLib(libName); test( "enhancments are skipped when the browser is not grade A", function(){
}); setGradeA(false);
$.testHelper.reloadLib(libName);
//TODO test that silentScroll is called on window load //NOTE easiest way to check for enhancements, not the most obvious
}); ok(!$("html").hasClass("ui-mobile"));
});
test( "enhancments are added when the browser is grade A", function(){
setGradeA(true);
$.testHelper.reloadLib(libName);
ok($("html").hasClass("ui-mobile"));
});
//TODO lots of duplication
test( "pageLoading doesn't add the dialog to the page when loading message is false", function(){
$.testHelper.alterExtend({loadingMessage: false});
$.testHelper.reloadLib(libName);
$.mobile.pageLoading(false);
ok(!$(".ui-loader").length);
});
test( "pageLoading doesn't add the dialog to the page when done is passed as true", function(){
$.testHelper.alterExtend({loadingMessage: true});
$.testHelper.reloadLib(libName);
// TODO add post reload callback
$('.ui-loader').remove();
$.mobile.pageLoading(true);
ok(!$(".ui-loader").length);
});
test( "pageLoading adds the dialog to the page when done is true", function(){
$.testHelper.alterExtend({loadingMessage: true});
$.testHelper.reloadLib(libName);
$.mobile.pageLoading(false);
ok($(".ui-loader").length);
});
var metaViewportSelector = "head meta[name=viewport]",
setViewPortContent = function(value){
$(metaViewportSelector).remove();
$.testHelper.alterExtend({metaViewportContent: value});
$.testHelper.reloadLib(libName);
};
test( "meta view port element is added to head when defined on mobile", function(){
setViewPortContent("width=device-width");
same($(metaViewportSelector).length, 1);
});
test( "meta view port element not added to head when not defined on mobile", function(){
setViewPortContent(false);
same($(metaViewportSelector).length, 0);
});
var findFirstPage = function() {
return $("[data-role='page']").first();
};
test( "active page and start page should be set to the fist page in the selected set", function(){
var firstPage = findFirstPage();
$.testHelper.reloadLib(libName);
same($.mobile.startPage, firstPage);
same($.mobile.activePage, firstPage);
});
test( "mobile viewport class is defined on the first page's parent", function(){
var firstPage = findFirstPage();
$.testHelper.reloadLib(libName);
ok(firstPage.parent().hasClass('ui-mobile-viewport'));
});
test( "mobile page container is the first page's parent", function(){
var firstPage = findFirstPage();
$.testHelper.reloadLib(libName);
same($.mobile.pageContainer, firstPage.parent());
});
test( "page loading is called on document ready", function(){
expect( 2 );
$.testHelper.alterExtend({ pageLoading: function(){
ok("called");
}});
$.testHelper.reloadLib(libName);
});
test( "hashchange triggered on document ready with single argument: true", function(){
expect( 2 );
$(window).bind("hashchange", function(ev, arg){
same(arg, true);
});
$.testHelper.reloadLib(libName);
});
});
})(jQuery);

View file

@ -2,71 +2,75 @@
* mobile core unit tests * mobile core unit tests
*/ */
var libName = "jquery.mobile.core.js", (function($){
scrollTimeout = 20, // TODO expose timing as an attribute var libName = "jquery.mobile.core.js",
scrollStartEnabledTimeout = 150; scrollTimeout = 20, // TODO expose timing as an attribute
scrollStartEnabledTimeout = 150;
module(libName, { module(libName, {
setup: function(){ setup: function(){
$("<div id='scroll-testing' style='height: 1000px'></div>").appendTo("body"); $("<div id='scroll-testing' style='height: 1000px'></div>").appendTo("body");
}, },
teardown: function(){ teardown: function(){
$("#scroll-testing").remove(); $("#scroll-testing").remove();
} }
}); });
var scrollUp = function( pos ){ var scrollUp = function( pos ){
$(window).scrollTop(1000); $(window).scrollTop(1000);
ok($(window).scrollTop() > 0); ok($(window).scrollTop() > 0);
if(pos) { if(pos) {
$.mobile.silentScroll(pos); $.mobile.silentScroll(pos);
} else { } else {
$.mobile.silentScroll(); $.mobile.silentScroll();
} }
}; };
test( "silent scroll scrolls the page to the top by default", function(){ test( "silent scroll scrolls the page to the top by default", function(){
scrollUp(); scrollUp();
stop(); stop();
setTimeout(function(){ setTimeout(function(){
same($(window).scrollTop(), 0); same($(window).scrollTop(), 0);
start(); start();
}, scrollTimeout); }, scrollTimeout);
}); });
test( "silent scroll scrolls the page to the passed y position", function(){ test( "silent scroll scrolls the page to the passed y position", function(){
var pos = 10; var pos = 10;
scrollUp(pos); scrollUp(pos);
stop(); stop();
setTimeout(function(){ setTimeout(function(){
same($(window).scrollTop(), pos); same($(window).scrollTop(), pos);
start(); start();
}, scrollTimeout); }, scrollTimeout);
}); });
// NOTE may be brittle depending on timing // NOTE may be brittle depending on timing
test( "silent scroll takes at least 20 ms to scroll to the top", function(){ test( "silent scroll takes at least 20 ms to scroll to the top", function(){
scrollUp(); scrollUp();
stop(); stop();
setTimeout(function(){ setTimeout(function(){
ok($(window).scrollTop() != 0); ok($(window).scrollTop() != 0);
start(); start();
}, scrollTimeout - 1); }, scrollTimeout - 1);
}); });
test( "scrolling marks scrollstart as disabled for 150 ms", function(){ test( "scrolling marks scrollstart as disabled for 150 ms", function(){
$.event.special.scrollstart.enabled = true; $.event.special.scrollstart.enabled = true;
scrollUp(); scrollUp();
ok(!$.event.special.scrollstart.enabled); ok(!$.event.special.scrollstart.enabled);
stop(); stop();
setTimeout(function(){ setTimeout(function(){
ok($.event.special.scrollstart.enabled); ok($.event.special.scrollstart.enabled);
start(); start();
}, scrollStartEnabledTimeout); }, scrollStartEnabledTimeout);
}); });
//TODO test that silentScroll is called on window load
})(jQuery);