2018-02-24 11:40:44 +08:00
|
|
|
import React from 'react';
|
2020-11-18 17:23:46 +08:00
|
|
|
import { mount } from 'enzyme';
|
2018-02-24 11:40:44 +08:00
|
|
|
import Badge from '../index';
|
2018-05-23 13:11:49 +08:00
|
|
|
import Tooltip from '../../tooltip';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2018-02-24 11:40:44 +08:00
|
|
|
|
|
|
|
describe('Badge', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Badge);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Badge);
|
2020-06-06 13:20:09 +08:00
|
|
|
rtlTest(() => (
|
|
|
|
<Badge count={5} offset={[10, 10]}>
|
|
|
|
<a href="#" className="head-example">
|
|
|
|
head
|
|
|
|
</a>
|
|
|
|
</Badge>
|
|
|
|
));
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2018-05-23 13:11:49 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2018-05-25 16:04:38 +08:00
|
|
|
it('badge dot not scaling count > 9', () => {
|
2018-02-24 11:40:44 +08:00
|
|
|
const badge = mount(<Badge count={10} dot />);
|
|
|
|
expect(badge.find('.ant-card-multiple-words').length).toBe(0);
|
|
|
|
});
|
2018-04-10 01:09:18 +08:00
|
|
|
|
2019-01-09 22:32:06 +08:00
|
|
|
it('badge should support float number', () => {
|
2020-11-18 17:23:46 +08:00
|
|
|
let wrapper = mount(<Badge count={3.5} />);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
|
2019-01-09 22:32:06 +08:00
|
|
|
wrapper = mount(<Badge count="3.5" />);
|
2020-11-18 17:23:46 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-03-05 13:29:53 +08:00
|
|
|
expect(() => wrapper.unmount()).not.toThrow();
|
2019-01-09 22:32:06 +08:00
|
|
|
});
|
|
|
|
|
2018-05-25 16:04:38 +08:00
|
|
|
it('badge dot not showing count == 0', () => {
|
2018-02-24 11:40:44 +08:00
|
|
|
const badge = mount(<Badge count={0} dot />);
|
|
|
|
expect(badge.find('.ant-badge-dot').length).toBe(0);
|
|
|
|
});
|
2018-04-10 01:09:18 +08:00
|
|
|
|
|
|
|
it('should have an overriden title attribute', () => {
|
|
|
|
const badge = mount(<Badge count={10} title="Custom title" />);
|
2018-12-07 16:17:45 +08:00
|
|
|
expect(
|
2020-05-15 10:47:03 +08:00
|
|
|
badge.find('.ant-scroll-number').getDOMNode().attributes.getNamedItem('title').value,
|
2018-12-07 16:17:45 +08:00
|
|
|
).toEqual('Custom title');
|
2018-04-10 01:09:18 +08:00
|
|
|
});
|
2018-05-23 13:11:49 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/10626
|
|
|
|
it('should be composable with Tooltip', () => {
|
2020-05-15 10:47:03 +08:00
|
|
|
const ref = React.createRef();
|
2018-05-23 13:11:49 +08:00
|
|
|
const wrapper = mount(
|
2020-05-15 10:47:03 +08:00
|
|
|
<Tooltip title="Fix the error" ref={ref}>
|
2018-05-23 13:11:49 +08:00
|
|
|
<Badge status="error" />
|
2018-12-07 16:17:45 +08:00
|
|
|
</Tooltip>,
|
2018-05-23 13:11:49 +08:00
|
|
|
);
|
|
|
|
wrapper.find('Badge').simulate('mouseenter');
|
|
|
|
jest.runAllTimers();
|
2020-05-15 10:47:03 +08:00
|
|
|
expect(ref.current.props.visible).toBe(true);
|
2018-05-23 13:11:49 +08:00
|
|
|
});
|
2018-05-25 17:00:32 +08:00
|
|
|
|
|
|
|
it('should render when count is changed', () => {
|
|
|
|
const wrapper = mount(<Badge count={9} />);
|
|
|
|
wrapper.setProps({ count: 10 });
|
|
|
|
jest.runAllTimers();
|
2020-11-18 18:32:00 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-05-25 17:00:32 +08:00
|
|
|
wrapper.setProps({ count: 11 });
|
|
|
|
jest.runAllTimers();
|
2020-11-18 18:32:00 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-05-25 17:00:32 +08:00
|
|
|
wrapper.setProps({ count: 11 });
|
|
|
|
jest.runAllTimers();
|
2020-11-18 18:32:00 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-05-31 12:38:32 +08:00
|
|
|
wrapper.setProps({ count: 111 });
|
|
|
|
jest.runAllTimers();
|
2020-11-18 18:32:00 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-05-25 17:00:32 +08:00
|
|
|
wrapper.setProps({ count: 10 });
|
|
|
|
jest.runAllTimers();
|
2020-11-18 18:32:00 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-05-25 17:00:32 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.setProps({ count: 9 });
|
2020-11-18 18:32:00 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-05-25 17:00:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be compatible with borderColor style', () => {
|
2020-11-18 17:23:46 +08:00
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Badge
|
|
|
|
count={4}
|
|
|
|
style={{ backgroundColor: '#fff', color: '#999', borderColor: '#d9d9d9' }}
|
|
|
|
/>,
|
|
|
|
);
|
2020-11-18 17:23:46 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-05-25 17:00:32 +08:00
|
|
|
});
|
2018-12-18 18:31:12 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/13694
|
|
|
|
it('should support offset when count is a ReactNode', () => {
|
2020-11-18 17:23:46 +08:00
|
|
|
const wrapper = mount(
|
2018-12-18 18:31:12 +08:00
|
|
|
<Badge count={<span className="custom" style={{ color: '#f5222d' }} />} offset={[10, 20]}>
|
2019-08-29 23:41:46 +08:00
|
|
|
<a href="#" className="head-example">
|
|
|
|
head
|
|
|
|
</a>
|
2018-12-18 18:31:12 +08:00
|
|
|
</Badge>,
|
|
|
|
);
|
2020-11-18 17:23:46 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-12-18 18:31:12 +08:00
|
|
|
});
|
2019-03-14 15:53:51 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/15349
|
|
|
|
it('should color style works on Badge', () => {
|
|
|
|
const wrapper = mount(<Badge style={{ color: 'red' }} status="success" text="Success" />);
|
|
|
|
expect(wrapper.find('.ant-badge-status-text').props().style.color).toBe('red');
|
|
|
|
});
|
2019-04-02 19:01:12 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/15799
|
|
|
|
it('render correct with negative number', () => {
|
2020-11-18 17:23:46 +08:00
|
|
|
const wrapper = mount(
|
2019-04-02 19:01:12 +08:00
|
|
|
<div>
|
|
|
|
<Badge count="-10" />
|
|
|
|
<Badge count={-10} />
|
|
|
|
</div>,
|
|
|
|
);
|
2020-11-18 17:23:46 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2019-04-02 19:01:12 +08:00
|
|
|
});
|
2020-02-11 12:56:18 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/21331
|
|
|
|
it('render Badge status/color when contains children', () => {
|
2020-11-18 17:23:46 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<div>
|
2020-02-11 12:56:18 +08:00
|
|
|
<Badge count={5} status="success">
|
|
|
|
<a />
|
|
|
|
</Badge>
|
|
|
|
<Badge count={5} color="blue">
|
|
|
|
<a />
|
|
|
|
</Badge>
|
|
|
|
<Badge count={5} color="#08c">
|
|
|
|
<a />
|
|
|
|
</Badge>
|
2020-11-18 17:23:46 +08:00
|
|
|
</div>,
|
2020-02-11 12:56:18 +08:00
|
|
|
);
|
2020-11-18 17:23:46 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-02-11 17:04:13 +08:00
|
|
|
});
|
2020-07-28 15:27:50 +08:00
|
|
|
|
2020-08-26 11:27:07 +08:00
|
|
|
it('Badge should work when status/color is empty string', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<>
|
|
|
|
<Badge color="" text="text" />
|
|
|
|
<Badge status="" text="text" />
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(wrapper.find('.ant-badge')).toHaveLength(2);
|
|
|
|
});
|
|
|
|
|
2020-07-28 15:27:50 +08:00
|
|
|
it('render Badge size when contains children', () => {
|
2020-11-18 17:23:46 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<div>
|
2020-07-28 15:27:50 +08:00
|
|
|
<Badge size="default" count={5}>
|
|
|
|
<a />
|
|
|
|
</Badge>
|
|
|
|
<Badge size="small" count={5}>
|
|
|
|
<a />
|
|
|
|
</Badge>
|
2020-11-18 17:23:46 +08:00
|
|
|
</div>,
|
2020-07-28 15:27:50 +08:00
|
|
|
);
|
2020-11-18 17:23:46 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-07-28 15:27:50 +08:00
|
|
|
});
|
2018-02-24 11:40:44 +08:00
|
|
|
});
|
2020-07-08 14:47:07 +08:00
|
|
|
|
|
|
|
describe('Ribbon', () => {
|
|
|
|
mountTest(Badge.Ribbon);
|
|
|
|
rtlTest(Badge.Ribbon);
|
|
|
|
|
|
|
|
describe('placement', () => {
|
|
|
|
it('works with `start` & `end` placement', () => {
|
|
|
|
const wrapperStart = mount(
|
|
|
|
<Badge.Ribbon placement="start">
|
|
|
|
<div />
|
|
|
|
</Badge.Ribbon>,
|
|
|
|
);
|
|
|
|
expect(wrapperStart.find('.ant-ribbon-placement-start').length).toEqual(1);
|
|
|
|
|
|
|
|
const wrapperEnd = mount(
|
|
|
|
<Badge.Ribbon placement="end">
|
|
|
|
<div />
|
|
|
|
</Badge.Ribbon>,
|
|
|
|
);
|
|
|
|
expect(wrapperEnd.find('.ant-ribbon-placement-end').length).toEqual(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('color', () => {
|
|
|
|
it('works with preset color', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Badge.Ribbon color="green">
|
|
|
|
<div />
|
|
|
|
</Badge.Ribbon>,
|
|
|
|
);
|
|
|
|
expect(wrapper.find('.ant-ribbon-color-green').length).toEqual(1);
|
|
|
|
});
|
|
|
|
it('works with custom color', () => {
|
|
|
|
const wrapperLeft = mount(
|
|
|
|
<Badge.Ribbon color="#888" placement="start">
|
|
|
|
<div />
|
|
|
|
</Badge.Ribbon>,
|
|
|
|
);
|
|
|
|
expect(wrapperLeft.find('.ant-ribbon').prop('style').background).toEqual('#888');
|
|
|
|
expect(wrapperLeft.find('.ant-ribbon-corner').prop('style').color).toEqual('#888');
|
|
|
|
const wrapperRight = mount(
|
|
|
|
<Badge.Ribbon color="#888" placement="end">
|
|
|
|
<div />
|
|
|
|
</Badge.Ribbon>,
|
|
|
|
);
|
|
|
|
expect(wrapperRight.find('.ant-ribbon').prop('style').background).toEqual('#888');
|
|
|
|
expect(wrapperRight.find('.ant-ribbon-corner').prop('style').color).toEqual('#888');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('text', () => {
|
|
|
|
it('works with string', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Badge.Ribbon text="cool">
|
|
|
|
<div />
|
|
|
|
</Badge.Ribbon>,
|
|
|
|
);
|
|
|
|
expect(wrapper.find('.ant-ribbon').text()).toEqual('cool');
|
|
|
|
});
|
|
|
|
it('works with element', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Badge.Ribbon text={<span className="cool" />}>
|
|
|
|
<div />
|
|
|
|
</Badge.Ribbon>,
|
|
|
|
);
|
|
|
|
expect(wrapper.find('.cool').length).toEqual(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|