DEV: Add basic acceptance tests for quote shortcut and fast-edit

DEV: Add basic acceptance tests for quote shortcut and fast-edit

diff --git a/app/assets/javascripts/discourse/tests/acceptance/fast-edit-test.js b/app/assets/javascripts/discourse/tests/acceptance/fast-edit-test.js
new file mode 100644
index 0000000..65caf80
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/acceptance/fast-edit-test.js
@@ -0,0 +1,69 @@
+import {
+  acceptance,
+  exists,
+  queryAll,
+  selectText,
+} from "discourse/tests/helpers/qunit-helpers";
+import { fillIn, triggerKeyEvent, visit } from "@ember/test-helpers";
+import { test } from "qunit";
+
+acceptance("Fast Edit", function (needs) {
+  needs.user();
+  needs.settings({
+    enable_fast_edit: true,
+  });
+
+  test("Fast edit button works", async function (assert) {
+    await visit("/t/internationalization-localization/280");
+
+    const textNode = document.querySelector("#post_1 .cooked p").childNodes[0];
+
+    await selectText(textNode, 9);
+    await click(".quote-button .quote-edit-label");
+
+    assert.ok(exists("#fast-edit-input"), "fast editor is open");
+    assert.equal(
+      queryAll("#fast-edit-input").val(),
+      "Any plans",
+      "contains selected text"
+    );
+
+    await fillIn("#fast-edit-input", "My edit");
+    await click(".save-fast-edit");
+
+    assert.notOk(exists("#fast-edit-input"), "fast editor is closed");
+  });
+
+  test("Works with keyboard shortcut", async function (assert) {
+    await visit("/t/internationalization-localization/280");
+
+    const textNode = document.querySelector("#post_1 .cooked p").childNodes[0];
+
+    await selectText(textNode, 9);
+    await triggerKeyEvent(document, "keypress", "e".charCodeAt(0));
+
+    assert.ok(exists("#fast-edit-input"), "fast editor is open");
+    assert.equal(
+      queryAll("#fast-edit-input").val(),
+      "Any plans",
+      "contains selected text"
+    );
+
+    await fillIn("#fast-edit-input", "My edit");
+    await click(".save-fast-edit");
+
+    assert.notOk(exists("#fast-edit-input"), "fast editor is closed");
+  });
+
+  test("Opens full composer for multi-line selection", async function (assert) {
+    await visit("/t/internationalization-localization/280");
+
+    const textNode = document.querySelector("#post_1 .cooked");
+
+    await selectText(textNode);
+    await click(".quote-button .quote-edit-label");
+
+    assert.notOk(exists("#fast-edit-input"), "fast editor is not open");
+    assert.ok(exists(".d-editor-input"), "the composer is open");
+  });
+});
diff --git a/app/assets/javascripts/discourse/tests/acceptance/topic-quote-button-test.js b/app/assets/javascripts/discourse/tests/acceptance/topic-quote-button-test.js
index d553538..121dc09 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/topic-quote-button-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/topic-quote-button-test.js
@@ -6,8 +6,8 @@ import {
   selectText,
 } from "discourse/tests/helpers/qunit-helpers";
 import I18n from "I18n";
-import { settled, visit } from "@ember/test-helpers";
-
+import { triggerKeyEvent, visit } from "@ember/test-helpers";
+import { test } from "qunit";
 
 // This tests are flaky on Firefox. Fails with `calling set on destroyed object`
 acceptance("Topic - Quote button - logged in", function (needs) {
@@ -124,3 +124,19 @@ acceptance("Topic - Quote button - anonymous", function (needs) {
     }
   );
 });
+
+acceptance("Topic - Quote button - keyboard shortcut", function (needs) {
+  needs.user();
+
+  test("Can use keyboard shortcut to quote selected text", async function (assert) {
+    await visit("/t/internationalization-localization/280");
+    await selectText("#post_1 .cooked");
+    await triggerKeyEvent(document, "keypress", "q".charCodeAt(0));
+    assert.ok(exists(".d-editor-input"), "the editor is open");
+
+    assert.ok(
+      queryAll(".d-editor-input").val().includes("Any plans to support"),
+      "editor includes selected text"
+    );
+  });
+});
diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js
index 76a0889..070fbd3 100644
--- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js
+++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js
@@ -495,9 +495,32 @@ export async function selectText(selector, endOffset = null) {
     range.setEnd(node, endOffset);
   }
 
-  const selection = window.getSelection();
-  selection.removeAllRanges();
-  selection.addRange(range);
+  const performSelection = () => {
+    const selection = window.getSelection();
+    selection.removeAllRanges();
+    selection.addRange(range);
+  };
+
+  if (LEGACY_ENV) {
+    // In the Ember CLI environment, the settled() helper seems to take care of waiting
+    // for this event to fire. In legacy, we need to do it manually.
+    let callback;
+    const selectEventFiredPromise = new Promise((resolve) => {
+      callback = resolve;
+      document.addEventListener("selectionchange", callback);
+    });
+
+    performSelection();
+
+    try {
+      await selectEventFiredPromise;
+    } finally {
+      document.removeEventListener("selectionchange", callback);
+    }
+  } else {
+    performSelection();
+  }
+
   await settled();
 }
 

GitHub sha: 821f14d8e0e690d32ee409059d887e74dbe2da54

This commit appears in #14563 which was approved by eviltrout. It was merged by davidtaylorhq.