2021-06-01 19:19:38 +00:00
|
|
|
|
import { fromCodepointToUnicode, fromHexcodeToCodepoint } from 'emojibase';
|
|
|
|
|
import { emojify, setEmojiConfig, stripEmojis, unemojify } from './emoji';
|
2021-04-14 09:03:08 +00:00
|
|
|
|
|
2021-08-18 05:46:56 +00:00
|
|
|
|
describe('util/emoji', () => {
|
2021-06-01 19:19:38 +00:00
|
|
|
|
beforeEach(() => {
|
|
|
|
|
setEmojiConfig({ unicodeEmoji: true });
|
2021-04-14 09:03:08 +00:00
|
|
|
|
});
|
|
|
|
|
|
2021-06-01 19:19:38 +00:00
|
|
|
|
describe('emojify', () => {
|
|
|
|
|
it('encodes known shortcodes', () => {
|
2021-11-08 12:16:58 +00:00
|
|
|
|
expect(emojify('Let it :bee:')).toBe('Let it 🐝');
|
2021-06-01 19:19:38 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('encodes aliases', () => {
|
|
|
|
|
const bee = emojify(':bee:');
|
|
|
|
|
const honeyBee = emojify(':honeybee:');
|
|
|
|
|
expect(bee).toEqual(honeyBee);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('omits unknown shortcodes', () => {
|
2021-11-08 12:16:58 +00:00
|
|
|
|
expect(emojify(':foo: :bar: :bee:')).toBe(':foo: :bar: 🐝');
|
2021-06-01 19:19:38 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('does not encode when config option is disabled', () => {
|
|
|
|
|
setEmojiConfig({ unicodeEmoji: false });
|
2021-11-08 12:16:58 +00:00
|
|
|
|
expect(emojify('Let it :bee:')).toBe('Let it :bee:');
|
2021-06-01 19:19:38 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('unemojify', () => {
|
|
|
|
|
it('strips emojis when the config has been set accordingly', () => {
|
|
|
|
|
const emoji = '🚀💎';
|
|
|
|
|
const otherText = 'regular text';
|
|
|
|
|
const text = `${emoji} ${otherText}`;
|
|
|
|
|
setEmojiConfig({ unicodeEmoji: false });
|
|
|
|
|
const result = unemojify(text);
|
|
|
|
|
expect(result).not.toContain(emoji);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('does not strip emojis when the config demands it', () => {
|
|
|
|
|
const emoji = '🚀💎';
|
|
|
|
|
const otherText = 'regular text';
|
|
|
|
|
const text = `${emoji} ${otherText}`;
|
|
|
|
|
setEmojiConfig({ unicodeEmoji: true });
|
|
|
|
|
const result = unemojify(text);
|
|
|
|
|
expect(result).toEqual(text);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('unsupported characters', () => {
|
|
|
|
|
const unsupported = '🪆';
|
|
|
|
|
|
|
|
|
|
it('uses replacement character', () => {
|
|
|
|
|
setEmojiConfig({ unicodeEmoji: false });
|
2021-11-08 12:16:58 +00:00
|
|
|
|
expect(unemojify(unsupported)).toBe('<27>');
|
2021-06-01 19:19:38 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('problem characters', () => {
|
|
|
|
|
it.each(['🚀', '💎', '🧹', '📦'])('converts %s forth and back', (char) => {
|
|
|
|
|
setEmojiConfig({ unicodeEmoji: false });
|
|
|
|
|
const codified = unemojify(char);
|
|
|
|
|
expect(codified).not.toEqual(char);
|
|
|
|
|
|
|
|
|
|
setEmojiConfig({ unicodeEmoji: true });
|
|
|
|
|
const emojified = emojify(codified);
|
|
|
|
|
expect(emojified).toEqual(char);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('stripEmojis', () => {
|
|
|
|
|
const makeEmoji = (hexCode: string): string =>
|
|
|
|
|
fromCodepointToUnicode(fromHexcodeToCodepoint(hexCode));
|
|
|
|
|
|
|
|
|
|
it('is independent of config option', () => {
|
|
|
|
|
const x: string = makeEmoji('26A0-FE0F');
|
|
|
|
|
const y: string = makeEmoji('26A0');
|
|
|
|
|
|
|
|
|
|
setEmojiConfig({ unicodeEmoji: true });
|
|
|
|
|
expect(stripEmojis(`foo ${x} bar`)).toEqual(`foo ${y} bar`);
|
|
|
|
|
|
|
|
|
|
setEmojiConfig({ unicodeEmoji: false });
|
|
|
|
|
expect(stripEmojis(`foo ${x} bar`)).toEqual(`foo ${y} bar`);
|
|
|
|
|
});
|
2021-04-14 09:03:08 +00:00
|
|
|
|
});
|
|
|
|
|
});
|