Revert "Export"
[framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.1.0 / tests / unit / core / core.js
1 /*
2  * mobile core unit tests
3  */
4
5 (function($){
6         var libName = "jquery.mobile.core",
7                         setGradeA = function(value, version) {
8                                 $.support.mediaquery = value;
9                                 $.mobile.browser.ie = version;
10                         },
11                         extendFn = $.extend;
12
13         module(libName, {
14                 setup: function(){
15                         // NOTE reset for gradeA tests
16                         $('html').removeClass('ui-mobile');
17
18                         // NOTE reset for pageLoading tests
19                         $('.ui-loader').remove();
20                 },
21                 teardown: function(){
22                         $.extend = extendFn;
23                 }
24         });
25
26         $.testHelper.excludeFileProtocol(function(){
27                 asyncTest( "grade A browser either supports media queries or is IE 7+", function(){
28                         setGradeA(false, 6);
29                         $.testHelper.deferredSequence([
30                                 function() {
31                                         return $.testHelper.reloadModule(libName);
32                                 },
33
34                                 function() {
35                                         ok(!$.mobile.gradeA());
36                                 },
37
38                                 function() {
39                                         setGradeA(true, 8);
40                                         return $.testHelper.reloadModule(libName);
41                                 },
42
43                                 function() {
44                                         ok($.mobile.gradeA());
45                                         start();
46                                 }
47                         ]);
48                 });
49         });
50
51         function clearNSNormalizeDictionary()
52         {
53                 var dict = $.mobile.nsNormalizeDict;
54                 for ( var prop in dict ) {
55                         delete dict[ prop ];
56                 }
57         }
58
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();
63
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");
68
69                 // Reset the namespace property cache for the next test.
70                 clearNSNormalizeDictionary();
71         });
72
73         test( "$.mobile.nsNormalize works properly with an empty namespace", function(){
74                 var realNs = $.mobile.ns;
75
76                 $.mobile.ns = "";
77
78                 // Start with a fresh namespace property cache, just in case
79                 // the previous test mucked with namespaces.
80                 clearNSNormalizeDictionary();
81
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");
86
87                 $.mobile.ns = realNs;
88
89                 // Reset the namespace property cache for the next test.
90                 clearNSNormalizeDictionary();
91         });
92
93         //data tests
94         test( "$.fn.jqmData and $.fn.jqmRemoveData methods are working properly", function(){
95                 var data;
96
97                 same( $("body").jqmData("foo", true), $("body"), "setting data returns the element" );
98
99                 same( $("body").jqmData("foo"), true, "getting data returns the right value" );
100
101                 same( $("body").data($.mobile.nsNormalize("foo")), true, "data was set using namespace" );
102
103                 same( $("body").jqmData("foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
104
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" );
108
109                 same( $("body").jqmData(), undefined, "passing no arguments returns undefined" );
110
111                 same( $("body").jqmData(undefined), undefined, "passing a single undefined argument returns undefined" );
112
113                 same( $("body").jqmData(undefined, undefined), undefined, "passing 2 undefined arguments returns undefined" );
114
115                 same( $("body").jqmRemoveData("foo"), $("body"), "jqmRemoveData returns the element" );
116
117                 same( $("body").jqmData("foo"), undefined, "jqmRemoveData properly removes namespaced data" );
118
119         });
120
121
122         test( "$.jqmData and $.jqmRemoveData methods are working properly", function(){
123                 same( $.jqmData(document.body, "foo", true), true, "setting data returns the value" );
124
125                 same( $.jqmData(document.body, "foo"), true, "getting data returns the right value" );
126
127                 same( $.data(document.body, $.mobile.nsNormalize("foo")), true, "data was set using namespace" );
128
129                 same( $.jqmData(document.body, "foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
130
131                 same( $.jqmData(document.body), undefined, "passing no arguments returns undefined" );
132
133                 same( $.jqmData(document.body, undefined), undefined, "passing a single undefined argument returns undefined" );
134
135                 same( $.jqmData(document.body, undefined, undefined), undefined, "passing 2 undefined arguments returns undefined" );
136
137                 same( $.jqmRemoveData(document.body, "foo"), undefined, "jqmRemoveData returns the undefined value" );
138
139                 same( $("body").jqmData("foo"), undefined, "jqmRemoveData properly removes namespaced data" );
140
141         });
142
143         test( "addDependents works properly", function() {
144                 same( $("#parent").jqmData('dependents'), undefined );
145                 $( "#parent" ).addDependents( $("#dependent") );
146                 same( $("#parent").jqmData('dependents').length, 1 );
147         });
148
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);
154         });
155
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;");
160         });
161
162         test( "closestPageData returns the parent's page data", function() {
163                 var pageChild = $( "#page-child" );
164
165                 $( "#parent-page" ).data( "page", { foo: "bar" } );
166                 same( $.mobile.closestPageData( pageChild ).foo, "bar" );
167         });
168
169         test( "closestPageData returns the parent dialog's page data", function() {
170                 var dialogChild = $( "#dialog-child" );
171
172                 $( "#parent-dialog" ).data( "page", { foo: "bar" } );
173                 same( $.mobile.closestPageData(dialogChild).foo, "bar" );
174         });
175
176         test( "test that $.fn.jqmHijackable works", function() {
177                 $.mobile.ignoreContentEnabled = true;
178
179                 same( $( "#hijacked-link" ).jqmHijackable().length, 1,
180                                         "a link without any association to data-ajax=false should be included");
181
182                 same( $( "#unhijacked-link-by-parent" ).jqmHijackable().length, 0,
183                                         "a link with a data-ajax=false parent should be excluded");
184
185                 same( $( "#unhijacked-link-by-attr" ).jqmHijackable().length, 0,
186                                         "a link with data-ajax=false should be excluded");
187
188                 $.mobile.ignoreContentEnabled = false;
189         });
190 })(jQuery);