From fd1657037adc56bfe619a6a6fe0defd4db896dd3 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 28 Mar 2022 14:09:48 +1100 Subject: [PATCH] add tests for emoji size rendering in messages --- ts/test/session/unit/utils/Emoji_test.ts | 132 ++++++++++++++++++++ ts/test/session/unit/utils/Initials_test.ts | 2 +- ts/util/emoji.ts | 7 +- 3 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 ts/test/session/unit/utils/Emoji_test.ts diff --git a/ts/test/session/unit/utils/Emoji_test.ts b/ts/test/session/unit/utils/Emoji_test.ts new file mode 100644 index 000000000..9391a08b5 --- /dev/null +++ b/ts/test/session/unit/utils/Emoji_test.ts @@ -0,0 +1,132 @@ +import { expect } from 'chai'; +import { getEmojiSizeClass } from '../../../../util/emoji'; + +describe('getEmojiSizeClass', () => { + describe('empty or null string', () => { + it('undefined as string', () => { + expect(getEmojiSizeClass(undefined as any)).to.be.equal('small', 'should have return small'); + }); + it('null as string', () => { + expect(getEmojiSizeClass(null as any)).to.be.equal('small', 'should have return small'); + }); + + it('empty string', () => { + expect(getEmojiSizeClass('')).to.be.equal('small', 'should have return small'); + }); + }); + + describe('with only characters not emojis of ascii/utf8', () => { + it('string of ascii only', () => { + expect( + getEmojiSizeClass('The ASCII compatible UTF-8 encoding of ISO 10646 and Unicode') + ).to.be.equal('small', 'should have return small'); + }); + + it('string of utf8 with weird chars but no', () => { + expect(getEmojiSizeClass('ASCII safety test: 1lI|, 0OD, 8B')).to.be.equal( + 'small', + 'should have return small' + ); + }); + + it('string of utf8 with weird chars', () => { + // taken from https://www.w3.org/2001/06/utf-8-test/UTF-8-demo.html + expect( + getEmojiSizeClass('ASCII safety test: 1lI|, 0OD, 8B, γιγνώσκειν, ὦ ἄνδρες დასასწრებად') + ).to.be.equal('small', 'should have return small'); + }); + + it('short string of utf8 with weird chars', () => { + // taken from https://www.w3.org/2001/06/utf-8-test/UTF-8-demo.html + expect(getEmojiSizeClass('დ')).to.be.equal('small', 'should have return small'); + }); + }); + + describe('with string containing utf8 emojis', () => { + describe('with string containing utf8 emojis and normal characters', () => { + it('one emoji after a normal sentence', () => { + expect( + getEmojiSizeClass('The SMILING FACE WITH HORNS character (😈) is assigned') + ).to.be.equal('small', 'should have return small'); + }); + + it('multiple emoji after a normal sentence', () => { + expect( + getEmojiSizeClass('The SMILING FACE WITH HORNS character (😈) is assigned 😈 😈') + ).to.be.equal('small', 'should have return small'); + }); + + it('multiple emoji before a normal sentence', () => { + expect( + getEmojiSizeClass('😈 😈The SMILING FACE WITH HORNS character () is assigned') + ).to.be.equal('small', 'should have return small'); + }); + + it('one emoji with just a space after', () => { + expect(getEmojiSizeClass('😈 ')).to.be.equal('jumbo', 'should have return jumbo'); + }); + + it('one emoji with just a space before', () => { + expect(getEmojiSizeClass(' 😈')).to.be.equal('jumbo', 'should have return jumbo'); + }); + + it('one emoji with just a space before & after', () => { + expect(getEmojiSizeClass(' 😈 ')).to.be.equal('jumbo', 'should have return jumbo'); + }); + }); + describe('with string containing only emojis ', () => { + it('one emoji without other characters', () => { + expect(getEmojiSizeClass('😈')).to.be.equal('jumbo', 'should have return jumbo'); + }); + + it('two emoji without other characters', () => { + expect(getEmojiSizeClass('😈😈')).to.be.equal('jumbo', 'should have return jumbo'); + }); + + it('3 emoji without other characters', () => { + expect(getEmojiSizeClass('😈😈😈')).to.be.equal('large', 'should have return large'); + }); + + it('4 emoji without other characters', () => { + expect(getEmojiSizeClass('😈😈😈😈')).to.be.equal('large', 'should have return large'); + }); + + it('5 emoji without other characters', () => { + expect(getEmojiSizeClass('😈😈😈😈😈')).to.be.equal('medium', 'should have return medium'); + }); + + it('6 emoji without other characters', () => { + expect(getEmojiSizeClass('😈😈😈😈😈😈')).to.be.equal( + 'medium', + 'should have return medium' + ); + }); + + it('7 emoji without other characters', () => { + expect(getEmojiSizeClass('😈😈😈😈😈😈😈')).to.be.equal( + 'small', + 'should have return small' + ); + }); + + it('lots of emojis without other characters', () => { + expect( + getEmojiSizeClass( + '😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈😈' + ) + ).to.be.equal('small', 'should have return small'); + }); + + it('lots of emojis without other characters except space', () => { + expect(getEmojiSizeClass('😈😈😈😈😈😈😈😈😈😈😈 😈😈 😈😈 😈😈 ')).to.be.equal( + 'small', + 'should have return small' + ); + }); + + it('3 emojis without other characters except space', () => { + expect(getEmojiSizeClass('😈 😈 😈 ')).to.be.equal('large', 'should have return small'); + }); + }); + }); +}); diff --git a/ts/test/session/unit/utils/Initials_test.ts b/ts/test/session/unit/utils/Initials_test.ts index 8f1d26ab5..e86cb4376 100644 --- a/ts/test/session/unit/utils/Initials_test.ts +++ b/ts/test/session/unit/utils/Initials_test.ts @@ -2,7 +2,7 @@ import { expect } from 'chai'; import { getInitials } from '../../../../util/getInitials'; describe('getInitials', () => { - describe('empty of null string', () => { + describe('empty or null string', () => { it('initials: return undefined if string is undefined', () => { expect(getInitials(undefined)).to.be.equal('0', 'should have return 0'); }); diff --git a/ts/util/emoji.ts b/ts/util/emoji.ts index 1f171a621..90357d5e4 100644 --- a/ts/util/emoji.ts +++ b/ts/util/emoji.ts @@ -18,14 +18,15 @@ function hasNormalCharacters(str: string) { } export function getEmojiSizeClass(str: string): SizeClassType { + if (!str || !str.length) { + return 'small'; + } if (hasNormalCharacters(str)) { return 'small'; } const emojiCount = getCountOfAllMatches(str); - if (emojiCount > 8) { - return 'small'; - } else if (emojiCount > 6) { + if (emojiCount > 6) { return 'small'; } else if (emojiCount > 4) { return 'medium';