More code coverage for JavascriptTest

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6807 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mariano.iglesias 2008-05-11 19:37:45 +00:00
parent 219dca11a0
commit 98cf0f47f5

View file

@ -66,6 +66,14 @@ class JavascriptTest extends UnitTestCase {
unset($this->View); unset($this->View);
} }
function testConstruct() {
$Javascript =& new JavascriptHelper(array('safe'));
$this->assertTrue($Javascript->safe);
$Javascript =& new JavascriptHelper(array('safe' => false));
$this->assertFalse($Javascript->safe);
}
function testLink() { function testLink() {
$result = $this->Javascript->link('script.js'); $result = $this->Javascript->link('script.js');
$expected = '<script type="text/javascript" src="js/script.js"></script>'; $expected = '<script type="text/javascript" src="js/script.js"></script>';
@ -257,6 +265,11 @@ class JavascriptTest extends UnitTestCase {
$this->assertPattern('/^<script[^<>]+type="text\/javascript">.+<\/script>$/s', $result); $this->assertPattern('/^<script[^<>]+type="text\/javascript">.+<\/script>$/s', $result);
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>/', $result); $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
$this->assertNoPattern('/^<script[^type]=[^<>]*>/', $result); $this->assertNoPattern('/^<script[^type]=[^<>]*>/', $result);
$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8), 'object' => array('inner' => array('value' => 1)));
$result = $this->Javascript->object($object);
$expected = '{"title":"New thing","indexes":[5,6,7,8],"object":{"inner":{"value":1}}}';
$this->assertEqual($result, $expected);
} }
function testScriptBlock() { function testScriptBlock() {
@ -478,6 +491,42 @@ class JavascriptTest extends UnitTestCase {
$expected = 'CakePHP: \\\'Rapid Development Framework\\\''; $expected = 'CakePHP: \\\'Rapid Development Framework\\\'';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
function testAfterRender() {
$this->Javascript->cacheEvents();
$result = $this->Javascript->event('myId', 'click', 'something();');
$this->assertNull($result);
ob_start();
$this->Javascript->afterRender();
$result = ob_get_clean();
$this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*.+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
$this->assertPattern('/^<script[^<>]+type="text\/javascript">.+' . str_replace('/', '\\/', preg_quote('Event.observe($(\'myId\'), \'click\', function(event) { something(); }, false);')) . '.+<\/script>$/s', $result);
$this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
$this->assertNoPattern('/^<script[^type]=[^<>]*>/', $result);
$result = $this->Javascript->getCache();
$this->assertTrue(empty($result));
$old = $this->Javascript->enabled;
$this->Javascript->enabled = false;
$this->Javascript->cacheEvents();
$result = $this->Javascript->event('myId', 'click', 'something();');
$this->assertNull($result);
ob_start();
$this->Javascript->afterRender();
$result = ob_get_clean();
$this->assertTrue(empty($result));
$result = $this->Javascript->getCache();
$this->assertPattern('/^' . str_replace('/', '\\/', preg_quote('Event.observe($(\'myId\'), \'click\', function(event) { something(); }, false);')) . '$/s', $result);
$this->Javascript->enabled = $old;
}
} }
?> ?>