1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
2 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
---|
3 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
---|
4 | <head> |
---|
5 | <title>script.aculo.us Unit test file</title> |
---|
6 | <meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
---|
7 | <script src="../../lib/prototype.js" type="text/javascript"></script> |
---|
8 | <script src="../../src/scriptaculous.js" type="text/javascript"></script> |
---|
9 | <script src="../../src/unittest.js" type="text/javascript"></script> |
---|
10 | <link rel="stylesheet" href="../test.css" type="text/css" /> |
---|
11 | </head> |
---|
12 | <body> |
---|
13 | <h1>script.aculo.us Unit test file</h1> |
---|
14 | <p> |
---|
15 | Test of sortable functions in dragdrop.js |
---|
16 | </p> |
---|
17 | |
---|
18 | <!-- Log output --> |
---|
19 | <div id="testlog"> </div> |
---|
20 | |
---|
21 | |
---|
22 | <ul id="sortable1"> |
---|
23 | <li id="item_1" class="a">item 1</li> |
---|
24 | <li id="item_2" class="c b">item 1<ul><li id="item_99">!!!</li></ul></li> |
---|
25 | <li id="item_3" class="b">item 1</li> |
---|
26 | <li id="item_xy" class="x y">item 1</li> |
---|
27 | <!-- a comment --> |
---|
28 | </ul> |
---|
29 | |
---|
30 | <div id="sortable2"> |
---|
31 | <div id="item_4">item 4</div> <div id="item_5">item 5</div> |
---|
32 | <img src="icon.png" alt="img"/> |
---|
33 | <!-- a comment --> |
---|
34 | </div> |
---|
35 | |
---|
36 | <div id="sortable3"> |
---|
37 | |
---|
38 | </div> |
---|
39 | |
---|
40 | <!-- |
---|
41 | By default, _ is the only valid seperator |
---|
42 | for the DOM ids. Complex element ids as in |
---|
43 | the form of "some_element_id_1" should also |
---|
44 | be parsed correctly (only the last part should |
---|
45 | be serialized) |
---|
46 | --> |
---|
47 | <ul id="sortable_complex"> |
---|
48 | <li id="a_b_item_1" class="a">item 1</li> |
---|
49 | <li id="ab_item_2" class="c b">item 1 |
---|
50 | <ul> |
---|
51 | <li id="item_99">!!!</li> |
---|
52 | </ul> |
---|
53 | </li> |
---|
54 | <li id="a-b-item_3z_33" class="b">item 1</li> |
---|
55 | <li id="a-item-xy" class="x y">item 1</li> |
---|
56 | <!-- a comment --> |
---|
57 | </ul> |
---|
58 | |
---|
59 | |
---|
60 | <ul id="sortable_specialcreate"> |
---|
61 | <li id="y1item">item 1</li> |
---|
62 | <li id="y2item">item 1<ul><li id="yyyy9928282hjhd">!!!</li></ul></li> |
---|
63 | </ul> |
---|
64 | |
---|
65 | <ul id="sortable_specialformat"> |
---|
66 | <li id="x1item">item 1</li> |
---|
67 | <li id="x2item">item 1<ul><li id="xxxxx88888item">!!!</li></ul></li> |
---|
68 | </ul> |
---|
69 | |
---|
70 | <!-- Tests follow --> |
---|
71 | <script type="text/javascript" language="javascript" charset="utf-8"> |
---|
72 | // <![CDATA[ |
---|
73 | |
---|
74 | new Test.Unit.Runner({ |
---|
75 | |
---|
76 | setup: function() { with(this) { |
---|
77 | Sortable.create('sortable1',{only:['a','b']}); |
---|
78 | Sortable.create('sortable2',{tag:'div'}); |
---|
79 | Sortable.create('sortable3'); |
---|
80 | Sortable.create('sortable_specialcreate',{ format:/(\d+)/ }); |
---|
81 | Sortable.create('sortable_specialformat'); |
---|
82 | Sortable.create('sortable_complex'); |
---|
83 | }}, |
---|
84 | |
---|
85 | teardown: function() { with(this) { |
---|
86 | Sortable.destroy('sortable1'); |
---|
87 | Sortable.destroy('sortable2'); |
---|
88 | Sortable.destroy('sortable3'); |
---|
89 | Sortable.destroy('sortable_specialformat'); |
---|
90 | Sortable.destroy('sortable_specialcreate'); |
---|
91 | Sortable.destroy('sortable_complex'); |
---|
92 | }}, |
---|
93 | |
---|
94 | testSortableSerializeSequenceBasics: function() { with(this) { |
---|
95 | assertEqual('sortable1[]=1&sortable1[]=2&sortable1[]=3', Sortable.serialize('sortable1')); |
---|
96 | |
---|
97 | // test empty sortable |
---|
98 | assertEqual('', Sortable.serialize('sortable3')); |
---|
99 | assertEnumEqual([], Sortable.sequence('sortable3')); |
---|
100 | |
---|
101 | Element.remove('item_4'); |
---|
102 | assertEqual('sortable2[]=5', Sortable.serialize('sortable2')); |
---|
103 | assertEnumEqual(['5'], Sortable.sequence('sortable2')); |
---|
104 | }}, |
---|
105 | |
---|
106 | testSortableSerializeFormat: function() { with(this) { |
---|
107 | // should correctly serialize from option given to Sortable.create() |
---|
108 | assertEqual('sortable_specialcreate[]=1&sortable_specialcreate[]=2', |
---|
109 | Sortable.serialize('sortable_specialcreate')); |
---|
110 | |
---|
111 | benchmark(function(){ |
---|
112 | Sortable.serialize('sortable_specialcreate') |
---|
113 | },1,'Sortable.serialize'); |
---|
114 | |
---|
115 | // test special format given explicitly |
---|
116 | assertEqual('sortable_specialformat[]=1&sortable_specialformat[]=2', |
---|
117 | Sortable.serialize('sortable_specialformat', {format:/(\d+)/})); |
---|
118 | |
---|
119 | // return full id |
---|
120 | assertEqual('sortable_specialformat[]=x1item&sortable_specialformat[]=x2item', |
---|
121 | Sortable.serialize('sortable_specialformat', {format:/(.*)/})); |
---|
122 | |
---|
123 | // test default format given explicitly |
---|
124 | assertEqual('sortable1[]=1&sortable1[]=2&sortable1[]=3', |
---|
125 | Sortable.serialize('sortable1',{format:/^[^_]*_(.*)$/})); |
---|
126 | |
---|
127 | // Ensure default options.format handles longer, more complex list |
---|
128 | // item IDs |
---|
129 | assertEqual('sortable_complex[]=1&sortable_complex[]=2&sortable_complex[]=33&sortable_complex[]=', |
---|
130 | Sortable.serialize('sortable_complex')); |
---|
131 | }}, |
---|
132 | |
---|
133 | testSortableSerializeRule: function() { with(this) { |
---|
134 | var ids = ['x','x-y','test_test','x_y_z','x_y-x_z']; |
---|
135 | ids.each(function(id){ |
---|
136 | assertEqual('1', |
---|
137 | (id+'_1').match(Sortable.SERIALIZE_RULE)[1]); |
---|
138 | }); |
---|
139 | |
---|
140 | assertNull('x'.match(Sortable.SERIALIZE_RULE)); |
---|
141 | }}, |
---|
142 | |
---|
143 | testSortableSerializeName: function() { with(this) { |
---|
144 | assertEqual('dumdidu[]=1&dumdidu[]=2', |
---|
145 | Sortable.serialize('sortable_specialcreate',{name:'dumdidu'})); |
---|
146 | }}, |
---|
147 | |
---|
148 | testSortableSequenceFormat: function() { with(this) { |
---|
149 | // shauld correctly serialize from option given to Sortable.create() |
---|
150 | assertEnumEqual(['1','2'], |
---|
151 | Sortable.sequence('sortable_specialcreate')); |
---|
152 | |
---|
153 | // test special format given explicitly |
---|
154 | assertEnumEqual(['1','2'], |
---|
155 | Sortable.sequence('sortable_specialformat', {format:/(\d+)/})); |
---|
156 | |
---|
157 | // return full id |
---|
158 | assertEnumEqual(['x1item','x2item'], |
---|
159 | Sortable.sequence('sortable_specialformat', {format:/(.*)/})); |
---|
160 | |
---|
161 | // test default format given explicitly |
---|
162 | assertEnumEqual(['1','2','3'], |
---|
163 | Sortable.sequence('sortable1',{format:/^[^_]*_(.*)$/})); |
---|
164 | }}, |
---|
165 | |
---|
166 | testSortableFindElements: function() { with(this) { |
---|
167 | assertEqual(3, Sortable.findElements($('sortable1'),{tag:'li',only:['a','b']}).length); |
---|
168 | benchmark(function(){ |
---|
169 | Sortable.findElements($('sortable1'),{tag:'li',only:['a','b']}) |
---|
170 | },1,'Sortable.findElements/1'); |
---|
171 | |
---|
172 | assertEqual(1, Sortable.findElements($('sortable1'),{tag:'li',only:['x']}).length); |
---|
173 | assertEqual(1, Sortable.findElements($('sortable1'),{tag:'li',only:'a'}).length); |
---|
174 | assertEqual(2, Sortable.findElements($('sortable1'),{tag:'li',only:'b'}).length); |
---|
175 | assertEqual(4, Sortable.findElements($('sortable1'),{tag:'li',only:['a','b','x']}).length); |
---|
176 | }}, |
---|
177 | |
---|
178 | testSortableSetSequence: function() { with(this) { |
---|
179 | // make sure assigning current sequence is a no-op |
---|
180 | var o = Sortable.sequence('sortable1'); |
---|
181 | Sortable.setSequence('sortable1', ['1','2','3']); |
---|
182 | assertEnumEqual(o, Sortable.sequence('sortable1')); |
---|
183 | |
---|
184 | // check new sequence |
---|
185 | Sortable.setSequence('sortable1', ['3','2','1']); |
---|
186 | assertEnumEqual(['3','2','1'], Sortable.sequence('sortable1')); |
---|
187 | |
---|
188 | // non-default format |
---|
189 | Sortable.setSequence('sortable_specialformat', ['2','1'], { format:/(\d+)/ }); |
---|
190 | assertEnumEqual(['2','1'], Sortable.sequence('sortable_specialformat')); |
---|
191 | |
---|
192 | // invalid sequence ids should be ignored |
---|
193 | Sortable.setSequence('sortable1', ['x', '1', 'y', '2', '3', 'z']); |
---|
194 | assertEnumEqual(['1', '2', '3'], Sortable.sequence('sortable1')); |
---|
195 | |
---|
196 | // elements omitted in new sequence should be cropped |
---|
197 | Sortable.setSequence('sortable1', ['1', '3']); |
---|
198 | assertEnumEqual(['1', '3'], Sortable.sequence('sortable1')); |
---|
199 | }} |
---|
200 | |
---|
201 | }, "testlog"); |
---|
202 | // ]]> |
---|
203 | </script> |
---|
204 | </body> |
---|
205 | </html> |
---|