import MockAdapter from 'axios-mock-adapter';
import waitForPromises from 'helpers/wait_for_promises';
import installGlEmojiElement from '~/behaviors/gl_emoji';
import { initEmojiMap, EMOJI_VERSION } from '~/emoji';
import * as EmojiUnicodeSupport from '~/emoji/support';
import axios from '~/lib/utils/axios_utils';
jest.mock('~/emoji/support');
describe('gl_emoji', () => {
let mock;
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(() => {
mock = new MockAdapter(axios);
mock.onGet(`/-/emojis/${EMOJI_VERSION}/emojis.json`).reply(200, emojiData);
return initEmojiMap().catch(() => {});
});
afterEach(() => {
mock.restore();
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',
'',
'❔',
``,
],
])('%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('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);
});
});