3 test("expando", function(){
6 equals("expando" in jQuery, true, "jQuery is exposing the expando");
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" );
14 jQuery.data(obj, 'test');
15 equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );
18 jQuery.data(obj, "foo", "bar");
19 equals( jQuery.expando in obj, true, "jQuery.data added an expando to the object" );
21 var id = obj[jQuery.expando]();
22 equals( id in jQuery.cache, false, "jQuery.data did not add an entry to jQuery.cache" );
24 equals( id.foo, "bar", "jQuery.data worked correctly" );
27 test("jQuery.data", function() {
29 var div = document.createElement("div");
31 ok( jQuery.data(div, "test") === undefined, "Check for no data exists" );
33 jQuery.data(div, "test", "success");
34 equals( jQuery.data(div, "test"), "success", "Check for added data" );
36 ok( jQuery.data(div, "notexist") === undefined, "Check for no data exists" );
38 var data = jQuery.data(div);
39 same( data, { "test": "success" }, "Return complete data set" );
41 jQuery.data(div, "test", "overwritten");
42 equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );
44 jQuery.data(div, "test", undefined);
45 equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");
47 jQuery.data(div, "test", null);
48 ok( jQuery.data(div, "test") === null, "Check for null data");
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." );
55 jQuery.data( obj, "prop", true );
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." );
60 equals( jQuery.data( obj, "prop" ), true, "Make sure the right value is retrieved." );
63 test(".data()", function() {
66 var div = jQuery("#foo");
67 div.data("test", "success");
68 same( div.data(), {test: "success"}, "data() get the entire data object" )
71 test(".data(String) and .data(String, Object)", function() {
73 var parent = jQuery("<div><div></div></div>"),
74 div = parent.children();
77 .bind("getData", function(){ ok( false, "getData bubbled." ) })
78 .bind("setData", function(){ ok( false, "setData bubbled." ) })
79 .bind("changeData", function(){ ok( false, "changeData bubbled." ) });
81 ok( div.data("test") === undefined, "Check for no data exists" );
83 div.data("test", "success");
84 equals( div.data("test"), "success", "Check for added data" );
86 div.data("test", "overwritten");
87 equals( div.data("test"), "overwritten", "Check for overwritten data" );
89 div.data("test", undefined);
90 equals( div.data("test"), "overwritten", "Check that data wasn't removed");
92 div.data("test", null);
93 ok( div.data("test") === null, "Check for null data");
95 ok( div.data("notexist") === undefined, "Check for no data exists" );
97 div.data("test", "overwritten");
98 var hits = {test:0}, gets = {test:0}, changes = {test:0, value:null};
101 function logChangeData(e,key,value) {
104 dataKey = dataKey + "." + e.namespace;
106 changes[key] += value;
107 changes.value = jQuery.data(e.target, dataKey);
111 .bind("setData",function(e,key,value){ hits[key] += value; })
112 .bind("setData.foo",function(e,key,value){ hits[key] += value; })
113 .bind("changeData",logChangeData)
114 .bind("changeData.foo",logChangeData)
115 .bind("getData",function(e,key){ gets[key] += 1; })
116 .bind("getData.foo",function(e,key){ gets[key] += 3; });
118 div.data("test.foo", 2);
119 equals( div.data("test"), "overwritten", "Check for original data" );
120 equals( div.data("test.foo"), 2, "Check for namespaced data" );
121 equals( div.data("test.bar"), "overwritten", "Check for unmatched namespace" );
122 equals( hits.test, 2, "Check triggered setter functions" );
123 equals( gets.test, 5, "Check triggered getter functions" );
124 equals( changes.test, 2, "Check sets raise changeData");
125 equals( changes.value, 2, "Check changeData after data has been set" );
130 changes.value = null;
133 equals( div.data("test"), 1, "Check for original data" );
134 equals( div.data("test.foo"), 2, "Check for namespaced data" );
135 equals( div.data("test.bar"), 1, "Check for unmatched namespace" );
136 equals( hits.test, 1, "Check triggered setter functions" );
137 equals( gets.test, 5, "Check triggered getter functions" );
138 equals( changes.test, 1, "Check sets raise changeData" );
139 equals( changes.value, 1, "Check changeData after data has been set" );
142 .bind("getData",function(e,key){ return key + "root"; })
143 .bind("getData.foo",function(e,key){ return key + "foo"; });
145 equals( div.data("test"), "testroot", "Check for original data" );
146 equals( div.data("test.foo"), "testfoo", "Check for namespaced data" );
147 equals( div.data("test.bar"), "testroot", "Check for unmatched namespace" );
150 var $elem = jQuery({});
151 equals( $elem.data('nothing'), undefined, "Non-existent data returns undefined");
152 equals( $elem.data('null',null).data('null'), null, "null's are preserved");
153 equals( $elem.data('emptyString','').data('emptyString'), '', "Empty strings are preserved");
154 equals( $elem.data('false',false).data('false'), false, "false's are preserved");
160 test(".data(Object)", function() {
163 var div = jQuery("<div/>");
165 div.data({ "test": "in", "test2": "in2" });
166 equals( div.data("test"), "in", "Verify setting an object in data." );
167 equals( div.data("test2"), "in2", "Verify setting an object in data." );
170 test("jQuery.removeData", function() {
172 var div = jQuery("#foo")[0];
173 jQuery.data(div, "test", "testing");
174 jQuery.removeData(div, "test");
175 equals( jQuery.data(div, "test"), undefined, "Check removal of data" );
178 test(".removeData()", function() {
180 var div = jQuery("#foo");
181 div.data("test", "testing");
182 div.removeData("test");
183 equals( div.data("test"), undefined, "Check removal of data" );
185 div.data("test", "testing");
186 div.data("test.foo", "testing2");
187 div.removeData("test.bar");
188 equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
189 equals( div.data("test"), "testing", "Make sure data is intact" );
191 div.removeData("test");
192 equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
193 equals( div.data("test"), undefined, "Make sure data is intact" );
195 div.removeData("test.foo");
196 equals( div.data("test.foo"), undefined, "Make sure data is intact" );