Commit 82003b96 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Add `annotation-user` helper module

Add helper module with function for determining what name string to
use when rendering the author of a given annotation (display name or
username)
parent eadfcfda
/**
* @typedef {import("../../types/api").Annotation} Annotation
*/
import { username } from './account-id';
/**
* What string should we use to represent the author (user) of a given
* annotation: a display name or a username?
*
* The nice, human-readable display name should be used when a display_name
* is available on the annotation AND:
* - The author (user) associated with the annotation is a third-party user, OR
* - The `client_display_names` feature flag is enabled
*
* Return the string that should be used for display on an annotation: either the
* username or the display name.
*
* @param {Pick<Annotation, 'user'|'user_info'>} annotation
* @param {boolean} isThirdPartyUser - Is the annotation's user third-party?
* @param {boolean} isFeatureFlagEnabled - Is the `client_display_names`
* feature flag enabled
* @returns {string}
*/
export function annotationDisplayName(
annotation,
isThirdPartyUser,
isFeatureFlagEnabled
) {
const useDisplayName = isFeatureFlagEnabled || isThirdPartyUser;
return useDisplayName && annotation.user_info?.display_name
? annotation.user_info.display_name
: username(annotation.user);
}
import { annotationDisplayName } from '../annotation-user';
describe('sidebar/helpers/annotation-user', () => {
const fakeAnnotations = {
withDisplayName: {
user: 'acct:albert@victoriana.com',
user_info: { display_name: 'Albert, Prince Consort' },
},
noDisplayName: {
user: 'acct:albert@victoriana.com',
user_info: {},
},
noUserInfo: {
user: 'acct:albert@victoriana.com',
},
};
[
{
annotation: fakeAnnotations.withDisplayName,
isThirdParty: false,
isFeatureEnabled: false,
expected: 'albert',
},
{
annotation: fakeAnnotations.withDisplayName,
isThirdParty: true,
isFeatureEnabled: false,
expected: 'Albert, Prince Consort',
},
{
annotation: fakeAnnotations.withDisplayName,
isThirdParty: false,
isFeatureEnabled: true,
expected: 'Albert, Prince Consort',
},
{
annotation: fakeAnnotations.withDisplayName,
isThirdParty: true,
isFeatureEnabled: true,
expected: 'Albert, Prince Consort',
},
{
annotation: fakeAnnotations.noDisplayName,
isThirdParty: true,
isFeatureEnabled: true,
expected: 'albert',
},
{
annotation: fakeAnnotations.noUserInfo,
isThirdParty: true,
isFeatureEnabled: true,
expected: 'albert',
},
].forEach(testCase => {
it('should return the appropriate author string for an annotation', () => {
assert.equal(
annotationDisplayName(
testCase.annotation,
testCase.isThirdParty,
testCase.isFeatureEnabled
),
testCase.expected
);
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment