import React, { useMemo } from 'react'; import type { RouterProps } from 'react-router-dom'; import { Link, MemoryRouter, Route, Routes, useLocation, useNavigate } from 'react-router-dom'; import Breadcrumb from '../index'; import { render, fireEvent } from '../../../tests/utils'; const Apps: React.FC = () => ( ); const breadcrumbNameMap = { '/apps': 'Application List', '/apps/1': 'Application1', '/apps/2': 'Application2', '/apps/1/detail': 'Detail', '/apps/2/detail': 'Detail', }; describe('react router', () => { beforeAll(() => { jest.useFakeTimers(); }); afterAll(() => { jest.useRealTimers(); }); // https://github.com/airbnb/enzyme/issues/875 it('react router 6', () => { const Home: React.FC = () => { const location = useLocation(); const navigate = useNavigate(); const pathSnippets = location.pathname.split('/').filter(i => i); const extraBreadcrumbItems = pathSnippets.map((_, index) => { const url = `/${pathSnippets.slice(0, index + 1).join('/')}`; return ( {breadcrumbNameMap[url as keyof typeof breadcrumbNameMap]} ); }); const breadcrumbItems = [ Home , ].concat(extraBreadcrumbItems); const componentProps = useMemo( () => ({ component: Apps } as unknown as RouterProps), [], ); const renderProps = useMemo( () => ({ render: () => Home Page } as unknown as RouterProps), [], ); return (
navigate('/')}>Home navigate('/apps')}>Application List
{breadcrumbItems}
); }; const { container } = render( , ); 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( 'Application List', ); }); it('react router 3', () => { 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 { asFragment } = render(); expect(asFragment().firstChild).toMatchSnapshot(); }); });