2018-02-24 11:40:44 +08:00
|
|
|
import React from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
2024-04-09 16:49:47 +08:00
|
|
|
import type { GetRef } from '../../_util/type';
|
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';
|
2024-05-11 13:54:40 +08:00
|
|
|
import { act, fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Tooltip from '../../tooltip';
|
|
|
|
import Badge from '../index';
|
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(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2018-05-23 13:11:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2018-05-23 13:11:49 +08:00
|
|
|
});
|
|
|
|
|
2024-05-11 13:54:40 +08:00
|
|
|
it('no strict warning', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2024-05-11 13:54:40 +08:00
|
|
|
const Comp = () => {
|
|
|
|
const [count, setCount] = React.useState<number | null>(9999);
|
2023-01-20 21:06:13 +08:00
|
|
|
|
2024-05-11 13:54:40 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Badge count={count}>
|
|
|
|
<span>Badge</span>
|
|
|
|
</Badge>
|
|
|
|
|
|
|
|
<br />
|
|
|
|
<br />
|
|
|
|
<br />
|
|
|
|
|
|
|
|
<button type="button" onClick={() => setCount(null)}>
|
|
|
|
click
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const { container } = render(<Comp />);
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('button')!);
|
|
|
|
await waitFakeTimer();
|
2023-01-20 21:06:13 +08:00
|
|
|
|
|
|
|
expect(errSpy).not.toHaveBeenCalled();
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2018-05-25 16:04:38 +08:00
|
|
|
it('badge dot not scaling count > 9', () => {
|
2022-06-15 10:43:56 +08:00
|
|
|
const { container } = render(<Badge count={10} dot />);
|
|
|
|
expect(container.querySelectorAll('.ant-card-multiple-words').length).toBe(0);
|
2018-02-24 11:40:44 +08:00
|
|
|
});
|
2018-04-10 01:09:18 +08:00
|
|
|
|
2019-01-09 22:32:06 +08:00
|
|
|
it('badge should support float number', () => {
|
2022-06-15 10:43:56 +08:00
|
|
|
const { container } = render(<Badge count={3.5} />);
|
|
|
|
expect(container.querySelectorAll('.ant-badge-multiple-words')[0].textContent).toEqual('3.5');
|
2020-11-18 17:23:46 +08:00
|
|
|
|
2022-06-15 10:43:56 +08:00
|
|
|
const { container: anotherContainer, unmount } = render(<Badge count="3.5" />);
|
|
|
|
expect(anotherContainer.querySelectorAll('.ant-badge-multiple-words')[0].textContent).toEqual(
|
|
|
|
'3.5',
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(() => 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', () => {
|
2022-06-15 10:43:56 +08:00
|
|
|
const { container } = render(<Badge count={0} dot />);
|
|
|
|
expect(container.querySelectorAll('.ant-badge-dot').length).toBe(0);
|
2018-02-24 11:40:44 +08:00
|
|
|
});
|
2018-04-10 01:09:18 +08:00
|
|
|
|
2023-02-23 21:56:43 +08:00
|
|
|
it('should have an overridden title attribute', () => {
|
2022-06-15 10:43:56 +08:00
|
|
|
const { container } = render(<Badge count={10} title="Custom title" />);
|
|
|
|
expect((container.querySelector('.ant-scroll-number')! as HTMLElement).title).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', () => {
|
2024-04-09 16:49:47 +08:00
|
|
|
const ref = React.createRef<GetRef<typeof Tooltip>>();
|
2022-04-17 22:36:36 +08:00
|
|
|
const { container } = render(
|
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
|
|
|
);
|
2021-01-29 14:27:36 +08:00
|
|
|
|
|
|
|
act(() => {
|
2022-06-15 10:43:56 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-badge')!);
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2021-01-29 14:27:36 +08:00
|
|
|
});
|
2023-02-15 10:21:28 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-open')).toBeTruthy();
|
2018-05-23 13:11:49 +08:00
|
|
|
});
|
2018-05-25 17:00:32 +08:00
|
|
|
|
|
|
|
it('should render when count is changed', () => {
|
2022-06-15 10:43:56 +08:00
|
|
|
const { asFragment, rerender } = render(<Badge count={9} />);
|
2021-01-29 14:27:36 +08:00
|
|
|
|
2022-06-15 10:43:56 +08:00
|
|
|
function updateMatch(count: number) {
|
|
|
|
rerender(<Badge count={count} />);
|
2021-01-29 14:27:36 +08:00
|
|
|
|
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-06-15 10:43:56 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2021-01-29 14:27:36 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMatch(10);
|
|
|
|
updateMatch(11);
|
|
|
|
updateMatch(11);
|
|
|
|
updateMatch(111);
|
|
|
|
updateMatch(10);
|
|
|
|
updateMatch(9);
|
2018-05-25 17:00:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be compatible with borderColor style', () => {
|
2022-06-15 10:43:56 +08:00
|
|
|
const { asFragment } = render(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Badge
|
|
|
|
count={4}
|
|
|
|
style={{ backgroundColor: '#fff', color: '#999', borderColor: '#d9d9d9' }}
|
|
|
|
/>,
|
|
|
|
);
|
2022-06-15 10:43:56 +08:00
|
|
|
expect(asFragment().firstChild).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', () => {
|
2022-06-15 10:43:56 +08:00
|
|
|
const { asFragment } = render(
|
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>,
|
|
|
|
);
|
2022-06-15 10:43:56 +08:00
|
|
|
expect(asFragment().firstChild).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', () => {
|
2022-06-15 10:43:56 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Badge style={{ color: 'red' }} status="success" text="Success" />,
|
|
|
|
);
|
|
|
|
expect((container.querySelector('.ant-badge-status-text')! as HTMLElement).style.color).toEqual(
|
|
|
|
'red',
|
|
|
|
);
|
2019-03-14 15:53:51 +08:00
|
|
|
});
|
2019-04-02 19:01:12 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/15799
|
|
|
|
it('render correct with negative number', () => {
|
2022-06-15 10:43:56 +08:00
|
|
|
const { asFragment } = render(
|
2019-04-02 19:01:12 +08:00
|
|
|
<div>
|
|
|
|
<Badge count="-10" />
|
|
|
|
<Badge count={-10} />
|
|
|
|
</div>,
|
|
|
|
);
|
2022-06-15 10:43:56 +08:00
|
|
|
expect(asFragment().firstChild).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
|
2021-08-02 12:22:30 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/31590
|
2020-02-11 12:56:18 +08:00
|
|
|
it('render Badge status/color when contains children', () => {
|
2022-06-15 10:43:56 +08:00
|
|
|
const { container, asFragment } = render(
|
2020-11-18 17:23:46 +08:00
|
|
|
<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
|
|
|
);
|
2022-06-15 10:43:56 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
|
|
|
expect(container.querySelectorAll('.ant-scroll-number-only-unit')[0].textContent).toBe('5');
|
|
|
|
expect(container.querySelectorAll('.ant-scroll-number-only-unit')[1].textContent).toBe('5');
|
|
|
|
expect(container.querySelectorAll('.ant-scroll-number-only-unit')[2].textContent).toBe('5');
|
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', () => {
|
2022-06-15 10:43:56 +08:00
|
|
|
const { container } = render(
|
2020-08-26 11:27:07 +08:00
|
|
|
<>
|
|
|
|
<Badge color="" text="text" />
|
2022-06-15 10:43:56 +08:00
|
|
|
<Badge status={'' as any} text="text" />
|
2020-08-26 11:27:07 +08:00
|
|
|
</>,
|
|
|
|
);
|
|
|
|
|
2022-06-15 10:43:56 +08:00
|
|
|
expect(container.querySelectorAll('.ant-badge')).toHaveLength(2);
|
2020-08-26 11:27:07 +08:00
|
|
|
});
|
2022-09-19 11:52:17 +08:00
|
|
|
|
|
|
|
it('Badge should display count when color and count are both exist', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<>
|
|
|
|
<Badge className="badge1" text="badge" color="pink" count={44} />
|
|
|
|
<Badge className="badge2" text="badge" color="pink" count={0} />
|
|
|
|
<Badge className="badge3" text="badge" color="pink" />
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(container.querySelectorAll('.ant-badge-count')).toHaveLength(1);
|
|
|
|
expect(container.querySelectorAll('[title="44"]')).toHaveLength(1);
|
|
|
|
expect(container.querySelectorAll('.ant-badge-status-dot')).toHaveLength(2);
|
|
|
|
});
|
2022-09-28 12:01:20 +08:00
|
|
|
|
|
|
|
it('Badge not render status-text when text is empty string', () => {
|
|
|
|
const { container } = render(<Badge status="default" text={undefined} />);
|
|
|
|
|
|
|
|
expect(container.querySelectorAll('.ant-badge > .ant-badge-status-text')).toHaveLength(0);
|
2022-09-28 14:37:04 +08:00
|
|
|
});
|
2022-11-24 23:53:22 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/38965
|
|
|
|
it('should display custom color and number is 0', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<>
|
|
|
|
<Badge count={0} showZero color="#ff0" />
|
|
|
|
<Badge count={0} showZero color="blue" />
|
|
|
|
<Badge count={0} showZero />
|
2023-01-20 21:06:13 +08:00
|
|
|
<Badge count={0} showZero color="green">
|
2022-11-24 23:53:22 +08:00
|
|
|
<div />
|
|
|
|
</Badge>
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
expect(container.querySelectorAll('.ant-badge-count')).toHaveLength(4);
|
|
|
|
expect(container.querySelectorAll('[title="0"]')).toHaveLength(4);
|
|
|
|
});
|
2023-06-29 10:40:56 +08:00
|
|
|
|
|
|
|
it('should support classNames and styles', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Badge
|
|
|
|
count={10}
|
|
|
|
classNames={{
|
|
|
|
root: 'test-root',
|
|
|
|
indicator: 'test-indicator',
|
|
|
|
}}
|
|
|
|
styles={{
|
|
|
|
root: { backgroundColor: 'yellow' },
|
|
|
|
indicator: { backgroundColor: 'blue' },
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
test
|
|
|
|
</Badge>,
|
|
|
|
);
|
|
|
|
|
|
|
|
const element = container.querySelector<HTMLSpanElement>('.ant-badge');
|
|
|
|
|
|
|
|
// classNames
|
|
|
|
expect(element).toHaveClass('test-root');
|
|
|
|
expect(element?.querySelector<HTMLElement>('sup')).toHaveClass('test-indicator');
|
|
|
|
|
|
|
|
// styles
|
|
|
|
expect(element).toHaveStyle({ backgroundColor: 'yellow' });
|
2023-07-26 17:50:37 +08:00
|
|
|
expect(element?.querySelector<HTMLElement>('sup')).toHaveStyle({ backgroundColor: 'blue' });
|
2023-06-29 10:40:56 +08:00
|
|
|
});
|
2018-02-24 11:40:44 +08:00
|
|
|
});
|