2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-10-21 16:53:32 +00:00
|
|
|
import { assert } from 'chai';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
|
|
|
|
import { EmojiCompletion } from '../../../quill/emoji/completion';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { EmojiData } from '../../../components/emoji/lib';
|
2020-10-21 16:53:32 +00:00
|
|
|
|
|
|
|
describe('emojiCompletion', () => {
|
|
|
|
let emojiCompletion: EmojiCompletion;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
let mockQuill: any;
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(function (this: Mocha.Context) {
|
2020-10-21 16:53:32 +00:00
|
|
|
mockQuill = {
|
|
|
|
getLeaf: sinon.stub(),
|
|
|
|
getSelection: sinon.stub(),
|
|
|
|
keyboard: {
|
|
|
|
addBinding: sinon.stub(),
|
|
|
|
},
|
|
|
|
on: sinon.stub(),
|
|
|
|
setSelection: sinon.stub(),
|
|
|
|
updateContents: sinon.stub(),
|
|
|
|
};
|
|
|
|
const options = {
|
2021-01-04 18:28:32 +00:00
|
|
|
onPickEmoji: sinon.stub(),
|
|
|
|
setEmojiPickerElement: sinon.stub(),
|
2020-10-21 16:53:32 +00:00
|
|
|
skinTone: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
emojiCompletion = new EmojiCompletion(mockQuill as any, options);
|
|
|
|
|
|
|
|
// Stub rendering to avoid missing DOM until we bring in Enzyme
|
|
|
|
emojiCompletion.render = sinon.stub();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getCurrentLeafTextPartitions', () => {
|
|
|
|
it('returns left and right text', () => {
|
|
|
|
mockQuill.getSelection.returns({ index: 0, length: 0 });
|
|
|
|
const blot = {
|
|
|
|
text: ':smile:',
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, 2]);
|
2021-11-11 22:43:05 +00:00
|
|
|
const [leftLeafText, rightLeafText] =
|
|
|
|
emojiCompletion.getCurrentLeafTextPartitions();
|
2020-10-21 16:53:32 +00:00
|
|
|
assert.equal(leftLeafText, ':s');
|
|
|
|
assert.equal(rightLeafText, 'mile:');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('onTextChange', () => {
|
|
|
|
let insertEmojiStub: sinon.SinonStub<
|
|
|
|
[EmojiData, number, number, (boolean | undefined)?],
|
|
|
|
void
|
|
|
|
>;
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
emojiCompletion.results = [{ short_name: 'joy' } as any];
|
|
|
|
emojiCompletion.index = 5;
|
|
|
|
insertEmojiStub = sinon
|
|
|
|
.stub(emojiCompletion, 'insertEmoji')
|
|
|
|
.callThrough();
|
|
|
|
});
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
afterEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
insertEmojiStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('given an emoji is not starting (no colon)', () => {
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
mockQuill.getSelection.returns({
|
|
|
|
index: 3,
|
|
|
|
length: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
const blot = {
|
|
|
|
text: 'smi',
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, 3]);
|
|
|
|
|
|
|
|
emojiCompletion.onTextChange();
|
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('does not show results', () => {
|
2020-10-21 16:53:32 +00:00
|
|
|
assert.equal(emojiCompletion.results.length, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-27 00:13:49 +00:00
|
|
|
describe('given a colon in a string (but not an emoji)', () => {
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-27 00:13:49 +00:00
|
|
|
mockQuill.getSelection.returns({
|
|
|
|
index: 5,
|
|
|
|
length: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
const blot = {
|
|
|
|
text: '10:30',
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, 5]);
|
|
|
|
|
|
|
|
emojiCompletion.onTextChange();
|
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('does not show results', () => {
|
2020-10-27 00:13:49 +00:00
|
|
|
assert.equal(emojiCompletion.results.length, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-21 16:53:32 +00:00
|
|
|
describe('given an emoji is starting but does not have 2 characters', () => {
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
mockQuill.getSelection.returns({
|
|
|
|
index: 2,
|
|
|
|
length: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
const blot = {
|
|
|
|
text: ':s',
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, 2]);
|
|
|
|
|
|
|
|
emojiCompletion.onTextChange();
|
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('does not show results', () => {
|
2020-10-21 16:53:32 +00:00
|
|
|
assert.equal(emojiCompletion.results.length, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('given an emoji is starting but does not match a short name', () => {
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
mockQuill.getSelection.returns({
|
|
|
|
index: 4,
|
|
|
|
length: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
const blot = {
|
|
|
|
text: ':smy',
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, 4]);
|
|
|
|
|
|
|
|
emojiCompletion.onTextChange();
|
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('does not show results', () => {
|
2020-10-21 16:53:32 +00:00
|
|
|
assert.equal(emojiCompletion.results.length, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('given an emoji is starting and matches short names', () => {
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
mockQuill.getSelection.returns({
|
|
|
|
index: 4,
|
|
|
|
length: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
const blot = {
|
|
|
|
text: ':smi',
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, 4]);
|
|
|
|
|
|
|
|
emojiCompletion.onTextChange();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('stores the results and renders', () => {
|
|
|
|
assert.equal(emojiCompletion.results.length, 10);
|
|
|
|
assert.equal((emojiCompletion.render as sinon.SinonStub).called, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('given an emoji was just completed', () => {
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
mockQuill.getSelection.returns({
|
|
|
|
index: 7,
|
|
|
|
length: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('and given it matches a short name', () => {
|
|
|
|
const text = ':smile:';
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
const blot = {
|
|
|
|
text,
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, 7]);
|
|
|
|
|
|
|
|
emojiCompletion.onTextChange();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inserts the emoji at the current cursor position', () => {
|
|
|
|
const [emoji, index, range] = insertEmojiStub.args[0];
|
|
|
|
|
|
|
|
assert.equal(emoji.short_name, 'smile');
|
|
|
|
assert.equal(index, 0);
|
|
|
|
assert.equal(range, 7);
|
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('does not show results', () => {
|
2020-10-21 16:53:32 +00:00
|
|
|
assert.equal(emojiCompletion.results.length, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-27 00:13:49 +00:00
|
|
|
describe('and given it matches a short name inside a larger string', () => {
|
|
|
|
const text = 'have a :smile: nice day';
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-27 00:13:49 +00:00
|
|
|
const blot = {
|
|
|
|
text,
|
|
|
|
};
|
|
|
|
mockQuill.getSelection.returns({
|
|
|
|
index: 13,
|
|
|
|
length: 0,
|
|
|
|
});
|
|
|
|
mockQuill.getLeaf.returns([blot, 13]);
|
|
|
|
|
|
|
|
emojiCompletion.onTextChange();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inserts the emoji at the current cursor position', () => {
|
|
|
|
const [emoji, index, range] = insertEmojiStub.args[0];
|
|
|
|
|
|
|
|
assert.equal(emoji.short_name, 'smile');
|
|
|
|
assert.equal(index, 7);
|
|
|
|
assert.equal(range, 7);
|
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('does not show results', () => {
|
2020-10-27 00:13:49 +00:00
|
|
|
assert.equal(emojiCompletion.results.length, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets the quill selection to the right cursor position', () => {
|
|
|
|
const [index, range] = mockQuill.setSelection.args[0];
|
|
|
|
|
|
|
|
assert.equal(index, 8);
|
|
|
|
assert.equal(range, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-21 16:53:32 +00:00
|
|
|
describe('and given it does not match a short name', () => {
|
|
|
|
const text = ':smyle:';
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
const blot = {
|
|
|
|
text,
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, 7]);
|
|
|
|
|
|
|
|
emojiCompletion.onTextChange();
|
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('does not show results', () => {
|
2020-10-21 16:53:32 +00:00
|
|
|
assert.equal(emojiCompletion.results.length, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('given an emoji was just completed from inside the colons', () => {
|
|
|
|
const validEmoji = ':smile:';
|
|
|
|
const invalidEmoji = ':smyle:';
|
|
|
|
const middleCursorIndex = validEmoji.length - 3;
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
mockQuill.getSelection.returns({
|
|
|
|
index: middleCursorIndex,
|
|
|
|
length: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('and given it matches a short name', () => {
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
const blot = {
|
|
|
|
text: validEmoji,
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, middleCursorIndex]);
|
|
|
|
|
|
|
|
emojiCompletion.onTextChange();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inserts the emoji at the current cursor position', () => {
|
|
|
|
const [emoji, index, range] = insertEmojiStub.args[0];
|
|
|
|
|
|
|
|
assert.equal(emoji.short_name, 'smile');
|
|
|
|
assert.equal(index, 0);
|
|
|
|
assert.equal(range, validEmoji.length);
|
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('does not show results', () => {
|
2020-10-21 16:53:32 +00:00
|
|
|
assert.equal(emojiCompletion.results.length, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('and given it does not match a short name', () => {
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
const blot = {
|
|
|
|
text: invalidEmoji,
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, middleCursorIndex]);
|
|
|
|
|
|
|
|
emojiCompletion.onTextChange();
|
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('does not show results', () => {
|
2020-10-21 16:53:32 +00:00
|
|
|
assert.equal(emojiCompletion.results.length, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-11-11 22:54:23 +00:00
|
|
|
|
|
|
|
describe('given a completeable emoji and colon was just pressed', () => {
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-11-11 22:54:23 +00:00
|
|
|
mockQuill.getSelection.returns({
|
|
|
|
index: 6,
|
|
|
|
length: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('and given it matches a short name', () => {
|
|
|
|
const text = ':smile';
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-11-11 22:54:23 +00:00
|
|
|
const blot = {
|
|
|
|
text,
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, 6]);
|
|
|
|
|
|
|
|
emojiCompletion.onTextChange(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inserts the emoji at the current cursor position', () => {
|
|
|
|
const [emoji, index, range] = insertEmojiStub.args[0];
|
|
|
|
|
|
|
|
assert.equal(emoji.short_name, 'smile');
|
|
|
|
assert.equal(index, 0);
|
|
|
|
assert.equal(range, 6);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not show results', () => {
|
|
|
|
assert.equal(emojiCompletion.results.length, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-10-21 16:53:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('completeEmoji', () => {
|
|
|
|
let insertEmojiStub: sinon.SinonStub<
|
|
|
|
[EmojiData, number, number, (boolean | undefined)?],
|
|
|
|
void
|
|
|
|
>;
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
emojiCompletion.results = [
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
{ short_name: 'smile' } as any,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
{ short_name: 'smile_cat' } as any,
|
|
|
|
];
|
|
|
|
emojiCompletion.index = 1;
|
|
|
|
insertEmojiStub = sinon.stub(emojiCompletion, 'insertEmoji');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('given a valid token', () => {
|
|
|
|
const text = ':smi';
|
|
|
|
const index = text.length;
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
mockQuill.getSelection.returns({
|
|
|
|
index,
|
|
|
|
length: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
const blot = {
|
|
|
|
text,
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, index]);
|
|
|
|
|
|
|
|
emojiCompletion.completeEmoji();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inserts the currently selected emoji at the current cursor position', () => {
|
|
|
|
const [emoji, insertIndex, range] = insertEmojiStub.args[0];
|
|
|
|
|
|
|
|
assert.equal(emoji.short_name, 'smile_cat');
|
|
|
|
assert.equal(insertIndex, 0);
|
|
|
|
assert.equal(range, text.length);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('given a valid token is not present', () => {
|
|
|
|
const text = 'smi';
|
|
|
|
const index = text.length;
|
|
|
|
|
2023-10-11 19:06:43 +00:00
|
|
|
beforeEach(() => {
|
2020-10-21 16:53:32 +00:00
|
|
|
mockQuill.getSelection.returns({
|
|
|
|
index,
|
|
|
|
length: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
const blot = {
|
|
|
|
text,
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf.returns([blot, index]);
|
|
|
|
|
|
|
|
emojiCompletion.completeEmoji();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not insert anything', () => {
|
|
|
|
assert.equal(insertEmojiStub.called, false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|