2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
|
|
|
import accessibilityTest from '../../../tests/shared/accessibilityTest';
|
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';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { render } from '../../../tests/utils';
|
2023-03-05 20:57:49 +08:00
|
|
|
import { resetWarned } from '../../_util/warning';
|
|
|
|
import type { ItemType } from '../Breadcrumb';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Breadcrumb from '../index';
|
2017-01-27 17:08:05 +08:00
|
|
|
|
|
|
|
describe('Breadcrumb', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Breadcrumb);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Breadcrumb);
|
2022-03-31 09:57:33 +08:00
|
|
|
accessibilityTest(Breadcrumb);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2017-03-17 18:56:30 +08:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
errorSpy.mockReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2019-07-31 10:47:34 +08:00
|
|
|
it('warns on non-Breadcrumb.Item and non-Breadcrumb.Separator children', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const MyCom: React.FC = () => <div>foo</div>;
|
2022-04-06 11:07:15 +08:00
|
|
|
render(
|
2017-01-27 17:08:05 +08:00
|
|
|
<Breadcrumb>
|
|
|
|
<MyCom />
|
2018-12-07 16:17:45 +08:00
|
|
|
</Breadcrumb>,
|
2017-01-27 17:08:05 +08:00
|
|
|
);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
2019-07-31 10:47:34 +08:00
|
|
|
"Warning: [antd: Breadcrumb] Only accepts Breadcrumb.Item and Breadcrumb.Separator as it's children",
|
2017-01-27 17:08:05 +08:00
|
|
|
);
|
|
|
|
});
|
2017-02-23 13:43:50 +08:00
|
|
|
|
2023-03-05 20:57:49 +08:00
|
|
|
it('warns on routes', () => {
|
|
|
|
render(
|
|
|
|
<Breadcrumb
|
|
|
|
routes={[
|
|
|
|
{
|
|
|
|
breadcrumbName: 'yyy',
|
|
|
|
} as any,
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Breadcrumb] `routes` is deprecated. Please use `items` instead.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render correct', () => {
|
|
|
|
const { asFragment } = render(
|
|
|
|
<Breadcrumb
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
path: '',
|
|
|
|
title: <span>xxx</span>,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'yyy',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('overlay deprecation warning set', () => {
|
|
|
|
it('legacy jsx', () => {
|
|
|
|
resetWarned();
|
|
|
|
render(
|
|
|
|
<Breadcrumb>
|
|
|
|
<Breadcrumb.Item overlay={<div>menu</div>}>
|
|
|
|
<a href="">General</a>
|
|
|
|
</Breadcrumb.Item>
|
|
|
|
</Breadcrumb>,
|
|
|
|
);
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Breadcrumb.Item] `overlay` is deprecated. Please use `menu` instead.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('items', () => {
|
|
|
|
resetWarned();
|
|
|
|
render(
|
|
|
|
<Breadcrumb
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
overlay: <div>menu</div>,
|
|
|
|
title: 'General',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Breadcrumb.Item] `overlay` is deprecated. Please use `menu` instead.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Breadcrumb.Item deprecation warning', () => {
|
2022-10-23 00:33:45 +08:00
|
|
|
render(
|
|
|
|
<Breadcrumb>
|
2023-03-05 20:57:49 +08:00
|
|
|
<Breadcrumb.Item>Location</Breadcrumb.Item>
|
2022-10-23 00:33:45 +08:00
|
|
|
</Breadcrumb>,
|
|
|
|
);
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
2023-03-05 20:57:49 +08:00
|
|
|
'Warning: [antd: Breadcrumb] `Breadcrumb.Item and Breadcrumb.Separator` is deprecated. Please use `items` instead.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Breadcrumb.separator deprecation warning', () => {
|
|
|
|
render(
|
|
|
|
<Breadcrumb>
|
|
|
|
<Breadcrumb.Separator>:</Breadcrumb.Separator>
|
|
|
|
</Breadcrumb>,
|
|
|
|
);
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Breadcrumb] `Breadcrumb.Item and Breadcrumb.Separator` is deprecated. Please use `items` instead.',
|
2022-10-23 00:33:45 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-01-18 15:39:00 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/40204
|
|
|
|
it('wrong overlay deprecation warning in Dropdown', () => {
|
2023-03-05 20:57:49 +08:00
|
|
|
const menuItems = [
|
2023-01-18 15:39:00 +08:00
|
|
|
{
|
|
|
|
key: '1',
|
|
|
|
label: (
|
|
|
|
<a target="_blank" rel="noopener noreferrer" href="http://www.alipay.com/">
|
|
|
|
General
|
|
|
|
</a>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
render(
|
2023-03-05 20:57:49 +08:00
|
|
|
<Breadcrumb
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
menu: { items: menuItems },
|
|
|
|
title: <a href="">General</a>,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
2023-01-18 15:39:00 +08:00
|
|
|
);
|
|
|
|
expect(errorSpy).not.toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Dropdown] `overlay` is deprecated. Please use `menu` instead.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-02-23 13:43:50 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/5015
|
|
|
|
it('should allow Breadcrumb.Item is null or undefined', () => {
|
2022-04-06 11:07:15 +08:00
|
|
|
const { asFragment } = render(
|
2017-02-23 13:43:50 +08:00
|
|
|
<Breadcrumb>
|
|
|
|
{null}
|
|
|
|
<Breadcrumb.Item>Home</Breadcrumb.Item>
|
|
|
|
{undefined}
|
2018-12-07 16:17:45 +08:00
|
|
|
</Breadcrumb>,
|
2017-02-23 13:43:50 +08:00
|
|
|
);
|
2022-04-06 11:07:15 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2017-02-23 13:43:50 +08:00
|
|
|
});
|
2017-03-29 15:13:20 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/5542
|
|
|
|
it('should not display Breadcrumb Item when its children is falsy', () => {
|
2022-06-28 18:47:21 +08:00
|
|
|
const { asFragment } = render(
|
2023-03-05 20:57:49 +08:00
|
|
|
<Breadcrumb
|
|
|
|
items={[
|
|
|
|
{} as any,
|
|
|
|
{
|
|
|
|
title: 'xxx',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'yyy',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
2017-03-29 15:13:20 +08:00
|
|
|
);
|
2022-06-28 18:47:21 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2017-03-29 15:13:20 +08:00
|
|
|
});
|
2019-05-06 12:04:39 +08:00
|
|
|
|
2019-08-20 15:58:37 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/18260
|
|
|
|
it('filter React.Fragment', () => {
|
2022-06-28 18:47:21 +08:00
|
|
|
const { asFragment } = render(
|
2019-08-20 15:58:37 +08:00
|
|
|
<Breadcrumb separator="">
|
|
|
|
<Breadcrumb.Item>Location</Breadcrumb.Item>
|
|
|
|
<Breadcrumb.Separator>:</Breadcrumb.Separator>
|
|
|
|
<>
|
|
|
|
<Breadcrumb.Item href="">Application Center</Breadcrumb.Item>
|
|
|
|
<Breadcrumb.Separator />
|
|
|
|
</>
|
|
|
|
</Breadcrumb>,
|
|
|
|
);
|
2022-06-28 18:47:21 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-08-20 15:58:37 +08:00
|
|
|
});
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
it('should render a menu', () => {
|
2023-03-05 20:57:49 +08:00
|
|
|
const items: ItemType[] = [
|
2019-05-06 12:04:39 +08:00
|
|
|
{
|
|
|
|
path: 'index',
|
2023-03-05 20:57:49 +08:00
|
|
|
title: 'home',
|
2019-05-06 12:04:39 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'first',
|
2023-03-05 20:57:49 +08:00
|
|
|
title: 'first',
|
|
|
|
menu: {
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
path: '/general',
|
|
|
|
title: 'General',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/layout',
|
|
|
|
title: 'Layout',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/navigation',
|
|
|
|
title: 'Navigation',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-05-06 12:04:39 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'second',
|
2023-03-05 20:57:49 +08:00
|
|
|
title: 'second',
|
2019-05-06 12:04:39 +08:00
|
|
|
},
|
2020-03-29 22:17:55 +08:00
|
|
|
{
|
|
|
|
path: 'third',
|
2023-03-05 20:57:49 +08:00
|
|
|
title: '',
|
2020-03-29 22:17:55 +08:00
|
|
|
},
|
2019-05-06 12:04:39 +08:00
|
|
|
];
|
2023-03-05 20:57:49 +08:00
|
|
|
const { asFragment } = render(<Breadcrumb items={items} />);
|
2022-06-28 18:47:21 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-05-06 12:04:39 +08:00
|
|
|
});
|
2019-07-09 11:46:21 +08:00
|
|
|
|
2023-03-05 20:57:49 +08:00
|
|
|
it('should accept undefined items', () => {
|
|
|
|
const { asFragment } = render(<Breadcrumb items={undefined!} />);
|
2022-06-28 18:47:21 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-07-09 11:46:21 +08:00
|
|
|
});
|
2019-09-26 22:28:08 +08:00
|
|
|
|
2019-09-23 11:52:29 +08:00
|
|
|
it('should support custom attribute', () => {
|
2022-06-28 18:47:21 +08:00
|
|
|
const { asFragment } = render(
|
2023-03-05 20:57:49 +08:00
|
|
|
(
|
|
|
|
<Breadcrumb
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
title: 'xxx',
|
|
|
|
// @ts-ignore
|
|
|
|
'data-custom': 'custom-item',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'yyy',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
data-custom="custom"
|
|
|
|
/>
|
|
|
|
) as React.ReactElement<any, string | React.JSXElementConstructor<any>>,
|
2019-09-23 11:52:29 +08:00
|
|
|
);
|
2022-06-28 18:47:21 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-09-23 11:52:29 +08:00
|
|
|
});
|
2020-03-29 22:17:55 +08:00
|
|
|
|
|
|
|
it('should support React.Fragment and falsy children', () => {
|
2022-06-28 18:47:21 +08:00
|
|
|
const { asFragment } = render(
|
2020-03-29 22:17:55 +08:00
|
|
|
<Breadcrumb>
|
|
|
|
<>
|
|
|
|
<Breadcrumb.Item>yyy</Breadcrumb.Item>
|
|
|
|
<Breadcrumb.Item>yyy</Breadcrumb.Item>
|
|
|
|
</>
|
|
|
|
<Breadcrumb.Item>yyy</Breadcrumb.Item>
|
|
|
|
{0}
|
|
|
|
{null}
|
|
|
|
{undefined}
|
|
|
|
</Breadcrumb>,
|
|
|
|
);
|
2022-06-28 18:47:21 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2020-03-29 22:17:55 +08:00
|
|
|
});
|
2020-08-03 21:34:29 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/25975
|
|
|
|
it('should support Breadcrumb.Item default separator', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const MockComponent: React.FC = () => (
|
2020-12-09 17:12:32 +08:00
|
|
|
<span>
|
|
|
|
<Breadcrumb.Item>Mock Node</Breadcrumb.Item>
|
|
|
|
</span>
|
|
|
|
);
|
2022-06-28 18:47:21 +08:00
|
|
|
const { asFragment } = render(
|
2020-08-03 21:34:29 +08:00
|
|
|
<Breadcrumb>
|
|
|
|
<Breadcrumb.Item>Location</Breadcrumb.Item>
|
|
|
|
<MockComponent />
|
|
|
|
<Breadcrumb.Item>Application Center</Breadcrumb.Item>
|
2020-08-03 21:53:13 +08:00
|
|
|
</Breadcrumb>,
|
2020-08-03 21:34:29 +08:00
|
|
|
);
|
2022-06-28 18:47:21 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2020-08-03 21:34:29 +08:00
|
|
|
});
|
2023-03-05 20:57:49 +08:00
|
|
|
|
2023-03-26 12:01:14 +08:00
|
|
|
it('should support Breadcrumb.Item customized menu items key', () => {
|
2023-03-25 14:00:20 +08:00
|
|
|
const key = 'test-key';
|
|
|
|
const { container } = render(
|
|
|
|
<Breadcrumb>
|
2023-03-26 12:01:14 +08:00
|
|
|
<Breadcrumb.Item dropdownProps={{ open: true }} menu={{ items: [{ key }] }}>
|
2023-03-25 14:00:20 +08:00
|
|
|
test-item
|
|
|
|
</Breadcrumb.Item>
|
|
|
|
</Breadcrumb>,
|
|
|
|
);
|
|
|
|
|
2023-03-26 12:01:14 +08:00
|
|
|
const item = container.querySelector<HTMLElement>('.ant-dropdown-menu-item');
|
2023-03-25 14:00:20 +08:00
|
|
|
|
|
|
|
expect(item?.getAttribute('data-menu-id')?.endsWith(key)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-10-14 11:37:48 +08:00
|
|
|
it('should support string `0` and number `0`', () => {
|
|
|
|
const { container } = render(
|
2023-03-05 20:57:49 +08:00
|
|
|
<Breadcrumb
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
title: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: '0',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
2022-10-14 11:37:48 +08:00
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.ant-breadcrumb-link')[0].textContent).toBe('0');
|
|
|
|
expect(container.querySelectorAll('.ant-breadcrumb-link')[1].textContent).toBe('0');
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
|
|
|
});
|
2022-11-15 15:10:47 +08:00
|
|
|
|
2022-11-15 15:51:34 +08:00
|
|
|
it('should console Error when `overlay` in props', () => {
|
2023-03-05 20:57:49 +08:00
|
|
|
resetWarned();
|
2022-11-15 15:10:47 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
render(
|
|
|
|
<Breadcrumb>
|
|
|
|
<Breadcrumb.Item overlay={<div>test</div>} />
|
|
|
|
</Breadcrumb>,
|
|
|
|
);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Breadcrumb.Item] `overlay` is deprecated. Please use `menu` instead.',
|
|
|
|
);
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
2022-11-15 15:51:34 +08:00
|
|
|
|
|
|
|
it('should not console Error when `overlay` not in props', () => {
|
2022-11-15 15:10:47 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2023-03-05 20:57:49 +08:00
|
|
|
render(<Breadcrumb items={[{ path: '/', title: 'Test' }]} />);
|
2022-11-15 15:10:47 +08:00
|
|
|
expect(errSpy).not.toHaveBeenCalled();
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
2023-03-17 11:26:36 +08:00
|
|
|
|
|
|
|
it('should use `onClick`', async () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
const wrapper = render(<Breadcrumb items={[{ title: 'test', onClick }]} />);
|
|
|
|
const item = await wrapper.findByText('test');
|
|
|
|
item.click();
|
|
|
|
expect(onClick).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
it('should use `className`', async () => {
|
|
|
|
const testClassName = 'testClassName';
|
|
|
|
const wrapper = render(<Breadcrumb items={[{ title: 'test', className: testClassName }]} />);
|
|
|
|
const item = await wrapper.findByText('test');
|
|
|
|
expect(item).toHaveClass(testClassName);
|
|
|
|
});
|
2023-03-21 16:00:28 +08:00
|
|
|
|
|
|
|
it('Breadcrumb.Item menu type', () => {
|
|
|
|
expect(<Breadcrumb.Item menu={{ selectable: true }} />).toBeTruthy();
|
|
|
|
});
|
2017-01-27 17:08:05 +08:00
|
|
|
});
|