DEV: extract emulateAutocomplete test helper (#21363)

This commit is contained in:
Andrei Prigorshnev 2023-05-04 18:09:05 +04:00 committed by GitHub
parent d6534bdb11
commit e7faef9d65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 53 deletions

View File

@ -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),

View File

@ -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(

View File

@ -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