import { describe, expect, it, vi } from 'vitest' vi.mock('@/services/api', () => ({ loadDataApi: vi.fn(), saveObjectApi: vi.fn(), })) vi.mock('@/stores/auth', () => ({ useAuthStore: () => ({ userInfo: { account: 'u001' } }), })) const { PREF_SCHEMA_TYPES, columnSettingsDelta, isEmptyColumnDelta, mergeColumnPref, sameColumnSettings, } = await import('@/components/fms-module-list/useModulePref') /** 通用栏位行:字段 + 宽度,数组顺序即通用顺序 */ const base = [ { b_field: 'b_id', b_width: 120, b_visible: 1 }, { b_field: 'b_name', b_width: 200, b_visible: 1 }, { b_field: 'b_amount', b_width: 160, b_visible: 1 }, ] /** 表格当前栏位设置(getColumnSettings 的形态) */ const current = (...pairs) => pairs.map(([b_field, b_width]) => ({ b_field, b_width })) describe('栏位设置:个人层合并', () => { it('无个人偏好时原样返回通用栏位', () => { expect(mergeColumnPref(base, null)).toBe(base) }) it('只覆盖宽度,保留通用行上的其它属性(b_visible 等)', () => { const merged = mergeColumnPref(base, { columns: { b_name: { width: 300 } } }) const name = merged.find((row) => row.b_field === 'b_name') expect(name.b_width).toBe(300) expect(name.b_visible).toBe(1) // 未覆盖的字段保持通用宽度 expect(merged.find((row) => row.b_field === 'b_id').b_width).toBe(120) }) it('按 order 重排并重编 b_xh(10/20/30)', () => { const merged = mergeColumnPref(base, { order: ['b_amount', 'b_id', 'b_name'] }) expect(merged.map((row) => row.b_field)).toEqual(['b_amount', 'b_id', 'b_name']) expect(merged.map((row) => row.b_xh)).toEqual([10, 20, 30]) }) it('order 未列出的字段保持原有相对顺序、追加在后', () => { const merged = mergeColumnPref(base, { order: ['b_amount'] }) expect(merged.map((row) => row.b_field)).toEqual(['b_amount', 'b_id', 'b_name']) }) it('个人层不得新增字段:order 里的未知字段一律忽略', () => { const merged = mergeColumnPref(base, { order: ['b_hack', 'b_id'] }) expect(merged.map((row) => row.b_field)).toEqual(['b_id', 'b_name', 'b_amount']) }) it('个人层不得给未知字段设宽度', () => { const merged = mergeColumnPref(base, { columns: { b_hack: { width: 999 } } }) expect(merged.some((row) => row.b_field === 'b_hack')).toBe(false) }) }) describe('栏位设置:增量计算', () => { it('完全一致时增量为空且不含 order(不把通用顺序固化)', () => { const delta = columnSettingsDelta( current(['b_id', 120], ['b_name', 200], ['b_amount', 160]), base, ) expect(delta.columns).toEqual({}) expect(delta.order).toBeUndefined() expect(isEmptyColumnDelta(delta)).toBe(true) }) it('只记改过的宽度,同宽字段不入增量(通用改宽仍能透传)', () => { const delta = columnSettingsDelta( current(['b_id', 120], ['b_name', 320], ['b_amount', 160]), base, ) expect(delta.columns).toEqual({ b_name: { width: 320 } }) expect(delta.order).toBeUndefined() }) it('顺序变化时整体记 order', () => { const delta = columnSettingsDelta( current(['b_amount', 160], ['b_id', 120], ['b_name', 200]), base, ) expect(delta.order).toEqual(['b_amount', 'b_id', 'b_name']) expect(isEmptyColumnDelta(delta)).toBe(false) }) it('宽度与顺序都变了,两部分都记', () => { const delta = columnSettingsDelta( current(['b_name', 320], ['b_id', 120], ['b_amount', 160]), base, ) expect(delta.columns).toEqual({ b_name: { width: 320 } }) expect(delta.order).toEqual(['b_name', 'b_id', 'b_amount']) }) it('未知字段不产生宽度增量', () => { const delta = columnSettingsDelta( current(['b_id', 120], ['b_name', 200], ['b_hack', 300]), base, ) expect(delta.columns).toEqual({}) }) }) describe('栏位设置:与基线比较', () => { it('宽度相同且顺序相同 → 未调整', () => { expect( sameColumnSettings(current(['b_id', 120], ['b_name', 200], ['b_amount', 160]), base), ).toBe(true) }) it('宽度不同 → 已调整', () => { expect( sameColumnSettings(current(['b_id', 120], ['b_name', 260], ['b_amount', 160]), base), ).toBe(false) }) it('顺序不同 → 已调整(即使宽度全同)', () => { expect( sameColumnSettings(current(['b_name', 200], ['b_id', 120], ['b_amount', 160]), base), ).toBe(false) }) it('数量不同 → 已调整', () => { expect(sameColumnSettings(current(['b_id', 120]), base)).toBe(false) }) it('当前宽度为 null 而通用有宽度 → 视为已调整', () => { expect( sameColumnSettings(current(['b_id', null], ['b_name', 200], ['b_amount', 160]), base), ).toBe(false) }) it('两侧宽度都是 null/未设置时视为一致', () => { const noWidth = [{ b_field: 'b_id', b_width: null }, { b_field: 'b_name' }] expect(sameColumnSettings(current(['b_id', null], ['b_name', undefined]), noWidth)).toBe(true) }) }) describe('栏位设置:schema 类型常量', () => { it('个人层栏位用 view,与查询布局的 query 区分开', () => { expect(PREF_SCHEMA_TYPES.view).toBe('view') expect(PREF_SCHEMA_TYPES.query).toBe('query') }) })