2022-09-05 19:41:32 +08:00
|
|
|
|
import React, { useMemo } from 'react';
|
|
|
|
|
import type { RouterProps } from 'react-router-dom';
|
2022-06-22 14:57:09 +08:00
|
|
|
|
import { Link, MemoryRouter, Route, Routes, useLocation, useNavigate } from 'react-router-dom';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
|
2022-06-29 14:27:39 +08:00
|
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
2018-05-25 20:18:23 +08:00
|
|
|
|
import Breadcrumb from '../index';
|
2018-05-25 15:33:22 +08:00
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
|
const Apps: React.FC = () => (
|
2018-05-25 20:18:23 +08:00
|
|
|
|
<ul className="app-list">
|
2018-05-25 15:33:22 +08:00
|
|
|
|
<li>
|
2018-05-25 20:18:23 +08:00
|
|
|
|
<Link to="/apps/1">Application1</Link>:<Link to="/apps/1/detail">Detail</Link>
|
2018-05-25 15:33:22 +08:00
|
|
|
|
</li>
|
|
|
|
|
<li>
|
2018-05-25 20:18:23 +08:00
|
|
|
|
<Link to="/apps/2">Application2</Link>:<Link to="/apps/2/detail">Detail</Link>
|
2018-05-25 15:33:22 +08:00
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const breadcrumbNameMap = {
|
|
|
|
|
'/apps': 'Application List',
|
|
|
|
|
'/apps/1': 'Application1',
|
|
|
|
|
'/apps/2': 'Application2',
|
|
|
|
|
'/apps/1/detail': 'Detail',
|
|
|
|
|
'/apps/2/detail': 'Detail',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
describe('react router', () => {
|
|
|
|
|
beforeAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
|
jest.useFakeTimers();
|
2018-05-25 15:33:22 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
|
jest.useRealTimers();
|
2018-05-25 15:33:22 +08:00
|
|
|
|
});
|
2019-11-05 00:19:02 +08:00
|
|
|
|
|
2021-11-11 21:34:07 +08:00
|
|
|
|
it('react router 6', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
|
const Home: React.FC = () => {
|
2021-11-11 21:34:07 +08:00
|
|
|
|
const location = useLocation();
|
|
|
|
|
const navigate = useNavigate();
|
2022-11-19 13:47:33 +08:00
|
|
|
|
const pathSnippets = location.pathname.split('/').filter((i) => i);
|
2019-09-25 10:46:59 +08:00
|
|
|
|
const extraBreadcrumbItems = pathSnippets.map((_, index) => {
|
|
|
|
|
const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
|
|
|
|
|
return (
|
|
|
|
|
<Breadcrumb.Item key={url}>
|
2022-09-05 19:41:32 +08:00
|
|
|
|
<Link to={url}>{breadcrumbNameMap[url as keyof typeof breadcrumbNameMap]}</Link>
|
2019-09-25 10:46:59 +08:00
|
|
|
|
</Breadcrumb.Item>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
const breadcrumbItems = [
|
|
|
|
|
<Breadcrumb.Item key="home">
|
|
|
|
|
<Link to="/">Home</Link>
|
|
|
|
|
</Breadcrumb.Item>,
|
|
|
|
|
].concat(extraBreadcrumbItems);
|
2022-09-05 19:41:32 +08:00
|
|
|
|
const componentProps = useMemo<RouterProps>(
|
2024-04-08 14:04:08 +08:00
|
|
|
|
() => ({ component: Apps }) as unknown as RouterProps,
|
2022-09-05 19:41:32 +08:00
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
const renderProps = useMemo<RouterProps>(
|
2024-04-08 14:04:08 +08:00
|
|
|
|
() => ({ render: () => <span>Home Page</span> }) as unknown as RouterProps,
|
2022-09-05 19:41:32 +08:00
|
|
|
|
[],
|
|
|
|
|
);
|
2019-09-25 10:46:59 +08:00
|
|
|
|
return (
|
|
|
|
|
<div className="demo">
|
|
|
|
|
<div className="demo-nav">
|
2021-11-11 21:34:07 +08:00
|
|
|
|
<a onClick={() => navigate('/')}>Home</a>
|
|
|
|
|
<a onClick={() => navigate('/apps')}>Application List</a>
|
2019-09-25 10:46:59 +08:00
|
|
|
|
</div>
|
2021-11-11 21:34:07 +08:00
|
|
|
|
<Routes>
|
2022-09-05 19:41:32 +08:00
|
|
|
|
<Route path="/apps" {...componentProps} />
|
|
|
|
|
<Route {...renderProps} />
|
2021-11-11 21:34:07 +08:00
|
|
|
|
</Routes>
|
2019-09-25 10:46:59 +08:00
|
|
|
|
<Breadcrumb>{breadcrumbItems}</Breadcrumb>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2021-11-11 21:34:07 +08:00
|
|
|
|
};
|
2022-06-28 18:47:21 +08:00
|
|
|
|
const { container } = render(
|
2018-05-25 15:33:22 +08:00
|
|
|
|
<MemoryRouter initialEntries={['/']} initialIndex={0}>
|
|
|
|
|
<Home />
|
2018-12-07 16:17:45 +08:00
|
|
|
|
</MemoryRouter>,
|
2018-05-25 15:33:22 +08:00
|
|
|
|
);
|
2022-06-28 18:47:21 +08:00
|
|
|
|
expect(container.querySelectorAll('.ant-breadcrumb-link').length).toBe(1);
|
|
|
|
|
expect(container.querySelectorAll('.ant-breadcrumb-link')[0].textContent).toBe('Home');
|
|
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelectorAll('.demo-nav a')[1]);
|
|
|
|
|
|
|
|
|
|
expect(container.querySelectorAll('.ant-breadcrumb-link').length).toBe(2);
|
|
|
|
|
expect(container.querySelectorAll('.ant-breadcrumb-link')[1].textContent).toBe(
|
2020-10-21 10:35:06 +08:00
|
|
|
|
'Application List',
|
|
|
|
|
);
|
2018-05-25 15:33:22 +08:00
|
|
|
|
});
|
2018-05-25 19:48:37 +08:00
|
|
|
|
|
|
|
|
|
it('react router 3', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
|
const routes = [
|
|
|
|
|
{
|
|
|
|
|
name: 'home',
|
|
|
|
|
breadcrumbName: 'Home',
|
|
|
|
|
path: '/',
|
|
|
|
|
childRoutes: [
|
|
|
|
|
{
|
|
|
|
|
name: 'apps',
|
|
|
|
|
breadcrumbName: 'Application List',
|
|
|
|
|
path: 'apps',
|
|
|
|
|
childRoutes: [
|
|
|
|
|
{
|
|
|
|
|
name: 'app',
|
|
|
|
|
breadcrumbName: 'Application:id',
|
|
|
|
|
path: ':id',
|
|
|
|
|
childRoutes: [
|
|
|
|
|
{
|
|
|
|
|
name: 'detail',
|
|
|
|
|
breadcrumbName: 'Detail',
|
|
|
|
|
path: 'detail',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'apps',
|
|
|
|
|
breadcrumbName: 'Application List',
|
|
|
|
|
path: 'apps',
|
|
|
|
|
childRoutes: [
|
|
|
|
|
{
|
|
|
|
|
name: 'app',
|
|
|
|
|
breadcrumbName: 'Application:id',
|
|
|
|
|
path: ':id',
|
|
|
|
|
childRoutes: [
|
|
|
|
|
{
|
|
|
|
|
name: 'detail',
|
|
|
|
|
breadcrumbName: 'Detail',
|
|
|
|
|
path: 'detail',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'app',
|
|
|
|
|
breadcrumbName: 'Application:id',
|
|
|
|
|
path: ':id',
|
|
|
|
|
childRoutes: [
|
|
|
|
|
{
|
|
|
|
|
name: 'detail',
|
|
|
|
|
breadcrumbName: 'Detail',
|
|
|
|
|
path: 'detail',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'detail',
|
|
|
|
|
breadcrumbName: 'Detail',
|
|
|
|
|
path: 'detail',
|
|
|
|
|
},
|
|
|
|
|
];
|
2022-06-28 18:47:21 +08:00
|
|
|
|
const { asFragment } = render(<Breadcrumb routes={routes} params={{ id: 1 }} />);
|
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2018-05-25 19:48:37 +08:00
|
|
|
|
});
|
2018-05-25 15:33:22 +08:00
|
|
|
|
});
|