2018-05-25 15:33:22 +08:00
|
|
|
|
import React from 'react';
|
2018-12-07 20:02:01 +08:00
|
|
|
|
import { Route, Switch, Link, withRouter, MemoryRouter } from 'react-router-dom';
|
2018-05-25 15:33:22 +08:00
|
|
|
|
import { mount } from 'enzyme';
|
2018-05-25 20:18:23 +08:00
|
|
|
|
import Breadcrumb from '../index';
|
2018-05-25 15:33:22 +08:00
|
|
|
|
|
|
|
|
|
const Apps = () => (
|
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',
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const Home = withRouter(props => {
|
2018-05-25 15:33:22 +08:00
|
|
|
|
const { location, history } = props;
|
|
|
|
|
const pathSnippets = location.pathname.split('/').filter(i => i);
|
|
|
|
|
const extraBreadcrumbItems = pathSnippets.map((_, index) => {
|
|
|
|
|
const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
|
|
|
|
|
return (
|
|
|
|
|
<Breadcrumb.Item key={url}>
|
2018-12-07 20:02:01 +08:00
|
|
|
|
<Link to={url}>{breadcrumbNameMap[url]}</Link>
|
2018-05-25 15:33:22 +08:00
|
|
|
|
</Breadcrumb.Item>
|
|
|
|
|
);
|
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const breadcrumbItems = [
|
2018-05-25 20:18:23 +08:00
|
|
|
|
<Breadcrumb.Item key="home">
|
|
|
|
|
<Link to="/">Home</Link>
|
2018-12-07 20:02:01 +08:00
|
|
|
|
</Breadcrumb.Item>,
|
|
|
|
|
].concat(extraBreadcrumbItems);
|
2018-05-25 15:33:22 +08:00
|
|
|
|
return (
|
2018-05-25 20:18:23 +08:00
|
|
|
|
<div className="demo">
|
|
|
|
|
<div className="demo-nav">
|
2018-05-25 15:33:22 +08:00
|
|
|
|
<a onClick={() => history.push('/')}>Home</a>
|
|
|
|
|
<a onClick={() => history.push('/apps')}>Application List</a>
|
|
|
|
|
</div>
|
|
|
|
|
<Switch>
|
2018-05-25 20:18:23 +08:00
|
|
|
|
<Route path="/apps" component={Apps} />
|
2018-05-25 15:33:22 +08:00
|
|
|
|
<Route render={() => <span>Home Page</span>} />
|
|
|
|
|
</Switch>
|
2018-12-07 20:02:01 +08:00
|
|
|
|
<Breadcrumb>{breadcrumbItems}</Breadcrumb>
|
2018-05-25 15:33:22 +08:00
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('react router', () => {
|
|
|
|
|
beforeAll(() => {
|
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
|
});
|
|
|
|
|
// https://github.com/airbnb/enzyme/issues/875
|
|
|
|
|
it('react router 4', () => {
|
|
|
|
|
const wrapper = mount(
|
|
|
|
|
<MemoryRouter initialEntries={['/']} initialIndex={0}>
|
|
|
|
|
<Home />
|
2018-12-07 20:02:01 +08:00
|
|
|
|
</MemoryRouter>,
|
2018-05-25 15:33:22 +08:00
|
|
|
|
);
|
|
|
|
|
expect(wrapper.find('BreadcrumbItem').length).toBe(1);
|
2018-12-07 20:02:01 +08:00
|
|
|
|
expect(
|
|
|
|
|
wrapper
|
|
|
|
|
.find('BreadcrumbItem .ant-breadcrumb-link')
|
|
|
|
|
.at(0)
|
|
|
|
|
.text(),
|
|
|
|
|
).toBe('Home');
|
|
|
|
|
wrapper
|
|
|
|
|
.find('.demo-nav a')
|
|
|
|
|
.at(1)
|
|
|
|
|
.simulate('click');
|
2018-05-25 15:33:22 +08:00
|
|
|
|
expect(wrapper.find('BreadcrumbItem').length).toBe(2);
|
2018-12-07 20:02:01 +08:00
|
|
|
|
expect(
|
|
|
|
|
wrapper
|
|
|
|
|
.find('BreadcrumbItem .ant-breadcrumb-link')
|
|
|
|
|
.at(1)
|
|
|
|
|
.text(),
|
|
|
|
|
).toBe('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 20:02:01 +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',
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
const wrapper = mount(<Breadcrumb routes={routes} params={{ id: 1 }} />);
|
2018-05-25 19:48:37 +08:00
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
|
});
|
2018-05-25 15:33:22 +08:00
|
|
|
|
});
|