From ba53330afd26f596aff755e89a6dd917882fd38e Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 28 Mar 2022 13:31:03 +1100 Subject: [PATCH] add tests for getInitials --- .../AvatarPlaceHolder/AvatarPlaceHolder.tsx | 5 +- ts/test/session/unit/utils/Initials_test.ts | 82 +++++++++++++++++++ ts/util/getInitials.ts | 37 ++++++--- 3 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 ts/test/session/unit/utils/Initials_test.ts diff --git a/ts/components/avatar/AvatarPlaceHolder/AvatarPlaceHolder.tsx b/ts/components/avatar/AvatarPlaceHolder/AvatarPlaceHolder.tsx index 644647eef..f4baa8c11 100644 --- a/ts/components/avatar/AvatarPlaceHolder/AvatarPlaceHolder.tsx +++ b/ts/components/avatar/AvatarPlaceHolder/AvatarPlaceHolder.tsx @@ -97,10 +97,7 @@ export const AvatarPlaceHolder = (props: Props) => { ); } - let initials = getInitials(name)?.toLocaleUpperCase() || '0'; - if (name.indexOf(' ') === -1) { - initials = name.substring(0, 2).toLocaleUpperCase(); - } + const initials = getInitials(name); const fontSize = Math.floor(initials.length > 1 ? diameter * 0.4 : diameter * 0.5); diff --git a/ts/test/session/unit/utils/Initials_test.ts b/ts/test/session/unit/utils/Initials_test.ts new file mode 100644 index 000000000..8f1d26ab5 --- /dev/null +++ b/ts/test/session/unit/utils/Initials_test.ts @@ -0,0 +1,82 @@ +import { expect } from 'chai'; +import { getInitials } from '../../../../util/getInitials'; + +describe('getInitials', () => { + describe('empty of null string', () => { + it('initials: return undefined if string is undefined', () => { + expect(getInitials(undefined)).to.be.equal('0', 'should have return 0'); + }); + + it('initials: return undefined if string is empty', () => { + expect(getInitials('')).to.be.equal('0', 'should have return 0'); + }); + + it('initials: return undefined if string is null', () => { + expect(getInitials(null as any)).to.be.equal('0', 'should have return 0'); + }); + }); + + describe('name is a pubkey', () => { + it('initials: return the first char after 05 if it starts with 05 and has length >2 ', () => { + expect(getInitials('052')).to.be.equal('2', 'should have return 2'); + }); + + it('initials: return the first char after 05 capitalized if it starts with 05 and has length >2 ', () => { + expect(getInitials('05bcd')).to.be.equal('B', 'should have return B'); + }); + + it('initials: return the first char after 05 if it starts with 05 and has length >2 ', () => { + expect(getInitials('059052052052052052052052')).to.be.equal('9', 'should have return 9'); + }); + }); + + describe('name has a space in its content', () => { + it('initials: return the first char of each first 2 words if a space is present ', () => { + expect(getInitials('John Doe')).to.be.equal('JD', 'should have return JD'); + }); + + it('initials: return the first char capitalized of each first 2 words if a space is present ', () => { + expect(getInitials('John doe')).to.be.equal('JD', 'should have return JD capitalized'); + }); + + it('initials: return the first char capitalized of each first 2 words if a space is present, even with more than 2 words ', () => { + expect(getInitials('John Doe Alice')).to.be.equal('JD', 'should have return JD capitalized'); + }); + + it('initials: return the first char capitalized of each first 2 words if a space is present, even with more than 2 words ', () => { + expect(getInitials('John doe Alice')).to.be.equal('JD', 'should have return JD capitalized'); + }); + + describe('name is not ascii', () => { + // ß maps to SS in uppercase + it('initials: shorten to 2 char at most if the uppercase form length is > 2 ', () => { + expect(getInitials('John ß')).to.be.equal('JS', 'should have return JS capitalized'); + }); + + it('initials: shorten to 2 char at most if the uppercase form length is > 2 ', () => { + expect(getInitials('ß ß')).to.be.equal('SS', 'should have return SS capitalized'); + }); + }); + }); + + describe('name has NO spaces in its content', () => { + it('initials: return the first 2 chars of the first word if the name has no space ', () => { + expect(getInitials('JOHNY')).to.be.equal('JO', 'should have return JO'); + }); + + it('initials: return the first 2 chars capitalized of the first word if the name has no space ', () => { + expect(getInitials('Johnny')).to.be.equal('JO', 'should have return JO'); + }); + + describe('name is not ascii', () => { + // ß maps to SS in uppercase + it('initials: shorten to 2 char at most if the uppercase form length is > 2 ', () => { + expect(getInitials('ß')).to.be.equal('SS', 'should have return SS capitalized'); + }); + + it('initials: shorten to 2 char at most if the uppercase form length is > 2 ', () => { + expect(getInitials('ßß')).to.be.equal('SS', 'should have return SS capitalized'); + }); + }); + }); +}); diff --git a/ts/util/getInitials.ts b/ts/util/getInitials.ts index 8c7bb4777..c2ac667b8 100644 --- a/ts/util/getInitials.ts +++ b/ts/util/getInitials.ts @@ -1,18 +1,35 @@ -export function getInitials(name?: string): string | undefined { +export function getInitials(name?: string): string { if (!name || !name.length) { - return; + return '0'; } if (name.length > 2 && name.startsWith('05')) { - return name[2]; + // Just the third char of the pubkey when the name is a pubkey + return upperAndShorten(name[2]); } - const initials = name - .split(' ') - .slice(0, 2) - .map(n => { - return n[0]; - }); + if (name.indexOf(' ') === -1) { + // there is no space, just return the first 2 chars of the name - return initials.join(''); + if (name.length > 1) { + return upperAndShorten(name.slice(0, 2)); + } + return upperAndShorten(name[0]); + } + + // name has a space, just extract the first char of each words + return upperAndShorten( + name + .split(' ') + .slice(0, 2) + .map(n => { + return n[0]; + }) + .join('') + ); +} + +function upperAndShorten(str: string) { + // believe it or not, some chars put in uppercase can be more than one char. (ß for instance) + return str.toLocaleUpperCase().slice(0, 2); }