summaryrefslogtreecommitdiff
path: root/spec/frontend/emoji_spec.js
blob: 25bc95e0dd6d4de629f11ab84f37a6d599dd9568 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import { glEmojiTag } from '~/emoji';
import isEmojiUnicodeSupported, {
  isFlagEmoji,
  isRainbowFlagEmoji,
  isKeycapEmoji,
  isSkinToneComboEmoji,
  isHorceRacingSkinToneComboEmoji,
  isPersonZwjEmoji,
} from '~/emoji/support/is_emoji_unicode_supported';

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

const emojiFixtureMap = {
  bomb: {
    name: 'bomb',
    moji: 'πŸ’£',
    unicodeVersion: '6.0',
  },
  construction_worker_tone5: {
    name: 'construction_worker_tone5',
    moji: 'πŸ‘·πŸΏ',
    unicodeVersion: '8.0',
  },
  five: {
    name: 'five',
    moji: '5️⃣',
    unicodeVersion: '3.0',
  },
  grey_question: {
    name: 'grey_question',
    moji: '❔',
    unicodeVersion: '6.0',
  },
};

function markupToDomElement(markup) {
  const div = document.createElement('div');
  div.innerHTML = markup;
  return div.firstElementChild;
}

function testGlEmojiImageFallback(element, name, src) {
  expect(element.tagName.toLowerCase()).toBe('img');
  expect(element.getAttribute('src')).toBe(src);
  expect(element.getAttribute('title')).toBe(`:${name}:`);
  expect(element.getAttribute('alt')).toBe(`:${name}:`);
}

const defaults = {
  forceFallback: false,
  sprite: false,
};

function testGlEmojiElement(element, name, unicodeVersion, unicodeMoji, options = {}) {
  const opts = { ...defaults, ...options };
  expect(element.tagName.toLowerCase()).toBe('gl-emoji');
  expect(element.dataset.name).toBe(name);
  expect(element.dataset.fallbackSrc.length).toBeGreaterThan(0);
  expect(element.dataset.unicodeVersion).toBe(unicodeVersion);

  const fallbackSpriteClass = `emoji-${name}`;
  if (opts.sprite) {
    expect(element.dataset.fallbackSpriteClass).toBe(fallbackSpriteClass);
  }

  if (opts.forceFallback && opts.sprite) {
    expect(element.getAttribute('class')).toBe(`emoji-icon ${fallbackSpriteClass}`);
  }

  if (opts.forceFallback && !opts.sprite) {
    // Check for image fallback
    testGlEmojiImageFallback(element.firstElementChild, name, element.dataset.fallbackSrc);
  } else {
    // Otherwise make sure things are still unicode text
    expect(element.textContent.trim()).toBe(unicodeMoji);
  }
}

