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" );
13 jQuery.data(obj, 'test');
14 equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );
17 jQuery.data(obj, "foo", "bar");
18 equals( jQuery.expando in obj, true, "jQuery.data added an expando to the object" );
20 var id = obj[jQuery.expando];
21 equals( id in jQuery.cache, false, "jQuery.data did not add an entry to jQuery.cache" );
23 equals( id.foo, "bar", "jQuery.data worked correctly" );
26 test("jQuery.data", function() {
28 var div = document.createElement("div");
30 ok( jQuery.data(div, "test") === undefined, "Check for no data exists" );
32 jQuery.data(div, "test", "success");
33 equals( jQuery.data(div, "test"), "success", "Check for added data" );
35 ok( jQuery.data(div, "notexist") === undefined, "Check for no data exists" );
37 var data = jQuery.data(div);
38 same( data, { "test": "success" }, "Return complete data set" );
40 jQuery.data(div, "test", "overwritten");
41 equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );
43 jQuery.data(div, "test", undefined);
44 equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");
46 jQuery.data(div, "test", null);
47 ok( jQuery.data(div, "test") === null, "Check for null data");
49 jQuery.data(div, { "test": "in", "test2": "in2" });
50 equals( jQuery.data(div, "test"), "in", "Verify setting an object in data." );
51 equals( jQuery.data(div, "test2"), "in2", "Verify setting an object in data." );
54 jQuery.data( obj, "prop", true );
56 ok( obj[ jQuery.expando ], "Data is being stored on the object." );
57 ok( obj[ jQuery.expando ].prop, "Data is being stored on the object." );
59 equals( jQuery.data( obj, "prop" ), true, "Make sure the right value is retrieved." );
62 test(".data()", function() {
65 var div = jQuery("#foo");
66 div.data("test", "success");
67 same( div.data(), {test: "success"}, "data() get the entire data object" )
70 test(".data(String) and .data(String, Object)", function() {
72 var div = jQuery("<div/>");
74 ok( div.data("test") === undefined, "Check for no data exists" );
76 div.data("test", "success");
77 equals( div.data("test"), "success", "Check for added data" );
79 div.data("test", "overwritten");
80 equals( div.data("test"), "overwritten", "Check for overwritten data" );
82 div.data("test", undefined);
83 equals( div.data("test"), "overwritten", "Check that data wasn't removed");
85 div.data("test", null);
86 ok( div.data("test") === null, "Check for null data");
88 ok( div.data("notexist") === undefined, "Check for no data exists" );
90 div.data("test", "overwritten");
91 var hits = {test:0}, gets = {test:0};
94 .bind("setData",function(e,key,value){ hits[key] += value; })
95 .bind("setData.foo",function(e,key,value){ hits[key] += value; })
96 .bind("getData",function(e,key){ gets[key] += 1; })
97 .bind("getData.foo",function(e,key){ gets[key] += 3; });
99 div.data("test.foo", 2);
100 equals( div.data("test"), "overwritten", "Check for original data" );
101 equals( div.data("test.foo"), 2, "Check for namespaced data" );
102 equals( div.data("test.bar"), "overwritten", "Check for unmatched namespace" );
103 equals( hits.test, 2, "Check triggered setter functions" );
104 equals( gets.test, 5, "Check triggered getter functions" );
110 equals( div.data("test"), 1, "Check for original data" );
111 equals( div.data("test.foo"), 2, "Check for namespaced data" );
112 equals( div.data("test.bar"), 1, "Check for unmatched namespace" );
113 equals( hits.test, 1, "Check triggered setter functions" );
114 equals( gets.test, 5, "Check triggered getter functions" );
120 .bind("getData",function(e,key){ return key + "root"; })
121 .bind("getData.foo",function(e,key){ return key + "foo"; });
123 equals( div.data("test"), "testroot", "Check for original data" );
124 equals( div.data("test.foo"), "testfoo", "Check for namespaced data" );
125 equals( div.data("test.bar"), "testroot", "Check for unmatched namespace" );
128 var $elem = jQuery({});
129 equals( $elem.data('nothing'), undefined, "Non-existent data returns undefined");
130 equals( $elem.data('null',null).data('null'), null, "null's are preserved");
131 equals( $elem.data('emptyString','').data('emptyString'), '', "Empty strings are preserved");
132 equals( $elem.data('false',false).data('false'), false, "false's are preserved");
138 test(".data(Object)", function() {
141 var div = jQuery("<div/>");
143 div.data({ "test": "in", "test2": "in2" });
144 equals( div.data("test"), "in", "Verify setting an object in data." );
145 equals( div.data("test2"), "in2", "Verify setting an object in data." );
148 test("jQuery.removeData", function() {
150 var div = jQuery("#foo")[0];
151 jQuery.data(div, "test", "testing");
152 jQuery.removeData(div, "test");
153 equals( jQuery.data(div, "test"), undefined, "Check removal of data" );
156 test(".removeData()", function() {
158 var div = jQuery("#foo");
159 div.data("test", "testing");
160 div.removeData("test");
161 equals( div.data("test"), undefined, "Check removal of data" );
163 div.data("test", "testing");
164 div.data("test.foo", "testing2");
165 div.removeData("test.bar");
166 equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
167 equals( div.data("test"), "testing", "Make sure data is intact" );
169 div.removeData("test");
170 equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
171 equals( div.data("test"), undefined, "Make sure data is intact" );
173 div.removeData("test.foo");
174 equals( div.data("test.foo"), undefined, "Make sure data is intact" );