Rework unit tests to check actual result elements.
[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("jQuery.hasData", function() {
82         expect(6);
83
84         function testData(obj) {
85                 equals( jQuery.hasData(obj), false, "No data exists" );
86                 jQuery.data( obj, "foo", "bar" );
87                 equals( jQuery.hasData(obj), true, "Data exists" );
88                 jQuery.removeData( obj, "foo" );
89                 equals( jQuery.hasData(obj), false, "Data was removed" );
90         }
91
92         testData(document.createElement('div'));
93         testData({});
94 });
95
96 test(".data()", function() {
97         expect(5);
98
99         var div = jQuery("#foo");
100         strictEqual( div.data("foo"), undefined, "Make sure that missing result is undefined" );
101
102         div.data("test", "success");
103         same( div.data(), {test: "success"}, "data() get the entire data object" );
104         strictEqual( div.data("foo"), undefined, "Make sure that missing result is still undefined" );
105
106         var nodiv = jQuery("#unfound");
107         equals( nodiv.data(), null, "data() on empty set returns null" );
108
109         var obj = { foo: "bar" };
110         equals( jQuery(obj).data(), obj, "Retrieve data object from a wrapped JS object (#7524)" );
111 })
112
113 test(".data(String) and .data(String, Object)", function() {
114         expect(29);
115         var parent = jQuery("<div><div></div></div>"),
116                 div = parent.children();
117
118         parent
119                 .bind("getData", function(){ ok( false, "getData bubbled." ) })
120                 .bind("setData", function(){ ok( false, "setData bubbled." ) })
121                 .bind("changeData", function(){ ok( false, "changeData bubbled." ) });
122
123         ok( div.data("test") === undefined, "Check for no data exists" );
124
125         div.data("test", "success");
126         equals( div.data("test"), "success", "Check for added data" );
127
128         div.data("test", "overwritten");
129         equals( div.data("test"), "overwritten", "Check for overwritten data" );
130
131         div.data("test", undefined);
132         equals( div.data("test"), "overwritten", "Check that data wasn't removed");
133
134         div.data("test", null);
135         ok( div.data("test") === null, "Check for null data");
136
137         ok( div.data("notexist") === undefined, "Check for no data exists" );
138
139         div.data("test", "overwritten");
140         var hits = {test:0}, gets = {test:0}, changes = {test:0, value:null};
141
142
143         function logChangeData(e,key,value) {
144                 var dataKey = key;
145                 if ( e.namespace ) {
146                         dataKey = dataKey + "." + e.namespace;
147                 }
148                 changes[key] += value;
149                 changes.value = jQuery.data(e.target, dataKey);
150         }
151
152         div
153                 .bind("setData",function(e,key,value){ hits[key] += value; })
154                 .bind("setData.foo",function(e,key,value){ hits[key] += value; })
155                 .bind("changeData",logChangeData)
156                 .bind("changeData.foo",logChangeData)
157                 .bind("getData",function(e,key){ gets[key] += 1; })
158                 .bind("getData.foo",function(e,key){ gets[key] += 3; });
159
160         div.data("test.foo", 2);
161         equals( div.data("test"), "overwritten", "Check for original data" );
162         equals( div.data("test.foo"), 2, "Check for namespaced data" );
163         equals( div.data("test.bar"), "overwritten", "Check for unmatched namespace" );
164         equals( hits.test, 2, "Check triggered setter functions" );
165         equals( gets.test, 5, "Check triggered getter functions" );
166         equals( changes.test, 2, "Check sets raise changeData");
167         equals( changes.value, 2, "Check changeData after data has been set" );
168
169         hits.test = 0;
170         gets.test = 0;
171         changes.test = 0;
172         changes.value = null;
173
174         div.data("test", 1);
175         equals( div.data("test"), 1, "Check for original data" );
176         equals( div.data("test.foo"), 2, "Check for namespaced data" );
177         equals( div.data("test.bar"), 1, "Check for unmatched namespace" );
178         equals( hits.test, 1, "Check triggered setter functions" );
179         equals( gets.test, 5, "Check triggered getter functions" );
180         equals( changes.test, 1, "Check sets raise changeData" );
181         equals( changes.value, 1, "Check changeData after data has been set" );
182
183         div
184                 .bind("getData",function(e,key){ return key + "root"; })
185                 .bind("getData.foo",function(e,key){ return key + "foo"; });
186
187         equals( div.data("test"), "testroot", "Check for original data" );
188         equals( div.data("test.foo"), "testfoo", "Check for namespaced data" );
189         equals( div.data("test.bar"), "testroot", "Check for unmatched namespace" );
190
191         // #3748
192         var $elem = jQuery({exists:true});
193         equals( $elem.data('nothing'), undefined, "Non-existent data returns undefined");
194         equals( $elem.data('null',null).data('null'), null, "null's are preserved");
195         equals( $elem.data('emptyString','').data('emptyString'), '', "Empty strings are preserved");
196         equals( $elem.data('false',false).data('false'), false, "false's are preserved");
197         equals( $elem.data('exists'), true, "Existing data is returned" );
198
199         // Clean up
200         $elem.removeData();
201         ok( jQuery.isEmptyObject( $elem[0] ), "removeData clears the object" );
202 });
203
204 test("data-* attributes", function() {
205         expect(37);
206         var div = jQuery("<div>"),
207                 child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>"),
208                 dummy = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>");
209
210         equals( div.data("attr"), undefined, "Check for non-existing data-attr attribute" );
211
212         div.attr("data-attr", "exists");
213         equals( div.data("attr"), "exists", "Check for existing data-attr attribute" );
214
215         div.attr("data-attr", "exists2");
216         equals( div.data("attr"), "exists", "Check that updates to data- don't update .data()" );
217
218         div.data("attr", "internal").attr("data-attr", "external");
219         equals( div.data("attr"), "internal", "Check for .data('attr') precedence (internal > external data-* attribute)" );
220
221         child.appendTo('#main');
222         equals( child.data("myobj"), "old data", "Value accessed from data-* attribute");
223
224         child.data("myobj", "replaced");
225         equals( child.data("myobj"), "replaced", "Original data overwritten");
226
227         child.data("ignored", "cache");
228         equals( child.data("ignored"), "cache", "Cached data used before DOM data-* fallback");
229
230         var obj = child.data(), obj2 = dummy.data(), check = [ "myobj", "ignored", "other" ], num = 0, num2 = 0;
231
232         for ( var i = 0, l = check.length; i < l; i++ ) {
233                 ok( obj[ check[i] ], "Make sure data- property exists when calling data-." );
234                 ok( obj2[ check[i] ], "Make sure data- property exists when calling data-." );
235         }
236
237         for ( var prop in obj ) {
238                 num++;
239         }
240
241         equals( num, check.length, "Make sure that the right number of properties came through." );
242
243         for ( var prop in obj2 ) {
244                 num2++;
245         }
246
247         equals( num2, check.length, "Make sure that the right number of properties came through." );
248
249         child.attr("data-other", "newvalue");
250
251         equals( child.data("other"), "test", "Make sure value was pulled in properly from a .data()." );
252
253         child
254                 .attr("data-true", "true")
255                 .attr("data-false", "false")
256                 .attr("data-five", "5")
257                 .attr("data-point", "5.5")
258                 .attr("data-pointe", "5.5E3")
259                 .attr("data-pointbad", "5..5")
260                 .attr("data-pointbad2", "-.")
261                 .attr("data-badjson", "{123}")
262                 .attr("data-badjson2", "[abc]")
263                 .attr("data-empty", "")
264                 .attr("data-space", " ")
265                 .attr("data-null", "null")
266                 .attr("data-string", "test");
267
268         strictEqual( child.data('true'), true, "Primitive true read from attribute");
269         strictEqual( child.data('false'), false, "Primitive false read from attribute");
270         strictEqual( child.data('five'), 5, "Primitive number read from attribute");
271         strictEqual( child.data('point'), 5.5, "Primitive number read from attribute");
272         strictEqual( child.data('pointe'), 5500, "Primitive number read from attribute");
273         strictEqual( child.data('pointbad'), "5..5", "Bad number read from attribute");
274         strictEqual( child.data('pointbad2'), "-.", "Bad number read from attribute");
275         strictEqual( child.data('badjson'), "{123}", "Bad number read from attribute");
276         strictEqual( child.data('badjson2'), "[abc]", "Bad number read from attribute");
277         strictEqual( child.data('empty'), "", "Empty string read from attribute");
278         strictEqual( child.data('space'), " ", "Empty string read from attribute");
279         strictEqual( child.data('null'), null, "Primitive null read from attribute");
280         strictEqual( child.data('string'), "test", "Typical string read from attribute");
281
282         child.remove();
283
284         // tests from metadata plugin
285         function testData(index, elem) {
286                 switch (index) {
287                 case 0:
288                         equals(jQuery(elem).data("foo"), "bar", "Check foo property");
289                         equals(jQuery(elem).data("bar"), "baz", "Check baz property");
290                         break;
291                 case 1:
292                         equals(jQuery(elem).data("test"), "bar", "Check test property");
293                         equals(jQuery(elem).data("bar"), "baz", "Check bar property");
294                         break;
295                 case 2:
296                         equals(jQuery(elem).data("zoooo"), "bar", "Check zoooo property");
297                         same(jQuery(elem).data("bar"), {"test":"baz"}, "Check bar property");
298                         break;
299                 case 3:
300                         equals(jQuery(elem).data("number"), true, "Check number property");
301                         same(jQuery(elem).data("stuff"), [2,8], "Check stuff property");
302                         break;
303                 default:
304                         ok(false, ["Assertion failed on index ", index, ", with data ", data].join(''));
305                 }
306         }
307
308         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>',
309                 elem = jQuery(metadata).appendTo('#main');
310
311         elem.find("li").each(testData);
312         elem.remove();
313 });
314
315 test(".data(Object)", function() {
316         expect(4);
317
318         var div = jQuery("<div/>");
319
320         div.data({ "test": "in", "test2": "in2" });
321         equals( div.data("test"), "in", "Verify setting an object in data" );
322         equals( div.data("test2"), "in2", "Verify setting an object in data" );
323
324         var obj = {test:"unset"},
325                 jqobj = jQuery(obj);
326         jqobj.data({ "test": "in", "test2": "in2" });
327         equals( obj.test, "in", "Verify setting an object on an object extends the object" );
328         equals( obj.test2, "in2", "Verify setting an object on an object extends the object" );
329 });
330
331 test("jQuery.removeData", function() {
332         expect(7);
333         var div = jQuery("#foo")[0];
334         jQuery.data(div, "test", "testing");
335         jQuery.removeData(div, "test");
336         equals( jQuery.data(div, "test"), undefined, "Check removal of data" );
337
338         jQuery.data(div, "test2", "testing");
339         jQuery.removeData( div );
340         ok( !jQuery.data(div, "test2"), "Make sure that the data property no longer exists." );
341         ok( !div[ jQuery.expando ], "Make sure the expando no longer exists, as well." );
342
343         var obj = {};
344         jQuery.data(obj, "test", "testing");
345         equals( obj.test, "testing", "verify data on plain object");
346         jQuery.removeData(obj, "test");
347         equals( jQuery.data(obj, "test"), undefined, "Check removal of data on plain object" );
348         equals( obj.test, undefined, "Check removal of data directly from plain object" );
349
350         jQuery.data( window, "BAD", true );
351         jQuery.removeData( window, "BAD" );
352         ok( !jQuery.data( window, "BAD" ), "Make sure that the value was not still set." );
353 });
354
355 test(".removeData()", function() {
356         expect(6);
357         var div = jQuery("#foo");
358         div.data("test", "testing");
359         div.removeData("test");
360         equals( div.data("test"), undefined, "Check removal of data" );
361
362         div.data("test", "testing");
363         div.data("test.foo", "testing2");
364         div.removeData("test.bar");
365         equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
366         equals( div.data("test"), "testing", "Make sure data is intact" );
367
368         div.removeData("test");
369         equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
370         equals( div.data("test"), undefined, "Make sure data is intact" );
371
372         div.removeData("test.foo");
373         equals( div.data("test.foo"), undefined, "Make sure data is intact" );
374 });