unis-oa/frontend/src/utils/tabPages.spec.ts

400 lines
18 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 「标签页」的口径:哪一页、谁在这一页、那一页装什么。
*
* 这里最值得钉的是两条会让人事后查很久的:
*
* 1. **外部页面那一页造出来的那一"格",key 必须还是标签页那一格自己的 key**。
* 后端发地址前按这个 key 重算可见性(``api/embeds.py:_field_perm``)——查不到这一格
* 就当"没有权限约束"放行。名字写错不会报错,只会把环节那一道闸静静拆掉。
* 2. **删一页必须把那一页上的格子挪走**。它们还参与必填与提交校验,只是没人看得见,
* 那是填不完的表单,而且没人知道为什么填不完。
*/
import { describe, expect, it } from 'vitest'
import {
badgeOf,
badgeView,
collapsedTabKeys,
embedSlotKey,
expandPerms,
fieldsOfTab,
removeTab,
slotKey,
slotPermOf,
tabEmbedField,
tabKeyOf,
tabProblems,
tabDefaultCollapsed,
tabsDefaultOf,
tabsFieldOf,
tabsModeOf,
tabsOf,
nextTabKey,
permShadowOf
} from '@/utils/tabPages'
import type { FormFieldDef } from '@/types'
const TABS = {
key: 'pages',
label: '标签页',
type: 'tabs',
tabs: [
{ key: 'tab_1', label: '申请信息', kind: 'form' },
{ key: 'tab_2', label: '合同详情', kind: 'embed', embed: { source: 'erp_contract' } }
]
} as unknown as FormFieldDef
function field(key: string, extra: Partial<FormFieldDef> = {}): FormFieldDef {
return { key, label: key, type: 'text', ...extra } as FormFieldDef
}
describe('页签清单:读得歪,也读得出', () => {
it('缺 key、缺名字、kind 乱写,一律补齐成能用的一页', () => {
const list = tabsOf({ key: 'pages', type: 'tabs', tabs: [{}, { key: 'x', label: '第二页', kind: 'nope' }] })
expect(list).toEqual([
{ key: 'tab_1', label: '第 1 页', kind: 'form', embed: undefined },
{ key: 'x', label: '第二页', kind: 'form', embed: undefined }
])
})
it('没摆这一格就是没摆:tabsFieldOf 回 null,整张表单照旧', () => {
expect(tabsFieldOf([field('a'), field('b')])).toBeNull()
expect(tabsOf(null)).toEqual([])
})
it('新增一页永远挑一个没被占用的标识', () => {
expect(nextTabKey(['tab_1', 'tab_2'])).toBe('tab_3')
expect(nextTabKey(['tab_2'])).toBe('tab_1')
})
})
describe('归属:没写的格子在第一页', () => {
const list = tabsOf(TABS)
it('tab 没写、写的还是已经不存在的页签,都回第一页', () => {
expect(tabKeyOf(field('a'), list)).toBe('tab_1')
expect(tabKeyOf(field('b', { tab: 'tab_2' }), list)).toBe('tab_2')
expect(tabKeyOf(field('c', { tab: 'deleted' }), list)).toBe('tab_1')
})
it('按页分格子时不把标签页自己算进去', () => {
const all = [TABS, field('a'), field('b', { tab: 'tab_2' })]
expect(fieldsOfTab(all, list, 'tab_1').map((item) => item.key)).toEqual(['a'])
expect(fieldsOfTab(all, list, 'tab_2').map((item) => item.key)).toEqual(['b'])
})
})
describe('删掉一页', () => {
it('那一页上的格子挪去挨着它的那一页,一个都不留', () => {
const all = [TABS, field('a'), field('b', { tab: 'tab_2' })]
const moved = removeTab(TABS, 'tab_2', all)
expect(moved.field.tabs?.map((tab) => tab.key)).toEqual(['tab_1'])
expect(moved.fields.find((item) => item.key === 'b')?.tab).toBe('tab_1')
})
it('删的是第一页时,格子跟着这一页走,而不是散到后面那一页去', () => {
const moved = removeTab(TABS, 'tab_1', [TABS, field('a')])
expect(moved.fields.find((item) => item.key === 'a')?.tab).toBe('tab_2')
})
it('只剩一页时不动它:删光了这张表单就没有页签了', () => {
const only = { ...TABS, tabs: [{ key: 'tab_1', label: '一页', kind: 'form' }] } as FormFieldDef
const moved = removeTab(only, 'tab_9', [only, field('a')])
expect(moved.field).toBe(only)
})
})
describe('外部页面那一页', () => {
it('那一"格"的 key 是标签页那一格的 key——换名字等于拆掉环节那道闸', () => {
const tab = tabsOf(TABS)[1]
const frame = tabEmbedField(TABS, tab)
expect(frame.key).toBe('pages')
expect(frame.type).toBe('embed')
expect(frame.embed?.source).toBe('erp_contract')
})
it('整页都是它:默认就是 fill + 窄屏也嵌,页签自己的配置说了算', () => {
const frame = tabEmbedField(TABS, tabsOf(TABS)[1])
expect(frame.embed).toMatchObject({ fit: 'fill', mobile: 'frame' })
const short = tabEmbedField(TABS, { key: 'tab_2', label: '矮一点', kind: 'embed', embed: { fit: 'fixed', height: 320 } })
expect(short.embed).toMatchObject({ fit: 'fixed', mobile: 'frame', height: 320 })
})
it('同一张标签页上几页外部页面的回写结论各记各的', () => {
expect(embedSlotKey('pages', 'tab_2')).toBe('pages#tab_2')
expect(embedSlotKey('pages', 'tab_3')).not.toBe(embedSlotKey('pages', 'tab_2'))
})
})
describe('保存那一刻要说的话', () => {
it('一页都没有=这格白摆', () => {
expect(tabProblems({ key: 'pages', label: '标签页', type: 'tabs', tabs: [] })).toEqual(['「标签页」还没添加任何一页'])
})
it('只有一页也要说:只有一页就用不上标签页', () => {
const problems = tabProblems({ ...TABS, tabs: [{ key: 'tab_1', label: '只有一页' }] })
expect(problems).toContain('「标签页」至少要有 2 页,只有一页就用不上标签页')
})
it('页签没名字、标识重复,都在保存时点名', () => {
const problems = tabProblems({
...TABS,
tabs: [
{ key: 'tab_1', label: '' },
{ key: 'tab_1', label: '重了' }
]
})
expect(problems).toContain('「标签页」第 1 页没有名字')
expect(problems).toContain('「标签页」第 2 页的标识与第 1 页重复')
})
it('外部页面那一页没挑页面,说清楚是哪一页', () => {
const problems = tabProblems({
...TABS,
tabs: [{ key: 'tab_2', label: '合同详情', kind: 'embed', embed: {} }]
})
expect(problems).toContain('「标签页」第 1 页「合同详情」还没选择要嵌入哪个页面')
})
it('那一页是外部页面却还压着格子:那些格子显示不出来,得说', () => {
const problems = tabProblems(TABS, { fieldCountOf: (key) => (key === 'tab_2' ? 3 : 1) })
expect(problems.join(';')).toContain('压在它上面的 3 个格子不会被显示出来')
})
it('回填点了不存在的字段、参数是那个页面用不上的,都跟着页签名一起点名', () => {
const problems = tabProblems(
{
...TABS,
tabs: [
{
key: 'tab_2',
label: '合同详情',
kind: 'embed',
embed: { source: 'erp_contract', params: { ghost: '{form.x}' }, writeback: { enabled: true, fields: ['ghost'] } }
}
]
},
{ fieldKeys: new Set(['amount']), declaredArgs: () => ['contractNo'] }
)
expect(problems.join(';')).toContain('这个页面用不上')
expect(problems.join(';')).toContain('要回填的字段「ghost」在表单上找不到')
})
it('那一页的地址由接口现给:那句「用不上」说的是接口与请求体,不是地址', () => {
const problems = tabProblems(
{
...TABS,
tabs: [
{
key: 'tab_2',
label: '合同详情',
kind: 'embed',
embed: { source: 'erp_contract', params: { ghost: '{form.x}' } }
}
]
},
{ fieldKeys: new Set(['amount']), declaredArgs: () => ['contractNo'], urlModeOf: () => 'api' }
)
expect(problems.join(';')).toContain('接口地址、请求头、请求体里都没写')
expect(problems.join(';')).not.toContain('地址里没写')
})
})
describe('环节把整片或某一页藏起来:落到每个格子头上(后端 expand_perms 同一份判据)', () => {
const rows = () => [TABS, field('a'), field('b', { tab: 'tab_2' }), field('c', { tab: 'tab_2' })]
it('没摆标签页、没配权限,都原样回,一个字都不加', () => {
expect(expandPerms([field('a'), field('b')], { a: 'readonly' })).toEqual({ a: 'readonly' })
expect(expandPerms(rows(), {})).toEqual({})
})
it('藏整片:页里每一格跟着一起藏,一条都不留', () => {
const next = expandPerms(rows(), { pages: 'hidden', b: 'editable' })
expect(Object.keys(next).sort()).toEqual(['a', 'b', 'c', 'pages'])
// 逐格那句"可编辑"盖不过页签那一句隐藏:留着可编辑等于要人为看不见的格子补输入框
expect(next.b).toBe('hidden')
})
it('藏一页只藏那一页的格子,另一页与没写归属的格子不受牵连', () => {
const next = expandPerms(rows(), { [slotKey('pages', 'tab_2')]: 'hidden' })
expect(Object.keys(next).sort()).toEqual(['b', 'c', 'pages#tab_2'])
expect(next.a).toBeUndefined()
})
it('没写归属的格子算第一页,跟着第一页一起藏', () => {
const next = expandPerms(rows(), { [slotKey('pages', 'tab_1')]: 'hidden' })
expect(next.a).toBe('hidden')
expect(next.b).toBeUndefined()
})
it('页签那一档只认「隐藏」:只读与必填仍归逐格那一层管', () => {
expect(expandPerms(rows(), { a: 'editable', [slotKey('pages', 'tab_2')]: 'readonly' })).toEqual({
a: 'editable',
'pages#tab_2': 'readonly'
})
})
it('展开不许改脏传进来的那份字典', () => {
const source = { pages: 'hidden' }
expandPerms(rows(), source)
expect(source).toEqual({ pages: 'hidden' })
})
it('页签权限挂点是 标签页key#页签key,与回写那个分区键同形', () => {
expect(slotKey('pages', 'tab_2')).toBe('pages#tab_2')
expect(slotKey('pages', 'tab_2')).toBe(embedSlotKey('pages', 'tab_2'))
expect(slotPermOf({ pages: 'readonly' }, 'pages', 'tab_2')).toBe('')
expect(slotPermOf(undefined, 'pages', 'tab_2')).toBe('')
})
})
describe('页签上那个状态角标', () => {
const tab = (badge: Record<string, unknown> = {}) => ({ key: 't2', label: '票据核验', badge })
it('没配就没有;配了但没挑格子也不算配过(渲染上不去,保存那一刻要说)', () => {
expect(badgeView({ key: 't1', label: '一' }, { check: 'passed' })).toBeNull()
expect(badgeOf(tab() as never)).toEqual({})
expect(badgeView(tab() as never, { check: 'passed' })).toBeNull()
const problems = tabProblems({ ...TABS, tabs: [{ key: 't1', label: '一', badge: {} }, { key: 't2', label: '二' }] })
expect(problems.join(';')).toContain('的角标没写要跟着哪一格的值走')
})
it('值落进对照表才挂;还没回传、值为空都不挂', () => {
const withMap = tab({ field: 'check', map: { passed: { text: '初审通过', tone: 'green' } } }) as never
expect(badgeView(withMap, {})).toBeNull()
expect(badgeView(withMap, { check: '' })).toBeNull()
expect(badgeView(withMap, { check: 0 })).toBeNull()
expect(badgeView(withMap, { check: 'passed' })).toEqual({ text: '初审通过', tone: 'green' })
})
it('表里没有又不兜底就什么都不挂——宁可少说,也不把人吓一跳', () => {
const withMap = tab({ field: 'check', map: { passed: '通过' } }) as never
expect(badgeView(withMap, { check: 'partial_v2' })).toBeNull()
const withOthers = tab({ field: 'check', map: { passed: '通过', '*': { tone: 'amber' } } }) as never
expect(badgeView(withOthers, { check: 'partial_v2' })).toEqual({ text: 'partial_v2', tone: 'amber' })
})
it('颜色写歪退回灰;指到没有的格子、颜色不认识、对照表不是对象,保存那一刻都要说', () => {
const weird = tab({ field: 'check', map: { x: { tone: 'rainbow' } } }) as never
expect(badgeView(weird, { check: 'x' })).toEqual({ text: 'x', tone: 'gray' })
const problems = tabProblems(
{ ...TABS, tabs: [{ key: 't1', label: '一', badge: { field: 'ghost' } }, { key: 't2', label: '二', badge: { field: 'check', map: { a: { tone: 'pink' } } } }] },
{ fieldKeys: new Set(['check']) }
).join(';')
expect(problems).toContain('的角标要跟着「ghost」的值走,表单上没有这一格')
expect(problems).toContain('的角标颜色不认识:pink')
})
})
describe('两种排法:页签翻页 / 分组收折', () => {
const PAGES = [
{ key: 'tab_1', label: '申请信息' },
{ key: 'tab_2', label: '票据核验', collapsed: true }
]
it('没写就是页签:存量表单不会某天醒来变成一整屏收折', () => {
expect(tabsModeOf({ key: 'pages', type: 'tabs', tabs: PAGES })).toBe('tabs')
expect(tabsModeOf({ key: 'pages', type: 'tabs', tabs: PAGES, tabsMode: 'collapse' })).toBe('collapse')
// 读不懂的值回默认档:把配好的分页悄悄换成另一种排法,比认不出来更糟
expect(tabsModeOf({ key: 'pages', type: 'tabs', tabs: PAGES, tabsMode: 'carousel' })).toBe('tabs')
})
it('「进来先合着」只在收折那一档算数', () => {
const collapse = { key: 'pages', type: 'tabs', tabs: PAGES, tabsMode: 'collapse' }
expect(collapsedTabKeys(collapse as unknown as FormFieldDef)).toEqual(['tab_2'])
expect(collapsedTabKeys({ key: 'pages', type: 'tabs', tabs: PAGES } as unknown as FormFieldDef)).toEqual([])
})
it('排法写歪了保存那一刻就挡(运行时它会安静回到页签那一档)', () => {
const field = { key: 'pages', label: '标签页', type: 'tabs', tabsMode: 'columns', tabs: PAGES }
expect(tabProblems(field as unknown as FormFieldDef)).toContain('「标签页」的排列方式不认识:columns')
expect(tabProblems({ ...field, tabsMode: 'collapse' } as unknown as FormFieldDef)).toEqual([])
})
it('重建那几页时把 collapsed 一起搬过来(漏了就等于那一档配了也白配)', () => {
const list = tabsOf({ key: 'pages', type: 'tabs', tabsMode: 'collapse', tabs: PAGES } as unknown as FormFieldDef)
expect(list.map((tab) => tab.collapsed)).toEqual([undefined, true])
})
})
describe('收折的默认开合:整张单那一档 + 段上那份覆盖', () => {
const THREE = [
{ key: 'tab_1', label: '一' },
{ key: 'tab_2', label: '二' },
{ key: 'tab_3', label: '三' }
]
const field = (tabsDefault?: string, tabs: Record<string, unknown>[] = THREE) =>
({ key: 'pages', type: 'tabs', tabsMode: 'collapse', ...(tabsDefault ? { tabsDefault } : {}), tabs }) as unknown as FormFieldDef
it('没写就是全展开;认不出来也按全展开', () => {
expect(tabsDefaultOf(field())).toBe('expand')
expect(tabsDefaultOf(field('first'))).toBe('first')
expect(tabsDefaultOf(field('up'))).toBe('expand')
expect(collapsedTabKeys(field())).toEqual([])
})
it('默认全收起就全合着;只展开第一段按那一段以外算', () => {
expect(collapsedTabKeys(field('collapse'))).toEqual(['tab_1', 'tab_2', 'tab_3'])
expect(collapsedTabKeys(field('first'))).toEqual(['tab_2', 'tab_3'])
// 排在最前面那一段被环节整段藏了:起头的那一段换成看得见的第一段
expect(collapsedTabKeys(field('first'), 'tab_2')).toEqual(['tab_1', 'tab_3'])
})
it('段上那份覆盖赢过默认,两个方向都能覆盖', () => {
expect(collapsedTabKeys(field('collapse', [{ key: 'tab_1', label: '一', collapsed: false }, ...THREE.slice(1)]))).toEqual([
'tab_2',
'tab_3'
])
expect(collapsedTabKeys(field('expand', [{ key: 'tab_1', label: '一', collapsed: true }, ...THREE.slice(1)]))).toEqual(['tab_1'])
})
it('覆盖只认真布尔值;重建那几页时 false 也得搬("先展开"那个覆盖不能配了白配)', () => {
expect(collapsedTabKeys(field('expand', [{ key: 'tab_1', label: '一', collapsed: 'yes' }, ...THREE.slice(1)]))).toEqual([])
const list = tabsOf(field('collapse'))
expect(list.map((tab) => tab.collapsed)).toEqual([undefined, undefined, undefined])
})
it('不覆盖时这段是什么状态,弹窗那句"跟默认一样"念的就是它', () => {
expect(tabDefaultCollapsed(field('collapse'), 0)).toBe(true)
expect(tabDefaultCollapsed(field('first'), 0)).toBe(false)
expect(tabDefaultCollapsed(field('first'), 1)).toBe(true)
expect(tabDefaultCollapsed(field('first'), 1, 'tab_2')).toBe(false)
})
it('默认写歪了保存那一刻就挡', () => {
expect(tabProblems({ ...field('sideways'), label: '标签页' })).toContain('「标签页」的默认开合不认识:sideways')
expect(tabProblems({ ...field('first'), label: '标签页' })).toEqual([])
})
})
describe('哪几格的逐格权限此刻是白设的(环节配置面板那句提示的判据)', () => {
const rows = () => [TABS, field('a'), field('b', { tab: 'tab_2' }), field('c', { tab: 'tab_2' })]
it('没摆标签页、没配权限:一句都不多说', () => {
expect(permShadowOf([field('a')], { a: 'hidden' })).toEqual({ hostKey: '', hiddenTabs: [], shadowed: {} })
expect(permShadowOf(rows(), {})).toEqual({ hostKey: '', hiddenTabs: [], shadowed: {} })
})
it('藏整片:除了「标签页」自己,每一格都标上 host', () => {
const shadow = permShadowOf(rows(), { pages: 'hidden', b: 'editable', c: 'hidden' })
expect(shadow.hostKey).toBe('pages')
expect(shadow.hiddenTabs).toEqual([])
// c 自己就配了隐藏,跟整片那一句结论一致,不用再多说一句
expect(shadow.shadowed).toEqual({ a: 'host', b: 'host' })
})
it('藏一页只标那一页的格子,并说是哪一页', () => {
const shadow = permShadowOf(rows(), { [slotKey('pages', 'tab_2')]: 'hidden', b: 'editable', a: 'required' })
expect(shadow.hostKey).toBe('')
expect(shadow.hiddenTabs).toEqual(['tab_2'])
// a 没写归属,算第一页,跟着第一页露着;b 与 c 都在藏起来那一页上,连没配过的也算看不见
expect(shadow.shadowed).toEqual({ b: 'tab:tab_2', c: 'tab:tab_2' })
})
it('逐格那几档压不过页签那一句隐藏:这正是生产上那张单的现场', () => {
const perms = { pages: 'hidden', a: 'editable', b: 'required' }
expect(permShadowOf(rows(), perms).shadowed.a).toBe('host')
// 展开权限之后 a 真的成了 hidden:界面念的那一句与运行时做的是同一件事
expect(expandPerms(rows(), perms).a).toBe('hidden')
})
})