import { initEmojiMock, clearEmojiMock } from 'helpers/emoji';
import waitForPromises from 'helpers/wait_for_promises';
import installGlEmojiElement from '~/behaviors/gl_emoji';
import { EMOJI_VERSION } from '~/emoji';
import * as EmojiUnicodeSupport from '~/emoji/support';
jest.mock('~/emoji/support');
describe('gl_emoji', () => {
const emojiData = {
grey_question: {
c: 'symbols',
e: '❔',
d: 'white question mark ornament',
u: '6.0',
},
bomb: {
c: 'objects',
e: '💣',
d: 'bomb',
u: '6.0',
},
};
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;
}
beforeEach(async () => {
await initEmojiMock(emojiData);
});
afterEach(() => {
clearEmojiMock();
document.body.innerHTML = '';
});
describe.each([
[
'bomb emoji just with name attribute',
'',
'💣',
`
`,
],
[
'bomb emoji with name attribute and unicode version',
'💣',
'💣',
`
`,
],
[
'bomb emoji with sprite fallback',
'',
'💣',
'💣',
],
[
'bomb emoji with image fallback',
'',
'💣',
'
',
],
[
'invalid emoji',
'',
'❔',
`
`,
],
[
'custom emoji with image fallback',
'',
'
',
'
',
],
])('%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);
});
});
it('escapes gl-emoji name', async () => {
const glEmojiElement = markupToDomElement(
"abc",
);
await waitForPromises();
expect(glEmojiElement.outerHTML).toBe(
'
',
);
});
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);
expect(window.gon.emoji_sprites_css_added).toBeFalsy();
markupToDomElement(
'',
);
await waitForPromises();
expect(document.head.querySelector(`link[href="${testPath}"]`).outerHTML).toBe(
'',
);
expect(window.gon.emoji_sprites_css_added).toBe(true);
});
});