From 954b93777730d3c8fe12f1d2ca3f6fb9203e656b Mon Sep 17 00:00:00 2001
From: catch <catch56@gmail.com>
Date: Mon, 24 Oct 2022 12:59:34 +0100
Subject: [PATCH] Issue #3316816 by Wim Leers, nod_: Stabilize
 FunctionalJavascript testing AJAX: make ::setValue() trigger both "input" and
 "formUpdated" events

(cherry picked from commit 6a1bac3cc0293ca7cc8c46612df2ba8470937a5c)
---
 .../DrupalSelenium2Driver.php                  | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php b/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php
index ecbe96237742..b89b5a4b92cc 100644
--- a/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php
+++ b/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php
@@ -168,6 +168,24 @@ public function setValue($xpath, $value) {
     $not_clickable_exception = NULL;
     $result = $this->waitFor(10, function () use (&$not_clickable_exception, $xpath, $value) {
       try {
+        // \Behat\Mink\Driver\Selenium2Driver::setValue() will call .blur() on
+        // the element, modify that to trigger the "input" and "change" events
+        // instead. They indicate the value has changed, rather than implying
+        // user focus changes.
+        $this->executeJsOnXpath($xpath, <<<JS
+var node = {{ELEMENT}};
+var original = node.blur;
+node.blur = function() {
+  node.dispatchEvent(new Event("input", {bubbles:true}));
+  node.dispatchEvent(new Event("change", {bubbles:true}));
+  // Do not wait for the debounce, which only triggers the 'formUpdated` event
+  // up to once every 0.3 seconds. In tests, no humans are typing, hence there
+  // is no need to debounce.
+  // @see Drupal.behaviors.formUpdated
+  node.dispatchEvent(new Event("formUpdated", {bubbles:true}));
+  node.blur = original;
+};
+JS);
         parent::setValue($xpath, $value);
         return TRUE;
       }
-- 
GitLab