Split the queue code out from data.js into a dedicated queue.js file (also split...
[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, false, "jQuery.data did not add an expando to the object" );
11         
12         jQuery.data(obj, true);
13         equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );
14         
15         jQuery.data(obj, 'test');
16         equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );
17         
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, true, "jQuery.data added an entry to jQuery.cache" );
23         
24         equals( jQuery.cache[id].foo, "bar", "jQuery.data worked correctly" );
25 });
26
27 test("jQuery.data", function() {
28         expect(6);
29         var div = jQuery("#foo")[0];
30         equals( jQuery.data(div, "test"), undefined, "Check for no data exists" );
31         
32         jQuery.data(div, "test", "success");
33         equals( jQuery.data(div, "test"), "success", "Check for added data" );
34         
35         var data = jQuery.data(div);
36         same( data, { "test": "success" }, "Return complete data set" );
37         
38         jQuery.data(div, "test", "overwritten");
39         equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );
40         
41         jQuery.data(div, "test", undefined);
42         equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");
43         
44         jQuery.data(div, "test", null);
45         ok( jQuery.data(div, "test") === null, "Check for null data");
46 });
47
48 test(".data()", function() {
49         expect(1);
50
51         var div = jQuery("#foo");
52         div.data("test", "success");
53         same( div.data(), {test: "success"}, "data() get the entire data object" )
54 })
55
56 test(".data(String) and .data(String, Object)", function() {
57         expect(22);
58         var div = jQuery("#foo");
59         equals( div.data("test"), undefined, "Check for no data exists" );
60         div.data("test", "success");
61         equals( div.data("test"), "success", "Check for added data" );
62         div.data("test", "overwritten");
63         equals( div.data("test"), "overwritten", "Check for overwritten data" );
64         div.data("test", undefined);
65         equals( div.data("test"), "overwritten", "Check that data wasn't removed");
66         div.data("test", null);
67         ok( div.data("test") === null, "Check for null data");
68
69         div.data("test", "overwritten");
70         var hits = {test:0}, gets = {test:0};
71
72         div
73                 .bind("setData",function(e,key,value){ hits[key] += value; })
74                 .bind("setData.foo",function(e,key,value){ hits[key] += value; })
75                 .bind("getData",function(e,key){ gets[key] += 1; })
76                 .bind("getData.foo",function(e,key){ gets[key] += 3; });
77
78         div.data("test.foo", 2);
79         equals( div.data("test"), "overwritten", "Check for original data" );
80         equals( div.data("test.foo"), 2, "Check for namespaced data" );
81         equals( div.data("test.bar"), "overwritten", "Check for unmatched namespace" );
82         equals( hits.test, 2, "Check triggered setter functions" );
83         equals( gets.test, 5, "Check triggered getter functions" );
84
85         hits.test = 0;
86         gets.test = 0;
87
88         div.data("test", 1);
89         equals( div.data("test"), 1, "Check for original data" );
90         equals( div.data("test.foo"), 2, "Check for namespaced data" );
91         equals( div.data("test.bar"), 1, "Check for unmatched namespace" );
92         equals( hits.test, 1, "Check triggered setter functions" );
93         equals( gets.test, 5, "Check triggered getter functions" );
94
95         hits.test = 0;
96         gets.test = 0;
97
98         div
99                 .bind("getData",function(e,key){ return key + "root"; })
100                 .bind("getData.foo",function(e,key){ return key + "foo"; });
101
102         equals( div.data("test"), "testroot", "Check for original data" );
103         equals( div.data("test.foo"), "testfoo", "Check for namespaced data" );
104         equals( div.data("test.bar"), "testroot", "Check for unmatched namespace" );
105         
106         // #3748
107         var $elem = jQuery({});
108         equals( $elem.data('nothing'), undefined, "Non-existent data returns undefined");
109         equals( $elem.data('null',null).data('null'), null, "null's are preserved");
110         equals( $elem.data('emptyString','').data('emptyString'), '', "Empty strings are preserved");
111         equals( $elem.data('false',false).data('false'), false, "false's are preserved");
112         
113         // Clean up
114         $elem.removeData();
115 });
116
117 test("jQuery.removeData", function() {
118         expect(1);
119         var div = jQuery("#foo")[0];
120         jQuery.data(div, "test", "testing");
121         jQuery.removeData(div, "test");
122         equals( jQuery.data(div, "test"), undefined, "Check removal of data" );
123 });
124
125 test(".removeData()", function() {
126         expect(6);
127         var div = jQuery("#foo");
128         div.data("test", "testing");
129         div.removeData("test");
130         equals( div.data("test"), undefined, "Check removal of data" );
131
132         div.data("test", "testing");
133         div.data("test.foo", "testing2");
134         div.removeData("test.bar");
135         equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
136         equals( div.data("test"), "testing", "Make sure data is intact" );
137
138         div.removeData("test");
139         equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
140         equals( div.data("test"), undefined, "Make sure data is intact" );
141
142         div.removeData("test.foo");
143         equals( div.data("test.foo"), undefined, "Make sure data is intact" );
144 });