Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gl_emoji_spec.js « behaviors « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c7f4fce0e4c1319cefc377edd6741304269a70fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { initEmojiMock, clearEmojiMock } from 'helpers/emoji';
import waitForPromises from 'helpers/wait_for_promises';
import { createMockClient } from 'helpers/mock_apollo_helper';
import installGlEmojiElement from '~/behaviors/gl_emoji';
import { EMOJI_VERSION } from '~/emoji';
import customEmojiQuery from '~/emoji/queries/custom_emoji.query.graphql';

import * as EmojiUnicodeSupport from '~/emoji/support';

let mockClient;

jest.mock('~/emoji/support');
jest.mock('~/lib/graphql', () => {
  return () => mockClient;
});

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;
  }

  afterEach(() => {
    clearEmojiMock();

    document.body.innerHTML = '';
  });

  describe('standard emoji', () => {
    beforeEach(async () => {
      await initEmojiMock(emojiData);
    });

    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>',
        `<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" align="absmiddle"></gl-emoji>`,
      ],
      [
        '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>',
        `<gl-emoji data-name="bomb" data-unicode-version="6.0"><img class="emoji" title=":bomb:" alt=":bomb:" src="/-/emojis/${EMOJI_VERSION}/bomb.png" align="absmiddle"></gl-emoji>`,
      ],
      [
        '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>',
        '<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" align="absmiddle"></gl-emoji>',
      ],
      [
        '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>',
        `<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" align="absmiddle"></gl-emoji>`,
      ],
      [
        '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>',
        '<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" 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" align="absmiddle"></gl-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('escapes gl-emoji name', async () => {
      const glEmojiElement = markupToDomElement(
        "<gl-emoji data-name='&#34;x=&#34y&#34 onload=&#34;alert(document.location.href)&#34;' data-unicode-version='x'>abc</gl-emoji>",
      );

      await waitForPromises();

      expect(glEmojiElement.outerHTML).toBe(
        '<gl-emoji data-name="&quot;x=&quot;y&quot; onload=&quot;alert(document.location.href)&quot;" data-unicode-version="x"><img class="emoji" title=":&quot;x=&quot;y&quot; onload=&quot;alert(document.location.href)&quot;:" alt=":&quot;x=&quot;y&quot; onload=&quot;alert(document.location.href)&quot;:" src="/-/emojis/2/grey_question.png" align="absmiddle"></gl-emoji>',
      );
    });

    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).toBe(undefined);

      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);
    });
  });

  describe('custom emoji', () => {
    beforeEach(async () => {
      mockClient = createMockClient([
        [
          customEmojiQuery,
          jest.fn().mockResolvedValue({
            data: {
              group: {
                id: 1,
                customEmoji: {
                  nodes: [{ id: 1, name: 'parrot', url: 'parrot.gif' }],
                },
              },
            },
          }),
        ],
      ]);

      window.gon = { features: { customEmoji: true } };
      document.body.dataset.groupFullPath = 'test-group';

      await initEmojiMock(emojiData);
    });

    afterEach(() => {
      window.gon = {};
      delete document.body.dataset.groupFullPath;
    });

    it('renders custom emoji', async () => {
      const glEmojiElement = markupToDomElement('<gl-emoji data-name="parrot"></gl-emoji>');

      await waitForPromises();

      const img = glEmojiElement.querySelector('img');

      expect(glEmojiElement.dataset.unicodeVersion).toBe('custom');
      expect(img.getAttribute('src')).toBe('parrot.gif');
    });
  });
});