Add some more tests to make sure that pulling in data- properties on an element with...
[jquery.git] / test / unit / data.js
1 module("data");
2
3 test("expando", function(){
4         expect(6);
5
6         equals("expando" in jQuery, true, "jQuery is exposing the expando");
7
8         var obj = {};
9         equals( jQuery.data(obj), obj, "jQuery.data(obj) returns the object");
10         equals( jQuery.expando in obj, false, "jQuery.data(obj) did not add an expando to the object" );
11
12         obj = {};
13         jQuery.data(obj, 'test');
14         equals( jQuery.expando in obj, false, "jQuery.data(obj,key) did not add an expando to the object" );
15
16         obj = {};
17         jQuery.data(obj, "foo", "bar");
18         equals( jQuery.expando in obj, false, "jQuery.data(obj,key,value) did not add an expando to the object" );
19         equals( obj.foo, "bar", "jQuery.data(obj,key,value) sets fields directly on the object." );
20 });
21
22 test("jQuery.acceptData", function() {
23         expect(7);
24
25         ok( jQuery.acceptData( document ), "document" );
26         ok( jQuery.acceptData( document.documentElement ), "documentElement" );
27         ok( jQuery.acceptData( {} ), "object" );
28         ok( !jQuery.acceptData( document.createElement("embed") ), "embed" );
29         ok( !jQuery.acceptData( document.createElement("applet") ), "applet" );
30
31         var flash = document.createElement("object");
32         flash.setAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
33         ok( jQuery.acceptData( flash ), "flash" );
34
35         var applet = document.createElement("object");
36         applet.setAttribute("classid", "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93");
37         ok( !jQuery.acceptData( applet ), "applet" );
38 });
39
40 test("jQuery.data", function() {
41         expect(15);
42         var div = document.createElement("div");
43
44         ok( jQuery.data(div, "test") === undefined, "Check for no data exists" );
45
46         jQuery.data(div, "test", "success");
47         equals( jQuery.data(div, "test"), "success", "Check for added data" );
48
49         ok( jQuery.data(div, "notexist") === undefined, "Check for no data exists" );
50
51         var data = jQuery.data(div);
52         same( data, { "test": "success" }, "Return complete data set" );
53
54         jQuery.data(div, "test", "overwritten");
55         equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );
56
57         jQuery.data(div, "test", undefined);
58         equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");
59
60         jQuery.data(div, "test", null);
61         ok( jQuery.data(div, "test") === null, "Check for null data");
62
63         jQuery.data(div, "test3", "orig");
64         jQuery.data(div, { "test": "in", "test2": "in2" });
65         equals( jQuery.data(div, "test"), "in", "Verify setting an object in data" );
66         equals( jQuery.data(div, "test2"), "in2", "Verify setting an object in data" );
67         equals( jQuery.data(div, "test3"), "orig", "Verify original not overwritten" );
68
69         var obj = {};
70         jQuery.data( obj, "prop", true );
71
72         ok( obj.prop, "Data is being stored on the object" );
73         equals( jQuery.data( obj, "prop" ), true, "Make sure the right value is retrieved" );
74
75         jQuery.data( window, "BAD", true );
76         ok( !window[ jQuery.expando ], "Make sure there is no expando on the window object." );
77         ok( !window.BAD, "And make sure that the property wasn't set directly on the window." );
78         ok( jQuery.data( window, "BAD" ), "Make sure that the value was set." );
79 });
80
81 test(".data()", function() {
82         expect(4);
83
84         var div = jQuery("#foo");
85         strictEqual( div.data("foo"), undefined, "Make sure that missing result is undefined" );
86
87         div.data("test", "success");
88         same( div.data(), {test: "success"}, "data() get the entire data object" );
89         strictEqual( div.data("foo"), undefined, "Make sure that missing result is still undefined" );
90
91         var nodiv = jQuery("#unfound");
92         equals( nodiv.data(), null, "data() on empty set returns null" );
93 })
94
95 test(".data(String) and .data(String, Object)", function() {
96         expect(29);
97         var parent = jQuery("<div><div></div></div>"),
98                 div = parent.children();
99
100         parent
101                 .bind("getData", function(){ ok( false, "getData bubbled." ) })
102                 .bind("setData", function(){ ok( false, "setData bubbled." ) })
103                 .bind("changeData", function(){ ok( false, "changeData bubbled." ) });
104
105         ok( div.data("test") === undefined, "Check for no data exists" );
106
107         div.data("test", "success");
108         equals( div.data("test"), "success", "Check for added data" );
109
110         div.data("test", "overwritten");
111         equals( div.data("test"), "overwritten", "Check for overwritten data" );
112
113         div.data("test", undefined);
114         equals( div.data("test"), "overwritten", "Check that data wasn't removed");
115
116         div.data("test", null);
117         ok( div.data("test") === null, "Check for null data");
118
119         ok( div.data("notexist") === undefined, "Check for no data exists" );
120
121         div.data("test", "overwritten");
122         var hits = {test:0}, gets = {test:0}, changes = {test:0, value:null};
123
124
125         function logChangeData(e,key,value) {
126                 var dataKey = key;
127                 if ( e.namespace ) {
128                         dataKey = dataKey + "." + e.namespace;
129                 }
130                 changes[key] += value;
131                 changes.value = jQuery.data(e.target, dataKey);
132         }
133
134         div
135                 .bind("setData",function(e,key,value){ hits[key] += value; })
136                 .bind("setData.foo",function(e,key,value){ hits[key] += value; })
137                 .bind("changeData",logChangeData)
138                 .bind("changeData.foo",logChangeData)
139                 .bind("getData",function(e,key){ gets[key] += 1; })
140                 .bind("getData.foo",function(e,key){ gets[key] += 3; });
141
142         div.data("test.foo", 2);
143         equals( div.data("test"), "overwritten", "Check for original data" );
144         equals( div.data("test.foo"), 2, "Check for namespaced data" );
145         equals( div.data("test.bar"), "overwritten", "Check for unmatched namespace" );
146         equals( hits.test, 2, "Check triggered setter functions" );
147         equals( gets.test, 5, "Check triggered getter functions" );
148         equals( changes.test, 2, "Check sets raise changeData");
149         equals( changes.value, 2, "Check changeData after data has been set" );
150
151         hits.test = 0;
152         gets.test = 0;
153         changes.test = 0;
154         changes.value = null;
155
156         div.data("test", 1);
157         equals( div.data("test"), 1, "Check for original data" );
158         equals( div.data("test.foo"), 2, "Check for namespaced data" );
159         equals( div.data("test.bar"), 1, "Check for unmatched namespace" );
160         equals( hits.test, 1, "Check triggered setter functions" );
161         equals( gets.test, 5, "Check triggered getter functions" );
162         equals( changes.test, 1, "Check sets raise changeData" );
163         equals( changes.value, 1, "Check changeData after data has been set" );
164
165         div
166                 .bind("getData",function(e,key){ return key + "root"; })
167                 .bind("getData.foo",function(e,key){ return key + "foo"; });
168
169         equals( div.data("test"), "testroot", "Check for original data" );
170         equals( div.data("test.foo"), "testfoo", "Check for namespaced data" );
171         equals( div.data("test.bar"), "testroot", "Check for unmatched namespace" );
172
173         // #3748
174         var $elem = jQuery({exists:true});
175         equals( $elem.data('nothing'), undefined, "Non-existent data returns undefined");
176         equals( $elem.data('null',null).data('null'), null, "null's are preserved");
177         equals( $elem.data('emptyString','').data('emptyString'), '', "Empty strings are preserved");
178         equals( $elem.data('false',false).data('false'), false, "false's are preserved");
179         equals( $elem.data('exists'), true, "Existing data is returned" );
180         
181         // Clean up
182         $elem.removeData();
183         ok( jQuery.isEmptyObject( $elem[0] ), "removeData clears the object" );
184 });
185
186 test("data-* attributes", function() {
187         expect(37);
188         var div = jQuery("<div>"),
189                 child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>"),
190                 dummy = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>");
191                 
192         equals( div.data("attr"), undefined, "Check for non-existing data-attr attribute" );
193
194         div.attr("data-attr", "exists");
195         equals( div.data("attr"), "exists", "Check for existing data-attr attribute" );
196
197         div.attr("data-attr", "exists2");
198         equals( div.data("attr"), "exists", "Check that updates to data- don't update .data()" );
199                 
200         div.data("attr", "internal").attr("data-attr", "external");
201         equals( div.data("attr"), "internal", "Check for .data('attr') precedence (internal > external data-* attribute)" );
202         
203         child.appendTo('#main');
204         equals( child.data("myobj"), "old data", "Value accessed from data-* attribute");
205
206         child.data("myobj", "replaced");
207         equals( child.data("myobj"), "replaced", "Original data overwritten");
208
209         child.data("ignored", "cache");
210         equals( child.data("ignored"), "cache", "Cached data used before DOM data-* fallback");
211
212         var obj = child.data(), obj2 = dummy.data(), check = [ "myobj", "ignored", "other" ], num = 0, num2 = 0;
213
214         for ( var i = 0, l = check.length; i < l; i++ ) {
215                 ok( obj[ check[i] ], "Make sure data- property exists when calling data-." );
216                 ok( obj2[ check[i] ], "Make sure data- property exists when calling data-." );
217         }
218
219         for ( var prop in obj ) {
220                 num++;
221         }
222
223         equals( num, check.length, "Make sure that the right number of properties came through." );
224
225         for ( var prop in obj2 ) {
226                 num2++;
227         }
228
229         equals( num2, check.length, "Make sure that the right number of properties came through." );
230
231         child.attr("data-other", "newvalue");
232
233         equals( child.data("other"), "test", "Make sure value was pulled in properly from a .data()." );
234
235         child
236                 .attr("data-true", "true")
237                 .attr("data-false", "false")
238                 .attr("data-five", "5")
239                 .attr("data-point", "5.5")
240                 .attr("data-pointe", "5.5E3")
241                 .attr("data-pointbad", "5..5")
242                 .attr("data-pointbad2", "-.")
243                 .attr("data-badjson", "{123}")
244                 .attr("data-badjson2", "[abc]")
245                 .attr("data-empty", "")
246                 .attr("data-space", " ")
247                 .attr("data-null", "null")
248                 .attr("data-string", "test");
249         
250         strictEqual( child.data('true'), true, "Primitive true read from attribute");
251         strictEqual( child.data('false'), false, "Primitive false read from attribute");
252         strictEqual( child.data('five'), 5, "Primitive number read from attribute");
253         strictEqual( child.data('point'), 5.5, "Primitive number read from attribute");
254         strictEqual( child.data('pointe'), 5500, "Primitive number read from attribute");
255         strictEqual( child.data('pointbad'), "5..5", "Bad number read from attribute");
256         strictEqual( child.data('pointbad2'), "-.", "Bad number read from attribute");
257         strictEqual( child.data('badjson'), "{123}", "Bad number read from attribute");
258         strictEqual( child.data('badjson2'), "[abc]", "Bad number read from attribute");
259         strictEqual( child.data('empty'), "", "Empty string read from attribute");
260         strictEqual( child.data('space'), " ", "Empty string read from attribute");
261         strictEqual( child.data('null'), null, "Primitive null read from attribute");
262         strictEqual( child.data('string'), "test", "Typical string read from attribute");
263
264         child.remove();
265         
266         // tests from metadata plugin
267         function testData(index, elem) {
268                 switch (index) {
269                 case 0:
270                         equals(jQuery(elem).data("foo"), "bar", "Check foo property");
271                         equals(jQuery(elem).data("bar"), "baz", "Check baz property");
272                         break;
273                 case 1:
274                         equals(jQuery(elem).data("test"), "bar", "Check test property");
275                         equals(jQuery(elem).data("bar"), "baz", "Check bar property");
276                         break;
277                 case 2:
278                         equals(jQuery(elem).data("zoooo"), "bar", "Check zoooo property");
279                         same(jQuery(elem).data("bar"), {"test":"baz"}, "Check bar property");
280                         break;
281                 case 3:
282                         equals(jQuery(elem).data("number"), true, "Check number property");
283                         same(jQuery(elem).data("stuff"), [2,8], "Check stuff property");
284                         break;
285                 default:
286                         ok(false, ["Assertion failed on index ", index, ", with data ", data].join(''));
287                 }
288         }
289         
290         var metadata = '<ol><li class="test test2" data-foo="bar" data-bar="baz" data-arr="[1,2]">Some stuff</li><li class="test test2" data-test="bar" data-bar="baz">Some stuff</li><li class="test test2" data-zoooo="bar" data-bar=\'{"test":"baz"}\'>Some stuff</li><li class="test test2" data-number=true data-stuff="[2,8]">Some stuff</li></ol>',
291                 elem = jQuery(metadata).appendTo('#main');
292         
293         elem.find("li").each(testData);
294         elem.remove();
295 });
296
297 test(".data(Object)", function() {
298         expect(4);
299
300         var div = jQuery("<div/>");
301
302         div.data({ "test": "in", "test2": "in2" });
303         equals( div.data("test"), "in", "Verify setting an object in data" );
304         equals( div.data("test2"), "in2", "Verify setting an object in data" );
305         
306         var obj = {test:"unset"},
307                 jqobj = jQuery(obj);
308         jqobj.data({ "test": "in", "test2": "in2" });
309         equals( obj.test, "in", "Verify setting an object on an object extends the object" );
310         equals( obj.test2, "in2", "Verify setting an object on an object extends the object" ); 
311 });
312
313 test("jQuery.removeData", function() {
314         expect(7);
315         var div = jQuery("#foo")[0];
316         jQuery.data(div, "test", "testing");
317         jQuery.removeData(div, "test");
318         equals( jQuery.data(div, "test"), undefined, "Check removal of data" );
319
320         jQuery.data(div, "test2", "testing");
321         jQuery.removeData( div );
322         ok( !jQuery.data(div, "test2"), "Make sure that the data property no longer exists." );
323         ok( !div[ jQuery.expando ], "Make sure the expando no longer exists, as well." );
324         
325         var obj = {};
326         jQuery.data(obj, "test", "testing");
327         equals( obj.test, "testing", "verify data on plain object");
328         jQuery.removeData(obj, "test");
329         equals( jQuery.data(obj, "test"), undefined, "Check removal of data on plain object" );
330         equals( obj.test, undefined, "Check removal of data directly from plain object" );      
331
332         jQuery.data( window, "BAD", true );
333         jQuery.removeData( window, "BAD" );
334         ok( !jQuery.data( window, "BAD" ), "Make sure that the value was not still set." );
335 });
336
337 test(".removeData()", function() {
338         expect(6);
339         var div = jQuery("#foo");
340         div.data("test", "testing");
341         div.removeData("test");
342         equals( div.data("test"), undefined, "Check removal of data" );
343
344         div.data("test", "testing");
345         div.data("test.foo", "testing2");
346         div.removeData("test.bar");
347         equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
348         equals( div.data("test"), "testing", "Make sure data is intact" );
349
350         div.removeData("test");
351         equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
352         equals( div.data("test"), undefined, "Make sure data is intact" );
353
354         div.removeData("test.foo");
355         equals( div.data("test.foo"), undefined, "Make sure data is intact" );
356 });