Skip to content

Commit

Permalink
Improve table column filter coverage (#2038)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored May 8, 2023
1 parent 56de13d commit 6c08e81
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 68 deletions.
2 changes: 1 addition & 1 deletion demos/_unit-test/scroll.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
$grid->setModel($model);

$makeClickJsToastFx = function (string $source) use ($grid) {
return new JsToast(['message' => new JsExpression('[] + [] + []', [$source, ' clicked: ', $grid->table->jsRow()->data('id')])]);
return new JsToast(['message' => new JsExpression('[] + [] + []', [$source, ' clicked: ', $grid->jsRow()->data('id')])]);
};

$grid->addActionButton(['icon' => 'bell'], $makeClickJsToastFx('action'));
Expand Down
5 changes: 3 additions & 2 deletions demos/collection/tablecolumnmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atk4\Ui\Grid;
use Atk4\Ui\Header;
use Atk4\Ui\Js\JsToast;
use Atk4\Ui\Table;
use Atk4\Ui\Text;
use Atk4\Ui\View;
Expand Down Expand Up @@ -40,9 +41,9 @@
Text::addTo($pop)->set('This popup is loaded dynamically');
});

// Another dropdown menu.
// another dropdown menu
$colTitle->addDropdown(['Change', 'Reorder', 'Update'], function (string $item) {
return 'Title item: ' . $item;
return new JsToast(['message' => 'Title item: ' . $item]);
});

// -----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion demos/collection/tablecolumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function init(): void
'value_not_always_present' => random_int(0, 100) > 50 ? 'have value' : '',
'interests' => '1st label, 2nd label',
'rating' => random_int(100, 300) / 100,
'note' => 'lorem ipsum lorem dorem lorem',
'note' => $id !== 3 ? 'lorem ipsum lorem dorem lorem' : null,
]);
}

Expand Down
2 changes: 1 addition & 1 deletion demos/interactive/loader2.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$countryLoader = Loader::addTo($c->addColumn(), ['loadEvent' => false, 'shim' => [Text::class, 'Select country on your left']]);

$grid->table->onRowClick($countryLoader->jsLoad(['id' => $grid->table->jsRow()->data('id')]));
$grid->table->onRowClick($countryLoader->jsLoad(['id' => $grid->jsRow()->data('id')]));

