import React from 'react'; import { render, mount } from 'enzyme'; import ReactIcon from '@ant-design/icons-react'; import Icon from '..'; import Tooltip from '../../tooltip'; import { getThemeFromTypeName, withThemeSuffix } from '../utils'; import mountTest from '../../../tests/shared/mountTest'; describe('Icon', () => { mountTest(Icon); it('should render to a ...', () => { const wrapper = render(); expect(wrapper).toMatchSnapshot(); }); it('should support basic usage', () => { const wrapper = render(
, ); expect(wrapper).toMatchSnapshot(); }); it('should support older usage', () => { const wrapper = render(
, ); expect(wrapper).toMatchSnapshot(); }); it('should support two-tone icon', () => { const wrapper = render(); expect(wrapper).toMatchSnapshot(); }); it('should support config global two-tone primary color', () => { const colors = ReactIcon.getTwoToneColors(); Icon.setTwoToneColor('#1890ff'); expect(Icon.getTwoToneColor()).toBe('#1890ff'); const wrapper = render(); expect(wrapper).toMatchSnapshot(); ReactIcon.setTwoToneColors(colors); }); it('should support pass svg paths as children', () => { const wrapper = render( Cool Home , ); expect(wrapper).toMatchSnapshot(); }); it('should give warning and render {null}', () => { const wrapper = render(); expect(wrapper).toMatchSnapshot(); }); it('should support wrapped by Tooltip', () => { const onVisibleChange = jest.fn(); const wrapper = mount( , ); expect(wrapper.find('i')).toHaveLength(1); const icon = wrapper.find('i').at(0); icon.simulate('mouseenter'); expect(onVisibleChange).toHaveBeenCalledWith(true); expect(wrapper.instance().tooltip.props.visible).toBe(true); icon.simulate('mouseleave'); expect(onVisibleChange).toHaveBeenCalledWith(false); expect(wrapper.instance().tooltip.props.visible).toBe(false); }); it('should support custom usage of children', () => { expect(() => { render(&E648); }).not.toThrow(); }); it('support render svg as component', () => { const renderSvg = () => ( ); const SvgIcon = props => ; expect(mount().render()).toMatchSnapshot(); }); describe('warning on conflicting theme', () => { let errorSpy; beforeEach(() => { errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); }); afterEach(() => { errorSpy.mockRestore(); }); it('does not warn', () => { mount(); expect(errorSpy).not.toHaveBeenCalled(); }); it('warns', () => { mount(); expect(errorSpy).toHaveBeenCalledWith( "Warning: [antd: Icon] The icon name 'clock-circle-o' already specify a theme 'outlined', the 'theme' prop 'filled' will be ignored.", ); }); }); describe('`component` prop', () => { it('can access to svg defs if has children', () => { const wrapper = render( ( {React.Children.map(svgProps.children, child => React.cloneElement( child, child.type === 'path' ? { fill: 'scriptUrl(#gradient)' } : {}, ), )} )} > Cool Home , ); expect(wrapper).toMatchSnapshot(); }); }); it('should support svg react component', () => { const SvgComponent = props => ( Custom Svg ); const wrapper = render( Cool Home , ); expect(wrapper).toMatchSnapshot(); }); }); describe('Icon.createFromIconfontCN()', () => { const IconFont = Icon.createFromIconfontCN({ scriptUrl: '//at.alicdn.com/t/font_8d5l8fzk5b87iudi.js', }); it('should support iconfont.cn', () => { const wrapper = render(
, ); expect(wrapper).toMatchSnapshot(); }); }); 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([ 'home-fill', 'home-o', 'home-twotone', 'home', 'home-o-fill', 'home-fill-o', 'home-o-twotone', 'home-o', ]); }); it('should report an error when there are deprecated typos in icon names', () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); render(); expect(errorSpy).toHaveBeenLastCalledWith( "Warning: [antd: Icon] Icon 'interation' was a typo and is now deprecated, please use 'interaction' instead.", ); render(); expect(errorSpy).toHaveBeenLastCalledWith( "Warning: [antd: Icon] Icon 'cross' was a typo and is now deprecated, please use 'close' instead.", ); render(); expect(errorSpy).toHaveBeenLastCalledWith( "Warning: [antd: Icon] Icon 'canlendar' was a typo and is now deprecated, please use 'calendar' instead.", ); render(); expect(errorSpy).toHaveBeenLastCalledWith( "Warning: [antd: Icon] Icon 'colum-height' was a typo and is now deprecated, please use 'column-height' instead.", ); expect(errorSpy).toHaveBeenCalledTimes(4); errorSpy.mockRestore(); }); });