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

197 lines
5.7 KiB
JavaScript
Raw Normal View History

2016-12-14 14:48:09 +08:00
import React from 'react';
2018-09-03 17:41:48 +08:00
import { render, mount } from 'enzyme';
import Icon from '..';
2018-09-01 20:01:28 +08:00
import ReactIcon from '@ant-design/icons-react';
2018-09-03 17:41:48 +08:00
import Tooltip from '../../tooltip';
2018-08-31 11:41:58 +08:00
import { getThemeFromTypeName, withThemeSuffix } from '../utils';
2016-12-14 14:48:09 +08:00
describe('Icon', () => {
2018-07-23 09:55:13 +08:00
it('should render to a <i class="xxx"><svg>...</svg></i>', () => {
2017-09-15 10:50:45 +08:00
const wrapper = render(
2018-07-23 09:55:13 +08:00
<Icon type="message" className="my-icon-classname" />
2017-09-15 10:50:45 +08:00
);
expect(wrapper).toMatchSnapshot();
2016-12-14 14:48:09 +08:00
});
2018-08-14 17:37:23 +08:00
2018-08-31 10:53:03 +08:00
it('should support basic usage', () => {
const wrapper = render(
<div>
<Icon type="home" />
<Icon type="setting" theme="filled" />
<Icon type="smile" theme="outlined" />
<Icon type="sync" spin />
<Icon type="loading" />
</div>
);
expect(wrapper).toMatchSnapshot();
});
it('should support older usage', () => {
const wrapper = render(
<div>
<Icon type="home-o" />
<Icon type="setting-fill" />
<Icon type="smile-o" />
<Icon type="check-circle-twotone" />
</div>
);
expect(wrapper).toMatchSnapshot();
});
2018-08-30 22:59:43 +08:00
it('should support two-tone icon', () => {
2018-08-14 17:37:23 +08:00
const wrapper = render(
2018-09-01 20:01:28 +08:00
<Icon type="check-circle" theme="twoTone" twoToneColor="#f5222d" />
2018-08-14 17:37:23 +08:00
);
expect(wrapper).toMatchSnapshot();
});
2018-08-31 10:53:03 +08:00
it('should support config global two-tone primary color', () => {
2018-09-01 20:01:28 +08:00
const colors = ReactIcon.getTwoToneColors();
Icon.setTwoToneColor('#1890ff');
2018-09-02 20:30:55 +08:00
expect(Icon.getTwoToneColor()).toBe('#1890ff');
2018-08-31 10:53:03 +08:00
const wrapper = render(
2018-08-31 11:14:51 +08:00
<Icon type="check-circle" theme="twoTone" />
2018-08-31 10:53:03 +08:00
);
expect(wrapper).toMatchSnapshot();
2018-09-01 20:01:28 +08:00
ReactIcon.setTwoToneColors(colors);
2018-08-31 10:53:03 +08:00
});
2018-08-15 17:21:02 +08:00
it('should support pass svg paths as children', () => {
const wrapper = render(
<Icon viewBox="0 0 24 24">
2018-08-14 17:37:23 +08:00
<title>Cool Home</title>
2018-08-15 17:21:02 +08:00
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" />
</Icon>
2018-08-14 17:37:23 +08:00
);
2018-08-15 17:21:02 +08:00
expect(wrapper).toMatchSnapshot();
});
2018-08-14 17:37:23 +08:00
2018-08-15 17:21:02 +08:00
it('should give warning and render <i>{null}</i>', () => {
2018-08-14 17:37:23 +08:00
const wrapper = render(
2018-08-15 17:21:02 +08:00
<Icon viewBox="0 0 24 24" />
2018-08-14 17:37:23 +08:00
);
expect(wrapper).toMatchSnapshot();
});
2018-09-03 17:41:48 +08:00
it('should support wrapped by Tooltip', () => {
const onVisibleChange = jest.fn();
const wrapper = mount(
<Tooltip
title="xxxxx"
mouseEnterDelay={0}
mouseLeaveDelay={0}
onVisibleChange={onVisibleChange}
>
<Icon type="home" />
</Tooltip>
);
expect(wrapper.find('i')).toHaveLength(1);
const icon = wrapper.find('i').at(0);
icon.simulate('mouseenter');
expect(onVisibleChange).toBeCalledWith(true);
expect(wrapper.instance().tooltip.props.visible).toBe(true);
icon.simulate('mouseleave');
expect(onVisibleChange).toBeCalledWith(false);
expect(wrapper.instance().tooltip.props.visible).toBe(false);
});
2018-08-15 17:21:02 +08:00
describe('`component` prop', () => {
it('can access to svg defs if has children', () => {
const wrapper = render(
<Icon
className="my-home-icon"
component={svgProps => (
<svg {...svgProps}>
<defs>
<linearGradient id="gradient">
<stop offset="20%" stopColor="#39F" />
<stop offset="90%" stopColor="#F3F" />
</linearGradient>
</defs>
{
React.Children.map(
svgProps.children,
child => React.cloneElement(
child,
2018-08-21 17:52:41 +08:00
child.type === 'path' ? { fill: 'scriptUrl(#gradient)' } : {}
2018-08-15 17:21:02 +08:00
)
)
}
</svg>
)}
>
<title>Cool Home</title>
<path d="'M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z'" />
</Icon>
);
expect(wrapper).toMatchSnapshot();
});
});
it('should support svg react component', () => {
const SvgComponent = props => (
<svg viewBox="0 0 24 24" {...props}>
<title>Cool Home</title>
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" />
</svg>
);
2018-08-14 17:37:23 +08:00
const wrapper = render(
2018-08-15 17:21:02 +08:00
<Icon
2018-08-14 17:37:23 +08:00
className="my-home-icon"
2018-08-15 17:21:02 +08:00
component={SvgComponent}
>
<title>Cool Home</title>
<path d="'M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z'" />
</Icon>
);
expect(wrapper).toMatchSnapshot();
});
});
describe('Icon.createFromIconfontCN()', () => {
const IconFont = Icon.createFromIconfontCN({
2018-08-21 17:52:41 +08:00
scriptUrl: '//at.alicdn.com/t/font_8d5l8fzk5b87iudi.js',
2018-08-15 17:21:02 +08:00
});
it('should support iconfont.cn', () => {
const wrapper = render(
<div className="icons-list">
2018-08-21 18:41:35 +08:00
<IconFont type="icon-tuichu" />
<IconFont type="icon-facebook" />
<IconFont type="icon-twitter" />
2018-08-15 17:21:02 +08:00
</div>
2018-08-14 17:37:23 +08:00
);
expect(wrapper).toMatchSnapshot();
});
2016-12-14 14:48:09 +08:00
});
2018-08-31 11:41:58 +08:00
describe('utils', () => {
it('getThemeFromTypeName() should work', () => {
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']
);
});
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' },
];
const result = testCases.map(({ type, theme }) => withThemeSuffix(type, theme));
expect(result).toEqual(
2018-09-02 19:53:29 +08:00
['home-fill', 'home-o', 'home-twotone', 'home',
'home-o-fill', 'home-fill-o', 'home-o-twotone', 'home-o']
2018-08-31 11:41:58 +08:00
);
});
});