Commit b328f7a8 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Update hook and component to use helper function for author names

parent 82003b96
import { isThirdPartyUser, username } from '../../helpers/account-id'; import { isThirdPartyUser, username } from '../../helpers/account-id';
import { annotationDisplayName } from '../../helpers/annotation-user';
import { withServices } from '../../service-context'; import { withServices } from '../../service-context';
/** /**
...@@ -27,15 +28,11 @@ function AnnotationUser({ annotation, features, serviceUrl, settings }) { ...@@ -27,15 +28,11 @@ function AnnotationUser({ annotation, features, serviceUrl, settings }) {
const username_ = username(user); const username_ = username(user);
// How should the user's name be displayed? // How should the user's name be displayed?
const displayName = (() => { const displayName = annotationDisplayName(
if (isFirstPartyUser && !features.flagEnabled('client_display_names')) { annotation,
return username_; !isFirstPartyUser,
} features.flagEnabled('client_display_names')
if (annotation.user_info && annotation.user_info.display_name) { );
return annotation.user_info.display_name;
}
return username_;
})();
const shouldLinkToActivity = isFirstPartyUser || settings.usernameUrl; const shouldLinkToActivity = isFirstPartyUser || settings.usernameUrl;
......
...@@ -7,6 +7,7 @@ import AnnotationUser, { $imports } from '../AnnotationUser'; ...@@ -7,6 +7,7 @@ import AnnotationUser, { $imports } from '../AnnotationUser';
describe('AnnotationUser', () => { describe('AnnotationUser', () => {
let fakeAnnotation; let fakeAnnotation;
let fakeAnnotationUser;
let fakeFeatures; let fakeFeatures;
let fakeIsThirdPartyUser; let fakeIsThirdPartyUser;
let fakeServiceUrl; let fakeServiceUrl;
...@@ -28,6 +29,11 @@ describe('AnnotationUser', () => { ...@@ -28,6 +29,11 @@ describe('AnnotationUser', () => {
fakeAnnotation = { fakeAnnotation = {
user: 'someone@hypothes.is', user: 'someone@hypothes.is',
}; };
fakeAnnotationUser = {
annotationDisplayName: sinon
.stub()
.callsFake(annotation => annotation.user),
};
fakeFeatures = { flagEnabled: sinon.stub() }; fakeFeatures = { flagEnabled: sinon.stub() };
fakeIsThirdPartyUser = sinon.stub().returns(false); fakeIsThirdPartyUser = sinon.stub().returns(false);
fakeServiceUrl = sinon.stub(); fakeServiceUrl = sinon.stub();
...@@ -40,6 +46,7 @@ describe('AnnotationUser', () => { ...@@ -40,6 +46,7 @@ describe('AnnotationUser', () => {
isThirdPartyUser: fakeIsThirdPartyUser, isThirdPartyUser: fakeIsThirdPartyUser,
username: fakeUsername, username: fakeUsername,
}, },
'../../helpers/annotation-user': fakeAnnotationUser,
}); });
}); });
...@@ -89,58 +96,17 @@ describe('AnnotationUser', () => { ...@@ -89,58 +96,17 @@ describe('AnnotationUser', () => {
}); });
}); });
describe('rendered user name', () => { it('renders the annotation author name', () => {
context('feature flag on', () => { fakeFeatures.flagEnabled.withArgs('client_display_names').returns(true);
beforeEach(() => {
fakeFeatures.flagEnabled.withArgs('client_display_names').returns(true);
});
it('should render a display name when feature flag on and info available', () => {
fakeAnnotation.user_info = {
display_name: 'Maple Oaks',
};
const wrapper = createAnnotationUser();
const linkEl = wrapper.find('a');
assert.equal(linkEl.text(), 'Maple Oaks');
});
it('should render a username when feature flag on but info not present', () => {
fakeUsername.returns('myusername');
const wrapper = createAnnotationUser(); createAnnotationUser();
const linkEl = wrapper.find('a');
assert.equal(linkEl.text(), 'myusername');
});
});
context('feature flag off', () => { assert.calledWith(
it('should render a username for first-party users when feature flag off', () => { fakeAnnotationUser.annotationDisplayName,
fakeFeatures.flagEnabled.returns(false); fakeAnnotation,
fakeUsername.returns('myusername'); false,
fakeAnnotation.user_info = { true
display_name: 'Maple Oaks', );
};
const wrapper = createAnnotationUser();
const linkEl = wrapper.find('a');
assert.equal(linkEl.text(), 'myusername');
});
it('should render a display name for third-party users', () => {
fakeAnnotation.user_info = {
display_name: 'Maple Oaks',
};
fakeIsThirdPartyUser.returns(true);
fakeFeatures.flagEnabled.returns(false);
const wrapper = createAnnotationUser();
assert.equal(wrapper.text(), 'Maple Oaks');
});
});
}); });
it( it(
......
...@@ -4,9 +4,17 @@ import { useUserFilterOptions } from '../use-filter-options'; ...@@ -4,9 +4,17 @@ import { useUserFilterOptions } from '../use-filter-options';
import { $imports } from '../use-filter-options'; import { $imports } from '../use-filter-options';
describe('sidebar/components/hooks/use-user-filter-options', () => { describe('sidebar/components/hooks/use-user-filter-options', () => {
let fakeAccountId;
let fakeStore; let fakeStore;
let fakeAnnotationUser;
let lastUserOptions; let lastUserOptions;
// Mock `annotationDisplayName` as if it's returning display names
let fakeAnnotationUserDisplay = annotation =>
annotation.user_info.display_name;
// Mock `annotationDisplayName` as if it's returning usernames
let fakeAnnotationUserUsername = annotation => annotation.user;
// Mount a dummy component to be able to use the hook // Mount a dummy component to be able to use the hook
function DummyComponent() { function DummyComponent() {
lastUserOptions = useUserFilterOptions(); lastUserOptions = useUserFilterOptions();
...@@ -15,33 +23,44 @@ describe('sidebar/components/hooks/use-user-filter-options', () => { ...@@ -15,33 +23,44 @@ describe('sidebar/components/hooks/use-user-filter-options', () => {
function annotationFixtures() { function annotationFixtures() {
return [ return [
{ {
user: 'acct:dingbat@localhost', user: 'dingbat',
user_info: { display_name: 'Ding Bat' }, user_info: { display_name: 'Ding Bat' },
}, },
{ {
user: 'acct:abalone@localhost', user: 'abalone',
user_info: { display_name: 'Aba Lone' }, user_info: { display_name: 'Aba Lone' },
}, },
{ {
user: 'acct:bananagram@localhost', user: 'bananagram',
user_info: { display_name: 'Zerk' }, user_info: { display_name: 'Zerk' },
}, },
{ {
user: 'acct:dingbat@localhost', user: 'dingbat',
user_info: { display_name: 'Ding Bat' }, user_info: { display_name: 'Ding Bat' },
}, },
]; ];
} }
beforeEach(() => { beforeEach(() => {
fakeAccountId = {
username: sinon.stub().returnsArg(0),
};
fakeAnnotationUser = {
annotationDisplayName: sinon.stub().callsFake(fakeAnnotationUserUsername),
};
fakeStore = { fakeStore = {
allAnnotations: sinon.stub().returns([]), allAnnotations: sinon.stub().returns([]),
authDomain: sinon.stub().returns('foo.com'),
getFocusFilters: sinon.stub().returns({}), getFocusFilters: sinon.stub().returns({}),
isFeatureEnabled: sinon.stub().returns(false), isFeatureEnabled: sinon.stub().returns(false),
profile: sinon.stub().returns({}), profile: sinon.stub().returns({}),
}; };
$imports.$mock({ $imports.$mock({
'../../helpers/account-id': fakeAccountId,
'../../helpers/annotation-user': fakeAnnotationUser,
'../../store/use-store': { useStoreProxy: () => fakeStore }, '../../store/use-store': { useStoreProxy: () => fakeStore },
}); });
}); });
...@@ -62,23 +81,10 @@ describe('sidebar/components/hooks/use-user-filter-options', () => { ...@@ -62,23 +81,10 @@ describe('sidebar/components/hooks/use-user-filter-options', () => {
]); ]);
}); });
it('should use display names if feature flag enabled', () => {
fakeStore.allAnnotations.returns(annotationFixtures());
fakeStore.isFeatureEnabled.withArgs('client_display_names').returns(true);
mount(<DummyComponent />);
assert.deepEqual(lastUserOptions, [
{ value: 'abalone', display: 'Aba Lone' },
{ value: 'dingbat', display: 'Ding Bat' },
{ value: 'bananagram', display: 'Zerk' },
]);
});
it('sorts the current user to the front with " (Me)" suffix', () => { it('sorts the current user to the front with " (Me)" suffix', () => {
fakeStore.allAnnotations.returns(annotationFixtures()); fakeStore.allAnnotations.returns(annotationFixtures());
fakeStore.profile.returns({ fakeStore.profile.returns({
userid: 'acct:bananagram@localhost', userid: 'bananagram',
}); });
mount(<DummyComponent />); mount(<DummyComponent />);
...@@ -114,7 +120,9 @@ describe('sidebar/components/hooks/use-user-filter-options', () => { ...@@ -114,7 +120,9 @@ describe('sidebar/components/hooks/use-user-filter-options', () => {
it('should add focused-user filter information', () => { it('should add focused-user filter information', () => {
fakeStore.allAnnotations.returns(annotationFixtures()); fakeStore.allAnnotations.returns(annotationFixtures());
fakeStore.isFeatureEnabled.withArgs('client_display_names').returns(true); fakeAnnotationUser.annotationDisplayName.callsFake(
fakeAnnotationUserDisplay
);
mount(<DummyComponent />); mount(<DummyComponent />);
...@@ -128,9 +136,6 @@ describe('sidebar/components/hooks/use-user-filter-options', () => { ...@@ -128,9 +136,6 @@ describe('sidebar/components/hooks/use-user-filter-options', () => {
it('always uses display name for focused user', () => { it('always uses display name for focused user', () => {
fakeStore.allAnnotations.returns(annotationFixtures()); fakeStore.allAnnotations.returns(annotationFixtures());
fakeStore.isFeatureEnabled
.withArgs('client_display_names')
.returns(false);
fakeStore.getFocusFilters.returns({ fakeStore.getFocusFilters.returns({
user: { value: 'carrotNumberOne', display: 'Numero Uno Zanahoria' }, user: { value: 'carrotNumberOne', display: 'Numero Uno Zanahoria' },
}); });
...@@ -146,10 +151,12 @@ describe('sidebar/components/hooks/use-user-filter-options', () => { ...@@ -146,10 +151,12 @@ describe('sidebar/components/hooks/use-user-filter-options', () => {
}); });
it('sorts the current user to the front with " (Me)" suffix', () => { it('sorts the current user to the front with " (Me)" suffix', () => {
fakeAnnotationUser.annotationDisplayName.callsFake(
fakeAnnotationUserDisplay
);
fakeStore.allAnnotations.returns(annotationFixtures()); fakeStore.allAnnotations.returns(annotationFixtures());
fakeStore.isFeatureEnabled.withArgs('client_display_names').returns(true);
fakeStore.profile.returns({ fakeStore.profile.returns({
userid: 'acct:bananagram@localhost', userid: 'bananagram',
}); });
mount(<DummyComponent />); mount(<DummyComponent />);
...@@ -164,9 +171,11 @@ describe('sidebar/components/hooks/use-user-filter-options', () => { ...@@ -164,9 +171,11 @@ describe('sidebar/components/hooks/use-user-filter-options', () => {
it('does not add (Me)" suffix if user has no annotations', () => { it('does not add (Me)" suffix if user has no annotations', () => {
fakeStore.allAnnotations.returns(annotationFixtures()); fakeStore.allAnnotations.returns(annotationFixtures());
fakeStore.isFeatureEnabled.withArgs('client_display_names').returns(true); fakeAnnotationUser.annotationDisplayName.callsFake(
fakeAnnotationUserDisplay
);
fakeStore.profile.returns({ fakeStore.profile.returns({
userid: 'acct:fakeid@localhost', userid: 'fakeid',
}); });
mount(<DummyComponent />); mount(<DummyComponent />);
......
import { useMemo } from 'preact/hooks'; import { useMemo } from 'preact/hooks';
import { useStoreProxy } from '../../store/use-store'; import { useStoreProxy } from '../../store/use-store';
import { username } from '../../helpers/account-id'; import { isThirdPartyUser, username } from '../../helpers/account-id';
import { annotationDisplayName } from '../../helpers/annotation-user';
/** @typedef {import('../../store/modules/filters').FilterOption} FilterOption */ /** @typedef {import('../../store/modules/filters').FilterOption} FilterOption */
...@@ -14,8 +15,9 @@ import { username } from '../../helpers/account-id'; ...@@ -14,8 +15,9 @@ import { username } from '../../helpers/account-id';
export function useUserFilterOptions() { export function useUserFilterOptions() {
const store = useStoreProxy(); const store = useStoreProxy();
const annotations = store.allAnnotations(); const annotations = store.allAnnotations();
const authDomain = store.authDomain();
const displayNamesEnabled = store.isFeatureEnabled('client_display_names');
const focusFilters = store.getFocusFilters(); const focusFilters = store.getFocusFilters();
const showDisplayNames = store.isFeatureEnabled('client_display_names');
const currentUsername = username(store.profile().userid); const currentUsername = username(store.profile().userid);
return useMemo(() => { return useMemo(() => {
...@@ -23,11 +25,11 @@ export function useUserFilterOptions() { ...@@ -23,11 +25,11 @@ export function useUserFilterOptions() {
const users = {}; const users = {};
annotations.forEach(annotation => { annotations.forEach(annotation => {
const username_ = username(annotation.user); const username_ = username(annotation.user);
const displayValue = users[username_] = annotationDisplayName(
showDisplayNames && annotation.user_info?.display_name annotation,
? annotation.user_info.display_name isThirdPartyUser(annotation.user, authDomain),
: username_; displayNamesEnabled
users[username_] = displayValue; );
}); });
// If user-focus is configured (even if not applied) add a filter // If user-focus is configured (even if not applied) add a filter
...@@ -60,5 +62,11 @@ export function useUserFilterOptions() { ...@@ -60,5 +62,11 @@ export function useUserFilterOptions() {
}); });
return userOptions; return userOptions;
}, [annotations, currentUsername, focusFilters.user, showDisplayNames]); }, [
annotations,
authDomain,
currentUsername,
displayNamesEnabled,
focusFilters.user,
]);
} }
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