import React from 'react';
import { mount } from 'enzyme';
import { act } from 'react-dom/test-utils';
import { fireEvent, render } from '@testing-library/react';
import Badge from '../index';
import Tooltip from '../../tooltip';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
describe('Badge', () => {
mountTest(Badge);
rtlTest(Badge);
rtlTest(() => (
head
));
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('badge dot not scaling count > 9', () => {
const badge = mount();
expect(badge.find('.ant-card-multiple-words').length).toBe(0);
});
it('badge should support float number', () => {
let wrapper = mount();
expect(wrapper.find('.ant-badge-multiple-words').first().text()).toEqual('3.5');
wrapper = mount();
expect(wrapper.find('.ant-badge-multiple-words').first().text()).toEqual('3.5');
expect(() => wrapper.unmount()).not.toThrow();
});
it('badge dot not showing count == 0', () => {
const badge = mount();
expect(badge.find('.ant-badge-dot').length).toBe(0);
});
it('should have an overriden title attribute', () => {
const badge = mount();
expect(
badge.find('.ant-scroll-number').getDOMNode().attributes.getNamedItem('title').value,
).toEqual('Custom title');
});
// https://github.com/ant-design/ant-design/issues/10626
it('should be composable with Tooltip', () => {
const ref = React.createRef();
const { container } = render(
,
);
act(() => {
fireEvent.mouseEnter(container.querySelector('.ant-badge'));
jest.runAllTimers();
});
expect(ref.current.props.visible).toBeTruthy();
});
it('should render when count is changed', () => {
const wrapper = mount();
function updateMatch(count) {
wrapper.setProps({ count });
act(() => {
jest.runAllTimers();
wrapper.update();
expect(wrapper.render()).toMatchSnapshot();
});
}
updateMatch(10);
updateMatch(11);
updateMatch(11);
updateMatch(111);
updateMatch(10);
updateMatch(9);
});
it('should be compatible with borderColor style', () => {
const wrapper = mount(
,
);
expect(wrapper.render()).toMatchSnapshot();
});
// https://github.com/ant-design/ant-design/issues/13694
it('should support offset when count is a ReactNode', () => {
const wrapper = mount(
} offset={[10, 20]}>
head
,
);
expect(wrapper.render()).toMatchSnapshot();
});
// https://github.com/ant-design/ant-design/issues/15349
it('should color style works on Badge', () => {
const wrapper = mount();
expect(wrapper.find('.ant-badge-status-text').props().style.color).toBe('red');
});
// https://github.com/ant-design/ant-design/issues/15799
it('render correct with negative number', () => {
const wrapper = mount(
,
);
expect(wrapper.render()).toMatchSnapshot();
});
// https://github.com/ant-design/ant-design/issues/21331
// https://github.com/ant-design/ant-design/issues/31590
it('render Badge status/color when contains children', () => {
const wrapper = mount(
,
);
expect(wrapper.render()).toMatchSnapshot();
expect(wrapper.find(Badge).at(0).find('.ant-scroll-number-only-unit').text()).toBe('5');
expect(wrapper.find(Badge).at(1).find('.ant-scroll-number-only-unit').text()).toBe('5');
expect(wrapper.find(Badge).at(2).find('.ant-scroll-number-only-unit').text()).toBe('5');
});
it('Badge should work when status/color is empty string', () => {
const wrapper = mount(
<>
>,
);
expect(wrapper.find('.ant-badge')).toHaveLength(2);
});
});