2022-06-22 14:57:09 +08:00
|
|
|
import { mount } from 'enzyme';
|
2017-06-07 11:18:31 +08:00
|
|
|
import React from 'react';
|
2020-08-29 13:43:52 +08:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import { act } from 'react-dom/test-utils';
|
2017-06-07 11:18:31 +08:00
|
|
|
import Avatar from '..';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
2020-08-29 13:43:52 +08:00
|
|
|
import useBreakpoint from '../../grid/hooks/useBreakpoint';
|
|
|
|
|
|
|
|
jest.mock('../../grid/hooks/useBreakpoint');
|
2017-06-07 11:18:31 +08:00
|
|
|
|
|
|
|
describe('Avatar Render', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Avatar);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Avatar);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2020-08-29 13:43:52 +08:00
|
|
|
const sizes = { xs: 24, sm: 32, md: 40, lg: 64, xl: 80, xxl: 100 };
|
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 });
|
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');
|
|
|
|
|
|
|
|
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';
|
2021-10-14 12:09:07 +08:00
|
|
|
const LOAD_SUCCESS_SRC = 'https://joeschmoe.io/api/v1/random';
|
2018-07-24 12:14:34 +08:00
|
|
|
|
|
|
|
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 });
|
2020-06-12 10:36:53 +08:00
|
|
|
expect(div.querySelector('img').getAttribute('src')).toBe(LOAD_FAILURE_SRC);
|
2018-07-24 12:14:34 +08:00
|
|
|
// mock img load Error, since jsdom do not load resource by default
|
|
|
|
// https://github.com/jsdom/jsdom/issues/1816
|
|
|
|
wrapper.find('img').simulate('error');
|
|
|
|
|
2022-04-15 15:39:06 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-07-24 12:14:34 +08:00
|
|
|
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';
|
2021-10-14 12:09:07 +08:00
|
|
|
const LOAD_SUCCESS_SRC = 'https://joeschmoe.io/api/v1/random';
|
2019-01-29 21:11:46 +08:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2022-04-15 15:39:06 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2019-01-29 21:11:46 +08:00
|
|
|
expect(wrapper.find('.ant-avatar-string').length).toBe(1);
|
2020-06-12 15:26:40 +08:00
|
|
|
// children should show, when image load error without onError return false
|
|
|
|
expect(wrapper.find('.ant-avatar-string').prop('style')).not.toHaveProperty('opacity', 0);
|
2019-01-29 21:11:46 +08:00
|
|
|
|
|
|
|
// simulate successful src url
|
|
|
|
wrapper.setProps({ src: LOAD_SUCCESS_SRC });
|
|
|
|
wrapper.update();
|
|
|
|
|
2022-04-15 15:39:06 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2019-01-29 21:11:46 +08:00
|
|
|
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', () => {
|
2022-04-18 21:02:11 +08:00
|
|
|
const { container, rerender } = render(<Avatar>Avatar</Avatar>);
|
|
|
|
expect(container.querySelector('.ant-avatar-string')).toMatchSnapshot();
|
2020-06-12 10:36:53 +08:00
|
|
|
|
2019-03-21 10:07:56 +08:00
|
|
|
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
|
|
|
|
get() {
|
|
|
|
if (this.className === 'ant-avatar-string') {
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
return 40;
|
|
|
|
},
|
|
|
|
});
|
2022-04-18 21:02:11 +08:00
|
|
|
|
|
|
|
rerender(<Avatar>xx</Avatar>);
|
|
|
|
expect(container.querySelector('.ant-avatar-string')).toMatchSnapshot();
|
2019-03-21 10:07:56 +08:00
|
|
|
});
|
2019-12-13 10:18:12 +08:00
|
|
|
|
2020-05-25 17:56:48 +08:00
|
|
|
it('should calculate scale of avatar children correctly with gap', () => {
|
|
|
|
const wrapper = mount(<Avatar gap={2}>Avatar</Avatar>);
|
2022-04-15 15:39:06 +08:00
|
|
|
expect(wrapper.find('.ant-avatar-string').render()).toMatchSnapshot();
|
2020-05-25 17:56:48 +08:00
|
|
|
});
|
|
|
|
|
2019-12-13 10:18:12 +08:00
|
|
|
it('should warning when pass a string as icon props', () => {
|
|
|
|
const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-04-06 11:07:15 +08:00
|
|
|
render(<Avatar size={64} icon="aa" />);
|
2019-12-13 10:18:12 +08:00
|
|
|
expect(warnSpy).not.toHaveBeenCalled();
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
render(<Avatar size={64} icon="user" />);
|
2019-12-13 10:18:12 +08:00
|
|
|
expect(warnSpy).toHaveBeenCalledWith(
|
|
|
|
`Warning: [antd: Avatar] \`icon\` is using ReactNode instead of string naming in v4. Please check \`user\` at https://ant.design/components/icon`,
|
|
|
|
);
|
|
|
|
warnSpy.mockRestore();
|
|
|
|
});
|
2020-05-25 16:27:02 +08:00
|
|
|
|
|
|
|
it('support size is number', () => {
|
|
|
|
const wrapper = mount(<Avatar size={100}>TestString</Avatar>);
|
2021-12-04 17:06:36 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-05-25 16:27:02 +08:00
|
|
|
});
|
2020-05-29 18:05:58 +08:00
|
|
|
|
2020-08-29 13:43:52 +08:00
|
|
|
Object.entries(sizes).forEach(([key, value]) => {
|
|
|
|
it(`adjusts component size to ${value} when window size is ${key}`, () => {
|
|
|
|
const wrapper = global.document.createElement('div');
|
|
|
|
|
|
|
|
useBreakpoint.mockReturnValue({ [key]: true });
|
|
|
|
act(() => {
|
|
|
|
ReactDOM.render(<Avatar size={sizes} />, wrapper);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-29 18:05:58 +08:00
|
|
|
it('support onMouseEnter', () => {
|
|
|
|
const onMouseEnter = jest.fn();
|
2022-04-17 22:36:36 +08:00
|
|
|
const { container } = render(<Avatar onMouseEnter={onMouseEnter}>TestString</Avatar>);
|
|
|
|
fireEvent.mouseEnter(container.firstChild);
|
2020-05-29 18:05:58 +08:00
|
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
|
|
});
|
2020-07-26 16:53:50 +08:00
|
|
|
|
|
|
|
it('fallback', () => {
|
|
|
|
const div = global.document.createElement('div');
|
|
|
|
global.document.body.appendChild(div);
|
|
|
|
const wrapper = mount(
|
2020-08-06 14:32:13 +08:00
|
|
|
<Avatar shape="circle" src="http://error.url">
|
2020-07-26 16:53:50 +08:00
|
|
|
A
|
|
|
|
</Avatar>,
|
|
|
|
{ attachTo: div },
|
|
|
|
);
|
|
|
|
wrapper.find('img').simulate('error');
|
|
|
|
wrapper.update();
|
2021-12-04 17:06:36 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-07-26 16:53:50 +08:00
|
|
|
wrapper.detach();
|
|
|
|
global.document.body.removeChild(div);
|
|
|
|
});
|
2021-07-06 12:16:46 +08:00
|
|
|
|
|
|
|
it('should exist crossorigin attribute', () => {
|
2021-10-14 12:09:07 +08:00
|
|
|
const LOAD_SUCCESS_SRC = 'https://joeschmoe.io/api/v1/random';
|
2021-07-06 12:16:46 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Avatar src={LOAD_SUCCESS_SRC} crossOrigin="anonymous">
|
|
|
|
crossorigin
|
|
|
|
</Avatar>,
|
|
|
|
);
|
|
|
|
expect(wrapper.html().includes('crossorigin')).toEqual(true);
|
|
|
|
expect(wrapper.find('img').prop('crossOrigin')).toEqual('anonymous');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not exist crossorigin attribute', () => {
|
2021-10-14 12:09:07 +08:00
|
|
|
const LOAD_SUCCESS_SRC = 'https://joeschmoe.io/api/v1/random';
|
2021-07-06 12:16:46 +08:00
|
|
|
const wrapper = mount(<Avatar src={LOAD_SUCCESS_SRC}>crossorigin</Avatar>);
|
|
|
|
expect(wrapper.html().includes('crossorigin')).toEqual(false);
|
|
|
|
expect(wrapper.find('img').prop('crossOrigin')).toEqual(undefined);
|
|
|
|
});
|
2022-08-08 13:30:49 +08:00
|
|
|
|
|
|
|
it('clickable', async () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
const { container } = render(<Avatar onClick={onClick}>TestString</Avatar>);
|
|
|
|
fireEvent.click(container.querySelector('.ant-avatar-string'));
|
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
2017-06-07 11:18:31 +08:00
|
|
|
});
|