protected function WidgetJSTest::assertListItems in Facets 8
Checks that the list items that wrap checkboxes are rendered correctly.
Parameters
array[] $expected: An array of expected properties, each an array with the following values:
- The expected checkbox value.
- The expected number of results, displayed in the checkbox label.
- The URI leading to the updated search results.
- A boolean indicating whether the checkbox is expected to be checked.
\Behat\Mink\Element\NodeElement[] $list_items: The list items to check.
1 call to WidgetJSTest::assertListItems()
- WidgetJSTest::testCheckboxWidget in tests/
src/ FunctionalJavascript/ WidgetJSTest.php - Tests checkbox widget.
File
- tests/
src/ FunctionalJavascript/ WidgetJSTest.php, line 202
Class
- WidgetJSTest
- Tests for the JS that transforms widgets into form elements.
Namespace
Drupal\Tests\facets\FunctionalJavascriptCode
protected function assertListItems(array $expected, array $list_items) : void {
$this
->assertCount(count($expected), $list_items);
foreach ($expected as $key => [
$keyword,
$count,
$uri,
$selected,
]) {
$list_item = $list_items[$key];
// The list element should be visible.
$this
->assertTrue($list_item
->isVisible());
// It should contain 1 input element (the checkbox). It should have the
// expected ID and CSS class.
$item_id = "llama-{$keyword}";
$this
->assertCount(1, $list_item
->findAll('css', 'input'));
$this
->assertCount(1, $list_item
->findAll('css', "input#{$item_id}[type='checkbox'].facets-checkbox"));
// It should contain a label for the checkbox.
$labels = $list_item
->findAll('css', "label[for={$item_id}]");
$this
->assertCount(1, $labels);
// The label should contain the search keyword and the result count. Since
// there can be multiple spaces or newlines between the keyword and the
// count, reduce them to a single space before asserting. The keyword and
// the count should be wrapped in elements with semantic classes.
$label = reset($labels);
$expected_text = "<span class=\"facet-item__value\">{$keyword}</span> <span class=\"facet-item__count\">({$count})</span>";
$this
->assertTrue($label
->isVisible());
$this
->assertEquals($expected_text, trim(preg_replace('/\\s+/', ' ', $label
->getHtml())));
// There should be a hidden link that leads to the updated search results.
// If a user checks a checkbox this hidden link is followed in JS.
$links = $list_item
->findAll('css', 'a');
$this
->assertCount(1, $links);
$link = reset($links);
// The link should not be visible.
$this
->assertFalse($link
->isVisible());
// The link should indicate that search engines shouldn't follow it.
$this
->assertEquals('nofollow', $link
->getAttribute('rel'));
// The link should have CSS classes that allow to attach our JS code.
$this
->assertEquals($item_id, $link
->getAttribute('data-drupal-facet-item-id'));
$this
->assertEquals($keyword, $link
->getAttribute('data-drupal-facet-item-value'));
// The link text should include the keyword as well as the count.
$this
->assertStringContainsString($expected_text, trim(preg_replace('/\\s+/', ' ', $link
->getHtml())));
}
}