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