2021-01-04 19:46:24 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
2020-11-03 01:19:52 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
import { assert } from 'chai';
|
|
|
|
import Delta from 'quill-delta';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { SinonStub } from 'sinon';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
import type { Quill, KeyboardStatic } from 'quill';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MutableRefObject } from 'react';
|
|
|
|
import type { MentionCompletionOptions } from '../../../quill/mentions/completion';
|
|
|
|
import { MentionCompletion } from '../../../quill/mentions/completion';
|
|
|
|
import type { ConversationType } from '../../../state/ducks/conversations';
|
2020-11-04 22:04:48 +00:00
|
|
|
import { MemberRepository } from '../../../quill/memberRepository';
|
2021-05-07 22:21:10 +00:00
|
|
|
import { getDefaultConversation } from '../../../test-both/helpers/getDefaultConversation';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2021-05-07 22:21:10 +00:00
|
|
|
const me: ConversationType = getDefaultConversation({
|
2020-11-03 01:19:52 +00:00
|
|
|
id: '666777',
|
|
|
|
uuid: 'pqrstuv',
|
|
|
|
title: 'Fred Savage',
|
|
|
|
firstName: 'Fred',
|
|
|
|
profileName: 'Fred S.',
|
|
|
|
type: 'direct',
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
markedUnread: false,
|
2020-11-11 17:36:05 +00:00
|
|
|
areWeAdmin: false,
|
2021-05-07 22:21:10 +00:00
|
|
|
isMe: true,
|
|
|
|
});
|
2020-11-03 01:19:52 +00:00
|
|
|
|
|
|
|
const members: Array<ConversationType> = [
|
2021-05-07 22:21:10 +00:00
|
|
|
getDefaultConversation({
|
2020-11-03 01:19:52 +00:00
|
|
|
id: '555444',
|
|
|
|
uuid: 'abcdefg',
|
|
|
|
title: 'Mahershala Ali',
|
|
|
|
firstName: 'Mahershala',
|
|
|
|
profileName: 'Mahershala A.',
|
|
|
|
type: 'direct',
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
markedUnread: false,
|
2020-11-11 17:36:05 +00:00
|
|
|
areWeAdmin: false,
|
2021-05-07 22:21:10 +00:00
|
|
|
}),
|
|
|
|
getDefaultConversation({
|
2020-11-03 01:19:52 +00:00
|
|
|
id: '333222',
|
|
|
|
uuid: 'hijklmno',
|
|
|
|
title: 'Shia LaBeouf',
|
|
|
|
firstName: 'Shia',
|
|
|
|
profileName: 'Shia L.',
|
|
|
|
type: 'direct',
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
markedUnread: false,
|
2020-11-11 17:36:05 +00:00
|
|
|
areWeAdmin: false,
|
2021-05-07 22:21:10 +00:00
|
|
|
}),
|
2020-11-03 01:19:52 +00:00
|
|
|
me,
|
|
|
|
];
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
describe('MentionCompletion', () => {
|
|
|
|
let mockQuill: Omit<
|
|
|
|
Partial<{ [K in keyof Quill]: SinonStub }>,
|
|
|
|
'keyboard'
|
|
|
|
> & {
|
|
|
|
keyboard: Partial<{ [K in keyof KeyboardStatic]: SinonStub }>;
|
|
|
|
};
|
|
|
|
let mentionCompletion: MentionCompletion;
|
2020-11-03 01:19:52 +00:00
|
|
|
|
|
|
|
beforeEach(function beforeEach() {
|
|
|
|
const memberRepositoryRef: MutableRefObject<MemberRepository> = {
|
|
|
|
current: new MemberRepository(members),
|
|
|
|
};
|
|
|
|
|
|
|
|
const options: MentionCompletionOptions = {
|
2021-05-03 23:24:40 +00:00
|
|
|
i18n: Object.assign(sinon.stub(), { getLocale: sinon.stub() }),
|
2020-11-03 01:19:52 +00:00
|
|
|
me,
|
|
|
|
memberRepositoryRef,
|
2021-01-04 18:28:32 +00:00
|
|
|
setMentionPickerElement: sinon.stub(),
|
2020-11-03 01:19:52 +00:00
|
|
|
};
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
mockQuill = {
|
|
|
|
getContents: sinon.stub(),
|
|
|
|
getLeaf: sinon.stub(),
|
|
|
|
getSelection: sinon.stub(),
|
|
|
|
keyboard: { addBinding: sinon.stub() },
|
|
|
|
on: sinon.stub(),
|
|
|
|
setSelection: sinon.stub(),
|
|
|
|
updateContents: sinon.stub(),
|
|
|
|
};
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
mentionCompletion = new MentionCompletion(
|
|
|
|
(mockQuill as unknown) as Quill,
|
|
|
|
options
|
|
|
|
);
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
sinon.stub(mentionCompletion, 'render');
|
2020-11-03 01:19:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('onTextChange', () => {
|
2021-07-08 21:55:48 +00:00
|
|
|
let possiblyShowMemberResultsStub: sinon.SinonStub<
|
|
|
|
[],
|
|
|
|
Array<ConversationType>
|
|
|
|
>;
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
possiblyShowMemberResultsStub = sinon.stub(
|
|
|
|
mentionCompletion,
|
|
|
|
'possiblyShowMemberResults'
|
|
|
|
);
|
2020-11-03 01:19:52 +00:00
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
describe('given a change that should show members', () => {
|
|
|
|
const newContents = new Delta().insert('@a');
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
mockQuill.getContents?.returns(newContents);
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
possiblyShowMemberResultsStub.returns(members);
|
2020-11-03 01:19:52 +00:00
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('shows member results', () => {
|
2020-11-03 01:19:52 +00:00
|
|
|
mentionCompletion.onTextChange();
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
assert.equal(mentionCompletion.results, members);
|
|
|
|
assert.equal(mentionCompletion.index, 0);
|
2020-11-03 01:19:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
describe('given a change that should clear results', () => {
|
|
|
|
const newContents = new Delta().insert('foo ');
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
let clearResultsStub: SinonStub<[], void>;
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
mentionCompletion.results = members;
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
mockQuill.getContents?.returns(newContents);
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
possiblyShowMemberResultsStub.returns([]);
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
clearResultsStub = sinon.stub(mentionCompletion, 'clearResults');
|
|
|
|
});
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('clears member results', () => {
|
2020-11-03 01:19:52 +00:00
|
|
|
mentionCompletion.onTextChange();
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
assert.equal(clearResultsStub.called, true);
|
2020-11-03 01:19:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('completeMention', () => {
|
2020-11-05 21:18:42 +00:00
|
|
|
describe('given a completable mention', () => {
|
|
|
|
let insertMentionStub: SinonStub<
|
|
|
|
[ConversationType, number, number, (boolean | undefined)?],
|
|
|
|
void
|
|
|
|
>;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mentionCompletion.results = members;
|
|
|
|
mockQuill.getSelection?.returns({ index: 5 });
|
|
|
|
mockQuill.getLeaf?.returns([{ text: '@shia' }, 5]);
|
|
|
|
|
|
|
|
insertMentionStub = sinon.stub(mentionCompletion, 'insertMention');
|
2020-11-03 01:19:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('inserts the currently selected mention at the current cursor position', () => {
|
2020-11-05 21:18:42 +00:00
|
|
|
mentionCompletion.completeMention(1);
|
|
|
|
|
|
|
|
const [
|
|
|
|
member,
|
|
|
|
distanceFromCursor,
|
|
|
|
adjustCursorAfterBy,
|
|
|
|
withTrailingSpace,
|
|
|
|
] = insertMentionStub.getCall(0).args;
|
|
|
|
|
|
|
|
assert.equal(member, members[1]);
|
|
|
|
assert.equal(distanceFromCursor, 0);
|
|
|
|
assert.equal(adjustCursorAfterBy, 5);
|
|
|
|
assert.equal(withTrailingSpace, true);
|
2020-11-03 01:19:52 +00:00
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('can infer the member to complete with', () => {
|
|
|
|
mentionCompletion.index = 1;
|
2020-11-03 01:19:52 +00:00
|
|
|
mentionCompletion.completeMention();
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
const [
|
|
|
|
member,
|
|
|
|
distanceFromCursor,
|
|
|
|
adjustCursorAfterBy,
|
|
|
|
withTrailingSpace,
|
|
|
|
] = insertMentionStub.getCall(0).args;
|
|
|
|
|
|
|
|
assert.equal(member, members[1]);
|
|
|
|
assert.equal(distanceFromCursor, 0);
|
|
|
|
assert.equal(adjustCursorAfterBy, 5);
|
|
|
|
assert.equal(withTrailingSpace, true);
|
2020-11-03 01:19:52 +00:00
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
describe('from the middle of a string', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mockQuill.getSelection?.returns({ index: 9 });
|
|
|
|
mockQuill.getLeaf?.returns([{ text: 'foo @shia bar' }, 9]);
|
2020-11-03 01:19:52 +00:00
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('inserts correctly', () => {
|
|
|
|
mentionCompletion.completeMention(1);
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
const [
|
|
|
|
member,
|
|
|
|
distanceFromCursor,
|
|
|
|
adjustCursorAfterBy,
|
|
|
|
withTrailingSpace,
|
|
|
|
] = insertMentionStub.getCall(0).args;
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
assert.equal(member, members[1]);
|
|
|
|
assert.equal(distanceFromCursor, 4);
|
|
|
|
assert.equal(adjustCursorAfterBy, 5);
|
|
|
|
assert.equal(withTrailingSpace, true);
|
|
|
|
});
|
2020-11-03 01:19:52 +00:00
|
|
|
});
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
describe('given a completable mention starting with a capital letter', () => {
|
|
|
|
const text = '@Sh';
|
|
|
|
const index = text.length;
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
beforeEach(function beforeEach() {
|
|
|
|
mockQuill.getSelection?.returns({ index });
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
const blot = {
|
|
|
|
text,
|
|
|
|
};
|
|
|
|
mockQuill.getLeaf?.returns([blot, index]);
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
mentionCompletion.completeMention(1);
|
|
|
|
});
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
it('inserts the currently selected mention at the current cursor position', () => {
|
|
|
|
const [
|
|
|
|
member,
|
|
|
|
distanceFromCursor,
|
|
|
|
adjustCursorAfterBy,
|
|
|
|
withTrailingSpace,
|
|
|
|
] = insertMentionStub.getCall(0).args;
|
|
|
|
|
|
|
|
assert.equal(member, members[1]);
|
|
|
|
assert.equal(distanceFromCursor, 0);
|
|
|
|
assert.equal(adjustCursorAfterBy, 3);
|
|
|
|
assert.equal(withTrailingSpace, true);
|
|
|
|
});
|
2020-11-03 01:19:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|