function AdminUiTest::assertNoAjaxRequestTriggered

Same name and namespace in other branches
  1. 11.x core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php \Drupal\Tests\ckeditor5\FunctionalJavascript\AdminUiTest::assertNoAjaxRequestTriggered()

Asserts that no (new) AJAX requests were triggered.

Parameters

int $expected_cumulative_ajax_request_count: The number of expected observed XHR requests since the page was loaded.

1 call to AdminUiTest::assertNoAjaxRequestTriggered()
AdminUiTest::testSettingsOnlyFireAjaxWithCkeditor5 in core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php
Confirm settings only trigger AJAX when select value is CKEditor 5.

File

core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php, line 92

Class

AdminUiTest
Tests for CKEditor 5 in the admin UI.

Namespace

Drupal\Tests\ckeditor5\FunctionalJavascript

Code

protected function assertNoAjaxRequestTriggered(int $expected_cumulative_ajax_request_count = 0) : void {
  // In case of no requests triggered at all yet.
  if ($expected_cumulative_ajax_request_count === 0) {
    $result = $this->getSession()
      ->evaluateScript(<<<JS
      (function() {
        return window.drupalCumulativeXhrCount;
      }())
JS
);
    $this->assertSame(0, $result);
  }
  else {
    // In case of the non-first AJAX request, ensure that no AJAX requests are
    // in progress.
    try {
      $this->assertSession()
        ->assertWaitOnAjaxRequest(500);
    } catch (\RuntimeException $e) {
      throw new \LogicException(sprintf('This call to %s claims there no AJAX request was triggered, but this is wrong: %s.', __METHOD__, $e->getMessage()));
    } catch (\LogicException $e) {
      // This is the intent: ::assertWaitOnAjaxRequest() should detect an
      // "incorrect" call, because this assertion is asserting *no* AJAX
      // requests have been triggered.
      assert(str_contains($e->getMessage(), 'Unnecessary'));
      $result = $this->getSession()
        ->evaluateScript(<<<JS
        (function() {
          return window.drupalCumulativeXhrCount;
        }())
JS
);
      $this->assertSame($expected_cumulative_ajax_request_count, $result);
    }
  }
  // Now that there definitely is no more AJAX request in progress, count the
  // number of actual XHR requests, ensure they match.
  $javascript = <<<JS
(function(){
  return window.performance
    .getEntries()
    .filter(entry => entry.initiatorType === 'xmlhttprequest')
    .length
})()
JS;
  $this->assertSame($expected_cumulative_ajax_request_count, $this->getSession()
    ->evaluateScript($javascript));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.