emoji_helpers.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright © 2016-2019 Threema GmbH (https://threema.ch/).
  3. *
  4. * This file is part of Threema Web.
  5. *
  6. * Threema Web is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. import {emojify, enlargeSingleEmoji, shortnameToUnicode} from '../../src/helpers/emoji';
  20. describe('Emoji Helpers', () => {
  21. describe('emojify', () => {
  22. it('emojifies with img tag', function() {
  23. expect(emojify('hello 🐦'))
  24. .toEqual('hello <img class="em" draggable="false" '
  25. + 'alt="🐦" src="emoji/png32/1f426.png" data-c="1f426"/>');
  26. });
  27. it('ignores certain codepoints', function() {
  28. expect(emojify('©')).toEqual('©');
  29. expect(emojify('®')).toEqual('®');
  30. expect(emojify('™')).toEqual('™');
  31. });
  32. });
  33. describe('shortnameToUnicode', () => {
  34. it('converts valid shortnames', function() {
  35. expect(shortnameToUnicode('+1')).toEqual('\ud83d\udc4d');
  36. expect(shortnameToUnicode('thumbup')).toEqual('\ud83d\udc4d');
  37. expect(shortnameToUnicode('thumbsup')).toEqual('\ud83d\udc4d');
  38. });
  39. it('returns null for unknown shortcodes', function() {
  40. expect(shortnameToUnicode('sömbsöp')).toBeNull();
  41. });
  42. it('handles multi-codepoint emoji', function() {
  43. expect(shortnameToUnicode('ch')).toEqual('\ud83c\udde8\ud83c\udded');
  44. });
  45. });
  46. describe('enlargeSingleEmoji', function() {
  47. const process = (text) => {
  48. return enlargeSingleEmoji(text, true)
  49. };
  50. const singleEmojiClassName = 'large-emoji';
  51. const crazy = '<img class="em" draggable="false"'
  52. + ' alt="🤪" src="emoji/png32/1f92a.png" data-c="1f92a">';
  53. const crazyLarge = '<img class="em ' + singleEmojiClassName
  54. + '" draggable="false" alt="🤪" src="emoji/png64/1f92a.png" data-c="1f92a">';
  55. const copyright = '<img class="em anotherclass" draggable="false"'
  56. + ' alt="©️" src="emoji/png32/a9.png" data-c="a9">';
  57. const copyrightLarge = '<img class="em ' + singleEmojiClassName
  58. + ' anotherclass" draggable="false" alt="©️" src="emoji/png64/a9.png" data-c="a9">';
  59. it('enlarges 1 emoji', () => {
  60. expect(process(crazy)).toEqual(crazyLarge);
  61. });
  62. it('enlarges 2 emoji', () => {
  63. expect(process(crazy + copyright)).toEqual(crazyLarge + copyrightLarge);
  64. });
  65. it('enlarges 3 emoji', () => {
  66. expect(process(crazy + copyright + crazy)).toEqual(crazyLarge + copyrightLarge + crazyLarge);
  67. });
  68. it('does not enlarge 4 emoji', () => {
  69. expect(process(crazy + copyright + crazy + copyright)).toEqual(crazy + copyright + crazy + copyright);
  70. });
  71. it('does not enlarge if non-emoji characters are contained', () => {
  72. expect(process(crazy + ' ')).toEqual(crazy + ' ');
  73. expect(process(crazy + 'a' + crazy)).toEqual(crazy + 'a' + crazy);
  74. });
  75. it('does not modify non emoji text', () => {
  76. const text = 'emoji e1 e1-people em em-people hello';
  77. expect(process(text)).toEqual(text);
  78. });
  79. it('does nothing if enlarge flag is set to false', () => {
  80. expect(enlargeSingleEmoji(crazy, false)).toEqual(crazy);
  81. });
  82. });
  83. });