This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/acceptance/composer-editor-mentions-test.js
Martin Brennan ac7bf98ad1
DEV: Load client site settings YML into JS tests (#18413)
Our method of loading a subset of client settings into tests via
tests/helpers/site-settings.js can be improved upon. Currently we have a
hardcoded subset of the client settings, which may get out of date and not have
the correct defaults. As well as this plugins do not get their settings into the
tests, so whenever you need a setting from a plugin, even if it has a default,
you have to do needs.setting({ ... }) which is inconvenient.

This commit introduces an ember CLI build step to take the site_settings.yml and
all the plugin settings.yml files, pull out the client settings, and dump them
into a variable in a single JS file we can load in our tests, so we have the
correct selection of settings and default values in our JS tests. It also fixes
many, many tests that were operating under incorrect assumptions or old
settings.

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
2022-11-08 09:17:43 +10:00

161 lines
4.9 KiB
JavaScript

import { test } from "qunit";
import { click, fillIn, triggerKeyEvent, visit } from "@ember/test-helpers";
import {
acceptance,
exists,
fakeTime,
loggedInUser,
query,
} from "discourse/tests/helpers/qunit-helpers";
import { setCaretPosition } from "discourse/lib/utilities";
acceptance("Composer - editor mentions", function (needs) {
let clock = null;
const status = {
emoji: "tooth",
description: "off to dentist",
ends_at: "2100-02-01T09:00:00.000Z",
};
needs.user();
needs.settings({ enable_mentions: true, allow_uncategorized_topics: true });
needs.hooks.afterEach(() => {
if (clock) {
clock.restore();
}
});
needs.pretender((server, helper) => {
server.get("/u/search/users", () => {
return helper.response({
users: [
{
username: "user",
name: "Some User",
avatar_template:
"https://avatars.discourse.org/v3/letter/t/41988e/{size}.png",
status,
},
{
username: "user2",
name: "Some User",
avatar_template:
"https://avatars.discourse.org/v3/letter/t/41988e/{size}.png",
},
],
});
});
});
test("selecting user mentions", async function (assert) {
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 click(".autocomplete.ac-user .selected");
assert.strictEqual(
query(".d-editor-input").value,
"abc @user ",
"should replace mention correctly"
);
});
test("selecting user mentions after deleting characters", async function (assert) {
await visit("/");
await click("#create-topic");
await fillIn(".d-editor-input", "abc @user a");
// Emulate user typing `@` and `u` in the editor
await triggerKeyEvent(".d-editor-input", "keydown", "Backspace");
await fillIn(".d-editor-input", "abc @user ");
await triggerKeyEvent(".d-editor-input", "keyup", "Backspace");
await triggerKeyEvent(".d-editor-input", "keydown", "Backspace");
await fillIn(".d-editor-input", "abc @user");
await triggerKeyEvent(".d-editor-input", "keyup", "Backspace");
await click(".autocomplete.ac-user .selected");
assert.strictEqual(
query(".d-editor-input").value,
"abc @user ",
"should replace mention correctly"
);
});
test("selecting user mentions after deleting characters mid sentence", async function (assert) {
await visit("/");
await click("#create-topic");
// Emulate user pressing backspace in the editor
const editor = query(".d-editor-input");
await fillIn(".d-editor-input", "abc @user 123");
await setCaretPosition(editor, 9);
await triggerKeyEvent(".d-editor-input", "keydown", "Backspace");
await fillIn(".d-editor-input", "abc @use 123");
await triggerKeyEvent(".d-editor-input", "keyup", "Backspace");
await setCaretPosition(editor, 8);
await triggerKeyEvent(".d-editor-input", "keydown", "Backspace");
await fillIn(".d-editor-input", "abc @us 123");
await triggerKeyEvent(".d-editor-input", "keyup", "Backspace");
await setCaretPosition(editor, 7);
await click(".autocomplete.ac-user .selected");
assert.strictEqual(
query(".d-editor-input").value,
"abc @user 123",
"should replace mention correctly"
);
});
test("shows status on search results when mentioning a user", async function (assert) {
const timezone = loggedInUser().timezone;
const now = moment(status.ends_at).add(-1, "hour").format();
clock = fakeTime(now, timezone, true);
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");
assert.ok(
exists(`.autocomplete .emoji[title='${status.emoji}']`),
"status emoji is shown"
);
assert.equal(
query(".autocomplete .status-description").textContent.trim(),
status.description,
"status description is shown"
);
assert.equal(
query(".autocomplete .relative-date").textContent.trim(),
"1h",
"status expiration time is shown"
);
});
});