debian-mirror-gitlab/spec/frontend/emoji/emoji_spec.js

477 lines
14 KiB
JavaScript
Raw Normal View History

2020-10-24 23:57:45 +05:30
import { trimText } from 'helpers/text_helper';
2021-01-03 14:25:43 +05:30
import { emojiFixtureMap, initEmojiMock, describeEmojiFields } from 'helpers/emoji';
import { glEmojiTag, searchEmoji, getEmoji } from '~/emoji';
2017-09-10 17:25:29 +05:30
import isEmojiUnicodeSupported, {
2017-08-17 22:00:37 +05:30
isFlagEmoji,
2018-03-17 18:26:18 +05:30
isRainbowFlagEmoji,
2017-08-17 22:00:37 +05:30
isKeycapEmoji,
isSkinToneComboEmoji,
isHorceRacingSkinToneComboEmoji,
isPersonZwjEmoji,
2017-09-10 17:25:29 +05:30
} from '~/emoji/support/is_emoji_unicode_supported';
2017-08-17 22:00:37 +05:30
const emptySupportMap = {
personZwj: false,
horseRacing: false,
flag: false,
skinToneModifier: false,
'9.0': false,
'8.0': false,
'7.0': false,
6.1: false,
'6.0': false,
5.2: false,
5.1: false,
4.1: false,
'4.0': false,
3.2: false,
'3.0': false,
1.1: false,
};
2020-07-28 23:09:34 +05:30
describe('gl_emoji', () => {
let mock;
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
beforeEach(async () => {
mock = await initEmojiMock();
2020-07-28 23:09:34 +05:30
});
afterEach(() => {
mock.restore();
});
2017-08-17 22:00:37 +05:30
describe('glEmojiTag', () => {
it('bomb emoji', () => {
const emojiKey = 'bomb';
const markup = glEmojiTag(emojiFixtureMap[emojiKey].name);
2020-07-28 23:09:34 +05:30
expect(trimText(markup)).toMatchInlineSnapshot(
`"<gl-emoji data-name=\\"bomb\\"></gl-emoji>"`,
2017-08-17 22:00:37 +05:30
);
});
it('bomb emoji with sprite fallback readiness', () => {
const emojiKey = 'bomb';
const markup = glEmojiTag(emojiFixtureMap[emojiKey].name, {
sprite: true,
});
2020-07-28 23:09:34 +05:30
expect(trimText(markup)).toMatchInlineSnapshot(
`"<gl-emoji data-fallback-sprite-class=\\"emoji-bomb\\" data-name=\\"bomb\\"></gl-emoji>"`,
2017-08-17 22:00:37 +05:30
);
});
});
describe('isFlagEmoji', () => {
2017-09-10 17:25:29 +05:30
it('should gracefully handle empty string', () => {
expect(isFlagEmoji('')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect flag_ac', () => {
expect(isFlagEmoji('🇦🇨')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect flag_us', () => {
expect(isFlagEmoji('🇺🇸')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect flag_zw', () => {
expect(isFlagEmoji('🇿🇼')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect flags', () => {
expect(isFlagEmoji('🎏')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect triangular_flag_on_post', () => {
expect(isFlagEmoji('🚩')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect single letter', () => {
expect(isFlagEmoji('🇦')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect >2 letters', () => {
expect(isFlagEmoji('🇦🇧🇨')).toBeFalsy();
});
});
2018-03-17 18:26:18 +05:30
describe('isRainbowFlagEmoji', () => {
it('should gracefully handle empty string', () => {
expect(isRainbowFlagEmoji('')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
it('should detect rainbow_flag', () => {
expect(isRainbowFlagEmoji('🏳🌈')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
it("should not detect flag_white on its' own", () => {
2018-03-17 18:26:18 +05:30
expect(isRainbowFlagEmoji('🏳')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
it("should not detect rainbow on its' own", () => {
2018-03-17 18:26:18 +05:30
expect(isRainbowFlagEmoji('🌈')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
it('should not detect flag_white with something else', () => {
expect(isRainbowFlagEmoji('🏳🔵')).toBeFalsy();
});
});
2017-08-17 22:00:37 +05:30
describe('isKeycapEmoji', () => {
2017-09-10 17:25:29 +05:30
it('should gracefully handle empty string', () => {
expect(isKeycapEmoji('')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect one(keycap)', () => {
expect(isKeycapEmoji('1⃣')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect nine(keycap)', () => {
expect(isKeycapEmoji('9⃣')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect ten(keycap)', () => {
expect(isKeycapEmoji('🔟')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect hash(keycap)', () => {
expect(isKeycapEmoji('#⃣')).toBeFalsy();
});
});
describe('isSkinToneComboEmoji', () => {
2017-09-10 17:25:29 +05:30
it('should gracefully handle empty string', () => {
expect(isSkinToneComboEmoji('')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect hand_splayed_tone5', () => {
expect(isSkinToneComboEmoji('🖐🏿')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect hand_splayed', () => {
expect(isSkinToneComboEmoji('🖐')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect lifter_tone1', () => {
expect(isSkinToneComboEmoji('🏋🏻')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect lifter', () => {
expect(isSkinToneComboEmoji('🏋')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect rowboat_tone4', () => {
expect(isSkinToneComboEmoji('🚣🏾')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect rowboat', () => {
expect(isSkinToneComboEmoji('🚣')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect individual tone emoji', () => {
expect(isSkinToneComboEmoji('🏻')).toBeFalsy();
});
});
describe('isHorceRacingSkinToneComboEmoji', () => {
2017-09-10 17:25:29 +05:30
it('should gracefully handle empty string', () => {
expect(isHorceRacingSkinToneComboEmoji('')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect horse_racing_tone2', () => {
expect(isHorceRacingSkinToneComboEmoji('🏇🏼')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect horse_racing', () => {
expect(isHorceRacingSkinToneComboEmoji('🏇')).toBeFalsy();
});
});
describe('isPersonZwjEmoji', () => {
2017-09-10 17:25:29 +05:30
it('should gracefully handle empty string', () => {
expect(isPersonZwjEmoji('')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect couple_mm', () => {
expect(isPersonZwjEmoji('👨‍❤️‍👨')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect couple_with_heart', () => {
expect(isPersonZwjEmoji('💑')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect couplekiss', () => {
expect(isPersonZwjEmoji('💏')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect family_mmb', () => {
expect(isPersonZwjEmoji('👨‍👨‍👦')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect family_mwgb', () => {
expect(isPersonZwjEmoji('👨‍👩‍👧‍👦')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect family', () => {
expect(isPersonZwjEmoji('👪')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should detect kiss_ww', () => {
expect(isPersonZwjEmoji('👩‍❤️‍💋‍👩')).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect girl', () => {
expect(isPersonZwjEmoji('👧')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect girl_tone5', () => {
expect(isPersonZwjEmoji('👧🏿')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect man', () => {
expect(isPersonZwjEmoji('👨')).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('should not detect woman', () => {
expect(isPersonZwjEmoji('👩')).toBeFalsy();
});
});
describe('isEmojiUnicodeSupported', () => {
2017-09-10 17:25:29 +05:30
it('should gracefully handle empty string with unicode support', () => {
2018-12-13 13:39:08 +05:30
const isSupported = isEmojiUnicodeSupported({ '1.0': true }, '', '1.0');
2017-09-10 17:25:29 +05:30
expect(isSupported).toBeTruthy();
});
2018-12-13 13:39:08 +05:30
2017-09-10 17:25:29 +05:30
it('should gracefully handle empty string without unicode support', () => {
2018-12-13 13:39:08 +05:30
const isSupported = isEmojiUnicodeSupported({}, '', '1.0');
2017-09-10 17:25:29 +05:30
expect(isSupported).toBeFalsy();
});
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
it('bomb(6.0) with 6.0 support', () => {
const emojiKey = 'bomb';
2020-05-24 23:13:21 +05:30
const unicodeSupportMap = { ...emptySupportMap, '6.0': true };
2017-08-17 22:00:37 +05:30
const isSupported = isEmojiUnicodeSupported(
unicodeSupportMap,
emojiFixtureMap[emojiKey].moji,
emojiFixtureMap[emojiKey].unicodeVersion,
);
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
expect(isSupported).toBeTruthy();
});
it('bomb(6.0) without 6.0 support', () => {
const emojiKey = 'bomb';
const unicodeSupportMap = emptySupportMap;
const isSupported = isEmojiUnicodeSupported(
unicodeSupportMap,
emojiFixtureMap[emojiKey].moji,
emojiFixtureMap[emojiKey].unicodeVersion,
);
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
expect(isSupported).toBeFalsy();
});
it('bomb(6.0) without 6.0 but with 9.0 support', () => {
const emojiKey = 'bomb';
2020-05-24 23:13:21 +05:30
const unicodeSupportMap = { ...emptySupportMap, '9.0': true };
2017-08-17 22:00:37 +05:30
const isSupported = isEmojiUnicodeSupported(
unicodeSupportMap,
emojiFixtureMap[emojiKey].moji,
emojiFixtureMap[emojiKey].unicodeVersion,
);
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
expect(isSupported).toBeFalsy();
});
it('construction_worker_tone5(8.0) without skin tone modifier support', () => {
const emojiKey = 'construction_worker_tone5';
2020-05-24 23:13:21 +05:30
const unicodeSupportMap = {
...emptySupportMap,
2017-08-17 22:00:37 +05:30
skinToneModifier: false,
'9.0': true,
'8.0': true,
'7.0': true,
6.1: true,
'6.0': true,
5.2: true,
5.1: true,
4.1: true,
'4.0': true,
3.2: true,
'3.0': true,
1.1: true,
2020-05-24 23:13:21 +05:30
};
2017-08-17 22:00:37 +05:30
const isSupported = isEmojiUnicodeSupported(
unicodeSupportMap,
emojiFixtureMap[emojiKey].moji,
emojiFixtureMap[emojiKey].unicodeVersion,
);
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
expect(isSupported).toBeFalsy();
});
it('use native keycap on >=57 chrome', () => {
const emojiKey = 'five';
2020-05-24 23:13:21 +05:30
const unicodeSupportMap = {
...emptySupportMap,
2017-08-17 22:00:37 +05:30
'3.0': true,
meta: {
isChrome: true,
chromeVersion: 57,
},
2020-05-24 23:13:21 +05:30
};
2017-08-17 22:00:37 +05:30
const isSupported = isEmojiUnicodeSupported(
unicodeSupportMap,
emojiFixtureMap[emojiKey].moji,
emojiFixtureMap[emojiKey].unicodeVersion,
);
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
expect(isSupported).toBeTruthy();
});
it('fallback keycap on <57 chrome', () => {
const emojiKey = 'five';
2020-05-24 23:13:21 +05:30
const unicodeSupportMap = {
...emptySupportMap,
2017-08-17 22:00:37 +05:30
'3.0': true,
meta: {
isChrome: true,
chromeVersion: 50,
},
2020-05-24 23:13:21 +05:30
};
2017-08-17 22:00:37 +05:30
const isSupported = isEmojiUnicodeSupported(
unicodeSupportMap,
emojiFixtureMap[emojiKey].moji,
emojiFixtureMap[emojiKey].unicodeVersion,
);
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
expect(isSupported).toBeFalsy();
});
});
2021-01-03 14:25:43 +05:30
describe('getEmoji', () => {
const { grey_question } = emojiFixtureMap;
describe('when query is undefined', () => {
it('should return null by default', () => {
expect(getEmoji()).toBe(null);
});
it('should return fallback emoji when fallback is true', () => {
expect(getEmoji(undefined, true).name).toEqual(grey_question.name);
});
});
});
describe('searchEmoji', () => {
const { atom, grey_question } = emojiFixtureMap;
const search = (query, opts) => searchEmoji(query, opts).map(({ name }) => name);
2021-03-08 18:12:59 +05:30
const mangle = (str) => str.slice(0, 1) + str.slice(-1);
const partial = (str) => str.slice(0, 2);
2021-01-03 14:25:43 +05:30
describe('with default options', () => {
2021-03-08 18:12:59 +05:30
const subject = (query) => search(query);
2021-01-03 14:25:43 +05:30
describeEmojiFields('with $field', ({ accessor }) => {
it(`should match by lower case: ${accessor(atom)}`, () => {
expect(subject(accessor(atom))).toContain(atom.name);
});
it(`should match by upper case: ${accessor(atom).toUpperCase()}`, () => {
expect(subject(accessor(atom).toUpperCase())).toContain(atom.name);
});
it(`should not match by partial: ${mangle(accessor(atom))}`, () => {
expect(subject(mangle(accessor(atom)))).not.toContain(atom.name);
});
});
it(`should match by unicode value: ${atom.moji}`, () => {
expect(subject(atom.moji)).toContain(atom.name);
});
it('should not return a fallback value', () => {
expect(subject('foo bar baz')).toHaveLength(0);
});
it('should not return a fallback value when query is falsey', () => {
expect(subject()).toHaveLength(0);
});
});
describe('with fuzzy match', () => {
2021-03-08 18:12:59 +05:30
const subject = (query) => search(query, { match: 'fuzzy' });
2021-01-03 14:25:43 +05:30
describeEmojiFields('with $field', ({ accessor }) => {
it(`should match by lower case: ${accessor(atom)}`, () => {
expect(subject(accessor(atom))).toContain(atom.name);
});
it(`should match by upper case: ${accessor(atom).toUpperCase()}`, () => {
expect(subject(accessor(atom).toUpperCase())).toContain(atom.name);
});
it(`should match by partial: ${mangle(accessor(atom))}`, () => {
expect(subject(mangle(accessor(atom)))).toContain(atom.name);
});
});
});
describe('with contains match', () => {
2021-03-08 18:12:59 +05:30
const subject = (query) => search(query, { match: 'contains' });
2021-01-03 14:25:43 +05:30
describeEmojiFields('with $field', ({ accessor }) => {
it(`should match by lower case: ${accessor(atom)}`, () => {
expect(subject(accessor(atom))).toContain(atom.name);
});
it(`should match by upper case: ${accessor(atom).toUpperCase()}`, () => {
expect(subject(accessor(atom).toUpperCase())).toContain(atom.name);
});
it(`should match by partial: ${partial(accessor(atom))}`, () => {
expect(subject(partial(accessor(atom)))).toContain(atom.name);
});
it(`should not match by mangled: ${mangle(accessor(atom))}`, () => {
expect(subject(mangle(accessor(atom)))).not.toContain(atom.name);
});
});
});
describe('with fallback', () => {
2021-03-08 18:12:59 +05:30
const subject = (query) => search(query, { fallback: true });
2021-01-03 14:25:43 +05:30
it.each`
query
${'foo bar baz'} | ${undefined}
`('should return a fallback value when given $query', ({ query }) => {
expect(subject(query)).toContain(grey_question.name);
});
});
describe('with name and alias fields', () => {
2021-03-08 18:12:59 +05:30
const subject = (query) => search(query, { fields: ['name', 'alias'] });
2021-01-03 14:25:43 +05:30
it(`should match by name: ${atom.name}`, () => {
expect(subject(atom.name)).toContain(atom.name);
});
it(`should match by alias: ${atom.aliases[0]}`, () => {
expect(subject(atom.aliases[0])).toContain(atom.name);
});
it(`should not match by description: ${atom.description}`, () => {
expect(subject(atom.description)).not.toContain(atom.name);
});
it(`should not match by unicode value: ${atom.moji}`, () => {
expect(subject(atom.moji)).not.toContain(atom.name);
});
});
});
2017-08-17 22:00:37 +05:30
});