sortable_test.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. <!-- Log output -->
  18. <div id="testlog"> </div>
  19. <ul id="sortable1">
  20. <li id="item_1" class="a">item 1</li>
  21. <li id="item_2" class="c b">item 1<ul><li id="item_99">!!!</li></ul></li>
  22. <li id="item_3" class="b">item 1</li>
  23. <li id="item_xy" class="x y">item 1</li>
  24. <!-- a comment -->
  25. </ul>
  26. <div id="sortable2">
  27. <div id="item_4">item 4</div> <div id="item_5">item 5</div>
  28. <img src="icon.png" alt="img"/>
  29. <!-- a comment -->
  30. </div>
  31. <div id="sortable3">
  32. </div>
  33. <!--
  34. By default, _ is the only valid seperator
  35. for the DOM ids. Complex element ids as in
  36. the form of "some_element_id_1" should also
  37. be parsed correctly (only the last part should
  38. be serialized)
  39. -->
  40. <ul id="sortable_complex">
  41. <li id="a_b_item_1" class="a">item 1</li>
  42. <li id="ab_item_2" class="c b">item 1
  43. <ul>
  44. <li id="item_99">!!!</li>
  45. </ul>
  46. </li>
  47. <li id="a-b-item_3z_33" class="b">item 1</li>
  48. <li id="a-item-xy" class="x y">item 1</li>
  49. <!-- a comment -->
  50. </ul>
  51. <ul id="sortable_specialcreate">
  52. <li id="y1item">item 1</li>
  53. <li id="y2item">item 1<ul><li id="yyyy9928282hjhd">!!!</li></ul></li>
  54. </ul>
  55. <ul id="sortable_specialformat">
  56. <li id="x1item">item 1</li>
  57. <li id="x2item">item 1<ul><li id="xxxxx88888item">!!!</li></ul></li>
  58. </ul>
  59. <!-- Tests follow -->
  60. <script type="text/javascript" language="javascript" charset="utf-8">
  61. // <![CDATA[
  62. new Test.Unit.Runner({
  63. setup: function() { with(this) {
  64. Sortable.create('sortable1',{only:['a','b']});
  65. Sortable.create('sortable2',{tag:'div'});
  66. Sortable.create('sortable3');
  67. Sortable.create('sortable_specialcreate',{ format:/(\d+)/ });
  68. Sortable.create('sortable_specialformat');
  69. Sortable.create('sortable_complex');
  70. }},
  71. teardown: function() { with(this) {
  72. Sortable.destroy('sortable1');
  73. Sortable.destroy('sortable2');
  74. Sortable.destroy('sortable3');
  75. Sortable.destroy('sortable_specialformat');
  76. Sortable.destroy('sortable_specialcreate');
  77. Sortable.destroy('sortable_complex');
  78. }},
  79. testSortableSerializeSequenceBasics: function() { with(this) {
  80. assertEqual('sortable1[]=1&sortable1[]=2&sortable1[]=3', Sortable.serialize('sortable1'));
  81. // test empty sortable
  82. assertEqual('', Sortable.serialize('sortable3'));
  83. assertEnumEqual([], Sortable.sequence('sortable3'));
  84. Element.remove('item_4');
  85. assertEqual('sortable2[]=5', Sortable.serialize('sortable2'));
  86. assertEnumEqual(['5'], Sortable.sequence('sortable2'));
  87. }},
  88. testSortableSerializeFormat: function() { with(this) {
  89. // should correctly serialize from option given to Sortable.create()
  90. assertEqual('sortable_specialcreate[]=1&sortable_specialcreate[]=2',
  91. Sortable.serialize('sortable_specialcreate'));
  92. benchmark(function(){
  93. Sortable.serialize('sortable_specialcreate')
  94. },1,'Sortable.serialize');
  95. // test special format given explicitly
  96. assertEqual('sortable_specialformat[]=1&sortable_specialformat[]=2',
  97. Sortable.serialize('sortable_specialformat', {format:/(\d+)/}));
  98. // return full id
  99. assertEqual('sortable_specialformat[]=x1item&sortable_specialformat[]=x2item',
  100. Sortable.serialize('sortable_specialformat', {format:/(.*)/}));
  101. // test default format given explicitly
  102. assertEqual('sortable1[]=1&sortable1[]=2&sortable1[]=3',
  103. Sortable.serialize('sortable1',{format:/^[^_]*_(.*)$/}));
  104. // Ensure default options.format handles longer, more complex list
  105. // item IDs
  106. assertEqual('sortable_complex[]=1&sortable_complex[]=2&sortable_complex[]=33&sortable_complex[]=',
  107. Sortable.serialize('sortable_complex'));
  108. }},
  109. testSortableSerializeRule: function() { with(this) {
  110. var ids = ['x','x-y','test_test','x_y_z','x_y-x_z'];
  111. ids.each(function(id){
  112. assertEqual('1',
  113. (id+'_1').match(Sortable.SERIALIZE_RULE)[1]);
  114. });
  115. assertNull('x'.match(Sortable.SERIALIZE_RULE));
  116. }},
  117. testSortableSerializeName: function() { with(this) {
  118. assertEqual('dumdidu[]=1&dumdidu[]=2',
  119. Sortable.serialize('sortable_specialcreate',{name:'dumdidu'}));
  120. }},
  121. testSortableSequenceFormat: function() { with(this) {
  122. // shauld correctly serialize from option given to Sortable.create()
  123. assertEnumEqual(['1','2'],
  124. Sortable.sequence('sortable_specialcreate'));
  125. // test special format given explicitly
  126. assertEnumEqual(['1','2'],
  127. Sortable.sequence('sortable_specialformat', {format:/(\d+)/}));
  128. // return full id
  129. assertEnumEqual(['x1item','x2item'],
  130. Sortable.sequence('sortable_specialformat', {format:/(.*)/}));
  131. // test default format given explicitly
  132. assertEnumEqual(['1','2','3'],
  133. Sortable.sequence('sortable1',{format:/^[^_]*_(.*)$/}));
  134. }},
  135. testSortableFindElements: function() { with(this) {
  136. assertEqual(3, Sortable.findElements($('sortable1'),{tag:'li',only:['a','b']}).length);
  137. benchmark(function(){
  138. Sortable.findElements($('sortable1'),{tag:'li',only:['a','b']})
  139. },1,'Sortable.findElements/1');
  140. assertEqual(1, Sortable.findElements($('sortable1'),{tag:'li',only:['x']}).length);
  141. assertEqual(1, Sortable.findElements($('sortable1'),{tag:'li',only:'a'}).length);
  142. assertEqual(2, Sortable.findElements($('sortable1'),{tag:'li',only:'b'}).length);
  143. assertEqual(4, Sortable.findElements($('sortable1'),{tag:'li',only:['a','b','x']}).length);
  144. }},
  145. testSortableSetSequence: function() { with(this) {
  146. // make sure assigning current sequence is a no-op
  147. var o = Sortable.sequence('sortable1');
  148. Sortable.setSequence('sortable1', ['1','2','3']);
  149. assertEnumEqual(o, Sortable.sequence('sortable1'));
  150. // check new sequence
  151. Sortable.setSequence('sortable1', ['3','2','1']);
  152. assertEnumEqual(['3','2','1'], Sortable.sequence('sortable1'));
  153. // non-default format
  154. Sortable.setSequence('sortable_specialformat', ['2','1'], { format:/(\d+)/ });
  155. assertEnumEqual(['2','1'], Sortable.sequence('sortable_specialformat'));
  156. // invalid sequence ids should be ignored
  157. Sortable.setSequence('sortable1', ['x', '1', 'y', '2', '3', 'z']);
  158. assertEnumEqual(['1', '2', '3'], Sortable.sequence('sortable1'));
  159. // elements omitted in new sequence should be cropped
  160. Sortable.setSequence('sortable1', ['1', '3']);
  161. assertEnumEqual(['1', '3'], Sortable.sequence('sortable1'));
  162. }}
  163. }, "testlog");
  164. // ]]>
  165. </script>
  166. </body>
  167. </html>