Skip to content

Commit

Permalink
[Avatar] Expose onError hook (#4052)
Browse files Browse the repository at this point in the history
  • Loading branch information
whizkydee committed Mar 10, 2021
1 parent 5eaf263 commit bf9a802
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f
### Enhancements

- Added `statusAndProgressLabelOverride` prop to `Badge` ([#4028](https://github.com/Shopify/polaris-react/pull/4028))
- Added an `onError` hook to the `Avatar` component ([#4052](https://github.com/Shopify/polaris-react/pull/4052))
- Added `zIndexOverride` prop to `Popover` ([#3937](https://github.com/Shopify/polaris-react/pull/3937))

### Bug fixes
Expand Down
8 changes: 7 additions & 1 deletion src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ export interface AvatarProps {
customer?: boolean;
/** URL of the avatar image which falls back to initials if the image fails to load */
source?: string;
/** Callback fired when the image fails to load */
onError?(): void;
/** Accessible label for the avatar image */
accessibilityLabel?: string;
}

export function Avatar({
name,
source,
onError,
initials,
customer,
size = 'medium',
Expand All @@ -61,7 +64,10 @@ export function Avatar({

const handleError = useCallback(() => {
setStatus(Status.Errored);
}, []);
if (onError) {
onError();
}
}, [onError]);
const handleLoad = useCallback(() => {
setStatus(Status.Loaded);
}, []);
Expand Down
17 changes: 17 additions & 0 deletions src/components/Avatar/tests/Avatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ describe('<Avatar />', () => {
});
});

describe('consumer-specified "onError" hook', () => {
it('gets invoked in the event of an error', () => {
const spy = jest.fn();
const avatar = mountWithApp(
<Avatar
size="large"
initials="DL"
source="image/path/"
onError={spy}
/>,
);

avatar.find(Image)!.trigger('onError');
expect(spy).toHaveBeenCalledTimes(1);
});
});

describe('on Error with changed props', () => {
it('re-renders the image if a the source prop is changed after an error', () => {
const workingSrc = 'image/goodPath/';
Expand Down

0 comments on commit bf9a802

Please sign in to comment.