Issue#2: 管理前端多标签导航+页签缓存(Gitea 工单:可独立关闭/一次全部关闭/切换后缓存)—— ①多标签导航=stores/tabs.ts(tab 身份=fullPath,参数页每实例一 tab 带 #id 后缀;home 固定不可关;登出 $reset 防跨角色残留;close 关当前回退相邻右邻优先/closeOthers/closeAll)+TagsBar.vue(纯原生元素守 G2 断言语义;✕独立关闭+中键关闭[auxclick button===1,@click.middle 对中键永不触发]+关闭其他+全部关闭;aurora token 风格 primary-soft 激活/圆角10px);②keep-alive 页签缓存=MainLayout router-view v-slot+include=cachedNames+key=fullPath;**29 个业务视图 defineOptions name=路由名**(28 新增+stats/profit 由 StatsProfitView 对齐——script setup 文件名推断名永不命中 include:15 个 index.vue 全叫 index/PascalCase≠kebab);ComingSoon(11 路由共用)刻意豁免(无本地状态,不缓存零副作用);③守门=view-cache-name.spec.ts 固化组件名=路由名约定(漏写=缓存静默失效,测试红而非线上)+verify-layout.mjs +5 项 tags-bar 断言(18→23)且凭据 env 化(SHOT_USER/SHOT_PASS 缺省 s2demo——admin 原密码首启打印一次不可知,硬编码 admin123 已不可登录 ISC:401);④门禁=eslint0/vue-tsc0/vitest 165(+17:tabs-store 7+守门 4+tags-bar 6)/build 9.99s chunk 剖面不变;verify-layout 23/23;Chrome 157 实机走查 17/17 含两项核心状态保留实证(结算页内 Tab 选中+筛选账号ID=2 切走切回均保留;截图 /tmp/issue2-*.png);验收单 issue2-frontend-tabs.md+CHANGELOG+memory-bank 回填
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
// tabs-store.spec.ts Issue#2:多标签导航状态机(登记去重/关闭导航语义/固定标签/缓存名去重)。
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { beforeEach, describe, expect, it } from 'vitest'
|
||||
import type { RouteLocationNormalizedLoaded } from 'vue-router'
|
||||
import { useTabsStore } from '@/stores/tabs'
|
||||
|
||||
/** 构造路由壳(测试专用,不依赖真 router 实例)。 */
|
||||
function r(
|
||||
fullPath: string,
|
||||
name: string,
|
||||
title: string,
|
||||
group = '运营管理',
|
||||
params: Record<string, string> = {},
|
||||
): RouteLocationNormalizedLoaded {
|
||||
return { fullPath, name, params, meta: { title, group } } as unknown as RouteLocationNormalizedLoaded
|
||||
}
|
||||
|
||||
describe('tabs store(Issue#2 多标签导航)', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia())
|
||||
})
|
||||
|
||||
it('初始态:仅固定 home 标签(不可关)', () => {
|
||||
const s = useTabsStore()
|
||||
expect(s.visited).toHaveLength(1)
|
||||
expect(s.visited[0]).toMatchObject({ path: '/home', name: 'home', affix: true })
|
||||
expect(s.close('/home', '/home')).toBeNull()
|
||||
expect(s.visited).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('addTab:登记 + 按 fullPath 去重 + 参数页标题带 #id 后缀', () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab(r('/channel', 'channel', '通道配置', '通道与策略'))
|
||||
s.addTab(r('/settle', 'settle', '结算管理', '统计结算'))
|
||||
s.addTab(r('/settle', 'settle', '结算管理', '统计结算')) // 重复到达不去重登记
|
||||
s.addTab(r('/account/12', 'account-detail', '账号详情', '运营管理', { id: '12' }))
|
||||
expect(s.visited.map((t) => t.path)).toEqual(['/home', '/channel', '/settle', '/account/12'])
|
||||
expect(s.visited[3]!.title).toBe('账号详情 #12')
|
||||
})
|
||||
|
||||
it('addTab 过滤:错误页(无 group)/无 title/无名路由不进标签', () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab({ fullPath: '/403', name: 'forbidden', params: {}, meta: { title: '无权限' } } as unknown as RouteLocationNormalizedLoaded)
|
||||
s.addTab({ fullPath: '/x', name: 'x', params: {}, meta: { group: '运营管理' } } as unknown as RouteLocationNormalizedLoaded)
|
||||
s.addTab({ fullPath: '/y', params: {}, meta: { title: 't', group: 'g' } } as unknown as RouteLocationNormalizedLoaded)
|
||||
expect(s.visited).toHaveLength(1) // 仅初始 home
|
||||
})
|
||||
|
||||
it('close:关后台 tab 返回 null 不导航;关当前 tab 返回右邻(末位取左邻)', () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab(r('/channel', 'channel', '通道配置', '通道与策略'))
|
||||
s.addTab(r('/settle', 'settle', '结算管理', '统计结算'))
|
||||
s.addTab(r('/policy', 'policy', '规则策略', '通道与策略'))
|
||||
// 关后台(settle 非当前)
|
||||
expect(s.close('/settle', '/policy')).toBeNull()
|
||||
expect(s.visited.map((t) => t.path)).toEqual(['/home', '/channel', '/policy'])
|
||||
// 关当前且在末位 → 左邻
|
||||
expect(s.close('/policy', '/policy')).toBe('/channel')
|
||||
// 关当前在中间 → 右邻
|
||||
s.addTab(r('/settle', 'settle', '结算管理', '统计结算'))
|
||||
s.addTab(r('/policy', 'policy', '规则策略', '通道与策略')) // [home, channel, settle, policy]
|
||||
expect(s.close('/settle', '/settle')).toBe('/policy')
|
||||
expect(s.visited.map((t) => t.path)).toEqual(['/home', '/channel', '/policy'])
|
||||
})
|
||||
|
||||
it('closeOthers:保固定标签与当前页', () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab(r('/channel', 'channel', '通道配置', '通道与策略'))
|
||||
s.addTab(r('/settle', 'settle', '结算管理', '统计结算'))
|
||||
s.closeOthers('/channel')
|
||||
expect(s.visited.map((t) => t.path)).toEqual(['/home', '/channel'])
|
||||
})
|
||||
|
||||
it('closeAll:仅固定标签保留并返回回退目标', () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab(r('/channel', 'channel', '通道配置', '通道与策略'))
|
||||
s.addTab(r('/settle', 'settle', '结算管理', '统计结算'))
|
||||
expect(s.closeAll()).toBe('/home')
|
||||
expect(s.visited.map((t) => t.path)).toEqual(['/home'])
|
||||
})
|
||||
|
||||
it('cachedNames:同名多实例(参数页)去重——keep-alive include 粒度=组件名', () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab(r('/account/1', 'account-detail', '账号详情', '运营管理', { id: '1' }))
|
||||
s.addTab(r('/account/2', 'account-detail', '账号详情', '运营管理', { id: '2' }))
|
||||
s.addTab(r('/account/create', 'account-create', '开户'))
|
||||
expect(s.cachedNames).toEqual(['home', 'account-detail', 'account-create'])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,97 @@
|
||||
// tags-bar.spec.ts Issue#2:标签条组件(渲染/独立关闭/关闭导航/全部关闭/固定标签)。
|
||||
// 惯例同 login-view.spec:vi.mock vue-router(useRoute 可变 fullPath + push mock)。
|
||||
import { mount } from '@vue/test-utils'
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import TagsBar from '@/components/TagsBar.vue'
|
||||
import { useTabsStore } from '@/stores/tabs'
|
||||
import type { RouteLocationNormalizedLoaded } from 'vue-router'
|
||||
|
||||
let currentPath = '/home'
|
||||
const pushMock = vi.fn()
|
||||
vi.mock('vue-router', async () => {
|
||||
const actual = await vi.importActual<typeof import('vue-router')>('vue-router')
|
||||
return {
|
||||
...actual,
|
||||
useRoute: () => ({ fullPath: currentPath }),
|
||||
useRouter: () => ({ push: pushMock }),
|
||||
}
|
||||
})
|
||||
|
||||
function r(fullPath: string, name: string, title: string): RouteLocationNormalizedLoaded {
|
||||
return { fullPath, name, params: {}, meta: { title, group: '运营管理' } } as unknown as RouteLocationNormalizedLoaded
|
||||
}
|
||||
|
||||
function mountBar() {
|
||||
const router = createRouter({ history: createWebHistory(), routes: [] })
|
||||
return mount(TagsBar, { global: { plugins: [router] } })
|
||||
}
|
||||
|
||||
describe('TagsBar(Issue#2 多标签导航条)', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia())
|
||||
pushMock.mockClear()
|
||||
currentPath = '/home'
|
||||
})
|
||||
|
||||
it('渲染:chips 来自 store,当前页 active,固定 home 无关闭按钮', () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab(r('/channel', 'channel', '通道配置'))
|
||||
s.addTab(r('/settle', 'settle', '结算管理'))
|
||||
const w = mountBar()
|
||||
expect(w.findAll('.tag-chip')).toHaveLength(3)
|
||||
expect(w.get('[data-testid="tag-home"]').classes()).toContain('active')
|
||||
expect(w.find('[data-testid="tag-home-close"]').exists()).toBe(false)
|
||||
expect(w.find('[data-testid="tag-channel-close"]').exists()).toBe(true)
|
||||
})
|
||||
|
||||
it('点击 chip 切换页面(push 该 fullPath)', async () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab(r('/settle', 'settle', '结算管理'))
|
||||
const w = mountBar()
|
||||
await w.get('[data-testid="tag-settle"]').trigger('click')
|
||||
expect(pushMock).toHaveBeenCalledWith('/settle')
|
||||
})
|
||||
|
||||
it('独立关闭后台 tab:store 移除且不导航', async () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab(r('/channel', 'channel', '通道配置'))
|
||||
s.addTab(r('/settle', 'settle', '结算管理'))
|
||||
const w = mountBar() // 当前= /home
|
||||
await w.get('[data-testid="tag-settle-close"]').trigger('click')
|
||||
expect(s.visited.map((t) => t.path)).toEqual(['/home', '/channel'])
|
||||
expect(pushMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('独立关闭当前 tab:跳右邻(工单核心诉求:可独立关闭)', async () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab(r('/channel', 'channel', '通道配置'))
|
||||
s.addTab(r('/settle', 'settle', '结算管理'))
|
||||
currentPath = '/channel'
|
||||
const w = mountBar()
|
||||
await w.get('[data-testid="tag-channel-close"]').trigger('click')
|
||||
expect(pushMock).toHaveBeenCalledWith('/settle')
|
||||
expect(s.visited.map((t) => t.path)).toEqual(['/home', '/settle'])
|
||||
})
|
||||
|
||||
it('全部关闭:仅保 home 并回退(工单核心诉求:一次全部关闭)', async () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab(r('/channel', 'channel', '通道配置'))
|
||||
s.addTab(r('/settle', 'settle', '结算管理'))
|
||||
const w = mountBar()
|
||||
await w.get('[data-testid="tags-close-all"]').trigger('click')
|
||||
expect(s.visited.map((t) => t.path)).toEqual(['/home'])
|
||||
expect(pushMock).toHaveBeenCalledWith('/home')
|
||||
})
|
||||
|
||||
it('关闭其他:保固定标签与当前页', async () => {
|
||||
const s = useTabsStore()
|
||||
s.addTab(r('/channel', 'channel', '通道配置'))
|
||||
s.addTab(r('/settle', 'settle', '结算管理'))
|
||||
currentPath = '/settle'
|
||||
const w = mountBar()
|
||||
await w.get('[data-testid="tags-close-others"]').trigger('click')
|
||||
expect(s.visited.map((t) => t.path)).toEqual(['/home', '/settle'])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,55 @@
|
||||
// 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 />')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user