56 lines
2.7 KiB
TypeScript
56 lines
2.7 KiB
TypeScript
// view-cache-name.spec.ts Issue#2 守门:keep-alive :include 按**组件名**匹配,
|
|
// 组件名必须=路由 name——新增业务页漏写 defineOptions 会让缓存**静默失效**
|
|
// (页面照常工作,仅切 tab 丢状态),此类回归必须在本测试红,而不是线上被发现。
|
|
// 豁免:ComingSoon 占位页(11 条路由共用,无本地状态可保,刻意不缓存)。
|
|
import { readFileSync } from 'node:fs'
|
|
import { dirname, join, resolve } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { asyncRoutes } from '@/router/routes'
|
|
|
|
const webRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..')
|
|
|
|
// 从 routes.ts 源码提取 路由名 → 视图文件 映射(name 与 component 动态 import
|
|
// 相邻的行内模式;redirect 路由天然无 component 不会入表)。
|
|
const routesSrc = readFileSync(join(webRoot, 'src/router/routes.ts'), 'utf8')
|
|
const viewFile = new Map<string, string>()
|
|
for (const m of routesSrc.matchAll(/name:\s*'([\w-]+)',\s*component:\s*\(\)\s*=>\s*import\('([^']+)'\)/g)) {
|
|
viewFile.set(m[1]!, m[2]!)
|
|
}
|
|
|
|
describe('Issue#2 视图缓存命名守门(组件名=路由名)', () => {
|
|
it('路由→文件映射提取成功(≥40,防正则/结构漂移静默跳检)', () => {
|
|
expect(viewFile.size).toBeGreaterThanOrEqual(40)
|
|
})
|
|
|
|
it('每个业务视图 SFC 含 defineOptions({ name: <路由名> })', () => {
|
|
const children = asyncRoutes[0]!.children!
|
|
const business = children.filter(
|
|
(c) => c.name && c.meta?.title && c.meta?.group && viewFile.has(String(c.name)),
|
|
)
|
|
expect(business.length).toBeGreaterThanOrEqual(40)
|
|
const missing: string[] = []
|
|
for (const c of business) {
|
|
const name = String(c.name)
|
|
const rel = viewFile.get(name)!
|
|
// 豁免:ComingSoon 占位页(共用组件,刻意不参与缓存)
|
|
if (rel.endsWith('ComingSoon.vue')) continue
|
|
const sfc = readFileSync(join(webRoot, rel.replace('@/', 'src/')), 'utf8')
|
|
if (!sfc.includes(`defineOptions({ name: '${name}' })`)) missing.push(`${name} → ${rel}`)
|
|
}
|
|
expect(missing, `缺缓存命名(keep-alive 会静默失效):\n${missing.join('\n')}`).toEqual([])
|
|
})
|
|
|
|
it('豁免口径固化:ComingSoon 不写 defineOptions(每次进入重挂载,零状态可丢)', () => {
|
|
const sfc = readFileSync(join(webRoot, 'src/views/coming/ComingSoon.vue'), 'utf8')
|
|
expect(sfc).not.toContain('defineOptions')
|
|
})
|
|
|
|
it('MainLayout 已挂 keep-alive(include=tabs.cachedNames,key=fullPath)', () => {
|
|
const sfc = readFileSync(join(webRoot, 'src/layouts/MainLayout.vue'), 'utf8')
|
|
expect(sfc).toContain(':include="tabs.cachedNames"')
|
|
expect(sfc).toContain(':key="viewRoute.fullPath"')
|
|
expect(sfc).toContain('<TagsBar />')
|
|
})
|
|
})
|