Skip to content
Permalink
Browse files

Adding test case for jquery engine

Adding some methods for jquery engine
Updating methods in base class
  • Loading branch information...
markstory committed Mar 14, 2009
1 parent 518e567 commit 20530f5ffb40eb707f5559ae8ed264eca2316fa1
@@ -25,6 +25,54 @@
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class jqueryEngineHelper extends AppHelper {
/**
* Create javascript selector for a CSS rule
*
* @param string $selector The selector that is targeted
* @param boolean $multiple Whether or not the selector could target more than one element.
* @return object instance of $this. Allows chained methods.
**/
function get($selector, $multiple = false) {
if ($selector == 'window' || $selector == 'document') {
$this->selection = "$(" . $selector .")";
} else {
$this->selection = "$('" . $selector ."')";
}
return $this;
}
/**
* Add an event to the script cache. Operates on the currently selected elements.
*
* @param string $type Type of event to bind to the current dom id
* @param string $callback The Javascript function you wish to trigger or the function literal
* @param boolean $wrap Whether you want your callback wrapped in ```function (event) { }```
* @return string completed event handler
**/
function event($type, $callback, $wrap = false) {
if ($wrap) {
$callback = 'function (event) {' . $callback . '}';
}
$out = $this->selection . ".bind('{$type}', $callback);";
return $out;
}
/**
* Create a domReady event. This is a special event in many libraries
*
* @param string $functionBody The code to run on domReady
* @return string completed domReady method
**/
function domReady($functionBody) {
return $this->get('document')->event('ready', $functionBody, true);
}
/**
* Create an iteration over the current selection result.
*
* @param string $method The method you want to apply to the selection
* @param string $callback The function body you wish to apply during the iteration.
* @return string completed iteration
**/
function each($callback) {
return $this->selection . '.each(function () {' . $callback . '});';
}
}
?>
@@ -268,9 +268,9 @@ class JsBaseEngineHelper extends AppHelper {
* The js snippet for the current selection.
*
* @var string
* @access protected
* @access public
**/
var $_selection;
var $selection;
/**
* Scripts that are queued for output
*
@@ -484,17 +484,18 @@ function getCache($clear = true) {
* @param boolean $multiple Whether or not the selector could target more than one element.
* @return object instance of $this. Allows chained methods.
**/
function select($selector, $multiple = false) {
function get($selector, $multiple = false) {
return $this;
}
/**
* Add an event to the script cache. Operates on the currently selected elements.
*
* @param string $type Type of event to bind to the current dom id
* @param string $callback The Javascript function you wish to trigger or the function literal
* @param boolean $wrap Whether you want your callback wrapped in ```function (event) { }```
* @return string completed event handler
**/
function addEvent($type, $callback) {
function event($type, $callback, $wrap = false) {
}
/**
@@ -509,11 +510,10 @@ function domReady($functionBody) {
/**
* Create an iteration over the current selection result.
*
* @param string $method The method you want to apply to the selection
* @param string $callback The function body you wish to apply during the iteration.
* @return string completed iteration
**/
function each($method, $callback) {
function each($callback) {
}
/**
@@ -0,0 +1,102 @@
<?php
/**
* JqueryEngineTestCase
*
*
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
* Copyright 2006-2008, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake.tests
* @subpackage cake.tests.cases.views.helpers
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Helper', array('Html', 'Js', 'JqueryEngine'));
class JqueryEngineHelperTestCase extends CakeTestCase {
/**
* startTest
*
* @return void
**/
function startTest() {
$this->Jquery =& new JqueryEngineHelper();
}
/**
* end test
*
* @return void
**/
function endTest() {
unset($this->Jquery);
}
/**
* test selector method
*
* @return void
**/
function testSelector() {
$result = $this->Jquery->get('#content');
$this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, "$('#content')");
$result = $this->Jquery->get('document');
$this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, "$(document)");
$result = $this->Jquery->get('window');
$this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, "$(window)");
$result = $this->Jquery->get('ul');
$this->assertEqual($result, $this->Jquery);
$this->assertEqual($this->Jquery->selection, "$('ul')");
}
/**
* test event binding
*
* @return void
**/
function testEvent() {
$result = $this->Jquery->get('#myLink')->event('click', 'doClick');
$expected = "$('#myLink').bind('click', doClick);";
$this->assertEqual($result, $expected);
$result = $this->Jquery->get('#myLink')->event('click', '$(this).hide();', true);
$expected = "\$('#myLink').bind('click', function (event) {\$(this).hide();});";
$this->assertEqual($result, $expected);
}
/**
* test dom ready event creation
*
* @return void
**/
function testDomReady() {
$result = $this->Jquery->domReady('foo.name = "bar";');
$expected = "\$(document).bind('ready', function (event) {foo.name = \"bar\";});";
$this->assertEqual($result, $expected);
}
/**
* test Each method
*
* @return void
**/
function testEach() {
$result = $this->Jquery->get('#foo')->each('$(this).hide();');
$expected = "\$('#foo').each(function () {\$(this).hide();});";
$this->assertEqual($result, $expected);
}
}
?>

0 comments on commit 20530f5

Please sign in to comment.
You can’t perform that action at this time.