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

This commit is contained in:
David Taylor 2021-10-08 18:00:08 +01:00
parent 9a3a5b19e9
commit 821f14d8e0
3 changed files with 113 additions and 5 deletions

View File

@ -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");
});
});

View File

@ -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"
);
});
});

View File

@ -495,9 +495,32 @@ export async function selectText(selector, endOffset = null) {
range.setEnd(node, endOffset);
}
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();
}