handle entries with no creator users

This commit is contained in:
Fernando Chavez 2023-09-01 13:14:29 +02:00
parent 4a7f4c4926
commit 2aeb3e8253
2 changed files with 57 additions and 2 deletions

View File

@ -6,9 +6,15 @@
* @property {string} username
* @property {string} email
*
* @type {(user: AdminUserNamesAttributes, formatMessage: import('react-intl').formatMessage) => string}
* @type {(user: AdminUserNamesAttributes | null, formatMessage: import('react-intl').formatMessage) => string}
*/
const getDisplayName = ({ firstname, lastname, username, email }, formatMessage) => {
const getDisplayName = (user, formatMessage) => {
if (!user) {
return '-';
}
const { firstname, lastname, username, email } = user;
if (username) {
return username;
}

View File

@ -0,0 +1,49 @@
import { getDisplayName } from '../getDisplayName';
const mockFormatMessage = jest.fn((message, values) => {
return `${values.firstname} ${values.lastname}`;
});
describe('getDisplayName', () => {
it('should return username if username is defined', () => {
expect(
getDisplayName(
{
username: 'johnDoe',
firstname: 'John',
lastname: 'Doe',
email: 'john@doe.com',
},
mockFormatMessage
)
).toEqual('johnDoe');
});
it('should return firstname and lastname if firstname is defined', () => {
expect(
getDisplayName(
{
firstname: 'John',
lastname: 'Doe',
email: 'john@doe.com',
},
mockFormatMessage
)
).toEqual('John Doe');
});
it('should return email if username and firstname are not defined', () => {
expect(
getDisplayName(
{
email: 'john@doe.com',
},
mockFormatMessage
)
).toEqual('john@doe.com');
});
it('should return a dash if user is null', () => {
expect(getDisplayName(null, mockFormatMessage)).toEqual('-');
});
});