import { dataSourceI18n } from '../i18n'; import { getLang } from '@cainiaofe/cn-i18n'; // 模拟 getCurrentLang 函数 jest.mock('@cainiaofe/cn-i18n', () => ({ getLang: jest.fn(), })); describe('dataSourceI18n', () => { it('应该根据当前语言更新数据源的标签', () => { // 设置当前语言为 'en' (getLang as jest.Mock).mockReturnValue('en'); const dataSource = [ { value: '1', label: '默认标签', i18n: { en: 'English Label', }, children: [ { value: '11', label: '子标签', i18n: { en: 'Child English Label', }, }, ], }, ]; const result = dataSourceI18n(dataSource); expect(result).toEqual([ { value: '1', label: 'English Label', i18n: { en: 'English Label', }, children: [ { value: '11', label: 'Child English Label', i18n: { en: 'Child English Label', }, }, ], }, ]); }); it('应该在没有 i18n 字段时保持原标签', () => { (getLang as jest.Mock).mockReturnValue('en'); const dataSource = [ { value: '1', label: '默认标签', }, ]; const result = dataSourceI18n(dataSource); expect(result).toEqual([ { value: '1', label: '默认标签', }, ]); }); it('应该处理空数据源', () => { (getLang as jest.Mock).mockReturnValue('en'); const result = dataSourceI18n(undefined); expect(result).toBeUndefined(); }); it('当输入非数组类型数据时,应该直接返回原始数据', () => { (getLang as jest.Mock).mockReturnValue('en'); const nonArrayData = { key: 'value' }; // @ts-expect-error 健壮性设计,此处忽略类型 const result = dataSourceI18n(nonArrayData); expect(result).toBe(nonArrayData); const nonArrayData2 = 123; // @ts-expect-error 健壮性设计,此处忽略类型 const result2 = dataSourceI18n(nonArrayData2); expect(result2).toBe(nonArrayData2); const nonArrayData3 = 'string'; // @ts-expect-error 健壮性设计,此处忽略类型 const result3 = dataSourceI18n(nonArrayData3); expect(result3).toBe(nonArrayData3); }); });