diff --git a/cake/tests/cases/libs/view/helpers/javascript.test.php b/cake/tests/cases/libs/view/helpers/javascript.test.php index 4e02db593..b071d8faf 100644 --- a/cake/tests/cases/libs/view/helpers/javascript.test.php +++ b/cake/tests/cases/libs/view/helpers/javascript.test.php @@ -66,6 +66,14 @@ class JavascriptTest extends UnitTestCase { 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() { $result = $this->Javascript->link('script.js'); $expected = ''; @@ -257,6 +265,11 @@ class JavascriptTest extends UnitTestCase { $this->assertPattern('/^]+type="text\/javascript">.+<\/script>$/s', $result); $this->assertPattern('/^]+type="text\/javascript"[^<>]*>/', $result); $this->assertNoPattern('/^]*>/', $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() { @@ -478,6 +491,42 @@ class JavascriptTest extends UnitTestCase { $expected = 'CakePHP: \\\'Rapid Development Framework\\\''; $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('/^]+>\s*' . str_replace('/', '\\/', preg_quote('//')) . '\s*<\/script>$/', $result); + $this->assertPattern('/^]+type="text\/javascript">.+' . str_replace('/', '\\/', preg_quote('Event.observe($(\'myId\'), \'click\', function(event) { something(); }, false);')) . '.+<\/script>$/s', $result); + $this->assertPattern('/^]+type="text\/javascript"[^<>]*>/', $result); + $this->assertNoPattern('/^]*>/', $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; + } } ?> \ No newline at end of file