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