function DialogTest::testCloseCancelsScheduledResize

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/FunctionalJavascriptTests/Ajax/DialogTest.php \Drupal\FunctionalJavascriptTests\Ajax\DialogTest::testCloseCancelsScheduledResize()

Tests that closing a dialog cancels a resize that is already scheduled.

Dialog resizing is debounced by 20ms. Closing a dialog removes its element from the DOM, which destroys the jQuery UI instance, so a resize that runs after the dialog closed would act on a destroyed dialog and throw.

See also

https://www.drupal.org/node/3472624

File

core/tests/Drupal/FunctionalJavascriptTests/Ajax/DialogTest.php, line 275

Class

DialogTest
Performs tests on opening and manipulating dialogs via AJAX commands.

Namespace

Drupal\FunctionalJavascriptTests\Ajax

Code

public function testCloseCancelsScheduledResize() : void {
  $this->drupalGet('ajax-test/dialog');
  $this->getSession()
    ->getPage()
    ->clickLink('Link 1 (modal)');
  $this->assertNotNull($this->assertSession()
    ->waitForElementVisible('css', 'div.ui-dialog'));
  // Schedule a resize, then close the dialog in the same tick so that the
  // resize is always still pending when the dialog goes away. Flag the end
  // of a period comfortably longer than the 20ms debounce, so that the test
  // does not assert before a resize would have run.
  $script = <<<SCRIPT
  (function () {
    window.dialogResizeElapsed = false;
    jQuery(document).trigger('drupalViewportOffsetChange', Drupal.displace.offsets);
    document.querySelector('.ui-dialog button[title="Close"]').click();
    window.setTimeout(function () {
      window.dialogResizeElapsed = true;
    }, 200);
  }())
  SCRIPT;
  $this->getSession()
    ->executeScript($script);
  $this->assertJsCondition('window.dialogResizeElapsed === true');
  $errors = $this->getSession()
    ->evaluateScript("JSON.parse(sessionStorage.getItem('js_testing_log_test.errors') || JSON.stringify([]))");
  $this->assertSame([], $errors);
}

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