summaryrefslogtreecommitdiffstatshomepage
path: root/core/tests/Drupal/FunctionalJavascriptTests/AjaxWaitTest.php
blob: 0a61c5f03cbe20ac866ee8622e13ee7d209793e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

declare(strict_types=1);

namespace Drupal\FunctionalJavascriptTests;

/**
 * Tests that unnecessary or untracked XHRs will cause a test failure.
 *
 * @group javascript
 * @group legacy
 */
class AjaxWaitTest extends WebDriverTestBase {

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'starterkit_theme';

  /**
   * Tests that an unnecessary wait triggers an error.
   */
  public function testUnnecessaryWait(): void {
    $this->drupalGet('user');

    $this->expectException(\RuntimeException::class);
    $this->expectExceptionMessage('There are no AJAX requests to wait for.');

    $this->assertSession()->assertWaitOnAjaxRequest(500);
  }

  /**
   * Tests that an untracked XHR triggers an error.
   */
  public function testUntrackedXhr(): void {
    $this->getSession()->executeScript(<<<JS
let xhr = new XMLHttpRequest();
xhr.open('GET', '/foobar');
xhr.send();
JS);
    $this->expectException(\RuntimeException::class);
    $this->expectExceptionMessage('0 XHR requests through jQuery, but 1 observed in the browser — this requires js_testing_ajax_request_test.js to be updated.');

    $this->assertSession()->assertExpectedAjaxRequest(1, 500);
  }

}