1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
| import Router from "../Router.ts"; import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
const HELLO = { template: '<div>Hello World</div>' };
describe('路由初始化', () => { let addEventListenerSpy; let originalPathname; let originalHash;
beforeEach(() => { originalPathname = window.location.pathname; originalHash = window.location.hash; addEventListenerSpy = vi.spyOn(window, 'addEventListener'); });
afterEach(() => { window.history.pushState({}, '', originalPathname); window.location.hash = originalHash; addEventListenerSpy.mockRestore(); });
it('路由构造函数默认值', () => { const router = new Router(); expect(router.mode).toBe('history'); expect(router.routes.size).toBe(0); });
it('路由构造函数自定义值', () => { const routes = [{ path: '/home', component: HELLO }]; const router = new Router({ mode: 'hash', routes }); expect(router.mode).toBe('hash'); expect(router.routes.size).toBe(1); });
it('处理路由变化时调用错误处理程序', async () => { const router = new Router(); const errorHandler = vi.fn(); const error = new Error('测试错误');
router.onError(errorHandler); router.beforeEach(() => { throw error; });
await router.handleRouteChange('/home');
expect(errorHandler).toHaveBeenCalledWith(error); });
it('readyResolve 在路由变化后应只调用一次', async () => { const router = new Router(); const readyResolveSpy = vi.spyOn(router, 'readyResolve');
router.addRoute('/home', HELLO); await router.push('/home');
expect(readyResolveSpy).toHaveBeenCalledTimes(1); });
it('convertToRegex 应该正确转换动态路由', () => { const router = new Router(); const regex = router.convertToRegex('/home/:id'); expect(regex).toBe('/home/([^\\s/]+)'); });
it('getCurrentPath 在不同模式下应返回正确路径', () => { const routerHistory = new Router({ mode: 'history' }); window.history.pushState({}, '', '/home'); expect(routerHistory.getCurrentPath()).toBe('/home');
const routerHash = new Router({ mode: 'hash' }); window.location.hash = '#/home'; expect(routerHash.getCurrentPath()).toBe('/home'); });
it('install 应该将路由器注入 Vue 应用', () => { const app = { config: { globalProperties: {} }, provide: vi.fn(),component: vi.fn()}; const router = new Router(); router.install(app); expect(app.config.globalProperties.$router).toBe(router); expect(app.provide).toHaveBeenCalledWith('router', router); });
it('init 方法应设置事件监听器并调用 onRouteChange', async () => { const routerHistory = new Router({ mode: 'history' }); expect(addEventListenerSpy).toHaveBeenCalledWith('popstate', expect.any(Function));
const routerHash = new Router({ mode: 'hash' }); expect(addEventListenerSpy).toHaveBeenCalledWith('hashchange', expect.any(Function));
const onRouteChangeSpyHistory = vi.spyOn(routerHistory, 'onRouteChange'); const onRouteChangeSpyHash = vi.spyOn(routerHash, 'onRouteChange'); await routerHistory.init(); await routerHash.init(); expect(onRouteChangeSpyHistory).toHaveBeenCalled(); expect(onRouteChangeSpyHash).toHaveBeenCalled(); });
it('matchRouteComponent 对于未匹配的路径应返回 null', () => { const router = new Router(); expect(router.matchRouteComponent('/unknown')).toBeNull(); });
it('在路由变化时 currentComponent 应该更新', async () => { const router = new Router(); router.addRoute('/home', HELLO); await router.push('/home'); expect(router.currentComponent.value).toEqual(HELLO); });
it('isReady 应该在路由变化后解析', async () => { const router = new Router(); const readyPromise = router.isReady(); await router.push('/home'); await expect(readyPromise).resolves.toBeUndefined(); });
it('测试 navigation 方法(go, back, forward)', () => { const router = new Router(); const goSpy = vi.spyOn(window.history, 'go'); const backSpy = vi.spyOn(window.history, 'back'); const forwardSpy = vi.spyOn(window.history, 'forward');
router.go(1); expect(goSpy).toHaveBeenCalledWith(1);
router.back(); expect(backSpy).toHaveBeenCalled();
router.forward(); expect(forwardSpy).toHaveBeenCalled(); });
it('router.addRoute 应该成功添加路由', () => { const router = new Router(); router.addRoute('/home', HELLO); expect(router.routes).toEqual(new Map([['/home', HELLO]])); });
it('router.addRoute 接受的不是个组件', () => { const router = new Router(); expect(() => { router.addRoute('/home', 'HELLO'); }).toThrowError('component is not a Vue component'); });
it('router.push 和 replace 方法应更新路径并调用 onRouteChange', async () => { const router = new Router(); router.addRoute('/home', HELLO); const onRouteChangeSpy = vi.spyOn(router, 'onRouteChange');
await router.push('/home'); expect(router.getCurrentPath()).toBe('/home'); expect(onRouteChangeSpy).toHaveBeenCalled();
await router.replace('/home'); expect(router.getCurrentPath()).toBe('/home'); expect(onRouteChangeSpy).toHaveBeenCalled(); });
it('router.replace 成功', async () => { const router = new Router(); router.addRoute('/home', HELLO); await router.replace('/home'); expect(router.getCurrentComponent().value).toEqual(HELLO); });
it('钩子执行顺序应该是 beforeEach -> beforeResolve -> afterEach', async () => { const router = new Router(); const callOrder = [];
router.beforeEach(() => { callOrder.push('beforeEach'); return Promise.resolve(); }); router.beforeResolve(() => { callOrder.push('beforeResolve'); return Promise.resolve(); }); router.afterEach(() => { callOrder.push('afterEach'); return Promise.resolve(); });
router.addRoute('/home', HELLO); await router.push('/home');
expect(callOrder).toEqual(['beforeEach', 'beforeResolve', 'afterEach']); });
it('onRouteChange 应该正确处理当前路径和未匹配路径', async () => { const router = new Router(); router.addRoute('/home', HELLO);
window.history.pushState({}, '', '/home'); await router.onRouteChange(); expect(router.currentRoute.value).toBe('/home'); expect(router.currentComponent.value).toBe(HELLO);
window.history.pushState({}, '', '/unknown'); await router.onRouteChange(); expect(router.currentRoute.value).toBe('/404'); });
it('getCurrentPath 方法在未定义模式下应返回根路径', () => { const router = new Router({ mode: 'invalid' }); expect(router.getCurrentPath()).toBe('/'); });
it('push 和 replace 方法在 hash 模式下应更新 hash 并调用 onRouteChange', async () => { const router = new Router({ mode: 'hash' }); router.addRoute('/home', HELLO); const onRouteChangeSpy = vi.spyOn(router, 'onRouteChange');
await router.push('/home'); expect(window.location.hash).toBe('#/home'); expect(onRouteChangeSpy).toHaveBeenCalled();
await router.replace('/home'); expect(window.location.hash).toBe('#/home'); expect(onRouteChangeSpy).toHaveBeenCalled(); });
it('push 和 replace 方法在 hash 模式下应正确处理空 hash', async () => { const router = new Router({ mode: 'hash' }); const onRouteChangeSpy = vi.spyOn(router, 'onRouteChange');
await router.push(''); expect(window.location.hash).toBe('#/'); expect(onRouteChangeSpy).toHaveBeenCalled();
await router.replace(''); expect(window.location.hash).toBe('#/'); expect(onRouteChangeSpy).toHaveBeenCalled(); });
it('构造函数应该调用 addRoutes 方法', () => { const addRoutesSpy = vi.spyOn(Router.prototype, 'addRoutes'); new Router({ routes: [{ path: '/home', component: HELLO }] }); expect(addRoutesSpy).toHaveBeenCalled(); addRoutesSpy.mockRestore(); });
it('调用 addRoutes 方法时应该添加所有路由', () => { const router = new Router(); const routes = [{ path: '/home', component: HELLO }, { path: '/about', component: HELLO }]; router.addRoutes(routes); expect(router.routes.size).toBe(2); expect(router.routes.get('/home')).toBe(HELLO); expect(router.routes.get('/about')).toBe(HELLO); });
it('readyResolve 在 handleRouteChange 方法中应被调用', async () => { const router = new Router(); const readyResolveSpy = vi.spyOn(router, 'readyResolve');
router.addRoute('/home', HELLO); await router.push('/home');
expect(readyResolveSpy).toHaveBeenCalled(); });
it('测试 beforeResolve 钩子', async () => { const router = new Router(); const beforeResolveHook = vi.fn().mockResolvedValue(undefined);
router.addRoute('/home', HELLO); router.beforeResolve(beforeResolveHook);
await router.push('/home');
expect(beforeResolveHook).toHaveBeenCalled(); });
it('测试 onError 钩子', async () => { const router = new Router(); const errorHook = vi.fn();
router.addRoute('/home', HELLO); router.onError(errorHook);
const error = new Error('测试错误'); router.beforeEach(() => { throw error; });
await router.push('/home');
expect(errorHook).toHaveBeenCalledWith(error); }); });
|