Skip to content

Commit

Permalink
fix(avatars): Add type to Fetch avatar (#3560)
Browse files Browse the repository at this point in the history
* fix(avatars): Add type for fetchAvatarUrl calls in USM

* fix(avatars): Add type for fetchAvatarUrl calls in USM

* fix(avatars): Add type for fetchAvatarUrl calls in USM

* fix(avatars): Add type for fetchAvatarUrl calls in USM

* fix(avatars): Add type for fetchAvatarUrl calls in USM

* fix(avatars): Add type for fetchAvatarUrl calls in USM

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
shahzadaziz and mergify[bot] authored May 30, 2024
1 parent f07d616 commit 14a194f
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/components/contact-datalist-item/ContactDatalistItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ContactDatalistItemProps {
name: string | null | undefined;
showAvatar?: boolean;
subtitle?: React.ReactNode;
type?: string | null | undefined;
}

interface ContactDatalistItemState {
Expand Down Expand Up @@ -48,11 +49,12 @@ class ContactDatalistItem extends React.PureComponent<ContactDatalistItemProps,
* @return {void}
*/
getAvatarUrl() {
const { getContactAvatarUrl, id } = this.props;
const { getContactAvatarUrl, id, type } = this.props;
Promise.resolve(
getContactAvatarUrl && id
? getContactAvatarUrl({
id,
type,
})
: undefined,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,34 @@ describe('components/contact-datalist-item/ContactDatalistItem', () => {
expect(wrapper.find(Avatar).length).toBe(1);
expect(wrapper.find(Avatar).props().avatarUrl).toBeUndefined();
});

test.each([['user'], [undefined], [null]])('should have avatar URL when the type prop is %s', type => {
const contactID = '123';
const getContactAvatarUrlMock = jest
.fn()
.mockImplementation(() => Promise.resolve(`/test?id=${contactID}`));
const wrapper = shallow(
<ContactDatalistItem
name="name"
id={contactID}
type={type}
showAvatar
getContactAvatarUrl={getContactAvatarUrlMock}
/>,
);
expect(wrapper.state('avatarUrl')).toBe(undefined);
const instance = wrapper.instance();

if (instance.componentDidMount) {
instance.componentDidMount();
}

setImmediate(() => {
wrapper.update();
expect(wrapper.find('LabelPillIcon').length).toBe(2);
expect(wrapper.find('LabelPillIcon[avatarUrl]').length).toBe(1);
expect(getContactAvatarUrlMock).toHaveBeenCalledWith({ id: contactID, type });
});
});
});
});
1 change: 1 addition & 0 deletions src/components/pill-selector-dropdown/PillSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ class PillSelectorBase extends React.Component<Props, State> {
id={option.id}
hasWarning={option.hasWarning}
isExternal={option.isExternalUser}
type={option.type}
/>
);
})
Expand Down
5 changes: 3 additions & 2 deletions src/components/pill-selector-dropdown/RoundPill.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Props = {
onRemove: () => any,
showAvatar?: boolean,
text: string,
type?: string | null,
};

