View source
<?php
namespace Drupal\Tests\views_infinite_scroll\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\views\Entity\View;
class InfiniteScrollTest extends WebDriverTestBase {
use NodeCreationTrait;
use ContentTypeCreationTrait;
protected static $modules = [
'views',
'views_ui',
'views_infinite_scroll',
'node',
];
protected $defaultTheme = 'classy';
protected function setUp() {
parent::setUp();
$this
->createContentType([
'type' => 'page',
]);
for ($i = 1; $i <= 11; $i++) {
$this
->createNode([
'status' => TRUE,
'type' => 'page',
]);
}
}
public function testInfiniteScroll() {
$this
->createView('click-to-load', [
'button_text' => 'Load More',
'automatically_load_content' => FALSE,
]);
$this
->drupalGet('click-to-load');
$this
->assertTotalNodes(3);
$this
->getSession()
->getPage()
->clickLink('Load More');
$this
->assertSession()
->waitForElement('css', '.node--type-page:nth-child(4)');
$this
->assertTotalNodes(6);
$this
->createView('initially-load-all', [
'button_text' => 'Load More',
'automatically_load_content' => FALSE,
'initially_load_all_pages' => TRUE,
]);
$this
->drupalGet('initially-load-all', [
'query' => [
'page' => 1,
],
]);
$this
->assertTotalNodes(6);
$this
->createView('automatic-load', [
'button_text' => 'Load More',
'automatically_load_content' => TRUE,
]);
$this
->getSession()
->resizeWindow(1200, 200);
$this
->drupalGet('automatic-load');
$this
->assertTotalNodes(3);
$this
->scrollTo(500);
$this
->assertSession()
->waitForElement('css', '.node--type-page:nth-child(4)');
$this
->assertTotalNodes(6);
$this
->createView('automatic-load-0', [
'button_text' => 'Load More',
'automatically_load_content' => TRUE,
], 0);
$this
->drupalGet('automatic-load-0');
$this
->assertTotalNodes(11);
$this
->createView('next-page-count', [
'button_text' => 'Load @next_page_count more of @total',
'automatically_load_content' => FALSE,
], 6);
$this
->drupalGet('next-page-count');
$this
->getSession()
->getPage()
->clickLink('Load 5 more of 11');
$this
->assertSession()
->waitForElement('css', '.node--type-page:nth-child(7)');
$this
->assertTotalNodes(11);
$this
->createView('remaining-items-count', [
'button_text' => 'Load @next_page_count more of @remaining_items_count remaining',
'automatically_load_content' => FALSE,
]);
$this
->drupalGet('remaining-items-count');
$this
->getSession()
->getPage()
->clickLink('Load 3 more of 8 remaining');
$this
->assertSession()
->waitForElement('css', '.node--type-page:nth-child(7)');
$this
->assertTotalNodes(6);
}
protected function assertTotalNodes($total) {
$this
->assertEquals($total, count($this
->getSession()
->getPage()
->findAll('css', '.node--type-page')));
}
protected function scrollTo($pixels) {
$this
->getSession()
->getDriver()
->executeScript("window.scrollTo(null, {$pixels});");
}
protected function createView($path, array $settings, $items_per_page = 3) {
View::create([
'label' => 'VIS Test',
'id' => $this
->randomMachineName(),
'base_table' => 'node_field_data',
'display' => [
'default' => [
'display_plugin' => 'default',
'id' => 'default',
'display_options' => [
'row' => [
'type' => 'entity:node',
'options' => [
'view_mode' => 'teaser',
],
],
'pager' => [
'type' => 'infinite_scroll',
'options' => [
'items_per_page' => $items_per_page,
'offset' => 0,
'views_infinite_scroll' => $settings,
],
],
'use_ajax' => TRUE,
],
],
'page_1' => [
'display_plugin' => 'page',
'id' => 'page_1',
'display_options' => [
'path' => $path,
],
],
],
])
->save();
\Drupal::service('router.builder')
->rebuild();
}
}