Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Avatar): expose onError #11285

Merged
merged 2 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions components/avatar/__tests__/Avatar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,41 @@ describe('Avatar Render', () => {
wrapper.detach();
global.document.body.removeChild(div);
});

it('should handle onError correctly', () => {
const LOAD_FAILURE_SRC = 'http://error.url';
const LOAD_SUCCESS_SRC = 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png';

const div = global.document.createElement('div');
global.document.body.appendChild(div);

class Foo extends React.Component {
state = {
src: LOAD_FAILURE_SRC,
}

handleImgError = () => {
this.setState({
src: LOAD_SUCCESS_SRC,
});
return false;
}

render() {
const { src } = this.state;
return <Avatar src={src} onError={this.handleImgError} />;
}
}

const wrapper = mount(<Foo />, { attachTo: div });
// mock img load Error, since jsdom do not load resource by default
// https://github.com/jsdom/jsdom/issues/1816
wrapper.find('img').simulate('error');

expect(wrapper.find(Avatar).instance().state.isImgExist).toBe(true);
expect(div.querySelector('img').getAttribute('src')).toBe(LOAD_SUCCESS_SRC);

wrapper.detach();
global.document.body.removeChild(div);
});
});
3 changes: 2 additions & 1 deletion components/avatar/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Avatars can be used to represent people or objects. It supports images, `Icon`s,
| shape | the shape of avatar | `circle` \| `square` | `circle` |
| size | the size of the avatar | `large` \| `small` \| `default` | `default` |
| src | the address of the image for an image avatar | string | - |
| alt | This attribute defines the alternative text describing the image | string | - |
| alt | This attribute defines the alternative text describing the image | string | - |
| onError | handler when img load error,return false to prevent default fallback behavior | () => boolean | - |
11 changes: 10 additions & 1 deletion components/avatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export interface AvatarProps {
className?: string;
children?: any;
alt?: string;
/* callback when img load error */
/* return false to prevent Avatar show default fallback behavior, then you can do fallback by your self*/
onError?: () => boolean;
}

export interface AvatarState {
Expand Down Expand Up @@ -72,7 +75,13 @@ export default class Avatar extends React.Component<AvatarProps, AvatarState> {
}
}

handleImgLoadError = () => this.setState({ isImgExist: false });
handleImgLoadError = () => {
const { onError } = this.props;
const errorFlag = onError ? onError() : undefined;
if (errorFlag !== false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这句没覆盖到。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2018-07-16 2 11 49

是旧的用例其实一直有问题,手动操作的 state,没有按 simulate 交互事件的方式去测试,我来把这个坑填下把

this.setState({ isImgExist: false });
}
}

render() {
const {
Expand Down
1 change: 1 addition & 0 deletions components/avatar/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ title: Avatar
| size | 设置头像的大小 | Enum{ 'large', 'small', 'default' } | `default` |
| src | 图片类头像的资源地址 | string | - |
| alt | 图像无法显示时的替代文本 | string | - |
| onError | 图片加载失败的事件,返回 false 会关闭组件默认的 fallback 行为 | () => boolean | - |