From 076540c83e495b2277db42030c27dfc4c35596cb Mon Sep 17 00:00:00 2001 From: afc163 Date: Fri, 25 May 2018 15:33:22 +0800 Subject: [PATCH] test: breadcrumd work with react-router --- .../breadcrumb/__tests__/router.test.js | 81 +++++++++++++++++++ package.json | 1 + 2 files changed, 82 insertions(+) create mode 100644 components/breadcrumb/__tests__/router.test.js diff --git a/components/breadcrumb/__tests__/router.test.js b/components/breadcrumb/__tests__/router.test.js new file mode 100644 index 0000000000..907670e548 --- /dev/null +++ b/components/breadcrumb/__tests__/router.test.js @@ -0,0 +1,81 @@ +import React from 'react'; +import { Route, Switch, Link, withRouter, MemoryRouter } from 'react-router-dom'; +import { Breadcrumb } from 'antd'; +import { mount } from 'enzyme'; + +const Apps = () => ( + +); + +const breadcrumbNameMap = { + '/apps': 'Application List', + '/apps/1': 'Application1', + '/apps/2': 'Application2', + '/apps/1/detail': 'Detail', + '/apps/2/detail': 'Detail', +}; + +const Home = withRouter((props) => { + 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 ( + + + {breadcrumbNameMap[url]} + + + ); + }); + const breadcrumbItems = [( + + Home + + )].concat(extraBreadcrumbItems); + return ( +
+
+ history.push('/')}>Home + history.push('/apps')}>Application List +
+ + + Home Page} /> + + + {breadcrumbItems} + +
+ ); +}); + +describe('react router', () => { + beforeAll(() => { + jest.useFakeTimers(); + }); + + afterAll(() => { + jest.useRealTimers(); + }); + // https://github.com/airbnb/enzyme/issues/875 + it('react router 4', () => { + const wrapper = mount( + + + + ); + expect(wrapper.find('BreadcrumbItem').length).toBe(1); + expect(wrapper.find('BreadcrumbItem .ant-breadcrumb-link').at(0).text()).toBe('Home'); + wrapper.find('.demo-nav a').at(1).simulate('click'); + expect(wrapper.find('BreadcrumbItem').length).toBe(2); + expect(wrapper.find('BreadcrumbItem .ant-breadcrumb-link').at(1).text()).toBe('Application List'); + }); +}); diff --git a/package.json b/package.json index b22c576ce7..9b9357cefb 100644 --- a/package.json +++ b/package.json @@ -153,6 +153,7 @@ "react-github-button": "^0.1.1", "react-infinite-scroller": "^1.0.15", "react-intl": "^2.0.1", + "react-router-dom": "^4.2.2", "react-sublime-video": "^0.2.0", "react-virtualized": "~9.19.0", "remark-frontmatter": "^1.1.0",