2022-01-26 12:08:38 +05:30
|
|
|
import { initEmojiMock, clearEmojiMock } from 'helpers/emoji';
|
2021-03-08 18:12:59 +05:30
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2020-07-28 23:09:34 +05:30
|
|
|
import installGlEmojiElement from '~/behaviors/gl_emoji';
|
2022-01-26 12:08:38 +05:30
|
|
|
import { EMOJI_VERSION } from '~/emoji';
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
import * as EmojiUnicodeSupport from '~/emoji/support';
|
|
|
|
|
|
|
|
jest.mock('~/emoji/support');
|
|
|
|
|
|
|
|
describe('gl_emoji', () => {
|
2020-11-24 15:15:51 +05:30
|
|
|
const emojiData = {
|
|
|
|
grey_question: {
|
|
|
|
c: 'symbols',
|
|
|
|
e: '❔',
|
|
|
|
d: 'white question mark ornament',
|
|
|
|
u: '6.0',
|
|
|
|
},
|
|
|
|
bomb: {
|
|
|
|
c: 'objects',
|
|
|
|
e: '💣',
|
|
|
|
d: 'bomb',
|
|
|
|
u: '6.0',
|
|
|
|
},
|
|
|
|
};
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
jest.spyOn(EmojiUnicodeSupport, 'default').mockReturnValue(true);
|
|
|
|
installGlEmojiElement();
|
|
|
|
});
|
|
|
|
|
|
|
|
function markupToDomElement(markup) {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.innerHTML = markup;
|
|
|
|
document.body.appendChild(div);
|
|
|
|
|
|
|
|
return div.firstElementChild;
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
beforeEach(async () => {
|
|
|
|
await initEmojiMock(emojiData);
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-01-26 12:08:38 +05:30
|
|
|
clearEmojiMock();
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
document.body.innerHTML = '';
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each([
|
|
|
|
[
|
|
|
|
'bomb emoji just with name attribute',
|
|
|
|
'<gl-emoji data-name="bomb"></gl-emoji>',
|
|
|
|
'<gl-emoji data-name="bomb" data-unicode-version="6.0" title="bomb">💣</gl-emoji>',
|
2023-06-20 00:43:36 +05:30
|
|
|
`<gl-emoji data-name="bomb" data-unicode-version="6.0" title="bomb"><img class="emoji" title=":bomb:" alt=":bomb:" src="/-/emojis/${EMOJI_VERSION}/bomb.png" width="16" height="16" align="absmiddle"></gl-emoji>`,
|
2020-07-28 23:09:34 +05:30
|
|
|
],
|
|
|
|
[
|
|
|
|
'bomb emoji with name attribute and unicode version',
|
|
|
|
'<gl-emoji data-name="bomb" data-unicode-version="6.0">💣</gl-emoji>',
|
|
|
|
'<gl-emoji data-name="bomb" data-unicode-version="6.0">💣</gl-emoji>',
|
2023-06-20 00:43:36 +05:30
|
|
|
`<gl-emoji data-name="bomb" data-unicode-version="6.0"><img class="emoji" title=":bomb:" alt=":bomb:" src="/-/emojis/${EMOJI_VERSION}/bomb.png" width="16" height="16" align="absmiddle"></gl-emoji>`,
|
2020-07-28 23:09:34 +05:30
|
|
|
],
|
|
|
|
[
|
|
|
|
'bomb emoji with sprite fallback',
|
|
|
|
'<gl-emoji data-fallback-sprite-class="emoji-bomb" data-name="bomb"></gl-emoji>',
|
|
|
|
'<gl-emoji data-fallback-sprite-class="emoji-bomb" data-name="bomb" data-unicode-version="6.0" title="bomb">💣</gl-emoji>',
|
|
|
|
'<gl-emoji data-fallback-sprite-class="emoji-bomb" data-name="bomb" data-unicode-version="6.0" title="bomb" class="emoji-icon emoji-bomb">💣</gl-emoji>',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'bomb emoji with image fallback',
|
|
|
|
'<gl-emoji data-fallback-src="/bomb.png" data-name="bomb"></gl-emoji>',
|
|
|
|
'<gl-emoji data-fallback-src="/bomb.png" data-name="bomb" data-unicode-version="6.0" title="bomb">💣</gl-emoji>',
|
2023-06-20 00:43:36 +05:30
|
|
|
'<gl-emoji data-fallback-src="/bomb.png" data-name="bomb" data-unicode-version="6.0" title="bomb"><img class="emoji" title=":bomb:" alt=":bomb:" src="/bomb.png" width="16" height="16" align="absmiddle"></gl-emoji>',
|
2020-07-28 23:09:34 +05:30
|
|
|
],
|
|
|
|
[
|
|
|
|
'invalid emoji',
|
|
|
|
'<gl-emoji data-name="invalid_emoji"></gl-emoji>',
|
|
|
|
'<gl-emoji data-name="grey_question" data-unicode-version="6.0" title="white question mark ornament">❔</gl-emoji>',
|
2023-06-20 00:43:36 +05:30
|
|
|
`<gl-emoji data-name="grey_question" data-unicode-version="6.0" title="white question mark ornament"><img class="emoji" title=":grey_question:" alt=":grey_question:" src="/-/emojis/${EMOJI_VERSION}/grey_question.png" width="16" height="16" align="absmiddle"></gl-emoji>`,
|
2020-07-28 23:09:34 +05:30
|
|
|
],
|
2022-06-21 17:19:12 +05:30
|
|
|
[
|
|
|
|
'custom emoji with image fallback',
|
|
|
|
'<gl-emoji data-fallback-src="https://cultofthepartyparrot.com/parrots/hd/parrot.gif" data-name="party-parrot" data-unicode-version="custom"></gl-emoji>',
|
2023-06-20 00:43:36 +05:30
|
|
|
'<gl-emoji data-fallback-src="https://cultofthepartyparrot.com/parrots/hd/parrot.gif" data-name="party-parrot" data-unicode-version="custom"><img class="emoji" title=":party-parrot:" alt=":party-parrot:" src="https://cultofthepartyparrot.com/parrots/hd/parrot.gif" width="16" height="16" align="absmiddle"></gl-emoji>',
|
|
|
|
'<gl-emoji data-fallback-src="https://cultofthepartyparrot.com/parrots/hd/parrot.gif" data-name="party-parrot" data-unicode-version="custom"><img class="emoji" title=":party-parrot:" alt=":party-parrot:" src="https://cultofthepartyparrot.com/parrots/hd/parrot.gif" width="16" height="16" align="absmiddle"></gl-emoji>',
|
2022-06-21 17:19:12 +05:30
|
|
|
],
|
2020-07-28 23:09:34 +05:30
|
|
|
])('%s', (name, markup, withEmojiSupport, withoutEmojiSupport) => {
|
|
|
|
it(`renders correctly with emoji support`, async () => {
|
|
|
|
jest.spyOn(EmojiUnicodeSupport, 'default').mockReturnValue(true);
|
|
|
|
const glEmojiElement = markupToDomElement(markup);
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(glEmojiElement.outerHTML).toBe(withEmojiSupport);
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`renders correctly without emoji support`, async () => {
|
|
|
|
jest.spyOn(EmojiUnicodeSupport, 'default').mockReturnValue(false);
|
|
|
|
const glEmojiElement = markupToDomElement(markup);
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(glEmojiElement.outerHTML).toBe(withoutEmojiSupport);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-01-12 12:59:36 +05:30
|
|
|
it('escapes gl-emoji name', async () => {
|
|
|
|
const glEmojiElement = markupToDomElement(
|
|
|
|
"<gl-emoji data-name='"x="y" onload="alert(document.location.href)"' data-unicode-version='x'>abc</gl-emoji>",
|
|
|
|
);
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(glEmojiElement.outerHTML).toBe(
|
2023-06-20 00:43:36 +05:30
|
|
|
'<gl-emoji data-name=""x="y" onload="alert(document.location.href)"" data-unicode-version="x"><img class="emoji" title=":"x="y" onload="alert(document.location.href)":" alt=":"x="y" onload="alert(document.location.href)":" src="/-/emojis/2/grey_question.png" width="16" height="16" align="absmiddle"></gl-emoji>',
|
2022-01-12 12:59:36 +05:30
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it('Adds sprite CSS if emojis are not supported', async () => {
|
|
|
|
const testPath = '/test-path.css';
|
|
|
|
jest.spyOn(EmojiUnicodeSupport, 'default').mockReturnValue(false);
|
|
|
|
window.gon.emoji_sprites_css_path = testPath;
|
|
|
|
|
|
|
|
expect(document.head.querySelector(`link[href="${testPath}"]`)).toBe(null);
|
2022-08-27 11:52:29 +05:30
|
|
|
expect(window.gon.emoji_sprites_css_added).toBe(undefined);
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
markupToDomElement(
|
|
|
|
'<gl-emoji data-fallback-sprite-class="emoji-bomb" data-name="bomb"></gl-emoji>',
|
|
|
|
);
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(document.head.querySelector(`link[href="${testPath}"]`).outerHTML).toBe(
|
|
|
|
'<link rel="stylesheet" href="/test-path.css">',
|
|
|
|
);
|
|
|
|
expect(window.gon.emoji_sprites_css_added).toBe(true);
|
|
|
|
});
|
|
|
|
});
|