PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/View/Helper/JqueryEngineHelperTest.php

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