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