describe('gl_emoji', () => {
  describe('glEmojiTag', () => {
    it('bomb emoji', () => {
      const emojiKey = 'bomb';
      const markup = glEmojiTag(emojiFixtureMap[emojiKey].name);
      const glEmojiElement = markupToDomElement(markup);
      testGlEmojiElement(
        glEmojiElement,
        emojiFixtureMap[emojiKey].name,
        emojiFixtureMap[emojiKey].unicodeVersion,
        emojiFixtureMap[emojiKey].moji,
      );
    });

    it('bomb emoji with image fallback', () => {
      const emojiKey = 'bomb';
      const markup = glEmojiTag(emojiFixtureMap[emojiKey].name, {
        forceFallback: true,
      });
      const glEmojiElement = markupToDomElement(markup);
      testGlEmojiElement(
        glEmojiElement,
        emojiFixtureMap[emojiKey].name,
        emojiFixtureMap[emojiKey].unicodeVersion,
        emojiFixtureMap[emojiKey].moji,
        {
          forceFallback: true,
        },
      );
    });

    it('bomb emoji with sprite fallback readiness', () => {
      const emojiKey = 'bomb';
      const markup = glEmojiTag(emojiFixtureMap[emojiKey].name, {
        sprite: true,
      });
      const glEmojiElement = markupToDomElement(markup);
      testGlEmojiElement(
        glEmojiElement,
        emojiFixtureMap[emojiKey].name,
        emojiFixtureMap[emojiKey].unicodeVersion,
        emojiFixtureMap[emojiKey].moji,
        {
          sprite: true,
        },
      );
    });

    it('bomb emoji with sprite fallback', () => {
      const emojiKey = 'bomb';
      const markup = glEmojiTag(emojiFixtureMap[emojiKey].name, {
        forceFallback: true,
        sprite: true,
      });
      const glEmojiElement = markupToDomElement(markup);
      testGlEmojiElement(
        glEmojiElement,
        emojiFixtureMap[emojiKey].name,
        emojiFixtureMap[emojiKey].unicodeVersion,
        emojiFixtureMap[emojiKey].moji,
        {
          forceFallback: true,
          sprite: true,
        },
      );
    });

    it('question mark when invalid emoji name given', () => {
      const name = 'invalid_emoji';
      const emojiKey = 'grey_question';
      const markup = glEmojiTag(name);
      const glEmojiElement = markupToDomElement(markup);
      testGlEmojiElement(
        glEmojiElement,
        emojiFixtureMap[emojiKey].name,
        emojiFixtureMap[emojiKey].unicodeVersion,
        emojiFixtureMap[emojiKey].moji,
      );
    });

    it('question mark with image fallback when invalid emoji name given', () => {
      const name = 'invalid_emoji';
      const emojiKey = 'grey_question';
      const markup = glEmojiTag(name, {
        forceFallback: true,
      });
      const glEmojiElement = markupToDomElement(markup);
      testGlEmojiElement(
        glEmojiElement,
        emojiFixtureMap[emojiKey].name,
        emojiFixtureMap[emojiKey].unicodeVersion,
        emojiFixtureMap[emojiKey].moji,
        {
          forceFallback: true,
        },
      );
    });
  });

  describe('isFlagEmoji', () => {
    it('should gracefully handle empty string', () => {
      expect(isFlagEmoji('')).toBeFalsy();
    });

    it('should detect flag_ac', () => {
      expect(isFlagEmoji('πŸ‡¦πŸ‡¨')).toBeTruthy();
    });

    it('should detect flag_us', () => {
      expect(isFlagEmoji('πŸ‡ΊπŸ‡Έ')).toBeTruthy();
    });

    it('should detect flag_zw', () => {
      expect(isFlagEmoji('πŸ‡ΏπŸ‡Ό')).toBeTruthy();
    });

    it('should not detect flags', () => {
      expect(isFlagEmoji('🎏')).toBeFalsy();
    });

    it('should not detect triangular_flag_on_post', () => {
      expect(isFlagEmoji('🚩')).toBeFalsy();
    });

    it('should not detect single letter', () => {
      expect(isFlagEmoji('πŸ‡¦')).toBeFalsy();
    });

    it('should not detect >2 letters', () => {
      expect(isFlagEmoji('πŸ‡¦πŸ‡§πŸ‡¨')).toBeFalsy();
    });
  });

  describe('isRainbowFlagEmoji', () => {
    it('should gracefully handle empty string', () => {
      expect(isRainbowFlagEmoji('')).toBeFalsy();
    });

    it('should detect rainbow_flag', () => {
      expect(isRainbowFlagEmoji('🏳🌈')).toBeTruthy();
    });

    it("should not detect flag_white on its' own", () => {
      expect(isRainbowFlagEmoji('🏳')).toBeFalsy();
    });

    it("should not detect rainbow on its' own", () => {
      expect(isRainbowFlagEmoji('🌈')).toBeFalsy();
    });

    it('should not detect flag_white with something else', () => {
      expect(isRainbowFlagEmoji('πŸ³πŸ”΅')).toBeFalsy();
    });
  });

  describe('isKeycapEmoji', () => {
    it('should gracefully handle empty string', () => {
      expect(isKeycapEmoji('')).toBeFalsy();
    });

    it('should detect one(keycap)', () => {
      expect(isKeycapEmoji('1️⃣')).toBeTruthy();
    });

    it('should detect nine(keycap)', () => {
      expect(isKeycapEmoji('9️⃣')).toBeTruthy();
    });

    it('should not detect ten(keycap)', () => {
      expect(isKeycapEmoji('πŸ”Ÿ')).toBeFalsy();
    });

    it('should not detect hash(keycap)', () => {
      expect(isKeycapEmoji('#⃣')).toBeFalsy();
    });
  });

  describe('isSkinToneComboEmoji', () => {
    it('should gracefully handle empty string', () => {
      expect(isSkinToneComboEmoji('')).toBeFalsy();
    });

    it('should detect hand_splayed_tone5', () => {
      expect(isSkinToneComboEmoji('πŸ–πŸΏ')).toBeTruthy();
    });

    it('should not detect hand_splayed', () => {
      expect(isSkinToneComboEmoji('πŸ–')).toBeFalsy();
    });

    it('should detect lifter_tone1', () => {
      expect(isSkinToneComboEmoji('πŸ‹πŸ»')).toBeTruthy();
    });

    it('should not detect lifter', () => {
      expect(isSkinToneComboEmoji('πŸ‹')).toBeFalsy();
    });

    it('should detect rowboat_tone4', () => {
      expect(isSkinToneComboEmoji('🚣🏾')).toBeTruthy();
    });

    it('should not detect rowboat', () => {
      expect(isSkinToneComboEmoji('🚣')).toBeFalsy();
    });

    it('should not detect individual tone emoji', () => {
      expect(isSkinToneComboEmoji('🏻')).toBeFalsy();
    });
  });

  describe('isHorceRacingSkinToneComboEmoji', () => {
    it('should gracefully handle empty string', () => {
      expect(isHorceRacingSkinToneComboEmoji('')).toBeFalsy();
    });

    it('should detect horse_racing_tone2', () => {
      expect(isHorceRacingSkinToneComboEmoji('πŸ‡πŸΌ')).toBeTruthy();
    });

    it('should not detect horse_racing', () => {
      expect(isHorceRacingSkinToneComboEmoji('πŸ‡')).toBeFalsy();
    });
  });

  describe('isPersonZwjEmoji', () => {
    it('should gracefully handle empty string', () => {
      expect(isPersonZwjEmoji('')).toBeFalsy();
    });

    it('should detect couple_mm', () => {
      expect(isPersonZwjEmoji('πŸ‘¨β€β€οΈβ€πŸ‘¨')).toBeTruthy();
    });

    it('should not detect couple_with_heart', () => {
      expect(isPersonZwjEmoji('πŸ’‘')).toBeFalsy();
    });

    it('should not detect couplekiss', () => {
      expect(isPersonZwjEmoji('πŸ’')).toBeFalsy();
    });

    it('should detect family_mmb', () => {
      expect(isPersonZwjEmoji('πŸ‘¨β€πŸ‘¨β€πŸ‘¦')).toBeTruthy();
    });

    it('should detect family_mwgb', () => {
      expect(isPersonZwjEmoji('πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦')).toBeTruthy();
    });

    it('should not detect family', () => {
      expect(isPersonZwjEmoji('πŸ‘ͺ')).toBeFalsy();
    });

    it('should detect kiss_ww', () => {
      expect(isPersonZwjEmoji('πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘©')).toBeTruthy();
    });

    it('should not detect girl', () => {
      expect(isPersonZwjEmoji('πŸ‘§')).toBeFalsy();
    });

    it('should not detect girl_tone5', () => {
      expect(isPersonZwjEmoji('πŸ‘§πŸΏ')).toBeFalsy();
    });

    it('should not detect man', () => {
      expect(isPersonZwjEmoji('πŸ‘¨')).toBeFalsy();
    });

    it('should not detect woman', () => {
      expect(isPersonZwjEmoji('πŸ‘©')).toBeFalsy();
    });
  });

  describe('isEmojiUnicodeSupported', () => {
    it('should gracefully handle empty string with unicode support', () => {
      const isSupported = isEmojiUnicodeSupported({ '1.0': true }, '', '1.0');

      expect(isSupported).toBeTruthy();
    });

    it('should gracefully handle empty string without unicode support', () => {
      const isSupported = isEmojiUnicodeSupported({}, '', '1.0');

      expect(isSupported).toBeFalsy();
    });

    it('bomb(6.0) with 6.0 support', () => {
      const emojiKey = 'bomb';
      const unicodeSupportMap = { ...emptySupportMap, '6.0': true };
      const isSupported = isEmojiUnicodeSupported(
        unicodeSupportMap,
        emojiFixtureMap[emojiKey].moji,
        emojiFixtureMap[emojiKey].unicodeVersion,
      );

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

      expect(isSupported).toBeFalsy();
    });

    it('bomb(6.0) without 6.0 but with 9.0 support', () => {
      const emojiKey = 'bomb';
      const unicodeSupportMap = { ...emptySupportMap, '9.0': true };
      const isSupported = isEmojiUnicodeSupported(
        unicodeSupportMap,
        emojiFixtureMap[emojiKey].moji,
        emojiFixtureMap[emojiKey].unicodeVersion,
      );

      expect(isSupported).toBeFalsy();
    });

    it('construction_worker_tone5(8.0) without skin tone modifier support', () => {
      const emojiKey = 'construction_worker_tone5';
      const unicodeSupportMap = {
        ...emptySupportMap,
        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,
      };
      const isSupported = isEmojiUnicodeSupported(
        unicodeSupportMap,
        emojiFixtureMap[emojiKey].moji,
        emojiFixtureMap[emojiKey].unicodeVersion,
      );

      expect(isSupported).toBeFalsy();
    });

    it('use native keycap on >=57 chrome', () => {
      const emojiKey = 'five';
      const unicodeSupportMap = {
        ...emptySupportMap,
        '3.0': true,
        meta: {
          isChrome: true,
          chromeVersion: 57,
        },
      };
      const isSupported = isEmojiUnicodeSupported(
        unicodeSupportMap,
        emojiFixtureMap[emojiKey].moji,
        emojiFixtureMap[emojiKey].unicodeVersion,
      );

      expect(isSupported).toBeTruthy();
    });

    it('fallback keycap on <57 chrome', () => {
      const emojiKey = 'five';
      const unicodeSupportMap = {
        ...emptySupportMap,
        '3.0': true,
        meta: {
          isChrome: true,
          chromeVersion: 50,
        },
      };
      const isSupported = isEmojiUnicodeSupported(
        unicodeSupportMap,
        emojiFixtureMap[emojiKey].moji,
        emojiFixtureMap[emojiKey].unicodeVersion,
      );

      expect(isSupported).toBeFalsy();
    });
  });
});