jquery-mobile/tests/unit/core/core.js
scottjehl b3a8230638 This commit includes simplifications to the hashchange event handling & changePage logic, which results in a few bug fixes and removal of some previous limitations. Details:
- jquery.mobile.core.js  no longer creates pages from every page and dialog in the DOM automatically at domready. Instead, pages are created as they are referenced via changePage, which can speed up pageload in multi-page documents, and means local "dialogs" referenced via data-rel="dialog" no longer need a data-role="dialog" attribute when served.

- in changePage, "from" is now allowed to be undefined. This simplifies the logic involved in showing the first page, which never had a "from" page, and previously needed a custom pageChange workaround to accommodate that.

- The pageshow event is no longer used as a callback for returning false and preventing the $.mobile.activePage from being set to the newly shown page. In other words, a page always becomes $.mobile.activePage once its shown now (the only reason this was optional before was because of a dialog restriction that's no longer true)

- the hashchange event logic for showing a particular page is now greatly simplified. It either shows the page referenced in location.hash, or if there's no hash it changes to the first page in the dom. This means every pageshow (including the first one) now uses pageChange internally.

- the hashchange event listener is no longer disabled when ajaxEnabled == false. Doing this before prevented local non-ajax page navigation from working properly. To disable hashchange listening, use $.mobile.urlHistory.listeningEnabled. We might consider defining (or moving) this on the $.mobile hash later as well for easier access.

- The internal var $.mobile.startPage is now $.mobile.firstPage, because it's not necessarily the page you started on, but merely a reference to the first "page" in the dom.

- Back buttons are auto-added to every page after the first one you first visit (this includes generated pages, such as those in a multipage document or nested listviews). Keep in mind that a "back" button does not take the place of a standard "home" link, and when building an app with jQuery Mobile, it's good to make use of both (particularly on deep-linked pages). Fixes #908
2011-01-31 17:05:57 -05:00

149 lines
No EOL
4 KiB
JavaScript

/*
* mobile core unit tests
*/
(function($){
var libName = "jquery.mobile.core.js",
setGradeA = function(value) { $.support.mediaquery = value; },
extendFn = $.extend;
module(libName, {
setup: function(){
// NOTE reset for gradeA tests
$('html').removeClass('ui-mobile');
// NOTE reset for pageLoading tests
$('.ui-loader').remove();
},
teardown: function(){
$.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(){
expect( 1 );
$(window.document).bind('mobileinit', function(event){
ok(true);
});
$.testHelper.reloadLib(libName);
});
test( "enhancments are skipped when the browser is not grade A", function(){
setGradeA(false);
$.testHelper.reloadLib(libName);
//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.firstPage, 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(){
$.testHelper.alterExtend({ pageLoading: function(){
start();
ok("called");
}});
stop();
$.testHelper.reloadLib(libName);
});
test( "hashchange triggered on document ready with single argument: true", function(){
$(window).bind("hashchange", function(ev, arg){
same(arg, true);
start();
});
stop();
$.testHelper.reloadLib(libName);
});
});
})(jQuery);