/** * shared/utils/userPicker.utils.test.ts * 用途:锁定公共选人弹窗的部门分组与筛选规则。 */ import { describe, expect, it } from 'vitest'; import { USER_PICKER_ALL_DEPT, USER_PICKER_NONE_DEPT, type UserPickerPerson, } from '@/shared/types/userPicker.types'; import { filterUserPickerPeople, listUserPickerDepartments, userPickerLabel, } from './userPicker.utils'; const people: UserPickerPerson[] = [ { id: 1, username: 'a', real_name: '钟淑惠', employee_id: '0100004', department: '财务部' }, { id: 2, username: 'b', real_name: '成秋兰', employee_id: '0203036', department: '关务部' }, { id: 3, username: 'c', real_name: '刘江', employee_id: '07090745', department: ' ' }, ]; describe('userPickerLabel', () => { it('prefers real name and appends employee id', () => { expect(userPickerLabel(people[0])).toBe('钟淑惠(0100004)'); expect(userPickerLabel(undefined, 9)).toBe('9'); }); }); describe('listUserPickerDepartments', () => { it('puts all first, named departments next, and unnamed last', () => { expect(listUserPickerDepartments(people)).toEqual([ { key: USER_PICKER_ALL_DEPT, count: 3 }, { key: '财务部', count: 1 }, { key: '关务部', count: 1 }, { key: USER_PICKER_NONE_DEPT, count: 1 }, ]); }); }); describe('filterUserPickerPeople', () => { it('filters by department and keyword', () => { expect(filterUserPickerPeople(people, { departmentKey: '财务部' }).map((row) => row.id)).toEqual([1]); expect(filterUserPickerPeople(people, { query: '0203' }).map((row) => row.id)).toEqual([2]); expect(filterUserPickerPeople(people, { departmentKey: USER_PICKER_NONE_DEPT }).map((row) => row.id)).toEqual([3]); }); });