Skip to content

Commit

Permalink
add lazy render test
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 21, 2022
1 parent b6543c6 commit 53b82d1
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions tests/JsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Atk4\Ui\Button;
use Atk4\Ui\Exception;
use Atk4\Ui\JsCallback;
use Atk4\Ui\JsExpression;
use Atk4\Ui\View;

class JsIntegrationTest extends TestCase
Expand Down Expand Up @@ -45,7 +46,7 @@ public function testChainTrue(): void
{
$v = new Button(['name' => 'b']);
$j = $v->js(true)->hide();
$v->getHtml();
$v->renderAll();

static::assertSame('(function () {
$(\'#b\').hide();
Expand All @@ -56,7 +57,7 @@ public function testChainClick(): void
{
$v = new Button(['name' => 'b']);
$v->js('click')->hide();
$v->getHtml();
$v->renderAll();

static::assertSame('(function () {
$(\'#b\').on(\'click\', function (event) {
Expand All @@ -71,7 +72,7 @@ public function testChainClickEmpty(): void
{
$v = new Button(['name' => 'b']);
$v->js('click', null);
$v->getHtml();
$v->renderAll();

static::assertSame('(function () {
$(\'#b\').on(\'click\', function (event) {
Expand All @@ -86,9 +87,9 @@ public function testChainClickEmpty(): void

public function testChainNested(): void
{
$bb = new View(['ui' => 'buttons']);
$b1 = Button::addTo($bb, ['name' => 'b1']);
$b2 = Button::addTo($bb, ['name' => 'b2']);
$v = new View(['ui' => 'buttons']);
$b1 = Button::addTo($v, ['name' => 'b1']);
$b2 = Button::addTo($v, ['name' => 'b2']);

$b1->on('click', [
'preventDefault' => false,
Expand All @@ -97,7 +98,7 @@ public function testChainNested(): void
$b2->js()->hide(),
]);
$b1->js(true)->data('x', 'y');
$bb->getHtml();
$v->renderAll();

static::assertSame('(function () {
$(\'#b1\').on(\'click\', function (event) {
Expand All @@ -107,7 +108,7 @@ public function testChainNested(): void
$(\'#b2\').hide();
});
$(\'#b1\').data(\'x\', \'y\');
})()', $bb->getJs());
})()', $v->getJs());
}

public function testChainNullReturn(): void
Expand All @@ -132,4 +133,32 @@ public function testChainUnsupportedTypeException(): void
$this->expectExceptionMessage('not renderable to JS');
$js->jsRender();
}

public function testChainJsCallbackLazyExecuteRender(): void
{
$v = new View();
$v->invokeInit();
$b = Button::addTo($v);

$jsCallback = new class() extends JsCallback {
public int $counter = 0;

public function jsExecute(): JsExpression
{
++$this->counter;

return parent::jsExecute();
}
};
$v->add($jsCallback);

$b->on('click', $jsCallback);
static::assertSame(0, $jsCallback->counter);

$v->renderAll();
static::assertSame(0, $jsCallback->counter);

$v->getJs();
static::assertSame(1, $jsCallback->counter);
}
}

0 comments on commit 53b82d1

Please sign in to comment.