2 * mobile core unit tests
6 var libName = "jquery.mobile.core",
7 setGradeA = function(value, version) {
8 $.support.mediaquery = value;
9 $.mobile.browser.ie = version;
15 // NOTE reset for gradeA tests
16 $('html').removeClass('ui-mobile');
18 // NOTE reset for pageLoading tests
19 $('.ui-loader').remove();
26 $.testHelper.excludeFileProtocol(function(){
27 asyncTest( "grade A browser either supports media queries or is IE 7+", function(){
29 $.testHelper.deferredSequence([
31 return $.testHelper.reloadModule(libName);
35 ok(!$.mobile.gradeA());
40 return $.testHelper.reloadModule(libName);
44 ok($.mobile.gradeA());
51 function clearNSNormalizeDictionary()
53 var dict = $.mobile.nsNormalizeDict;
54 for ( var prop in dict ) {
59 test( "$.mobile.nsNormalize works properly with namespace defined (test default)", function(){
60 // Start with a fresh namespace property cache, just in case
61 // the previous test mucked with namespaces.
62 clearNSNormalizeDictionary();
64 equal($.mobile.nsNormalize("foo"), "nstestFoo", "appends ns and initcaps");
65 equal($.mobile.nsNormalize("fooBar"), "nstestFooBar", "leaves capped strings intact");
66 equal($.mobile.nsNormalize("foo-bar"), "nstestFooBar", "changes dashed strings");
67 equal($.mobile.nsNormalize("foo-bar-bak"), "nstestFooBarBak", "changes multiple dashed strings");
69 // Reset the namespace property cache for the next test.
70 clearNSNormalizeDictionary();
73 test( "$.mobile.nsNormalize works properly with an empty namespace", function(){
74 var realNs = $.mobile.ns;
78 // Start with a fresh namespace property cache, just in case
79 // the previous test mucked with namespaces.
80 clearNSNormalizeDictionary();
82 equal($.mobile.nsNormalize("foo"), "foo", "leaves uncapped and undashed");
83 equal($.mobile.nsNormalize("fooBar"), "fooBar", "leaves capped strings intact");
84 equal($.mobile.nsNormalize("foo-bar"), "fooBar", "changes dashed strings");
85 equal($.mobile.nsNormalize("foo-bar-bak"), "fooBarBak", "changes multiple dashed strings");
89 // Reset the namespace property cache for the next test.
90 clearNSNormalizeDictionary();
94 test( "$.fn.jqmData and $.fn.jqmRemoveData methods are working properly", function(){
97 same( $("body").jqmData("foo", true), $("body"), "setting data returns the element" );
99 same( $("body").jqmData("foo"), true, "getting data returns the right value" );
101 same( $("body").data($.mobile.nsNormalize("foo")), true, "data was set using namespace" );
103 same( $("body").jqmData("foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
105 data = $.extend( {}, $("body").data() );
106 delete data[ $.expando ]; //discard the expando for that test
107 same( data , { "nstestFoo": true }, "passing .data() no arguments returns a hash with all set properties" );
109 same( $("body").jqmData(), undefined, "passing no arguments returns undefined" );
111 same( $("body").jqmData(undefined), undefined, "passing a single undefined argument returns undefined" );
113 same( $("body").jqmData(undefined, undefined), undefined, "passing 2 undefined arguments returns undefined" );
115 same( $("body").jqmRemoveData("foo"), $("body"), "jqmRemoveData returns the element" );
117 same( $("body").jqmData("foo"), undefined, "jqmRemoveData properly removes namespaced data" );
122 test( "$.jqmData and $.jqmRemoveData methods are working properly", function(){
123 same( $.jqmData(document.body, "foo", true), true, "setting data returns the value" );
125 same( $.jqmData(document.body, "foo"), true, "getting data returns the right value" );
127 same( $.data(document.body, $.mobile.nsNormalize("foo")), true, "data was set using namespace" );
129 same( $.jqmData(document.body, "foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
131 same( $.jqmData(document.body), undefined, "passing no arguments returns undefined" );
133 same( $.jqmData(document.body, undefined), undefined, "passing a single undefined argument returns undefined" );
135 same( $.jqmData(document.body, undefined, undefined), undefined, "passing 2 undefined arguments returns undefined" );
137 same( $.jqmRemoveData(document.body, "foo"), undefined, "jqmRemoveData returns the undefined value" );
139 same( $("body").jqmData("foo"), undefined, "jqmRemoveData properly removes namespaced data" );
143 test( "addDependents works properly", function() {
144 same( $("#parent").jqmData('dependents'), undefined );
145 $( "#parent" ).addDependents( $("#dependent") );
146 same( $("#parent").jqmData('dependents').length, 1 );
149 test( "removeWithDependents removes the parent element and ", function(){
150 $( "#parent" ).addDependents( $("#dependent") );
151 same($( "#parent, #dependent" ).length, 2);
152 $( "#parent" ).removeWithDependents();
153 same($( "#parent, #dependent" ).length, 0);
156 test( "$.fn.getEncodedText should return the encoded value where $.fn.text doesn't", function() {
157 same( $("#encoded").text(), "foo>");
158 same( $("#encoded").getEncodedText(), "foo>");
159 same( $("#unencoded").getEncodedText(), "var foo;");
162 test( "closestPageData returns the parent's page data", function() {
163 var pageChild = $( "#page-child" );
165 $( "#parent-page" ).data( "page", { foo: "bar" } );
166 same( $.mobile.closestPageData( pageChild ).foo, "bar" );
169 test( "closestPageData returns the parent dialog's page data", function() {
170 var dialogChild = $( "#dialog-child" );
172 $( "#parent-dialog" ).data( "page", { foo: "bar" } );
173 same( $.mobile.closestPageData(dialogChild).foo, "bar" );
176 test( "test that $.fn.jqmHijackable works", function() {
177 $.mobile.ignoreContentEnabled = true;
179 same( $( "#hijacked-link" ).jqmHijackable().length, 1,
180 "a link without any association to data-ajax=false should be included");
182 same( $( "#unhijacked-link-by-parent" ).jqmHijackable().length, 0,
183 "a link with a data-ajax=false parent should be excluded");
185 same( $( "#unhijacked-link-by-attr" ).jqmHijackable().length, 0,
186 "a link with data-ajax=false should be excluded");
188 $.mobile.ignoreContentEnabled = false;