2022-08-28 22:41:44 +08:00
|
|
|
import React, { useState } from 'react';
|
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';
|
2023-08-18 14:54:35 +08:00
|
|
|
import ConfigProvider from '../../config-provider';
|
2020-08-29 13:43:52 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
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 };
|
2022-08-28 22:41:44 +08:00
|
|
|
let originOffsetWidth: PropertyDescriptor['get'];
|
2019-03-21 10:07:56 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
// Mock offsetHeight
|
2022-08-28 22:41:44 +08:00
|
|
|
originOffsetWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetWidth')?.get;
|
2019-03-21 10:07:56 +08:00
|
|
|
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', () => {
|
2022-08-28 22:41:44 +08:00
|
|
|
const { container } = render(<Avatar>TestString</Avatar>);
|
|
|
|
expect(container.querySelectorAll('.ant-avatar-string').length).toBe(1);
|
2017-06-07 11:18:31 +08:00
|
|
|
});
|
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);
|
2022-08-28 22:41:44 +08:00
|
|
|
const { container } = render(<Avatar src="http://error.url">Fallback</Avatar>);
|
|
|
|
fireEvent.error(container.querySelector('img')!);
|
|
|
|
const children = container.querySelectorAll('.ant-avatar-string');
|
2018-04-22 13:09:42 +08:00
|
|
|
expect(children.length).toBe(1);
|
2022-08-28 22:41:44 +08:00
|
|
|
expect(children[0].innerHTML).toBe('Fallback');
|
2018-04-22 13:09:42 +08:00
|
|
|
global.document.body.removeChild(div);
|
|
|
|
});
|
2018-07-24 12:14:34 +08:00
|
|
|
|
|
|
|
it('should handle onError correctly', () => {
|
2022-08-28 22:41:44 +08:00
|
|
|
const LOAD_FAILURE_SRC = 'http://error.url/';
|
2023-04-08 21:01:51 +08:00
|
|
|
const LOAD_SUCCESS_SRC = 'https://xsgames.co/randomusers/avatar.php?g=pixel';
|
2022-08-28 22:41:44 +08:00
|
|
|
const Foo: React.FC = () => {
|
|
|
|
const [avatarSrc, setAvatarSrc] = useState<typeof LOAD_FAILURE_SRC | typeof LOAD_SUCCESS_SRC>(
|
|
|
|
LOAD_FAILURE_SRC,
|
|
|
|
);
|
|
|
|
const onError = (): boolean => {
|
|
|
|
setAvatarSrc(LOAD_SUCCESS_SRC);
|
2018-07-24 12:14:34 +08:00
|
|
|
return false;
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2022-08-28 22:41:44 +08:00
|
|
|
return <Avatar src={avatarSrc} onError={onError} />;
|
|
|
|
};
|
|
|
|
const { container } = render(<Foo />);
|
|
|
|
expect(container.querySelector('img')?.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
|
2022-08-28 22:41:44 +08:00
|
|
|
fireEvent.error(container.querySelector('img')!);
|
|
|
|
expect(container.querySelector('img')?.src).toBe(LOAD_SUCCESS_SRC);
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2018-07-24 12:14:34 +08:00
|
|
|
});
|
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';
|
2023-04-08 21:01:51 +08:00
|
|
|
const LOAD_SUCCESS_SRC = 'https://xsgames.co/randomusers/avatar.php?g=pixel';
|
2019-01-29 21:11:46 +08:00
|
|
|
|
|
|
|
const div = global.document.createElement('div');
|
|
|
|
global.document.body.appendChild(div);
|
|
|
|
|
|
|
|
// simulate error src url
|
2022-08-28 22:41:44 +08:00
|
|
|
const { container, rerender } = render(<Avatar src={LOAD_FAILURE_SRC}>Fallback</Avatar>);
|
|
|
|
|
|
|
|
fireEvent.error(container.querySelector('img')!);
|
2019-01-29 21:11:46 +08:00
|
|
|
|
2022-08-28 22:41:44 +08:00
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
|
|
|
expect(container.querySelectorAll('.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
|
2022-08-28 22:41:44 +08:00
|
|
|
expect(container.querySelector<HTMLDivElement>('.ant-avatar-string')?.style).not.toHaveProperty(
|
|
|
|
'opacity',
|
|
|
|
0,
|
|
|
|
);
|
2019-01-29 21:11:46 +08:00
|
|
|
|
|
|
|
// simulate successful src url
|
2022-08-28 22:41:44 +08:00
|
|
|
rerender(<Avatar src={LOAD_SUCCESS_SRC}>Fallback</Avatar>);
|
2019-01-29 21:11:46 +08:00
|
|
|
|
2022-08-28 22:41:44 +08:00
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
|
|
|
expect(container.querySelectorAll('.ant-avatar-image').length).toBe(1);
|
2019-01-29 21:11:46 +08:00
|
|
|
|
|
|
|
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() {
|
2022-08-28 22:41:44 +08:00
|
|
|
return this.className === 'ant-avatar-string' ? 100 : 40;
|
2019-03-21 10:07:56 +08:00
|
|
|
},
|
|
|
|
});
|
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', () => {
|
2022-08-28 22:41:44 +08:00
|
|
|
const { container } = render(<Avatar gap={2}>Avatar</Avatar>);
|
|
|
|
expect(container.querySelector('.ant-avatar-string')).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', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
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', () => {
|
2022-08-28 22:41:44 +08:00
|
|
|
const { container } = render(<Avatar size={100}>TestString</Avatar>);
|
|
|
|
expect(container.firstChild).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}`, () => {
|
2022-08-28 22:41:44 +08:00
|
|
|
(useBreakpoint as any).mockReturnValue({ [key]: true });
|
2023-07-21 12:33:05 +08:00
|
|
|
const { container } = render(<Avatar size={sizes} />);
|
|
|
|
expect(container).toMatchSnapshot();
|
2020-08-29 13:43:52 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-29 18:05:58 +08:00
|
|
|
it('support onMouseEnter', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onMouseEnter = jest.fn();
|
2022-08-28 22:41:44 +08:00
|
|
|
const { container } = render(<Avatar {...{ 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);
|
2022-08-28 22:41:44 +08:00
|
|
|
const { container } = render(
|
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>,
|
|
|
|
);
|
2022-08-28 22:41:44 +08:00
|
|
|
fireEvent.error(container.querySelector('img')!);
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2020-07-26 16:53:50 +08:00
|
|
|
global.document.body.removeChild(div);
|
|
|
|
});
|
2021-07-06 12:16:46 +08:00
|
|
|
|
|
|
|
it('should exist crossorigin attribute', () => {
|
2023-04-08 21:01:51 +08:00
|
|
|
const LOAD_SUCCESS_SRC = 'https://xsgames.co/randomusers/avatar.php?g=pixel';
|
2022-08-28 22:41:44 +08:00
|
|
|
const crossOrigin = 'anonymous';
|
|
|
|
const { container } = render(
|
|
|
|
<Avatar src={LOAD_SUCCESS_SRC} crossOrigin={crossOrigin}>
|
2021-07-06 12:16:46 +08:00
|
|
|
crossorigin
|
|
|
|
</Avatar>,
|
|
|
|
);
|
2022-08-28 22:41:44 +08:00
|
|
|
expect(container.querySelector('img')?.crossOrigin).toBeTruthy();
|
|
|
|
expect(container.querySelector('img')?.crossOrigin).toEqual(crossOrigin);
|
2021-07-06 12:16:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not exist crossorigin attribute', () => {
|
2023-04-08 21:01:51 +08:00
|
|
|
const LOAD_SUCCESS_SRC = 'https://xsgames.co/randomusers/avatar.php?g=pixel';
|
2022-08-28 22:41:44 +08:00
|
|
|
const { container } = render(<Avatar src={LOAD_SUCCESS_SRC}>crossorigin</Avatar>);
|
|
|
|
expect(container.querySelector('img')?.crossOrigin).toBeFalsy();
|
|
|
|
expect(container.querySelector('img')?.crossOrigin).toEqual('');
|
2021-07-06 12:16:46 +08:00
|
|
|
});
|
2022-08-08 13:30:49 +08:00
|
|
|
|
2022-08-28 22:41:44 +08:00
|
|
|
it('clickable', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onClick = jest.fn();
|
2022-08-08 13:30:49 +08:00
|
|
|
const { container } = render(<Avatar onClick={onClick}>TestString</Avatar>);
|
2022-08-28 22:41:44 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-avatar-string')!);
|
2022-08-08 13:30:49 +08:00
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
2023-07-27 20:04:32 +08:00
|
|
|
|
|
|
|
it('Avatar.Group support shape props', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Avatar.Group shape="square">
|
|
|
|
<Avatar>A</Avatar>
|
|
|
|
<Avatar shape="circle">B</Avatar>
|
|
|
|
<Avatar>C</Avatar>
|
|
|
|
<Avatar shape="circle">D</Avatar>
|
|
|
|
</Avatar.Group>,
|
|
|
|
);
|
|
|
|
const avatars = container?.querySelectorAll<HTMLSpanElement>('.ant-avatar-group .ant-avatar');
|
|
|
|
expect(avatars?.[0]).toHaveClass('ant-avatar-square');
|
|
|
|
expect(avatars?.[1]).toHaveClass('ant-avatar-circle');
|
|
|
|
expect(avatars?.[2]).toHaveClass('ant-avatar-square');
|
|
|
|
expect(avatars?.[3]).toHaveClass('ant-avatar-circle');
|
|
|
|
});
|
2023-08-18 14:54:35 +08:00
|
|
|
|
|
|
|
it('should apply the componentSize of CP', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<>
|
|
|
|
<ConfigProvider componentSize="small">
|
|
|
|
<Avatar>test</Avatar>
|
|
|
|
</ConfigProvider>
|
|
|
|
<ConfigProvider componentSize="large">
|
|
|
|
<Avatar>test</Avatar>
|
|
|
|
</ConfigProvider>
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
expect(container.querySelector('.ant-avatar-sm')).toBeTruthy();
|
|
|
|
expect(container.querySelector('.ant-avatar-lg')).toBeTruthy();
|
|
|
|
});
|
2017-06-07 11:18:31 +08:00
|
|
|
});
|