| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- /**
- * utils/indicator-compare 多指标同图归一化纯函数单测
- * 覆盖:PRD §4.3 P2-4 / design §1.2.1 第二段 / design §1092
- */
- import * as ic from '@/utils/indicator-compare'
- describe('indicator-compare 多指标同图归一化', () => {
- describe('MAX_COMPARE_INDICATORS', () => {
- test('上限为 3', () => {
- expect(ic.MAX_COMPARE_INDICATORS).toBe(3)
- })
- })
- describe('pointNumeric', () => {
- test('numericValue 优先', () => {
- expect(ic.pointNumeric({ value: 'abc', numericValue: 2.5 })).toBe(2.5)
- })
- test('回退 value', () => {
- expect(ic.pointNumeric({ value: '3.62' })).toBe(3.62)
- })
- test('非数字返回 null', () => {
- expect(ic.pointNumeric({ value: 'N/A' })).toBe(null)
- })
- test('null/undefined 输入返回 null', () => {
- expect(ic.pointNumeric(null)).toBe(null)
- expect(ic.pointNumeric(undefined)).toBe(null)
- expect(ic.pointNumeric({})).toBe(null)
- })
- test('numericValue 为 0 正确返回 0', () => {
- expect(ic.pointNumeric({ value: '0', numericValue: 0 })).toBe(0)
- })
- })
- describe('normalizeValue', () => {
- test('正常线性映射', () => {
- expect(ic.normalizeValue(5, 0, 10)).toBeCloseTo(0.5)
- expect(ic.normalizeValue(0, 0, 10)).toBeCloseTo(0)
- expect(ic.normalizeValue(10, 0, 10)).toBeCloseTo(1)
- })
- test('min === max 恒定值 → 0.5', () => {
- expect(ic.normalizeValue(3.62, 3.62, 3.62)).toBe(0.5)
- })
- test('越界值夹到 [0,1]', () => {
- expect(ic.normalizeValue(-1, 0, 10)).toBe(0)
- expect(ic.normalizeValue(15, 0, 10)).toBe(1)
- })
- test('null/min/max 安全返回 null', () => {
- expect(ic.normalizeValue(null, 0, 10)).toBe(null)
- expect(ic.normalizeValue(5, null, 10)).toBe(null)
- expect(ic.normalizeValue(5, 0, null)).toBe(null)
- })
- test('NaN 输入返回 null', () => {
- expect(ic.normalizeValue(NaN, 0, 10)).toBe(null)
- })
- })
- describe('computeRealRange', () => {
- test('空 points → count=0', () => {
- var r = ic.computeRealRange({ points: [] })
- expect(r.count).toBe(0)
- expect(r.min).toBe(null)
- expect(r.max).toBe(null)
- })
- test('undefined item → count=0', () => {
- expect(ic.computeRealRange(null).count).toBe(0)
- expect(ic.computeRealRange(undefined).count).toBe(0)
- })
- test('单点 → count=1,无波动', () => {
- var r = ic.computeRealRange({ points: [{ value: '3.62', numericValue: 3.62 }] })
- expect(r.count).toBe(1)
- expect(r.min).toBeCloseTo(3.62)
- expect(r.max).toBeCloseTo(3.62)
- expect(r.hasVariance).toBe(false)
- })
- test('恒定值 ≥2 点 → count≥2 但 hasVariance=false', () => {
- var r = ic.computeRealRange({
- points: [{ value: '3.62', numericValue: 3.62 }, { value: '3.62', numericValue: 3.62 }]
- })
- expect(r.count).toBe(2)
- expect(r.min).toBeCloseTo(3.62)
- expect(r.max).toBeCloseTo(3.62)
- expect(r.hasVariance).toBe(false)
- })
- test('正常变化 → hasVariance=true', () => {
- var r = ic.computeRealRange({
- points: [
- { value: '3.62', numericValue: 3.62 },
- { value: '4.10', numericValue: 4.10 }
- ]
- })
- expect(r.count).toBe(2)
- expect(r.min).toBeCloseTo(3.62)
- expect(r.max).toBeCloseTo(4.10)
- expect(r.hasVariance).toBe(true)
- })
- test('有效数值过滤 NaN / 单位不同时 numericValue 仍可用', () => {
- var r = ic.computeRealRange({
- points: [
- { value: '10%', numericValue: 10 },
- { value: 'abc' } // numericValue 缺失且 value 不可转数字
- ]
- })
- expect(r.count).toBe(1)
- expect(r.min).toBeCloseTo(10)
- expect(r.max).toBeCloseTo(10)
- })
- })
- describe('toggleCompareSelection', () => {
- test('第 1 个添加成功', () => {
- var res = ic.toggleCompareSelection([], 101)
- expect(res.changed).toBe(true)
- expect(res.rejected).toBe(false)
- expect(res.ids).toEqual([101])
- })
- test('添加第 2、3 个均成功', () => {
- var res = ic.toggleCompareSelection([101], 102)
- expect(res.ids).toEqual([101, 102])
- res = ic.toggleCompareSelection(res.ids, 103)
- expect(res.ids).toEqual([101, 102, 103])
- })
- test('第 4 个被拒(上限 3)', () => {
- var res = ic.toggleCompareSelection([101, 102, 103], 104)
- expect(res.changed).toBe(false)
- expect(res.rejected).toBe(true)
- expect(res.ids).toEqual([101, 102, 103])
- })
- test('取消勾选移除', () => {
- var res = ic.toggleCompareSelection([101, 102], 102)
- expect(res.ids).toEqual([101])
- expect(res.changed).toBe(true)
- })
- test('重复添加忽略', () => {
- var res = ic.toggleCompareSelection([101], 101)
- expect(res.ids).toEqual([])
- })
- test('传 null/undefined definitionId 不改变 ids', () => {
- var res = ic.toggleCompareSelection([101], null)
- expect(res.changed).toBe(false)
- expect(res.ids).toEqual([101])
- res = ic.toggleCompareSelection([101], undefined)
- expect(res.changed).toBe(false)
- })
- test('自定义 max 生效', () => {
- var res = ic.toggleCompareSelection([101, 102], 103, 2)
- expect(res.rejected).toBe(true)
- })
- })
- describe('buildCompareSeries', () => {
- var baseItem = function(defId, name, unit, pts) {
- return { definitionId: defId, name: name, unit: unit, points: pts }
- }
- test('无有效数值 → 不纳入 series', () => {
- var items = [baseItem(1, 'A', '%', [{ reportDate: '2026-01-01', value: 'N/A' }])]
- var s = ic.buildCompareSeries(items)
- expect(s.length).toBe(0)
- })
- test('constant 系列 hasVariance=false,drawLine=true(≥1 个点始终画点)', () => {
- var items = [baseItem(1, 'A', '%', [{ reportDate: '2026-01-01', value: '5', numericValue: 5 }])]
- var s = ic.buildCompareSeries(items)
- expect(s.length).toBe(1)
- expect(s[0].realMin).toBeCloseTo(5)
- expect(s[0].realMax).toBeCloseTo(5)
- expect(s[0].realCount).toBe(1)
- expect(s[0].hasVariance).toBe(false)
- // 单点:n<3 → drawLine=false(红线:n<3 不连假线)
- expect(s[0].drawLine).toBe(false)
- expect(s[0].points.length).toBe(1)
- expect(s[0].points[0].value).toBeCloseTo(0.5) // min==max → 0.5
- })
- test('两期(n=2)→ drawLine=false(红线),数据仍在 series 中用于展示双点', () => {
- var items = [baseItem(1, 'LDL-C', 'mmol/L', [
- { reportDate: '2026-01-01', value: '3.62', numericValue: 3.62 },
- { reportDate: '2026-06-01', value: '4.10', numericValue: 4.10 }
- ])]
- var s = ic.buildCompareSeries(items)
- expect(s.length).toBe(1)
- expect(s[0].drawLine).toBe(false)
- expect(s[0].realCount).toBe(2)
- })
- test('三期(n=3)→ drawLine=true', () => {
- var items = [baseItem(1, 'X', 'mmol/L', [
- { reportDate: '2026-01', value: '1', numericValue: 1 },
- { reportDate: '2026-02', value: '2', numericValue: 2 },
- { reportDate: '2026-03', value: '3', numericValue: 3 }
- ])]
- var s = ic.buildCompareSeries(items)
- expect(s[0].drawLine).toBe(true)
- })
- test('混合单位(mmol/L 与 %)各自归一化独立', () => {
- var items = [
- baseItem(1, 'A', 'mmol/L', [
- { reportDate: '2026-01-01', value: '3.62', numericValue: 3.62 },
- { reportDate: '2026-06-01', value: '4.10', numericValue: 4.10 }
- ]),
- baseItem(2, 'B', '%', [
- { reportDate: '2026-01-01', value: '50', numericValue: 50 },
- { reportDate: '2026-06-01', value: '80', numericValue: 80 }
- ])
- ]
- var s = ic.buildCompareSeries(items)
- expect(s.length).toBe(2)
- // A 映射 [3.62, 4.10] → [0, 1]
- expect(s[0].points[0].value).toBeCloseTo(0)
- expect(s[0].points[1].value).toBeCloseTo(1)
- // B 映射 [50, 80] → [0, 1]
- expect(s[1].points[0].value).toBeCloseTo(0)
- expect(s[1].points[1].value).toBeCloseTo(1)
- // 真实范围保留
- expect(s[0].realMin).toBeCloseTo(3.62)
- expect(s[0].realMax).toBeCloseTo(4.10)
- expect(s[1].realMin).toBeCloseTo(50)
- expect(s[1].realMax).toBeCloseTo(80)
- // 颜色轮转
- expect(s[0].color).toBe('#F97316')
- expect(s[1].color).toBe('#0EA5E9')
- })
- test('legendText 格式:有波动 → "name(unit) · 真实范围 min~max"', () => {
- var items = [baseItem(1, 'LDL-C', 'mmol/L', [
- { reportDate: '2026-01-01', value: '3.62', numericValue: 3.62 },
- { reportDate: '2026-06-01', value: '4.10', numericValue: 4.10 }
- ])]
- var s = ic.buildCompareSeries(items)
- expect(s[0].legendText).toMatch(/真实范围/)
- expect(s[0].legendText).toContain('LDL-C')
- })
- test('legendText 格式:仅 1 期 → "仅 1 期 x(无趋势)"', () => {
- var items = [baseItem(1, 'A', '%', [{ reportDate: '2026-01-01', value: '5', numericValue: 5 }])]
- var s = ic.buildCompareSeries(items)
- expect(s[0].legendText).toMatch(/仅 1 期/)
- })
- test('legendText 格式:无波动 → "无波动 恒定 x"', () => {
- var items = [baseItem(1, 'A', '%', [
- { reportDate: '2026-01-01', value: '5', numericValue: 5 },
- { reportDate: '2026-06-01', value: '5', numericValue: 5 }
- ])]
- var s = ic.buildCompareSeries(items)
- expect(s[0].legendText).toMatch(/无波动/)
- })
- test('无有效数值的条目跳过,不占 color 位置', () => {
- var items = [
- baseItem(1, 'A', '%', [{ reportDate: '2026-01-01', value: 'N/A' }]),
- baseItem(2, 'B', '%', [{ reportDate: '2026-01-01', value: '5', numericValue: 5 }])
- ]
- var s = ic.buildCompareSeries(items)
- expect(s.length).toBe(1)
- expect(s[0].color).toBe('#F97316') // 仍然用 palette[0](首个有效)
- })
- })
- describe('buildCompareAllDates', () => {
- test('空数组 → []', () => {
- expect(ic.buildCompareAllDates([])).toEqual([])
- })
- test('只含 points → 升序并集', () => {
- var items = [
- { points: [{ reportDate: '2026-03-01' }, { reportDate: '2026-05-01' }] },
- { points: [{ reportDate: '2026-01-01' }, { reportDate: '2026-05-01' }] }
- ]
- var dates = ic.buildCompareAllDates(items)
- expect(dates).toEqual(['2026-01-01', '2026-03-01', '2026-05-01'])
- })
- test('缺失期(missingPeriods)也并入 X 轴', () => {
- var items = [{ points: [{ reportDate: '2026-01-01' }, { reportDate: '2026-03-01' }], missingPeriods: [{ reportDate: '2026-02-01' }] }]
- var dates = ic.buildCompareAllDates(items)
- expect(dates).toEqual(['2026-01-01', '2026-02-01', '2026-03-01'])
- })
- test('日期截断到 yyyy-MM-dd', () => {
- var items = [{ points: [{ reportDate: '2026-01-15T14:30:00' }] }]
- var dates = ic.buildCompareAllDates(items)
- expect(dates).toEqual(['2026-01-15'])
- })
- })
- describe('buildComparePeriodRows', () => {
- test('缺失期 → 该期未检测', () => {
- var items = [
- { definitionId: 1, name: 'A', unit: 'mmol/L', points: [{ reportDate: '2026-01-01', value: '3.62', numericValue: 3.62 }, { reportDate: '2026-03-01', value: '4.10', numericValue: 4.10 }], missingPeriods: [{ reportDate: '2026-02-01' }] }
- ]
- var rows = ic.buildComparePeriodRows(items)
- expect(rows.length).toBe(3)
- // 按日期倒序(新→旧)
- expect(rows[0].dateText).toContain('03-01')
- expect(rows[1].dateText).toContain('02-01')
- expect(rows[2].dateText).toContain('01-01')
- // 缺失期单元格
- expect(rows[1].cells[0].valueText).toBe('该期未检测')
- expect(rows[1].cells[0].isMissing).toBe(true)
- })
- test('真实值读回带单位', () => {
- var items = [{ definitionId: 1, name: 'LDL-C', unit: 'mmol/L', points: [{ reportDate: '2026-01-01', value: '3.62', numericValue: 3.62 }] }]
- var rows = ic.buildComparePeriodRows(items)
- expect(rows[0].cells[0].valueText).toContain('3.62')
- expect(rows[0].cells[0].valueText).toContain('mmol/L')
- })
- test('多指标列对齐', () => {
- var items = [
- { definitionId: 1, name: 'A', unit: 'mmol/L', points: [{ reportDate: '2026-01-01', value: '1.0', numericValue: 1 }] },
- { definitionId: 2, name: 'B', unit: '%', points: [{ reportDate: '2026-01-01', value: '50', numericValue: 50 }] }
- ]
- var rows = ic.buildComparePeriodRows(items)
- expect(rows[0].cells.length).toBe(2)
- expect(rows[0].cells[0].name).toBe('A')
- expect(rows[0].cells[1].name).toBe('B')
- })
- })
- describe('buildLegendText', () => {
- test('count=0 → 暂无数据', () => {
- expect(ic.buildLegendText({ points: [] })).toBe(' · 暂无数据')
- })
- test('count=1 → 仅 1 期 无趋势', () => {
- var t = ic.buildLegendText({ name: 'A', unit: '%', points: [{ numericValue: 5 }] })
- expect(t).toContain('仅 1 期')
- expect(t).toContain('无趋势')
- })
- test('count≥2 且 min===max → 无波动 恒定', () => {
- var t = ic.buildLegendText({ name: 'A', unit: '%', points: [{ numericValue: 5 }, { numericValue: 5 }] })
- expect(t).toContain('无波动')
- expect(t).toContain('恒定')
- expect(t).toContain('5.00')
- })
- test('count=2 有波动 → 追加(仅 2 期,未连线)', () => {
- var t = ic.buildLegendText({ name: 'LDL-C', unit: 'mmol/L', points: [{ numericValue: 3.62 }, { numericValue: 4.10 }] })
- expect(t).toContain('仅 2 期,未连线')
- expect(t).toContain('真实范围')
- })
- test('count≥3 有波动 → 仅真实范围', () => {
- var t = ic.buildLegendText({ name: 'X', unit: 'mg/dL', points: [{ numericValue: 1 }, { numericValue: 2 }, { numericValue: 3 }] })
- expect(t).toContain('真实范围')
- expect(t).not.toContain('仅 2 期')
- })
- })
- })
|