JqueryEngineHelperTest.php 11 KB

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