You are here

public function InfiniteScrollTest::testInfiniteScroll in Views Infinite Scroll 8

Test infinite scrolling under different conditions.

File

tests/src/FunctionalJavascript/InfiniteScrollTest.php, line 55

Class

InfiniteScrollTest
Test views infinite scroll.

Namespace

Drupal\Tests\views_infinite_scroll\FunctionalJavascript

Code

public function testInfiniteScroll() {

  // Test manually clicking a view.
  $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);

  // Test loading a page past the first.
  $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);

  // Test the view automatically loading.
  $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);

  // Test the view automatically loading with 0 items per page. This will
  // result in there being no pager, which is a case that our preprocess
  // function needs to handle.
  // @see views_infinite_scroll_preprocess_views_infinite_scroll_pager()
  $this
    ->createView('automatic-load-0', [
    'button_text' => 'Load More',
    'automatically_load_content' => TRUE,
  ], 0);
  $this
    ->drupalGet('automatic-load-0');
  $this
    ->assertTotalNodes(11);

  // Test @next_page_count and @total token.
  $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);

  // Test @remaining_items_count token.
  $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);
}