import { renderHook, act } from '@testing-library/react-hooks'; import {useHistory} from './useHistory'; import { RouterProvider } from './provider'; describe('验证路由', () => { it('验证useHistory所有的方法', () => { const { result } = renderHook(() => useHistory(), {wrapper: RouterProvider}); // 注册路由 / act(() => { result.current.registerRoute({ path: '/', title: '桌面', }); }); expect(result.current.state.routes).toEqual({ '/': { path: '/', url: '/', title: '桌面', moduleId: '', moduleName: '', menu: '', isMatch: false, children: {} } }); // 注册路由 /moduleList act(() => { result.current.registerRoute({ path: '/moduleList', title: '云中台', }); }); expect(result.current.state.routes).toEqual({ '/': { path: '/', url: '/', title: '桌面', moduleId: '', moduleName: '', menu: '', isMatch: false, children: {} }, '/moduleList': { path: '/moduleList', url: '/moduleList', title: '云中台', moduleId: '', moduleName: '', menu: '', isMatch: false, children: {} } }); // 注册路由 /:moduleName/:moduleId/:menu? act(() => { result.current.registerRoute({ path: '/:moduleName/:moduleId/:menu?', title: '云模块' }) }); expect(result.current.state.history).toHaveLength(0); expect(result.current.state.routes).toEqual({ '/': { path: '/', url: '/', title: '桌面', moduleId: '', moduleName: '', menu: '', isMatch: false, children: {} }, '/moduleList': { path: '/moduleList', url: '/moduleList', title: '云中台', moduleId: '', moduleName: '', menu: '', isMatch: false, children: {} }, '/:moduleName/:moduleId/:menu?': { path: '/:moduleName/:moduleId/:menu?', url: '/:moduleName/:moduleId/:menu?', title: '云模块', moduleId: '', moduleName: '', menu: '', isMatch: false, children: {} } }); // 点击导航:云中台 act(() => { result.current.goto('/moduleList') }); expect(result.current.state.history).toEqual(['/moduleList']); console.log('点击导航:云中台', result.current.state.history) expect(result.current.state.current).toEqual({ path: '/moduleList', url: '/moduleList', title: '云中台', moduleId: '', moduleName: '', menu: '', isMatch: false, children: {} }); // 点击模块:商品基础资料 act(() => { result.current.goto('/goodsBasicInfo/DM27005074D931174416476959029971'); }); expect(result.current.state.history).toEqual(['/moduleList', '/:moduleName/:moduleId/:menu?']); expect(result.current.state.current).toEqual({ path: '/:moduleName/:moduleId/:menu?', url: '/goodsBasicInfo/DM27005074D931174416476959029971', title: '云模块', moduleId: 'DM27005074D931174416476959029971', moduleName: 'goodsBasicInfo', menu: '', isMatch: true, search: '', children: {} }); // 注册路由 /goodsBasicInfo/DM27005074D931174416476959029971 act(() => { result.current.registerRoute({ parentPath: '/:moduleName/:moduleId/:menu?', path: '/goodsManage/categoryManage', title: '类目管理', moduleId: 'DM27005074D931174416476959029971', moduleName: 'goodsBasicInfo', isDefault: true }) }); expect(result.current.state.current).toEqual({ path: '/goodsManage/categoryManage', url: '/goodsBasicInfo/DM27005074D931174416476959029971', title: '类目管理', moduleId: 'DM27005074D931174416476959029971', moduleName: 'goodsBasicInfo', menu: '', isMatch: true, children: {}, search: '' }); expect(result.current.state.history).toEqual(['/moduleList', '/:moduleName/:moduleId/:menu?']) expect(result.current.state.routes).toEqual({ '/': { path: '/', url: '/', title: '桌面', moduleId: '', moduleName: '', menu: '', isMatch: false, children: {} }, '/moduleList': { path: '/moduleList', url: '/moduleList', title: '云中台', moduleId: '', moduleName: '', menu: '', isMatch: false, children: {} }, '/:moduleName/:moduleId/:menu?': { path: '/:moduleName/:moduleId/:menu?', url: '/:moduleName/:moduleId/:menu?', title: '云模块', moduleId: 'DM27005074D931174416476959029971', moduleName: 'goodsBasicInfo', menu: '', default: '/goodsManage/categoryManage', isMatch: false, children: { '/goodsManage/categoryManage': { path: '/goodsManage/categoryManage', url: '/goodsBasicInfo/DM27005074D931174416476959029971', title: '类目管理', moduleId: 'DM27005074D931174416476959029971', moduleName: 'goodsBasicInfo', menu: 'goodsManagecategoryManage', isMatch: false, isDefault: true, children: {} } } } }); }); });