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/unit/lib/emoji-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

174 lines
6.3 KiB
JavaScript

import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
import { emojiSearch } from "pretty-text/emoji";
import { emojiUnescape } from "discourse/lib/text";
import { test } from "qunit";
import { IMAGE_VERSION as v } from "pretty-text/emoji/version";
discourseModule("Unit | Utility | emoji", function () {
test("emojiUnescape", function (assert) {
const testUnescape = (input, expected, description, settings = {}) => {
const originalSettings = {};
for (const [key, value] of Object.entries(settings)) {
originalSettings[key] = this.siteSettings[key];
this.siteSettings[key] = value;
}
assert.strictEqual(emojiUnescape(input), expected, description);
for (const [key, value] of Object.entries(originalSettings)) {
this.siteSettings[key] = value;
}
};
testUnescape(
"Not emoji :O) :frog) :smile)",
"Not emoji :O) :frog) :smile)",
"title without emoji"
);
testUnescape(
"Not emoji :frog :smile",
"Not emoji :frog :smile",
"end colon is not optional"
);
testUnescape(
"emoticons :)",
`emoticons <img width=\"20\" height=\"20\" src='/images/emoji/twitter/slight_smile.png?v=${v}' title='slight_smile' alt='slight_smile' class='emoji'>`,
"emoticons are still supported"
);
testUnescape(
"With emoji :O: :frog: :smile:",
`With emoji <img width=\"20\" height=\"20\" src='/images/emoji/twitter/o.png?v=${v}' title='O' alt='O' class='emoji'> <img width=\"20\" height=\"20\" src='/images/emoji/twitter/frog.png?v=${v}' title='frog' alt='frog' class='emoji'> <img width=\"20\" height=\"20\" src='/images/emoji/twitter/smile.png?v=${v}' title='smile' alt='smile' class='emoji'>`,
"title with emoji"
);
testUnescape(
"a:smile:a",
"a:smile:a",
"word characters not allowed next to emoji"
);
testUnescape(
"(:frog:) :)",
`(<img width=\"20\" height=\"20\" src='/images/emoji/twitter/frog.png?v=${v}' title='frog' alt='frog' class='emoji'>) <img width=\"20\" height=\"20\" src='/images/emoji/twitter/slight_smile.png?v=${v}' title='slight_smile' alt='slight_smile' class='emoji'>`,
"non-word characters allowed next to emoji"
);
testUnescape(
":smile: hi",
`<img width=\"20\" height=\"20\" src='/images/emoji/twitter/smile.png?v=${v}' title='smile' alt='smile' class='emoji'> hi`,
"start of line"
);
testUnescape(
"hi :smile:",
`hi <img width=\"20\" height=\"20\" src='/images/emoji/twitter/smile.png?v=${v}' title='smile' alt='smile' class='emoji'>`,
"end of line"
);
testUnescape(
"hi :blonde_woman:t4:",
`hi <img width=\"20\" height=\"20\" src='/images/emoji/twitter/blonde_woman/4.png?v=${v}' title='blonde_woman:t4' alt='blonde_woman:t4' class='emoji'>`,
"support for skin tones"
);
testUnescape(
"hi :blonde_woman:t4: :blonde_man:t6:",
`hi <img width=\"20\" height=\"20\" src='/images/emoji/twitter/blonde_woman/4.png?v=${v}' title='blonde_woman:t4' alt='blonde_woman:t4' class='emoji'> <img width=\"20\" height=\"20\" src='/images/emoji/twitter/blonde_man/6.png?v=${v}' title='blonde_man:t6' alt='blonde_man:t6' class='emoji'>`,
"support for multiple skin tones"
);
testUnescape(
"hi :blonde_man:t6",
"hi :blonde_man:t6",
"end colon not optional for skin tones"
);
testUnescape(
"emoticons :)",
"emoticons :)",
"no emoticons when emojis are disabled",
{ enable_emoji: false }
);
testUnescape(
"emoji :smile:",
"emoji :smile:",
"no emojis when emojis are disabled",
{ enable_emoji: false }
);
testUnescape(
"emoticons :)",
"emoticons :)",
"no emoticons when emoji shortcuts are disabled",
{ enable_emoji_shortcuts: false }
);
testUnescape(
"Hello 😊 World",
`Hello <img width=\"20\" height=\"20\" src='/images/emoji/twitter/blush.png?v=${v}' title='blush' alt='blush' class='emoji'> World`,
"emoji from Unicode emoji"
);
testUnescape(
"Hello😊World",
"Hello😊World",
"keeps Unicode emoji when inline translation disabled",
{
enable_inline_emoji_translation: false,
}
);
testUnescape(
"Hello😊World",
`Hello<img width=\"20\" height=\"20\" src='/images/emoji/twitter/blush.png?v=${v}' title='blush' alt='blush' class='emoji'>World`,
"emoji from Unicode emoji when inline translation enabled",
{
enable_inline_emoji_translation: true,
}
);
testUnescape(
"hi:smile:",
"hi:smile:",
"no emojis when inline translation disabled",
{
enable_inline_emoji_translation: false,
}
);
testUnescape(
"hi:smile:",
`hi<img width=\"20\" height=\"20\" src='/images/emoji/twitter/smile.png?v=${v}' title='smile' alt='smile' class='emoji'>`,
"emoji when inline translation enabled",
{ enable_inline_emoji_translation: true }
);
assert.strictEqual(
emojiUnescape(":smile:", { tabIndex: "0" }),
`<img width=\"20\" height=\"20\" src='/images/emoji/twitter/smile.png?v=${v}' title='smile' alt='smile' class='emoji' tabindex='0'>`,
"emoji when tabindex is enabled"
);
});
test("Emoji search", function (assert) {
// able to find an alias
assert.strictEqual(emojiSearch("+1").length, 1);
// able to find middle of line search
assert.strictEqual(emojiSearch("check", { maxResults: 3 }).length, 3);
// appends diversity
assert.deepEqual(emojiSearch("woman_artist", { diversity: 5 }), [
"woman_artist:t5",
]);
assert.deepEqual(emojiSearch("woman_artist", { diversity: 2 }), [
"woman_artist:t2",
]);
// no diversity appended for emojis that can't be diversified
assert.deepEqual(emojiSearch("green_apple", { diversity: 3 }), [
"green_apple",
]);
});
test("search does not return duplicated results", function (assert) {
const matches = emojiSearch("bow").filter(
(emoji) => emoji === "bowing_man"
);
assert.deepEqual(matches, ["bowing_man"]);
});
test("search does partial-match on emoji aliases", function (assert) {
const matches = emojiSearch("instru");
assert.ok(matches.includes("woman_teacher"));
assert.ok(matches.includes("violin"));
});
});