comparison cake/tests/cases/libs/view/helpers/jquery_engine.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * JqueryEngineTestCase
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP : Rapid Development Framework <http://www.cakephp.org/>
8 * Copyright 2006-2010, Cake Software Foundation, Inc.
9 * 1785 E. Sahara Avenue, Suite 490-204
10 * Las Vegas, Nevada 89104
11 *
12 * Licensed under The MIT License
13 * Redistributions of files must retain the above copyright notice.
14 *
15 * @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
16 * @link http://cakephp.org CakePHP Project
17 * @package cake.tests
18 * @subpackage cake.tests.cases.views.helpers
19 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20 */
21 App::import('Helper', array('Html', 'Js', 'JqueryEngine'));
22
23 class JqueryEngineHelperTestCase extends CakeTestCase {
24 /**
25 * startTest
26 *
27 * @return void
28 */
29 function startTest() {
30 $this->Jquery =& new JqueryEngineHelper();
31 }
32
33 /**
34 * end test
35 *
36 * @return void
37 */
38 function endTest() {
39 unset($this->Jquery);
40 }
41
42 /**
43 * test selector method
44 *
45 * @return void
46 */
47 function testSelector() {
48 $result = $this->Jquery->get('#content');
49 $this->assertEqual($result, $this->Jquery);
50 $this->assertEqual($this->Jquery->selection, '$("#content")');
51
52 $result = $this->Jquery->get('document');
53 $this->assertEqual($result, $this->Jquery);
54 $this->assertEqual($this->Jquery->selection, '$(document)');
55
56 $result = $this->Jquery->get('window');
57 $this->assertEqual($result, $this->Jquery);
58 $this->assertEqual($this->Jquery->selection, '$(window)');
59
60 $result = $this->Jquery->get('ul');
61 $this->assertEqual($result, $this->Jquery);
62 $this->assertEqual($this->Jquery->selection, '$("ul")');
63 }
64
65 /**
66 * test event binding
67 *
68 * @return void
69 */
70 function testEvent() {
71 $this->Jquery->get('#myLink');
72 $result = $this->Jquery->event('click', 'doClick', array('wrap' => false));
73 $expected = '$("#myLink").bind("click", doClick);';
74 $this->assertEqual($result, $expected);
75
76 $result = $this->Jquery->event('click', '$(this).show();', array('stop' => false));
77 $expected = '$("#myLink").bind("click", function (event) {$(this).show();});';
78 $this->assertEqual($result, $expected);
79
80 $result = $this->Jquery->event('click', '$(this).hide();');
81 $expected = '$("#myLink").bind("click", function (event) {$(this).hide();'."\n".'return false;});';
82 $this->assertEqual($result, $expected);
83 }
84
85 /**
86 * test dom ready event creation
87 *
88 * @return void
89 */
90 function testDomReady() {
91 $result = $this->Jquery->domReady('foo.name = "bar";');
92 $expected = '$(document).ready(function () {foo.name = "bar";});';
93 $this->assertEqual($result, $expected);
94 }
95
96 /**
97 * test Each method
98 *
99 * @return void
100 */
101 function testEach() {
102 $this->Jquery->get('#foo');
103 $result = $this->Jquery->each('$(this).hide();');
104 $expected = '$("#foo").each(function () {$(this).hide();});';
105 $this->assertEqual($result, $expected);
106 }
107
108 /**
109 * test Effect generation
110 *
111 * @return void
112 */
113 function testEffect() {
114 $this->Jquery->get('#foo');
115 $result = $this->Jquery->effect('show');
116 $expected = '$("#foo").show();';
117 $this->assertEqual($result, $expected);
118
119 $result = $this->Jquery->effect('hide');
120 $expected = '$("#foo").hide();';
121 $this->assertEqual($result, $expected);
122
123 $result = $this->Jquery->effect('hide', array('speed' => 'fast'));
124 $expected = '$("#foo").hide("fast");';
125 $this->assertEqual($result, $expected);
126
127 $result = $this->Jquery->effect('fadeIn');
128 $expected = '$("#foo").fadeIn();';
129 $this->assertEqual($result, $expected);
130
131 $result = $this->Jquery->effect('fadeOut');
132 $expected = '$("#foo").fadeOut();';
133 $this->assertEqual($result, $expected);
134
135 $result = $this->Jquery->effect('slideIn');
136 $expected = '$("#foo").slideDown();';
137 $this->assertEqual($result, $expected);
138
139 $result = $this->Jquery->effect('slideOut');
140 $expected = '$("#foo").slideUp();';
141 $this->assertEqual($result, $expected);
142
143 $result = $this->Jquery->effect('slideDown');
144 $expected = '$("#foo").slideDown();';
145 $this->assertEqual($result, $expected);
146
147 $result = $this->Jquery->effect('slideUp');
148 $expected = '$("#foo").slideUp();';
149 $this->assertEqual($result, $expected);
150 }
151
152 /**
153 * Test Request Generation
154 *
155 * @return void
156 */
157 function testRequest() {
158 $result = $this->Jquery->request(array('controller' => 'posts', 'action' => 'view', 1));
159 $expected = '$.ajax({url:"\\/posts\\/view\\/1"});';
160 $this->assertEqual($result, $expected);
161
162 $result = $this->Jquery->request(array('controller' => 'posts', 'action' => 'view', 1), array(
163 'update' => '#content'
164 ));
165 $expected = '$.ajax({dataType:"html", success:function (data, textStatus) {$("#content").html(data);}, url:"\/posts\/view\/1"});';
166 $this->assertEqual($result, $expected);
167
168 $result = $this->Jquery->request('/people/edit/1', array(
169 'method' => 'post',
170 'before' => 'doBefore',
171 'complete' => 'doComplete',
172 'success' => 'doSuccess',
173 'error' => 'handleError',
174 'type' => 'json',
175 'data' => array('name' => 'jim', 'height' => '185cm'),
176 'wrapCallbacks' => false
177 ));
178 $expected = '$.ajax({beforeSend:doBefore, complete:doComplete, data:"name=jim&height=185cm", dataType:"json", error:handleError, success:doSuccess, type:"post", url:"\\/people\\/edit\\/1"});';
179 $this->assertEqual($result, $expected);
180
181 $result = $this->Jquery->request('/people/edit/1', array(
182 'update' => '#updated',
183 'success' => 'doFoo',
184 'method' => 'post',
185 'wrapCallbacks' => false
186 ));
187 $expected = '$.ajax({dataType:"html", success:function (data, textStatus) {doFoo$("#updated").html(data);}, type:"post", url:"\\/people\\/edit\\/1"});';
188 $this->assertEqual($result, $expected);
189
190 $result = $this->Jquery->request('/people/edit/1', array(
191 'update' => '#updated',
192 'success' => 'doFoo',
193 'method' => 'post',
194 'dataExpression' => true,
195 'data' => '$("#someId").serialize()',
196 'wrapCallbacks' => false
197 ));
198 $expected = '$.ajax({data:$("#someId").serialize(), dataType:"html", success:function (data, textStatus) {doFoo$("#updated").html(data);}, type:"post", url:"\\/people\\/edit\\/1"});';
199 $this->assertEqual($result, $expected);
200
201 $result = $this->Jquery->request('/people/edit/1', array(
202 'success' => 'doFoo',
203 'before' => 'doBefore',
204 'method' => 'post',
205 'dataExpression' => true,
206 'data' => '$("#someId").serialize()',
207 ));
208 $expected = '$.ajax({beforeSend:function (XMLHttpRequest) {doBefore}, data:$("#someId").serialize(), success:function (data, textStatus) {doFoo}, type:"post", url:"\\/people\\/edit\\/1"});';
209 $this->assertEqual($result, $expected);
210 }
211
212 /**
213 * test that alternate jQuery object values work for request()
214 *
215 * @return void
216 */
217 function testRequestWithAlternateJqueryObject() {
218 $this->Jquery->jQueryObject = '$j';
219
220 $result = $this->Jquery->request('/people/edit/1', array(
221 'update' => '#updated',
222 'success' => 'doFoo',
223 'method' => 'post',
224 'dataExpression' => true,
225 'data' => '$j("#someId").serialize()',
226 'wrapCallbacks' => false
227 ));
228 $expected = '$j.ajax({data:$j("#someId").serialize(), dataType:"html", success:function (data, textStatus) {doFoo$j("#updated").html(data);}, type:"post", url:"\\/people\\/edit\\/1"});';
229 $this->assertEqual($result, $expected);
230 }
231
232 /**
233 * test sortable list generation
234 *
235 * @return void
236 */
237 function testSortable() {
238 $this->Jquery->get('#myList');
239 $result = $this->Jquery->sortable(array(
240 'distance' => 5,
241 'containment' => 'parent',
242 'start' => 'onStart',
243 'complete' => 'onStop',
244 'sort' => 'onSort',
245 'wrapCallbacks' => false
246 ));
247 $expected = '$("#myList").sortable({containment:"parent", distance:5, sort:onSort, start:onStart, stop:onStop});';
248 $this->assertEqual($result, $expected);
249
250 $result = $this->Jquery->sortable(array(
251 'distance' => 5,
252 'containment' => 'parent',
253 'start' => 'onStart',
254 'complete' => 'onStop',
255 'sort' => 'onSort',
256 ));
257 $expected = '$("#myList").sortable({containment:"parent", distance:5, sort:function (event, ui) {onSort}, start:function (event, ui) {onStart}, stop:function (event, ui) {onStop}});';
258 $this->assertEqual($result, $expected);
259 }
260
261 /**
262 * test drag() method
263 *
264 * @return void
265 */
266 function testDrag() {
267 $this->Jquery->get('#element');
268 $result = $this->Jquery->drag(array(
269 'container' => '#content',
270 'start' => 'onStart',
271 'drag' => 'onDrag',
272 'stop' => 'onStop',
273 'snapGrid' => array(10, 10),
274 'wrapCallbacks' => false
275 ));
276 $expected = '$("#element").draggable({containment:"#content", drag:onDrag, grid:[10,10], start:onStart, stop:onStop});';
277 $this->assertEqual($result, $expected);
278
279 $result = $this->Jquery->drag(array(
280 'container' => '#content',
281 'start' => 'onStart',
282 'drag' => 'onDrag',
283 'stop' => 'onStop',
284 'snapGrid' => array(10, 10),
285 ));
286 $expected = '$("#element").draggable({containment:"#content", drag:function (event, ui) {onDrag}, grid:[10,10], start:function (event, ui) {onStart}, stop:function (event, ui) {onStop}});';
287 $this->assertEqual($result, $expected);
288 }
289
290 /**
291 * test drop() method
292 *
293 * @return void
294 */
295 function testDrop() {
296 $this->Jquery->get('#element');
297 $result = $this->Jquery->drop(array(
298 'accept' => '.items',
299 'hover' => 'onHover',
300 'leave' => 'onExit',
301 'drop' => 'onDrop',
302 'wrapCallbacks' => false
303 ));
304 $expected = '$("#element").droppable({accept:".items", drop:onDrop, out:onExit, over:onHover});';
305 $this->assertEqual($result, $expected);
306
307 $result = $this->Jquery->drop(array(
308 'accept' => '.items',
309 'hover' => 'onHover',
310 'leave' => 'onExit',
311 'drop' => 'onDrop',
312 ));
313 $expected = '$("#element").droppable({accept:".items", drop:function (event, ui) {onDrop}, out:function (event, ui) {onExit}, over:function (event, ui) {onHover}});';
314 $this->assertEqual($result, $expected);
315 }
316
317 /**
318 * test slider generation
319 *
320 * @return void
321 */
322 function testSlider() {
323 $this->Jquery->get('#element');
324 $result = $this->Jquery->slider(array(
325 'complete' => 'onComplete',
326 'change' => 'onChange',
327 'min' => 0,
328 'max' => 10,
329 'value' => 2,
330 'direction' => 'vertical',
331 'wrapCallbacks' => false
332 ));
333 $expected = '$("#element").slider({change:onChange, max:10, min:0, orientation:"vertical", stop:onComplete, value:2});';
334 $this->assertEqual($result, $expected);
335
336 $result = $this->Jquery->slider(array(
337 'complete' => 'onComplete',
338 'change' => 'onChange',
339 'min' => 0,
340 'max' => 10,
341 'value' => 2,
342 'direction' => 'vertical',
343 ));
344 $expected = '$("#element").slider({change:function (event, ui) {onChange}, max:10, min:0, orientation:"vertical", stop:function (event, ui) {onComplete}, value:2});';
345 $this->assertEqual($result, $expected);
346 }
347
348 /**
349 * test the serializeForm method
350 *
351 * @return void
352 */
353 function testSerializeForm() {
354 $this->Jquery->get('#element');
355 $result = $this->Jquery->serializeForm(array('isForm' => false));
356 $expected = '$("#element").closest("form").serialize();';
357 $this->assertEqual($result, $expected);
358
359 $result = $this->Jquery->serializeForm(array('isForm' => true));
360 $expected = '$("#element").serialize();';
361 $this->assertEqual($result, $expected);
362
363 $result = $this->Jquery->serializeForm(array('isForm' => false, 'inline' => true));
364 $expected = '$("#element").closest("form").serialize()';
365 $this->assertEqual($result, $expected);
366 }
367 }