$countryLoader->set(function (Loader $p) {
Form::addTo($p)->setModel((new Country($p->getApp()->db))->load($_GET['id']));
Expand Down
40 changes: 20 additions & 20 deletions src/Behat/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ protected function getFinishedScript(): string
/**
* Wait till jQuery AJAX request finished and no animation is perform.
*/
protected function jqueryWait(string $extraWaitCondition = 'true', int $maxWaitdurationMs = 5000): void
protected function jqueryWait(string $extraWaitCondition = 'true', array $args = [], int $maxWaitdurationMs = 5000): void
{
$finishedScript = '(' . $this->getFinishedScript() . ') && (' . $extraWaitCondition . ')';

$s = microtime(true);
$c = 0;
while (microtime(true) - $s <= $maxWaitdurationMs / 1000) {
$this->getSession()->wait($maxWaitdurationMs, $finishedScript);
$this->getSession()->wait($maxWaitdurationMs, $finishedScript, $args);
usleep(10_000);
if ($this->getSession()->evaluateScript($finishedScript)) {
if ($this->getSession()->evaluateScript($finishedScript, $args)) { // TODO wait() uses evaluateScript(), dedup
if (++$c >= 2) {
return;
}
Expand Down Expand Up @@ -313,7 +313,7 @@ public function iPressMenuButton(string $btnLabel, string $selector): void
{
$menu = $this->findElement(null, $selector);
$link = $this->findElement($menu, '//a[text()="' . $btnLabel . '"]');
$this->getSession()->executeScript('$(\'#' . $link->getAttribute('id') . '\').click()');
$link->click();
}

/**
Expand Down Expand Up @@ -353,15 +353,16 @@ public function iClickLink(string $label): void
public function iClickUsingSelector(string $selector): void
{
$element = $this->findElement(null, $selector);
$this->getSession()->executeScript('$(arguments[0]).click()', [$element]);
$element->click();
}

/**
* @Then I click paginator page :arg1
*/
public function iClickPaginatorPage(string $pageNumber): void
{
$this->getSession()->executeScript('$(\'a.item[data-page=' . $pageNumber . ']\').click()');
$element = $this->findElement(null, 'a.item[data-page="' . $pageNumber . '"]');
$element->click();
}

/**
Expand Down Expand Up @@ -418,7 +419,9 @@ public function iFillModalField(string $fieldName, string $value): void
*/
public function iClickCloseModal(): void
{
$this->getSession()->executeScript('$(\'.modal.visible.active.front > i.icon.close\')[0].click()');
$modal = $this->findElement(null, '.modal.visible.active.front');
$closeIcon = $this->findElement($modal, '//i.icon.close');
$closeIcon->click();
}

/**
Expand Down Expand Up @@ -483,8 +486,7 @@ public function iClickTabWithTitle(string $tabTitle): void
{
$tabMenu = $this->findElement(null, '.ui.tabular.menu');
$link = $this->findElement($tabMenu, '//a[text()="' . $tabTitle . '"]');

$this->getSession()->executeScript('$(\'#' . $link->getAttribute('id') . '\').click()');
$link->click();
}

/**
Expand Down Expand Up @@ -540,24 +542,23 @@ public function iSearchGridFor(string $text): void
*/
public function iSelectValueInLookup(string $value, string $inputName): void
{
$isSelectorXpath = $this->parseSelector($inputName)[0] === 'xpath';

// get dropdown item from Fomantic-UI which is direct parent of input html element
$lookupElem = $this->findElement(null, '//input[@name="' . $inputName . '"]/parent::div');
$lookupElem = $this->findElement(null, ($isSelectorXpath ? $inputName : '//input[@name="' . $inputName . '"]') . '/parent::div');

// open dropdown and wait till fully opened (just a click is not triggering it)
$this->getSession()->executeScript('$(\'#' . $lookupElem->getAttribute('id') . '\').dropdown(\'show\')');
$this->jqueryWait('$(\'#' . $lookupElem->getAttribute('id') . '\').hasClass(\'visible\')');
$this->getSession()->executeScript('$(arguments[0]).dropdown(\'show\')', [$lookupElem]);
$this->jqueryWait('$(arguments[0]).hasClass(\'visible\')', [$lookupElem]);

// select value
$valueElem = $this->findElement($lookupElem, '//div[text()="' . $value . '"]');
$this->getSession()->executeScript('$(\'#' . $lookupElem->getAttribute('id') . '\').dropdown(\'set selected\', ' . $valueElem->getAttribute('data-value') . ');');
$this->getSession()->executeScript('$(arguments[0]).dropdown(\'set selected\', arguments[1]);', [$lookupElem, $valueElem->getAttribute('data-value')]);
$this->jqueryWait();

// hide dropdown and wait till fully closed
$this->getSession()->executeScript('$(\'#' . $lookupElem->getAttribute('id') . '\').dropdown(\'hide\');');
$this->jqueryWait();
// for unknown reasons, dropdown very often remains visible in CI, so hide twice
$this->getSession()->executeScript('$(\'#' . $lookupElem->getAttribute('id') . '\').dropdown(\'hide\');');
$this->jqueryWait('!$(\'#' . $lookupElem->getAttribute('id') . '\').hasClass(\'visible\')');
$this->getSession()->executeScript('$(arguments[0]).dropdown(\'hide\');', [$lookupElem]);
$this->jqueryWait('!$(arguments[0]).hasClass(\'visible\')', [$lookupElem]);
}

/**
Expand Down Expand Up @@ -710,8 +711,7 @@ public function iClickFilterColumnName(string $columnName): void
{
$column = $this->findElement(null, "th[data-column='" . $columnName . "']");
$icon = $this->findElement($column, 'i');

$this->getSession()->executeScript('$(\'#' . $icon->getAttribute('id') . '\').click()');
$icon->click();
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/Form/Control/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class Checkbox extends Form\Control

public function __construct($label = [])
{
if (func_num_args() > 1) { // prevent bad usage
throw new \Error('Too many method arguments');
}

parent::__construct($label);

$this->label = $this->content;
Expand Down
24 changes: 0 additions & 24 deletions src/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,6 @@ protected function initTable(): Table
return $table;
}

/**
* Set Table\Column\Actions seed.
*
* @param array $seed
*/
public function setActionDecorator($seed): void
{
$this->actionButtonsDecorator = $seed;
}

/**
* Set Table\Column\ActionMenu seed.
*
* @param array $seed
*/
public function setActionMenuDecorator($seed): void
{
$this->actionMenuDecorator = $seed;
}

/**
* Add new column to grid. If column with this name already exists,
* an. Simply calls Table::addColumn(), so check that method out.
Expand Down Expand Up @@ -450,10 +430,6 @@ private function getActionMenu()
*/
public function addActionMenuFromModel(string $appliesTo = null): void
{
if (!$this->model) {
throw new Exception('Model not set, set it prior to add item');
}

foreach ($this->model->getUserActions($appliesTo) as $action) {
$this->addActionMenuItem($action);
}
Expand Down
9 changes: 0 additions & 9 deletions src/Table/Column/FilterModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ protected function init(): void
$this->afterInit();
}

/**
* Perform further initialization.
*/
public function afterInit(): void
{
$this->addField('name', ['default' => $this->lookupField->shortName, 'system' => true]);
Expand All @@ -116,9 +113,6 @@ public function afterInit(): void
});
}

/**
* Recall filter model data.
*/
public function recallData(): ?array
{
return $this->recall('data');
Expand All @@ -142,9 +136,6 @@ public function getFormDisplayRules(): array
return [];
}

/**
* Check if this model is using session or not.
*/
public function clearData(): void
{
$this->forget();
Expand Down
2 changes: 1 addition & 1 deletion src/Table/Column/FilterPopup.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function init(): void

$this->form = Form::addTo($this)->addClass('');
$this->form->buttonSave->addClass('');
$this->form->addGroup('Where ' . $this->field->getCaption() . ' :');
$this->form->addGroup('Where ' . $this->field->getCaption() . ':');

$this->form->buttonSave->set('Set');

Expand Down
5 changes: 3 additions & 2 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Atk4\Data\Model;
use Atk4\Data\Persistence;
use Atk4\Ui\Js\Jquery;
use Atk4\Ui\Js\JsBlock;
use Atk4\Ui\Js\JsChain;
use Atk4\Ui\Js\JsExpression;
use Atk4\Ui\Js\JsExpressionable;
Expand Down Expand Up @@ -1122,11 +1123,11 @@ public function getJsRenderActions(): string
$actions = [];
foreach ($this->_jsActions as $eventActions) {
foreach ($eventActions as $action) {
$actions[] = $action->jsRender();
$actions[] = $action;
}
}

return implode('; ', $actions);
return (new JsBlock($actions))->jsRender();
}

/**
Expand Down
25 changes: 24 additions & 1 deletion tests-behat/filter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@ Feature: Table Filter

Scenario:
Given I am on "collection/tablefilter.php"
Then I should see "Clear Filters"
Then I should see "Australia"
Then I click filter column name "atk_fp_country__name"
When I fill in "value" with "united kingdom"
Then I press button "Set"
Then I should not see "Australia"
Then I should see "United Kingdom"
Then I click filter column name "atk_fp_country__phonecode"
When I fill field using "//div.popup[5]//input[@name='value']" with "44"
When I click using selector "//div.popup[5]//div[text()='Set']"
Then I should see "United Kingdom"
Then I click filter column name "atk_fp_country__phonecode"
When I fill field using "//div.popup[5]//input[@name='value']" with "4"
When I click using selector "//div.popup[5]//div[text()='Set']"
Then I should not see "United Kingdom"
Then I should see "No records"
Then I click filter column name "atk_fp_country__phonecode"
When I click using selector "//div.popup[5]//div[text()='Clear']"
Then I should not see "No records"
Then I should see "United Kingdom"
Then I click filter column name "is_uk"
Then I select value "Is No" in lookup "//div.popup[6]//input[@name='op']"
When I click using selector "//div.popup[6]//div[text()='Set']"
Then I should see "No records"
Then I click filter column name "is_uk"
Then I select value "Is Yes" in lookup "//div.popup[6]//input[@name='op']"
When I click using selector "//div.popup[6]//div[text()='Set']"
Then I should see "United Kingdom"
Then I press menu button "Clear Filters" using selector ".ui.menu.atk-grid-menu"
Then I should not see "United Kingdom"
Then I should see "Australia"
3 changes: 3 additions & 0 deletions tests-behat/grid.feature
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ Feature: Grid
Then I should not see "This popup is loaded dynamically"
When I click using selector "(//th//div.atk-table-dropdown)[2]/i"
Then I should see "This popup is loaded dynamically"
When I click using selector "(//th//div.atk-table-dropdown)[3]/div.dropdown"
When I click using selector "(//th//div.atk-table-dropdown)[3]/div.dropdown/div.menu/div.item[2]"
Then Toast display should contain text "Title item: Reorder"
18 changes: 18 additions & 0 deletions tests/TableColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Atk4\Ui\Tests;

use Atk4\Core\Phpunit\TestCase;
use Atk4\Ui\Table\Column;

class TableColumnTest extends TestCase
{
public function testTooManyArgumentsConstructorError(): void
{
$this->expectException(\Error::class);
$this->expectExceptionMessage('Too many method arguments');
new Column([], []);
}
}
Loading

0 comments on commit 6c08e81

Please sign in to comment.