summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/ajax/scriptaculous/test/unit/sortable_test.html
blob: c2bafd0b9db6158158384fd817a5ac75d78bb5de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>script.aculo.us Unit test file</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script src="../../lib/prototype.js" type="text/javascript"></script>
  <script src="../../src/scriptaculous.js" type="text/javascript"></script>
  <script src="../../src/unittest.js" type="text/javascript"></script>
  <link rel="stylesheet" href="../test.css" type="text/css" />
</head>
<body>
<h1>script.aculo.us Unit test file</h1>
<p>
  Test of sortable functions in dragdrop.js
</p>

<!-- Log output -->
<div id="testlog"> </div>


<ul id="sortable1">
  <li id="item_1" class="a">item 1</li>
  <li id="item_2" class="c b">item 1<ul><li id="item_99">!!!</li></ul></li>
  <li id="item_3" class="b">item 1</li>
  <li id="item_xy" class="x y">item 1</li>
  <!-- a comment -->
</ul>

<div id="sortable2">
  <div id="item_4">item 4</div>  <div id="item_5">item 5</div>
  <img src="icon.png" alt="img"/>
  <!-- a comment -->
</div>

<div id="sortable3">
  
</div>

<!-- 
  By default, _ is the only valid seperator
  for the DOM ids. Complex element ids as in
  the form of "some_element_id_1" should also
  be parsed correctly (only the last part should
  be serialized)
-->
<ul id="sortable_complex">
  <li id="a_b_item_1" class="a">item 1</li>
  <li id="ab_item_2" class="c b">item 1
    <ul>
      <li id="item_99">!!!</li>
    </ul>
  </li>
  <li id="a-b-item_3z_33" class="b">item 1</li>
  <li id="a-item-xy" class="x y">item 1</li>
  <!-- a comment -->
</ul>


<ul id="sortable_specialcreate">
  <li id="y1item">item 1</li>
  <li id="y2item">item 1<ul><li id="yyyy9928282hjhd">!!!</li></ul></li>
</ul>

<ul id="sortable_specialformat">
  <li id="x1item">item 1</li>
  <li id="x2item">item 1<ul><li id="xxxxx88888item">!!!</li></ul></li>
</ul>

