ant-design-vue/components/icon/__tests__/index.test.js

234 lines
6.2 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import { mount } from '@vue/test-utils';
import Icon from '..';
import VueIcon from '@ant-design/icons-vue';
import { getThemeFromTypeName, withThemeSuffix } from '../utils';
import { cloneElement } from '../../_util/vnode';
describe('Icon', () => {
2018-11-27 14:30:30 +00:00
it('should render to a <i class="xxx"><svg>...</svg></i>', () => {
const wrapper = mount({
2019-02-01 09:23:00 +00:00
render() {
2019-01-12 03:33:27 +00:00
return <Icon type="message" class="my-icon-classname" />;
},
2019-01-12 03:33:27 +00:00
});
expect(wrapper.html()).toMatchSnapshot();
});
2018-11-27 14:30:30 +00:00
it('should support basic usage', () => {
const wrapper = mount({
2019-02-01 09:23:00 +00:00
render() {
2018-11-27 14:30:30 +00:00
return (
<div>
2019-01-12 03:33:27 +00:00
<Icon type="home" />
<Icon type="setting" theme="filled" />
<Icon type="smile" theme="outlined" />
<Icon type="sync" spin />
<Icon type="loading" />
</div>
);
2018-11-27 14:30:30 +00:00
},
2019-01-12 03:33:27 +00:00
});
expect(wrapper.html()).toMatchSnapshot();
});
2018-11-27 14:30:30 +00:00
it('should support older usage', () => {
const wrapper = mount({
2019-02-01 09:23:00 +00:00
render() {
2018-11-27 14:30:30 +00:00
return (
<div>
2019-01-12 03:33:27 +00:00
<Icon type="home-o" />
<Icon type="setting-fill" />
<Icon type="smile-o" />
<Icon type="check-circle-twotone" />
</div>
);
2018-11-27 14:30:30 +00:00
},
2019-01-12 03:33:27 +00:00
});
expect(wrapper.html()).toMatchSnapshot();
});
2018-11-27 14:30:30 +00:00
it('should support two-tone icon', () => {
const wrapper = mount({
2019-02-01 09:23:00 +00:00
render() {
2019-01-12 03:33:27 +00:00
return <Icon type="check-circle" theme="twoTone" twoToneColor="#f5222d" />;
2018-11-27 14:30:30 +00:00
},
2019-01-12 03:33:27 +00:00
});
expect(wrapper.html()).toMatchSnapshot();
});
2018-11-27 14:30:30 +00:00
it('should support config global two-tone primary color', () => {
2019-01-12 03:33:27 +00:00
const colors = VueIcon.getTwoToneColors();
Icon.setTwoToneColor('#1890ff');
expect(Icon.getTwoToneColor()).toBe('#1890ff');
2018-11-27 14:30:30 +00:00
const wrapper = mount({
2019-02-01 09:23:00 +00:00
render() {
2019-01-12 03:33:27 +00:00
return <Icon type="check-circle" theme="twoTone" />;
2018-11-27 14:30:30 +00:00
},
2019-01-12 03:33:27 +00:00
});
2018-11-27 14:30:30 +00:00
2019-01-12 03:33:27 +00:00
expect(wrapper.html()).toMatchSnapshot();
VueIcon.setTwoToneColors(colors);
});
2018-11-27 14:30:30 +00:00
it('should support pass svg paths as children', () => {
const wrapper = mount({
2019-02-01 09:23:00 +00:00
render() {
2019-01-12 03:33:27 +00:00
return (
<Icon viewBox="0 0 24 24">
<title>Cool Home</title>
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" />
</Icon>
);
2018-11-27 14:30:30 +00:00
},
2019-01-12 03:33:27 +00:00
});
expect(wrapper.html()).toMatchSnapshot();
});
2018-11-27 14:30:30 +00:00
it('should give warning and render <i>{null}</i>', () => {
const wrapper = mount({
2019-02-01 09:23:00 +00:00
render() {
2019-01-12 03:33:27 +00:00
return <Icon viewBox="0 0 24 24" />;
2018-11-27 14:30:30 +00:00
},
2019-01-12 03:33:27 +00:00
});
expect(wrapper.html()).toMatchSnapshot();
});
2018-11-27 14:30:30 +00:00
it('should support custom usage of children', () => {
expect(() => {
2019-01-12 03:33:27 +00:00
mount({
render() {
return <Icon type="custom">&E648</Icon>;
},
});
}).not.toThrow();
});
2018-11-27 14:30:30 +00:00
describe('warning on conflicting theme', () => {
2019-01-12 03:33:27 +00:00
let errorSpy;
2018-11-27 14:30:30 +00:00
beforeEach(() => {
2019-01-12 03:33:27 +00:00
errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
});
2018-11-27 14:30:30 +00:00
afterEach(() => {
2019-01-12 03:33:27 +00:00
errorSpy.mockRestore();
});
2018-11-27 14:30:30 +00:00
it('does not warn', () => {
2019-01-12 03:33:27 +00:00
mount({
render() {
return <Icon type="clock-circle-o" theme="outlined" />;
},
});
expect(errorSpy).not.toBeCalled();
});
2018-11-27 14:30:30 +00:00
it('warns', () => {
2019-01-12 03:33:27 +00:00
mount({
render() {
return <Icon type="clock-circle-o" theme="filled" />;
},
});
2018-11-27 14:30:30 +00:00
expect(errorSpy).toBeCalledWith(
2019-01-12 03:33:27 +00:00
"Warning: The icon name 'clock-circle-o' already specify a theme 'outlined', the 'theme' prop 'filled' will be ignored.",
);
});
});
2018-11-27 14:30:30 +00:00
describe('`component` prop', () => {
it('can access to svg defs if has children', () => {
const wrapper = mount({
2019-01-12 03:33:27 +00:00
render() {
2018-11-27 14:30:30 +00:00
const component = {
2019-01-12 03:33:27 +00:00
render() {
2018-11-27 14:30:30 +00:00
return (
<svg>
<defs>
2019-01-12 03:33:27 +00:00
<linearGradient id="gradient">
<stop offset="20%" stopColor="#39F" />
<stop offset="90%" stopColor="#F3F" />
2018-11-27 14:30:30 +00:00
</linearGradient>
</defs>
2019-01-12 03:33:27 +00:00
{this.$slots.default.map(child => {
cloneElement(child, {
attrs: child.type === 'path' ? { fill: 'scriptUrl(#gradient)' } : {},
});
})}
2018-11-27 14:30:30 +00:00
</svg>
2019-01-12 03:33:27 +00:00
);
2018-11-27 14:30:30 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-11-27 14:30:30 +00:00
return (
2019-01-12 03:33:27 +00:00
<Icon class="my-home-icon" component={component}>
2018-11-27 14:30:30 +00:00
<title>Cool Home</title>
<path d="'M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z'" />
2019-01-12 03:33:27 +00:00
</Icon>
);
2018-11-27 14:30:30 +00:00
},
2019-01-12 03:33:27 +00:00
});
expect(wrapper.html()).toMatchSnapshot();
});
});
2018-11-27 14:30:30 +00:00
2019-09-10 13:54:22 +00:00
it('should support svg vue component', () => {
2018-11-27 14:30:30 +00:00
const SvgComponent = {
2019-01-12 03:33:27 +00:00
render() {
2018-11-27 14:30:30 +00:00
return (
2019-01-12 03:33:27 +00:00
<svg viewBox="0 0 24 24">
2018-11-27 14:30:30 +00:00
<title>Cool Home</title>
2019-01-12 03:33:27 +00:00
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" />
2018-11-27 14:30:30 +00:00
</svg>
2019-01-12 03:33:27 +00:00
);
2018-11-27 14:30:30 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-11-27 14:30:30 +00:00
const wrapper = mount({
2019-01-12 03:33:27 +00:00
render() {
2018-11-27 14:30:30 +00:00
return (
2019-01-12 03:33:27 +00:00
<Icon class="my-home-icon" component={SvgComponent}>
2018-11-27 14:30:30 +00:00
<title>Cool Home</title>
<path d="'M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z'" />
</Icon>
2019-01-12 03:33:27 +00:00
);
2018-11-27 14:30:30 +00:00
},
2019-01-12 03:33:27 +00:00
});
expect(wrapper.html()).toMatchSnapshot();
});
});
2018-11-27 14:30:30 +00:00
describe('utils', () => {
it('getThemeFromTypeName() should work', () => {
2019-01-12 03:33:27 +00:00
const testCases = [
'check-circle',
'check-circle-o',
'check-circle-fill',
'check-circle-twotone',
];
const result = testCases.map(type => getThemeFromTypeName(type));
expect(result).toEqual([null, 'outlined', 'filled', 'twoTone']);
});
2018-11-27 14:30:30 +00:00
it('withThemeSuffix() should work', () => {
const testCases = [
{ type: 'home', theme: 'filled' },
{ type: 'home', theme: 'outlined' },
{ type: 'home', theme: 'twoTone' },
{ type: 'home', theme: 'This-is-the-secret' },
{ type: 'home-o', theme: 'filled' },
{ type: 'home-fill', theme: 'outlined' },
{ type: 'home-o', theme: 'twoTone' },
{ type: 'home-o', theme: 'This-is-the-secret' },
2019-01-12 03:33:27 +00:00
];
const result = testCases.map(({ type, theme }) => withThemeSuffix(type, theme));
expect(result).toEqual([
'home-fill',
'home-o',
'home-twotone',
'home',
'home-o-fill',
'home-fill-o',
'home-o-twotone',
'home-o',
]);
});
});