2019-01-12 03:33:27 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import { asyncExpect } from '@/tests/utils';
|
|
|
|
import Avatar from '..';
|
2021-05-12 11:56:23 +00:00
|
|
|
import useBreakpoint from '../../_util/hooks/useBreakpoint';
|
|
|
|
|
|
|
|
jest.mock('../../_util/hooks/useBreakpoint');
|
2018-05-18 02:10:17 +00:00
|
|
|
|
|
|
|
describe('Avatar Render', () => {
|
2020-03-07 11:45:13 +00:00
|
|
|
let originOffsetWidth;
|
2021-05-12 11:56:23 +00:00
|
|
|
const sizes = { xs: 24, sm: 32, md: 40, lg: 64, xl: 80, xxl: 100 };
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
});
|
2018-05-18 02:10:17 +00:00
|
|
|
it('Render long string correctly', () => {
|
|
|
|
const wrapper = mount(Avatar, {
|
|
|
|
slots: {
|
2020-08-14 02:02:52 +00:00
|
|
|
default: () => 'TestString',
|
2018-05-18 02:10:17 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
const children = wrapper.findAll('.ant-avatar-string');
|
|
|
|
expect(children.length).toBe(1);
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
it('should render fallback string correctly', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
global.document.body.innerHTML = '';
|
2018-09-05 13:28:54 +00:00
|
|
|
const wrapper = mount(Avatar, {
|
|
|
|
slots: {
|
2020-08-14 02:02:52 +00:00
|
|
|
default: () => 'Fallback',
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2020-07-25 07:46:49 +00:00
|
|
|
props: {
|
2018-09-05 13:28:54 +00:00
|
|
|
src: 'http://error.url',
|
|
|
|
},
|
2020-07-25 07:46:49 +00:00
|
|
|
attachTo: 'body',
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.find('img').trigger('error');
|
|
|
|
}, 0);
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const children = wrapper.findAll('.ant-avatar-string');
|
|
|
|
expect(children.length).toBe(1);
|
2020-08-14 02:02:52 +00:00
|
|
|
expect(children[0].text()).toBe('Fallback');
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
2019-05-28 03:37:38 +00:00
|
|
|
it('should handle onError correctly', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
global.document.body.innerHTML = '';
|
|
|
|
const LOAD_FAILURE_SRC = 'http://error.url';
|
|
|
|
const LOAD_SUCCESS_SRC = 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png';
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
const Foo = {
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2018-09-05 13:28:54 +00:00
|
|
|
return {
|
|
|
|
src: LOAD_FAILURE_SRC,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
handleImgError() {
|
|
|
|
this.src = LOAD_SUCCESS_SRC;
|
|
|
|
return false;
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { src } = this;
|
|
|
|
return <Avatar src={src} loadError={this.handleImgError} />;
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2021-05-10 10:11:18 +00:00
|
|
|
const wrapper = mount(Foo, { attachTo: 'body' });
|
2019-04-09 12:39:49 +00:00
|
|
|
await asyncExpect(() => {
|
|
|
|
// mock img load Error, since jsdom do not load resource by default
|
|
|
|
// https://github.com/jsdom/jsdom/issues/1816
|
|
|
|
wrapper.find('img').trigger('error');
|
|
|
|
}, 0);
|
|
|
|
await asyncExpect(() => {
|
2021-05-10 10:11:18 +00:00
|
|
|
expect(wrapper.find('img')).not.toBeNull();
|
2019-04-09 12:39:49 +00:00
|
|
|
}, 0);
|
|
|
|
await asyncExpect(() => {
|
2021-05-10 10:11:18 +00:00
|
|
|
expect(wrapper.find('img').attributes('src')).toBe(LOAD_SUCCESS_SRC);
|
2019-04-09 12:39:49 +00:00
|
|
|
}, 0);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2019-03-11 11:58:32 +00:00
|
|
|
|
2019-05-28 03:37:38 +00:00
|
|
|
it('should show image on success after a failure state', async () => {
|
2019-03-11 11:58:32 +00:00
|
|
|
global.document.body.innerHTML = '';
|
|
|
|
const LOAD_FAILURE_SRC = 'http://error.url';
|
|
|
|
const LOAD_SUCCESS_SRC = 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png';
|
|
|
|
|
|
|
|
const Foo = {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
src: LOAD_FAILURE_SRC,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
const { src } = this;
|
2019-03-11 13:06:53 +00:00
|
|
|
return <Avatar src={src}>Fallback</Avatar>;
|
2019-03-11 11:58:32 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-07-25 07:46:49 +00:00
|
|
|
const wrapper = mount(Foo, { sync: false, attachTo: 'body' });
|
2019-03-11 13:06:53 +00:00
|
|
|
await asyncExpect(() => {
|
|
|
|
wrapper.find('img').trigger('error');
|
|
|
|
}, 0);
|
|
|
|
await asyncExpect(() => {
|
2021-05-10 10:11:18 +00:00
|
|
|
expect(wrapper.findComponent({ name: 'AAvatar' }).findAll('img').length).toBe(0);
|
2019-03-11 13:06:53 +00:00
|
|
|
expect(wrapper.findAll('.ant-avatar-string').length).toBe(1);
|
2019-04-09 12:39:49 +00:00
|
|
|
}, 0);
|
2019-03-11 11:58:32 +00:00
|
|
|
|
2019-03-11 13:06:53 +00:00
|
|
|
await asyncExpect(() => {
|
2020-08-14 02:02:52 +00:00
|
|
|
wrapper.vm.src = LOAD_SUCCESS_SRC;
|
2019-03-11 13:06:53 +00:00
|
|
|
});
|
|
|
|
await asyncExpect(() => {
|
2021-05-10 10:11:18 +00:00
|
|
|
expect(wrapper.findComponent({ name: 'AAvatar' }).findAll('img').length).toBe(1);
|
2019-03-11 13:06:53 +00:00
|
|
|
expect(wrapper.findAll('.ant-avatar-image').length).toBe(1);
|
2019-04-09 12:39:49 +00:00
|
|
|
}, 0);
|
2019-03-11 11:58:32 +00:00
|
|
|
});
|
2021-05-12 11:56:23 +00:00
|
|
|
|
|
|
|
it('should calculate scale of avatar children correctly', async () => {
|
|
|
|
let wrapper = mount({
|
|
|
|
render() {
|
|
|
|
return <Avatar>Avatar</Avatar>;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await asyncExpect(() => {
|
|
|
|
expect(wrapper.find('.ant-avatar-string')).toMatchSnapshot();
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
|
|
|
|
get() {
|
|
|
|
if (this.className === 'ant-avatar-string') {
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
return 40;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
wrapper = mount({
|
|
|
|
render() {
|
|
|
|
return <Avatar>xx</Avatar>;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await asyncExpect(() => {
|
|
|
|
expect(wrapper.find('.ant-avatar-string')).toMatchSnapshot();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should calculate scale of avatar children correctly with gap', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
render() {
|
|
|
|
return <Avatar gap={2}>Avatar</Avatar>;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await asyncExpect(() => {
|
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.entries(sizes).forEach(([key, value]) => {
|
|
|
|
it(`adjusts component size to ${value} when window size is ${key}`, async () => {
|
|
|
|
useBreakpoint.mockReturnValue({ value: { [key]: true } });
|
|
|
|
|
|
|
|
const wrapper = mount({
|
|
|
|
render() {
|
|
|
|
return <Avatar size={sizes} />;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await asyncExpect(() => {
|
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fallback', async () => {
|
|
|
|
const div = global.document.createElement('div');
|
|
|
|
global.document.body.appendChild(div);
|
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Avatar shape="circle" src="http://error.url">
|
|
|
|
A
|
|
|
|
</Avatar>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ attachTo: div },
|
|
|
|
);
|
|
|
|
await asyncExpect(async () => {
|
|
|
|
await wrapper.find('img').trigger('error');
|
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
wrapper.unmount();
|
|
|
|
global.document.body.removeChild(div);
|
|
|
|
}, 0);
|
|
|
|
});
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|