Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
b328f7a8
Commit
b328f7a8
authored
Mar 12, 2021
by
Lyza Danger Gardner
Committed by
Lyza Gardner
Mar 15, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update hook and component to use helper function for author names
parent
82003b96
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
93 deletions
+73
-93
AnnotationUser.js
src/sidebar/components/Annotation/AnnotationUser.js
+6
-9
AnnotationUser-test.js
...sidebar/components/Annotation/test/AnnotationUser-test.js
+16
-50
use-filter-options-test.js
src/sidebar/components/hooks/test/use-filter-options-test.js
+35
-26
use-filter-options.js
src/sidebar/components/hooks/use-filter-options.js
+16
-8
No files found.
src/sidebar/components/Annotation/AnnotationUser.js
View file @
b328f7a8
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
;
...
...
src/sidebar/components/Annotation/test/AnnotationUser-test.js
View file @
b328f7a8
...
@@ -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
(
...
...
src/sidebar/components/hooks/test/use-filter-options-test.js
View file @
b328f7a8
...
@@ -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@localhos
t'
,
user
:
'
dingba
t'
,
user_info
:
{
display_name
:
'Ding Bat'
},
user_info
:
{
display_name
:
'Ding Bat'
},
},
},
{
{
user
:
'a
cct:abalone@localhost
'
,
user
:
'a
balone
'
,
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@localhos
t'
,
user
:
'
dingba
t'
,
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
/>
);
...
...
src/sidebar/components/hooks/use-filter-options.js
View file @
b328f7a8
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
,
]);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment