2017-06-07 11:18:31 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import Avatar from '..';
|
|
|
|
|
|
|
|
describe('Avatar Render', () => {
|
2019-03-21 10:07:56 +08:00
|
|
|
let originOffsetWidth;
|
|
|
|
beforeAll(() => {
|
|
|
|
// Mock offsetHeight
|
|
|
|
originOffsetWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetWidth').get;
|
|
|
|
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
|
|
|
|
get() {
|
|
|
|
if (this.className === 'ant-avatar-string') {
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
return 80;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
// Restore Mock offsetHeight
|
|
|
|
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
|
|
|
|
get: originOffsetWidth,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-07 11:18:31 +08:00
|
|
|
it('Render long string correctly', () => {
|
|
|
|
const wrapper = mount(<Avatar>TestString</Avatar>);
|
|
|
|
const children = wrapper.find('.ant-avatar-string');
|
|
|
|
expect(children.length).toBe(1);
|
|
|
|
});
|
2018-04-22 13:09:42 +08:00
|
|
|
|
|
|
|
it('should render fallback string correctly', () => {
|
|
|
|
const div = global.document.createElement('div');
|
|
|
|
global.document.body.appendChild(div);
|
|
|
|
|
|
|
|
const wrapper = mount(<Avatar src="http://error.url">Fallback</Avatar>, { attachTo: div });
|
2019-03-21 10:07:56 +08:00
|
|
|
wrapper.instance().setScale = jest.fn(() => {
|
|
|
|
if (wrapper.state().scale === 0.5) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wrapper.instance().setState({ scale: 0.5 });
|
|
|
|
});
|
2018-07-24 12:14:34 +08:00
|
|
|
wrapper.find('img').simulate('error');
|
2018-04-22 13:09:42 +08:00
|
|
|
|
|
|
|
const children = wrapper.find('.ant-avatar-string');
|
|
|
|
expect(children.length).toBe(1);
|
|
|
|
expect(children.text()).toBe('Fallback');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(wrapper.instance().setScale).toHaveBeenCalled();
|
2018-10-28 23:45:36 +08:00
|
|
|
expect(div.querySelector('.ant-avatar-string').style.transform).toContain('scale(0.5)');
|
2018-04-22 13:09:42 +08:00
|
|
|
|
|
|
|
wrapper.detach();
|
|
|
|
global.document.body.removeChild(div);
|
|
|
|
});
|
2018-07-24 12:14:34 +08:00
|
|
|
|
|
|
|
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,
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2018-07-24 12:14:34 +08:00
|
|
|
|
|
|
|
handleImgError = () => {
|
|
|
|
this.setState({
|
|
|
|
src: LOAD_SUCCESS_SRC,
|
|
|
|
});
|
|
|
|
return false;
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2018-07-24 12:14:34 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2019-01-29 21:11:46 +08:00
|
|
|
|
|
|
|
it('should show image on success after a failure state', () => {
|
|
|
|
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);
|
|
|
|
|
|
|
|
// simulate error src url
|
|
|
|
const wrapper = mount(<Avatar src={LOAD_FAILURE_SRC}>Fallback</Avatar>, { attachTo: div });
|
|
|
|
wrapper.find('img').simulate('error');
|
|
|
|
|
|
|
|
expect(wrapper.find(Avatar).instance().state.isImgExist).toBe(false);
|
|
|
|
expect(wrapper.find('.ant-avatar-string').length).toBe(1);
|
|
|
|
|
|
|
|
// simulate successful src url
|
|
|
|
wrapper.setProps({ src: LOAD_SUCCESS_SRC });
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
expect(wrapper.find(Avatar).instance().state.isImgExist).toBe(true);
|
|
|
|
expect(wrapper.find('.ant-avatar-image').length).toBe(1);
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
wrapper.detach();
|
|
|
|
global.document.body.removeChild(div);
|
|
|
|
});
|
2019-03-21 10:07:56 +08:00
|
|
|
|
|
|
|
it('should calculate scale of avatar children correctly', () => {
|
|
|
|
const wrapper = mount(<Avatar>Avatar</Avatar>);
|
|
|
|
expect(wrapper.state().scale).toBe(0.72);
|
|
|
|
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
|
|
|
|
get() {
|
|
|
|
if (this.className === 'ant-avatar-string') {
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
return 40;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
wrapper.setProps({ children: 'xx' });
|
|
|
|
expect(wrapper.state().scale).toBe(0.32);
|
|
|
|
});
|
2017-06-07 11:18:31 +08:00
|
|
|
});
|