type State = {
Expand Down Expand Up @@ -82,8 +83,8 @@ class RoundPill extends React.PureComponent<Props, State> {
* @return {void}
*/
getAvatarUrl() {
const { getPillImageUrl, id } = this.props;
Promise.resolve(getPillImageUrl && id ? getPillImageUrl({ id }) : undefined)
const { getPillImageUrl, id, type } = this.props;
Promise.resolve(getPillImageUrl && id ? getPillImageUrl({ id, type }) : undefined)
.then(this.getAvatarUrlHandler)
.catch(() => {
// noop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';

import RoundPill from '../RoundPill';

describe('components/RoundPill-selector-dropdown/RoundPill', () => {
Expand Down Expand Up @@ -113,4 +112,23 @@ describe('components/RoundPill-selector-dropdown/RoundPill', () => {
expect(wrapper.find('LabelPillIcon').length).toBe(2);
expect(wrapper.find('LabelPillIcon[avatarUrl]').length).toBe(0);
});

test.each([['user'], [undefined], [null]])('should have avatar URL when the type prop is %s', type => {
const contactID = '123';
const getPillImageUrlMock = jest.fn().mockImplementation(() => Promise.resolve(`/test?id=${contactID}`));
const wrapper = shallow(
<RoundPill name="name" id={contactID} type={type} showAvatar getPillImageUrl={getPillImageUrlMock} />,
);
expect(wrapper.state('avatarUrl')).toBe(undefined);
const instance = wrapper.instance();

instance.componentDidMount();

setImmediate(() => {
wrapper.update();
expect(wrapper.find('LabelPillIcon').length).toBe(2);
expect(wrapper.find('LabelPillIcon[avatarUrl]').length).toBe(1);
expect(getPillImageUrlMock).toHaveBeenCalledWith({ id: contactID, type });
});
});
});
1 change: 1 addition & 0 deletions src/components/pill-selector-dropdown/flowTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type RoundOption = {
hasWarning: boolean,
id?: number | string,
isExternalUser: boolean,
type?: string,
} & Option;

export type SelectedRoundOptions = Array<RoundOption> | List<RoundOption>;
Expand Down
3 changes: 2 additions & 1 deletion src/features/unified-share-modal/ContactsField.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,12 @@ class ContactsField extends React.Component<Props, State> {
validateForError={validateForError}
validator={validator}
>
{contacts.map(({ email, isExternalUser, text = null, id }) => (
{contacts.map(({ email, isExternalUser, text = null, id, type }) => (
<ContactDatalistItem
getContactAvatarUrl={getContactAvatarUrl}
key={id}
id={id}
type={type}
isExternal={isExternalUser}
name={text}
subtitle={email || groupLabel}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ exports[`features/unified-share-modal/ContactsField render should have scrollabl
showAvatar={false}
subtitle="[email protected]"
title="W User"
type="user"
/>
<ContactDatalistItem
id="12345"
Expand All @@ -164,6 +165,7 @@ exports[`features/unified-share-modal/ContactsField render should have scrollabl
showAvatar={false}
subtitle="[email protected]"
title="X User"
type="group"
/>
<ContactDatalistItem
id="23456"
Expand All @@ -173,6 +175,7 @@ exports[`features/unified-share-modal/ContactsField render should have scrollabl
showAvatar={false}
subtitle="[email protected]"
title="Y User"
type="user"
/>
<ContactDatalistItem
id="34567"
Expand All @@ -182,6 +185,7 @@ exports[`features/unified-share-modal/ContactsField render should have scrollabl
showAvatar={false}
subtitle="[email protected]"
title="Z User"
type="user"
/>
<ContactDatalistItem
id="12"
Expand All @@ -191,6 +195,7 @@ exports[`features/unified-share-modal/ContactsField render should have scrollabl
showAvatar={false}
subtitle="[email protected]"
title="a b"
type="user"
/>
<ContactDatalistItem
id="13"
Expand All @@ -200,6 +205,7 @@ exports[`features/unified-share-modal/ContactsField render should have scrollabl
showAvatar={false}
subtitle="[email protected]"
title="a b"
type="user"
/>
<ContactDatalistItem
id="14"
Expand All @@ -209,6 +215,7 @@ exports[`features/unified-share-modal/ContactsField render should have scrollabl
showAvatar={false}
subtitle="[email protected]"
title="a c"
type="user"
/>
<ContactDatalistItem
id="14"
Expand All @@ -218,6 +225,7 @@ exports[`features/unified-share-modal/ContactsField render should have scrollabl
showAvatar={false}
subtitle="[email protected]"
title="a d"
type="user"
/>
<ContactDatalistItem
id="14"
Expand All @@ -227,6 +235,7 @@ exports[`features/unified-share-modal/ContactsField render should have scrollabl
showAvatar={false}
subtitle="[email protected]"
title="a e"
type="user"
/>
</PillSelectorDropdown>
`;
Expand Down
2 changes: 1 addition & 1 deletion src/features/unified-share-modal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export interface ContactType {
isExternalUser?: boolean;
name?: string;
text?: string;
type?: string;
type?: string | null | undefined;
value?: string;
}

0 comments on commit 14a194f

Please sign in to comment.