A platform for community discussion. Free, open, simple.
DEV: extract emulateAutocomplete test helper (#21363)
Andrei Prigorshneve7faef9d6594
diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-editor-mentions-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-editor-mentions-test.jsindex 864915c9412..2f474d946e6 100644--- a/app/assets/javascripts/discourse/tests/acceptance/composer-editor-mentions-test.js+++ b/app/assets/javascripts/discourse/tests/acceptance/composer-editor-mentions-test.js@@ -2,6 +2,7 @@ import { test } from "qunit";import { click, fillIn, triggerKeyEvent, visit } from "@ember/test-helpers";import {acceptance,+ emulateAutocomplete,exists,fakeTime,loggedInUser,@@ -64,19 +65,7 @@ acceptance("Composer - editor mentions", function (needs) {await visit("/");await click("#create-topic");- // Emulate user pressing backspace in the editor- const editor = query(".d-editor-input");-- await triggerKeyEvent(".d-editor-input", "keydown", "@");- await fillIn(".d-editor-input", "abc @");- await setCaretPosition(editor, 5);- await triggerKeyEvent(".d-editor-input", "keyup", "@");-- await triggerKeyEvent(".d-editor-input", "keydown", "U");- await fillIn(".d-editor-input", "abc @u");- await setCaretPosition(editor, 6);- await triggerKeyEvent(".d-editor-input", "keyup", "U");-+ await emulateAutocomplete(".d-editor-input", "abc @u");await click(".autocomplete.ac-user .selected");assert.strictEqual(@@ -145,14 +134,7 @@ acceptance("Composer - editor mentions", function (needs) {await visit("/");await click("#create-topic");- // emulate typing in "abc @u"- const editor = query(".d-editor-input");- await fillIn(".d-editor-input", "@");- await setCaretPosition(editor, 5);- await triggerKeyEvent(".d-editor-input", "keyup", "@");- await fillIn(".d-editor-input", "@u");- await setCaretPosition(editor, 6);- await triggerKeyEvent(".d-editor-input", "keyup", "U");+ await emulateAutocomplete(".d-editor-input", "@u");assert.ok(exists(`.autocomplete .emoji[title='${status.emoji}']`),@@ -174,20 +156,14 @@ acceptance("Composer - editor mentions", function (needs) {await visit("/");await click("#create-topic");- await fillIn(".d-editor-input", "abc @");- await triggerKeyEvent(".d-editor-input", "keyup", "@");- await fillIn(".d-editor-input", "abc @u");- await triggerKeyEvent(".d-editor-input", "keyup", "U");+ await emulateAutocomplete(".d-editor-input", "abc @u");assert.deepEqual([...queryAll(".ac-user .username")].map((e) => e.innerText),["user", "user2", "user_group", "foo"]);- await fillIn(".d-editor-input", "abc @");- await triggerKeyEvent(".d-editor-input", "keyup", "@");- await fillIn(".d-editor-input", "abc @f");- await triggerKeyEvent(".d-editor-input", "keyup", "F");+ await emulateAutocomplete(".d-editor-input", "abc @f");assert.deepEqual([...queryAll(".ac-user .username")].map((e) => e.innerText),diff --git a/app/assets/javascripts/discourse/tests/acceptance/hashtag-autocomplete-test.js b/app/assets/javascripts/discourse/tests/acceptance/hashtag-autocomplete-test.jsindex 2849bd73a00..f6051259273 100644--- a/app/assets/javascripts/discourse/tests/acceptance/hashtag-autocomplete-test.js+++ b/app/assets/javascripts/discourse/tests/acceptance/hashtag-autocomplete-test.js@@ -1,28 +1,10 @@-import { setCaretPosition } from "discourse/lib/utilities";-import { acceptance, query } from "discourse/tests/helpers/qunit-helpers";import {- click,- fillIn,- settled,- triggerKeyEvent,- visit,-} from "@ember/test-helpers";+ acceptance,+ emulateAutocomplete,+} from "discourse/tests/helpers/qunit-helpers";+import { click, visit } from "@ember/test-helpers";import { test } from "qunit";-async function typeHashtagAutocomplete() {- const composerInput = query(".d-editor-input");- await fillIn(".d-editor-input", "abc #");- await triggerKeyEvent(".d-editor-input", "keydown", "#");- await fillIn(".d-editor-input", "abc #");- await setCaretPosition(composerInput, 5);- await triggerKeyEvent(".d-editor-input", "keyup", "#");- await triggerKeyEvent(".d-editor-input", "keydown", "O");- await fillIn(".d-editor-input", "abc #o");- await setCaretPosition(composerInput, 6);- await triggerKeyEvent(".d-editor-input", "keyup", "O");- await settled();-}-acceptance("#hashtag autocompletion in composer", function (needs) {needs.user();needs.settings({@@ -71,7 +53,9 @@ acceptance("#hashtag autocompletion in composer", function (needs) {test(":emoji: unescape in autocomplete search results", async function (assert) {await visit("/t/internationalization-localization/280");await click("#topic-footer-buttons .btn.create");- await typeHashtagAutocomplete();++ await emulateAutocomplete(".d-editor-input", "abc #o");+assert.dom(".hashtag-autocomplete__option").exists({ count: 3 });assert.dom(diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.jsindex cbf2a1afb51..3f3bd8b358f 100644--- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js+++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js@@ -11,7 +11,12 @@ import {mergeSettings,} from "discourse/tests/helpers/site-settings";import { forceMobile, resetMobile } from "discourse/lib/mobile";-import { getApplication, settled } from "@ember/test-helpers";+import {+ fillIn,+ getApplication,+ settled,+ triggerKeyEvent,+} from "@ember/test-helpers";import { getOwner } from "discourse-common/lib/get-owner";import { run } from "@ember/runloop";import { setupApplicationTest } from "ember-qunit";@@ -579,6 +584,16 @@ export async function paste(element, text, otherClipboardData = {}) {return e;}+export async function emulateAutocomplete(inputSelector, text) {+ await triggerKeyEvent(inputSelector, "keydown", "Backspace");+ await fillIn(inputSelector, `${text} `);+ await triggerKeyEvent(inputSelector, "keyup", "Backspace");++ await triggerKeyEvent(inputSelector, "keydown", "Backspace");+ await fillIn(inputSelector, text);+ await triggerKeyEvent(inputSelector, "keyup", "Backspace");+}+// The order of attributes can vary in different browsers. When comparing// HTML strings from the DOM, this function helps to normalize them to make// comparison work cross-browser