Files
knowledge-governance/web/src/QADepartmentStatistics.test.tsx
T
姚章浩 f533c63fef
build / build (push) Successful in 1m13s
fix(qaops): 移除部门名称后的直属人数小字
只移除本部门直属人数标签及无用样式,保留总人数独立列、部门展开、直属人员、兼属及统计值。

验证:标签不再出现的回归先红后绿,11项相关测试及类型检查通过。完整门禁、候选与上线验收待执行。
2026-10-24 13:58:02 +08:00

84 lines
5.2 KiB
TypeScript

import { it, expect } from 'vitest'
import { renderToStaticMarkup } from 'react-dom/server'
import { DepartmentStatisticsView, QARegionalStatistics } from './QADepartmentStatistics'
import { parseDepartmentStatistics } from './qaDepartmentStatistics'
import { departmentStatisticsFixture } from './qaDepartmentStatistics.fixture'
it('offers all, natural-month and natural-week scopes and never switches the selected-department action to a second mapping page', () => {
const html = renderToStaticMarkup(<QARegionalStatistics />)
for (const label of ['按累计统计', '按自然月统计', '按自然周统计', '绑定选中部门']) expect(html).toContain(label)
for (const label of ['按部门', '按区域', '按人']) expect(html).toContain('>' + label + '<')
expect(html).toContain('aria-label="分析维度"')
expect(html).not.toContain('设置选中部门的区域')
expect(html).not.toContain('返回部门统计')
})
it('renders six columns with total people separate from visitors and no fabricated zero ratio', () => {
const data = parseDepartmentStatistics(departmentStatisticsFixture, { period: 'all' }).snapshot!
const html = renderToStaticMarkup(<DepartmentStatisticsView data={data} />)
const headers = [...html.matchAll(/<th[^>]*>([^<]+)<\/th>/g)].map((m) => m[1])
expect(headers).toEqual(['部门 / 人员', '总人数', '访问人数', '访问比例', '访问次数', '最近提问时间'])
const root = html.match(/<tr[^>]*data-department-id="root"[^>]*>([\s\S]*?)<\/tr>/)![1]
const cells = [...root.matchAll(/<td[^>]*>([\s\S]*?)<\/td>/g)].map((m) => m[1])
expect(cells).toHaveLength(6)
expect(cells[1]).toBe('3')
expect(cells[2]).toBe('2')
expect(html).not.toContain('qa-department-roster')
expect(html).toContain('66.7%')
expect(html).toContain('50.0%')
expect(html).not.toContain('覆盖服务')
expect(html).toContain('data-ratio="unknown"')
expect(html).not.toContain('NaN')
})
it('shows direct people beneath each expanded department with a multi-department badge and readable department paths', () => {
const data = parseDepartmentStatistics(departmentStatisticsFixture, { period: 'all' }).snapshot!
const html = renderToStaticMarkup(<DepartmentStatisticsView data={data} collapsed={new Set()} />)
expect(html.match(/data-person-key=/g)).toHaveLength(4)
expect(html.match(/>Fixture Alpha</g)).toHaveLength(2)
expect(html.match(/>Fixture Beta</g)).toHaveLength(1)
expect(html.match(/>兼属 2 部门</g)).toHaveLength(2)
expect(html).not.toContain('本部门直属')
expect(html).not.toContain('qa-department-direct')
expect(html).toContain('Fixture Root/Fixture Child')
expect(html).toContain('未命名人员')
expect(html).toContain('<summary')
const people = [...html.matchAll(/<tr[^>]*data-person-key="[^"]+"[^>]*>([\s\S]*?)<\/tr>/g)]
expect(people).toHaveLength(4)
for (const person of people) {
const cells = [...person[1].matchAll(/<td[^>]*>([\s\S]*?)<\/td>/g)].map((m) => m[1])
expect(cells).toHaveLength(6)
expect(cells.slice(1, 4)).toEqual(['—', '—', '—'])
expect(person[1]).toContain('人员行不单独计算部门总人数')
}
})
it('hides direct people together with collapsed departments without changing branch metrics', () => {
const data = parseDepartmentStatistics(departmentStatisticsFixture, { period: 'all' }).snapshot!
const html = renderToStaticMarkup(<DepartmentStatisticsView data={data} collapsed={new Set(['root'])} />)
expect(html).not.toContain('data-person-key=')
expect(html).toContain('data-total-people="3"')
})
it('defaults to first-level departments and keeps deeper leaves manually expandable without changing metrics', () => {
const data = parseDepartmentStatistics(departmentStatisticsFixture, { period: 'all' }).snapshot!
const child = data.rows.find((d) => d.id === 'child')!
data.rows.push({ ...child, id: 'deep', parent_id: 'child', name: 'Deep Fixture' })
for (const p of data.people) p.department_ids = p.department_ids.map((id) => (id === 'child' ? 'deep' : id))
const initial = renderToStaticMarkup(<DepartmentStatisticsView data={data} />)
expect(initial).toContain('data-department-id="root"')
expect(initial).toContain('data-department-id="child"')
expect(initial).not.toContain('data-department-id="deep"')
expect(initial).not.toContain('data-parent-department="deep"')
const parentOpened = renderToStaticMarkup(<DepartmentStatisticsView data={data} collapsed={new Set(['deep'])} />)
expect(parentOpened).toContain('展开部门 Deep Fixture')
expect(parentOpened).not.toContain('data-parent-department="deep"')
expect(initial).toContain('data-total-people="3"')
expect(parentOpened).toContain('data-total-people="3"')
const expanded = renderToStaticMarkup(<DepartmentStatisticsView data={data} collapsed={new Set()} />)
expect(expanded).toContain('data-parent-department="deep"')
})
it('keeps parent values unchanged when child departments are collapsed', () => {
const data = parseDepartmentStatistics(departmentStatisticsFixture, { period: 'all' }).snapshot!
const html = renderToStaticMarkup(<DepartmentStatisticsView data={data} collapsed={new Set(['root'])} />)
expect(html).toContain('data-department-id="root"')
expect(html).not.toContain('data-department-id="child"')
expect(html).toContain('66.7%')
expect(html).toContain('总人数')
})