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

221 lines
6.4 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>', () => {
2018-12-07 16:17:45 +08:00
const wrapper = render(<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" />
2018-12-07 16:17:45 +08:00
</div>,
2018-08-31 10:53:03 +08:00
);
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" />
2018-12-07 16:17:45 +08:00
</div>,
2018-08-31 10:53:03 +08:00
);
expect(wrapper).toMatchSnapshot();
});
2018-08-30 22:59:43 +08:00
it('should support two-tone icon', () => {
2018-12-07 16:17:45 +08:00
const wrapper = render(<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-12-07 16:17:45 +08:00
const wrapper = render(<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" />
2018-12-07 16:17:45 +08:00
</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-12-07 16:17:45 +08:00
const wrapper = render(<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" />
2018-12-07 16:17:45 +08:00
</Tooltip>,
2018-09-03 17:41:48 +08:00
);
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);
});
it('should support custom usage of children', () => {
expect(() => {
render(<Icon type="custom">&E648</Icon>);
}).not.toThrow();
});
describe('warning on conflicting theme', () => {
let errorSpy;
beforeEach(() => {
errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
errorSpy.mockRestore();
});
it('does not warn', () => {
mount(<Icon type="clock-circle-o" theme="outlined" />);
expect(errorSpy).not.toBeCalled();
});
it('warns', () => {
mount(<Icon type="clock-circle-o" theme="filled" />);
expect(errorSpy).toBeCalledWith(
2018-12-07 16:17:45 +08:00
"Warning: The icon name 'clock-circle-o' already specify a theme 'outlined', the 'theme' prop 'filled' will be ignored.",
);
});
});
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>
2018-12-07 16:17:45 +08:00
{React.Children.map(svgProps.children, child =>
React.cloneElement(
child,
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'" />
2018-12-07 16:17:45 +08:00
</Icon>,
2018-08-15 17:21:02 +08:00
);
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-12-07 16:17:45 +08:00
<Icon className="my-home-icon" component={SvgComponent}>
2018-08-15 17:21:02 +08:00
<title>Cool Home</title>
<path d="'M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z'" />
2018-12-07 16:17:45 +08:00
</Icon>,
2018-08-15 17:21:02 +08:00
);
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-12-07 16:17:45 +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', () => {
2018-12-07 16:17:45 +08:00
const testCases = [
'check-circle',
'check-circle-o',
'check-circle-fill',
'check-circle-twotone',
];
2018-08-31 11:41:58 +08:00
const result = testCases.map(type => getThemeFromTypeName(type));
2018-12-07 16:17:45 +08:00
expect(result).toEqual([null, 'outlined', 'filled', 'twoTone']);
2018-08-31 11:41:58 +08: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' },
];
const result = testCases.map(({ type, theme }) => withThemeSuffix(type, theme));
2018-12-07 16:17:45 +08:00
expect(result).toEqual([
'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
});
});