2021-01-14 18:07:05 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
2020-11-03 01:19:52 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
import { RefObject } from 'react';
|
|
|
|
import Delta from 'quill-delta';
|
|
|
|
|
|
|
|
import { matchMention } from '../../../quill/mentions/matchers';
|
2020-11-04 22:04:48 +00:00
|
|
|
import { MemberRepository } from '../../../quill/memberRepository';
|
2020-11-03 01:19:52 +00:00
|
|
|
import { ConversationType } from '../../../state/ducks/conversations';
|
2021-05-07 22:21:10 +00:00
|
|
|
import { getDefaultConversation } from '../../../test-both/helpers/getDefaultConversation';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
|
|
|
class FakeTokenList<T> extends Array<T> {
|
|
|
|
constructor(elements: Array<T>) {
|
|
|
|
super();
|
|
|
|
elements.forEach(element => this.push(element));
|
|
|
|
}
|
|
|
|
|
|
|
|
contains(searchElement: T) {
|
|
|
|
return this.includes(searchElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const createMockElement = (
|
|
|
|
className: string,
|
|
|
|
dataset: Record<string, string>
|
|
|
|
): HTMLElement =>
|
|
|
|
(({
|
|
|
|
classList: new FakeTokenList([className]),
|
|
|
|
dataset,
|
|
|
|
} as unknown) as HTMLElement);
|
|
|
|
|
|
|
|
const createMockAtMentionElement = (
|
|
|
|
dataset: Record<string, string>
|
|
|
|
): HTMLElement => createMockElement('module-message-body__at-mention', dataset);
|
|
|
|
|
|
|
|
const createMockMentionBlotElement = (
|
|
|
|
dataset: Record<string, string>
|
|
|
|
): HTMLElement => createMockElement('mention-blot', dataset);
|
|
|
|
|
2021-05-07 22:21:10 +00:00
|
|
|
const memberMahershala: ConversationType = 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
|
|
|
});
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2021-05-07 22:21:10 +00:00
|
|
|
const memberShia: ConversationType = 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
|
|
|
|
|
|
|
const members: Array<ConversationType> = [memberMahershala, memberShia];
|
|
|
|
|
|
|
|
const memberRepositoryRef: RefObject<MemberRepository> = {
|
|
|
|
current: new MemberRepository(members),
|
|
|
|
};
|
|
|
|
|
|
|
|
const matcher = matchMention(memberRepositoryRef);
|
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
type Mention = {
|
2020-11-03 01:19:52 +00:00
|
|
|
uuid: string;
|
|
|
|
title: string;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
type MentionInsert = {
|
2020-11-03 01:19:52 +00:00
|
|
|
mention: Mention;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2020-11-03 01:19:52 +00:00
|
|
|
|
|
|
|
const isMention = (insert?: unknown): insert is MentionInsert => {
|
|
|
|
if (insert) {
|
|
|
|
if (Object.getOwnPropertyNames(insert).includes('mention')) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const EMPTY_DELTA = new Delta();
|
|
|
|
|
|
|
|
describe('matchMention', () => {
|
|
|
|
it('handles an AtMentionify from clipboard', () => {
|
|
|
|
const result = matcher(
|
|
|
|
createMockAtMentionElement({
|
|
|
|
id: memberMahershala.id,
|
|
|
|
title: memberMahershala.title,
|
|
|
|
}),
|
|
|
|
EMPTY_DELTA
|
|
|
|
);
|
|
|
|
const { ops } = result;
|
|
|
|
|
|
|
|
assert.isNotEmpty(ops);
|
|
|
|
|
|
|
|
const [op] = ops;
|
|
|
|
const { insert } = op;
|
|
|
|
|
|
|
|
if (isMention(insert)) {
|
|
|
|
const { title, uuid } = insert.mention;
|
|
|
|
|
|
|
|
assert.equal(title, memberMahershala.title);
|
|
|
|
assert.equal(uuid, memberMahershala.uuid);
|
|
|
|
} else {
|
|
|
|
assert.fail('insert is invalid');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles an MentionBlot from clipboard', () => {
|
|
|
|
const result = matcher(
|
|
|
|
createMockMentionBlotElement({
|
|
|
|
uuid: memberMahershala.uuid || '',
|
|
|
|
title: memberMahershala.title,
|
|
|
|
}),
|
|
|
|
EMPTY_DELTA
|
|
|
|
);
|
|
|
|
const { ops } = result;
|
|
|
|
|
|
|
|
assert.isNotEmpty(ops);
|
|
|
|
|
|
|
|
const [op] = ops;
|
|
|
|
const { insert } = op;
|
|
|
|
|
|
|
|
if (isMention(insert)) {
|
|
|
|
const { title, uuid } = insert.mention;
|
|
|
|
|
|
|
|
assert.equal(title, memberMahershala.title);
|
|
|
|
assert.equal(uuid, memberMahershala.uuid);
|
|
|
|
} else {
|
|
|
|
assert.fail('insert is invalid');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('converts a missing AtMentionify to string', () => {
|
|
|
|
const result = matcher(
|
|
|
|
createMockAtMentionElement({
|
|
|
|
id: 'florp',
|
|
|
|
title: 'Nonexistent',
|
|
|
|
}),
|
|
|
|
EMPTY_DELTA
|
|
|
|
);
|
|
|
|
const { ops } = result;
|
|
|
|
|
|
|
|
assert.isNotEmpty(ops);
|
|
|
|
|
|
|
|
const [op] = ops;
|
|
|
|
const { insert } = op;
|
|
|
|
|
|
|
|
if (isMention(insert)) {
|
|
|
|
assert.fail('insert is invalid');
|
|
|
|
} else {
|
|
|
|
assert.equal(insert, '@Nonexistent');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('converts a missing MentionBlot to string', () => {
|
|
|
|
const result = matcher(
|
|
|
|
createMockMentionBlotElement({
|
|
|
|
uuid: 'florp',
|
|
|
|
title: 'Nonexistent',
|
|
|
|
}),
|
|
|
|
EMPTY_DELTA
|
|
|
|
);
|
|
|
|
const { ops } = result;
|
|
|
|
|
|
|
|
assert.isNotEmpty(ops);
|
|
|
|
|
|
|
|
const [op] = ops;
|
|
|
|
const { insert } = op;
|
|
|
|
|
|
|
|
if (isMention(insert)) {
|
|
|
|
assert.fail('insert is invalid');
|
|
|
|
} else {
|
|
|
|
assert.equal(insert, '@Nonexistent');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes other clipboard elements through', () => {
|
|
|
|
const result = matcher(createMockElement('ignore', {}), EMPTY_DELTA);
|
|
|
|
assert.equal(result, EMPTY_DELTA);
|
|
|
|
});
|
|
|
|
});
|