2022-04-14 20:31:50 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-04-14 20:31:50 +00:00
|
|
|
Copyright 2022 Ryan Browne <code@commonlawfeature.com>
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-04-14 20:31:50 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import EmojiProvider from "../../src/autocomplete/EmojiProvider";
|
|
|
|
import { mkStubRoom } from "../test-utils/test-utils";
|
2022-07-21 13:27:11 +00:00
|
|
|
import { add } from "../../src/emojipicker/recent";
|
|
|
|
import { stubClient } from "../test-utils";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
|
2022-04-14 20:31:50 +00:00
|
|
|
|
|
|
|
const EMOJI_SHORTCODES = [
|
|
|
|
":+1",
|
|
|
|
":heart",
|
|
|
|
":grinning",
|
|
|
|
":hand",
|
|
|
|
":man",
|
|
|
|
":sweat",
|
|
|
|
":monkey",
|
|
|
|
":boat",
|
|
|
|
":mailbox",
|
|
|
|
":cop",
|
|
|
|
":bow",
|
|
|
|
":kiss",
|
|
|
|
":golf",
|
|
|
|
];
|
|
|
|
|
|
|
|
// Some emoji shortcodes are too short and do not actually trigger autocompletion until the ending `:`.
|
|
|
|
// This means that we cannot compare their autocompletion before and after the ending `:` and have
|
|
|
|
// to simply assert that the final completion with the colon is the exact emoji.
|
2022-12-12 11:24:14 +00:00
|
|
|
const TOO_SHORT_EMOJI_SHORTCODE = [{ emojiShortcode: ":o", expectedEmoji: "⭕️" }];
|
2022-04-14 20:31:50 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("EmojiProvider", function () {
|
2022-04-14 20:31:50 +00:00
|
|
|
const testRoom = mkStubRoom(undefined, undefined, undefined);
|
2022-07-21 13:27:11 +00:00
|
|
|
stubClient();
|
2023-06-05 17:12:23 +00:00
|
|
|
MatrixClientPeg.safeGet();
|
2022-04-14 20:31:50 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it.each(EMOJI_SHORTCODES)("Returns consistent results after final colon %s", async function (emojiShortcode) {
|
2022-04-14 20:31:50 +00:00
|
|
|
const ep = new EmojiProvider(testRoom);
|
2022-12-12 11:24:14 +00:00
|
|
|
const range = { beginning: true, start: 0, end: 3 };
|
2022-04-14 20:31:50 +00:00
|
|
|
const completionsBeforeColon = await ep.getCompletions(emojiShortcode, range);
|
2022-12-12 11:24:14 +00:00
|
|
|
const completionsAfterColon = await ep.getCompletions(emojiShortcode + ":", range);
|
2022-04-14 20:31:50 +00:00
|
|
|
|
|
|
|
const firstCompletionWithoutColon = completionsBeforeColon[0].completion;
|
|
|
|
const firstCompletionWithColon = completionsAfterColon[0].completion;
|
|
|
|
|
|
|
|
expect(firstCompletionWithoutColon).toEqual(firstCompletionWithColon);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it.each(TOO_SHORT_EMOJI_SHORTCODE)(
|
|
|
|
"Returns correct results after final colon $emojiShortcode",
|
|
|
|
async ({ emojiShortcode, expectedEmoji }) => {
|
|
|
|
const ep = new EmojiProvider(testRoom);
|
|
|
|
const range = { beginning: true, start: 0, end: 3 };
|
|
|
|
const completions = await ep.getCompletions(emojiShortcode + ":", range);
|
2022-04-14 20:31:50 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(completions[0].completion).toEqual(expectedEmoji);
|
|
|
|
},
|
|
|
|
);
|
2022-07-21 13:27:11 +00:00
|
|
|
|
2023-02-27 17:09:15 +00:00
|
|
|
it("Recently used emojis are correctly sorted", async function () {
|
2022-07-21 13:27:11 +00:00
|
|
|
add("😘"); //kissing_heart
|
2023-02-27 17:09:15 +00:00
|
|
|
add("💗"); //heartpulse
|
|
|
|
add("💗"); //heartpulse
|
|
|
|
add("😍"); //heart_eyes
|
2022-07-21 13:27:11 +00:00
|
|
|
|
2023-02-27 17:09:15 +00:00
|
|
|
const ep = new EmojiProvider(testRoom);
|
|
|
|
const completionsList = await ep.getCompletions(":heart", { beginning: true, start: 0, end: 6 });
|
|
|
|
expect(completionsList[0]?.component?.props.title).toEqual(":heartpulse:");
|
|
|
|
expect(completionsList[1]?.component?.props.title).toEqual(":heart_eyes:");
|
|
|
|
});
|
2022-07-21 13:27:11 +00:00
|
|
|
|
2023-02-27 17:09:15 +00:00
|
|
|
it("Exact match in recently used takes the lead", async function () {
|
|
|
|
add("😘"); //kissing_heart
|
|
|
|
add("💗"); //heartpulse
|
|
|
|
add("💗"); //heartpulse
|
|
|
|
add("😍"); //heart_eyes
|
|
|
|
|
|
|
|
add("❤️"); //heart
|
|
|
|
const ep = new EmojiProvider(testRoom);
|
|
|
|
const completionsList = await ep.getCompletions(":heart", { beginning: true, start: 0, end: 6 });
|
2022-07-21 13:27:11 +00:00
|
|
|
|
2023-02-27 17:09:15 +00:00
|
|
|
expect(completionsList[0]?.component?.props.title).toEqual(":heart:");
|
|
|
|
expect(completionsList[1]?.component?.props.title).toEqual(":heartpulse:");
|
|
|
|
expect(completionsList[2]?.component?.props.title).toEqual(":heart_eyes:");
|
2022-07-21 13:27:11 +00:00
|
|
|
});
|
2022-04-14 20:31:50 +00:00
|
|
|
});
|