<!-- Tests follow -->
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[

  new Test.Unit.Runner({
    
    setup: function() { with(this) {
      Sortable.create('sortable1',{only:['a','b']});
      Sortable.create('sortable2',{tag:'div'});
      Sortable.create('sortable3');
      Sortable.create('sortable_specialcreate',{ format:/(\d+)/ });
      Sortable.create('sortable_specialformat');
      Sortable.create('sortable_complex');
    }},
    
    teardown: function() { with(this) {
      Sortable.destroy('sortable1');
      Sortable.destroy('sortable2');
      Sortable.destroy('sortable3');
      Sortable.destroy('sortable_specialformat');
      Sortable.destroy('sortable_specialcreate');
      Sortable.destroy('sortable_complex');
    }},
    
    testSortableSerializeSequenceBasics: function() { with(this) {
      assertEqual('sortable1[]=1&sortable1[]=2&sortable1[]=3', Sortable.serialize('sortable1'));      
      
      // test empty sortable
      assertEqual('', Sortable.serialize('sortable3'));
      assertEnumEqual([], Sortable.sequence('sortable3'));
      
      Element.remove('item_4');
      assertEqual('sortable2[]=5', Sortable.serialize('sortable2'));
      assertEnumEqual(['5'], Sortable.sequence('sortable2'));
    }},
    
    testSortableSerializeFormat: function() { with(this) {
      // should correctly serialize from option given to Sortable.create()
      assertEqual('sortable_specialcreate[]=1&sortable_specialcreate[]=2', 
        Sortable.serialize('sortable_specialcreate'));
        
      benchmark(function(){
        Sortable.serialize('sortable_specialcreate')
      },1,'Sortable.serialize');
      
      // test special format given explicitly
      assertEqual('sortable_specialformat[]=1&sortable_specialformat[]=2', 
        Sortable.serialize('sortable_specialformat', {format:/(\d+)/}));
 
      // return full id
      assertEqual('sortable_specialformat[]=x1item&sortable_specialformat[]=x2item', 
        Sortable.serialize('sortable_specialformat', {format:/(.*)/}));
      
      // test default format given explicitly
      assertEqual('sortable1[]=1&sortable1[]=2&sortable1[]=3', 
        Sortable.serialize('sortable1',{format:/^[^_]*_(.*)$/}));
        
      // Ensure default options.format handles longer, more complex list
      // item IDs
      assertEqual('sortable_complex[]=1&sortable_complex[]=2&sortable_complex[]=33&sortable_complex[]=',
        Sortable.serialize('sortable_complex'));
    }},
    
    testSortableSerializeRule: function() { with(this) {
      var ids = ['x','x-y','test_test','x_y_z','x_y-x_z'];
      ids.each(function(id){
        assertEqual('1',
          (id+'_1').match(Sortable.SERIALIZE_RULE)[1]);
      });
      
      assertNull('x'.match(Sortable.SERIALIZE_RULE));
    }},

    testSortableSerializeName: function() { with(this) {
      assertEqual('dumdidu[]=1&dumdidu[]=2',
      Sortable.serialize('sortable_specialcreate',{name:'dumdidu'}));
    }},
    
    testSortableSequenceFormat: function() { with(this) {
      // shauld correctly serialize from option given to Sortable.create()
      assertEnumEqual(['1','2'], 
        Sortable.sequence('sortable_specialcreate'));
      
      // test special format given explicitly
      assertEnumEqual(['1','2'], 
        Sortable.sequence('sortable_specialformat', {format:/(\d+)/}));
 
      // return full id
      assertEnumEqual(['x1item','x2item'], 
        Sortable.sequence('sortable_specialformat', {format:/(.*)/}));
      
      // test default format given explicitly
      assertEnumEqual(['1','2','3'], 
        Sortable.sequence('sortable1',{format:/^[^_]*_(.*)$/}));
    }},
    
    testSortableFindElements: function() { with(this) {
      assertEqual(3, Sortable.findElements($('sortable1'),{tag:'li',only:['a','b']}).length);
      benchmark(function(){
        Sortable.findElements($('sortable1'),{tag:'li',only:['a','b']})
      },1,'Sortable.findElements/1');
      
      assertEqual(1, Sortable.findElements($('sortable1'),{tag:'li',only:['x']}).length);
      assertEqual(1, Sortable.findElements($('sortable1'),{tag:'li',only:'a'}).length);
      assertEqual(2, Sortable.findElements($('sortable1'),{tag:'li',only:'b'}).length);
      assertEqual(4, Sortable.findElements($('sortable1'),{tag:'li',only:['a','b','x']}).length);
    }},

    testSortableSetSequence: function() { with(this) {
      // make sure assigning current sequence is a no-op 
      var o = Sortable.sequence('sortable1');
      Sortable.setSequence('sortable1', ['1','2','3']);
      assertEnumEqual(o, Sortable.sequence('sortable1'));
      
      // check new sequence   
      Sortable.setSequence('sortable1', ['3','2','1']);
      assertEnumEqual(['3','2','1'], Sortable.sequence('sortable1'));
      
      // non-default format
      Sortable.setSequence('sortable_specialformat', ['2','1'], { format:/(\d+)/ });
      assertEnumEqual(['2','1'], Sortable.sequence('sortable_specialformat'));
      
      // invalid sequence ids should be ignored
      Sortable.setSequence('sortable1', ['x', '1', 'y', '2', '3', 'z']);
      assertEnumEqual(['1', '2', '3'], Sortable.sequence('sortable1'));
      
      // elements omitted in new sequence should be cropped
      Sortable.setSequence('sortable1', ['1', '3']);
      assertEnumEqual(['1', '3'], Sortable.sequence('sortable1'));
    }}

  }, "testlog");
// ]]>
</script>
</body>
</html>