3b0d825edfaad758a3871fa85f403250037cd169
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.0.1pre / tests / unit / core / core.js
1 /*
2  * mobile core unit tests
3  */
4
5 (function($){
6         var libName = "jquery.mobile.core.js",
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                 test( "grade A browser either supports media queries or is IE 7+", function(){
28                         setGradeA(false, 6);
29                         $.testHelper.reloadLib(libName);
30                         ok(!$.mobile.gradeA());
31
32                         setGradeA(true, 8);
33                         $.testHelper.reloadLib(libName);
34                         ok($.mobile.gradeA());
35                 });
36         });
37
38         function clearNSNormalizeDictionary()
39         {
40                 var dict = $.mobile.nsNormalizeDict;
41                 for ( var prop in dict ) {
42                         delete dict[ prop ];
43                 }
44         }
45
46         test( "$.mobile.nsNormalize works properly with namespace defined (test default)", function(){
47                 // Start with a fresh namespace property cache, just in case
48                 // the previous test mucked with namespaces.
49                 clearNSNormalizeDictionary();
50
51                 equal($.mobile.nsNormalize("foo"), "nstestFoo", "appends ns and initcaps");
52                 equal($.mobile.nsNormalize("fooBar"), "nstestFooBar", "leaves capped strings intact");
53                 equal($.mobile.nsNormalize("foo-bar"), "nstestFooBar", "changes dashed strings");
54                 equal($.mobile.nsNormalize("foo-bar-bak"), "nstestFooBarBak", "changes multiple dashed strings");
55
56                 // Reset the namespace property cache for the next test.
57                 clearNSNormalizeDictionary();
58         });
59
60         test( "$.mobile.nsNormalize works properly with an empty namespace", function(){
61                 var realNs = $.mobile.ns;
62
63                 $.mobile.ns = "";
64
65                 // Start with a fresh namespace property cache, just in case
66                 // the previous test mucked with namespaces.
67                 clearNSNormalizeDictionary();
68
69                 equal($.mobile.nsNormalize("foo"), "foo", "leaves uncapped and undashed");
70                 equal($.mobile.nsNormalize("fooBar"), "fooBar", "leaves capped strings intact");
71                 equal($.mobile.nsNormalize("foo-bar"), "fooBar", "changes dashed strings");
72                 equal($.mobile.nsNormalize("foo-bar-bak"), "fooBarBak", "changes multiple dashed strings");
73
74                 $.mobile.ns = realNs;
75
76                 // Reset the namespace property cache for the next test.
77                 clearNSNormalizeDictionary();
78         });
79
80         //data tests
81         test( "$.fn.jqmData and $.fn.jqmRemoveData methods are working properly", function(){
82                 var data;
83
84                 same( $("body").jqmData("foo", true), $("body"), "setting data returns the element" );
85
86                 same( $("body").jqmData("foo"), true, "getting data returns the right value" );
87
88                 same( $("body").data($.mobile.nsNormalize("foo")), true, "data was set using namespace" );
89
90                 same( $("body").jqmData("foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
91
92                 data = $.extend( {}, $("body").data() );
93                 delete data[ $.expando ]; //discard the expando for that test
94                 same( data , { "nstestFoo": true }, "passing .data() no arguments returns a hash with all set properties" );
95
96                 same( $("body").jqmData(), undefined, "passing no arguments returns undefined" );
97
98                 same( $("body").jqmData(undefined), undefined, "passing a single undefined argument returns undefined" );
99
100                 same( $("body").jqmData(undefined, undefined), undefined, "passing 2 undefined arguments returns undefined" );
101
102                 same( $("body").jqmRemoveData("foo"), $("body"), "jqmRemoveData returns the element" );
103
104                 same( $("body").jqmData("foo"), undefined, "jqmRemoveData properly removes namespaced data" );
105
106         });
107
108
109         test( "$.jqmData and $.jqmRemoveData methods are working properly", function(){
110                 same( $.jqmData(document.body, "foo", true), true, "setting data returns the value" );
111
112                 same( $.jqmData(document.body, "foo"), true, "getting data returns the right value" );
113
114                 same( $.data(document.body, $.mobile.nsNormalize("foo")), true, "data was set using namespace" );
115
116                 same( $.jqmData(document.body, "foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
117
118                 same( $.jqmData(document.body), undefined, "passing no arguments returns undefined" );
119
120                 same( $.jqmData(document.body, undefined), undefined, "passing a single undefined argument returns undefined" );
121
122                 same( $.jqmData(document.body, undefined, undefined), undefined, "passing 2 undefined arguments returns undefined" );
123
124                 same( $.jqmRemoveData(document.body, "foo"), undefined, "jqmRemoveData returns the undefined value" );
125
126                 same( $("body").jqmData("foo"), undefined, "jqmRemoveData properly removes namespaced data" );
127
128         });
129
130         test( "addDependents works properly", function() {
131                 same( $("#parent").jqmData('dependents'), undefined );
132                 $( "#parent" ).addDependents( $("#dependent") );
133                 same( $("#parent").jqmData('dependents').length, 1 );
134         });
135
136         test( "removeWithDependents removes the parent element and ", function(){
137                 $( "#parent" ).addDependents( $("#dependent") );
138                 same($( "#parent, #dependent" ).length, 2);
139                 $( "#parent" ).removeWithDependents();
140                 same($( "#parent, #dependent" ).length, 0);
141         });
142
143         test( "$.fn.getEncodedText should return the encoded value where $.fn.text doesn't", function() {
144                 same( $("#encoded").text(), "foo>");
145                 same( $("#encoded").getEncodedText(), "foo>");
146                 same( $("#unencoded").getEncodedText(), "foo");
147         });
148 })(jQuery);