You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
4.0 KiB
120 lines
4.0 KiB
import {
|
|
formatThousands,
|
|
formatCurrency,
|
|
formatViews,
|
|
isValidEmail,
|
|
onboardingStepLabel,
|
|
splitYearMonth,
|
|
formatDateTime,
|
|
} from '../utils';
|
|
import { ONBOARDING_STEP_ORDER } from '../constants';
|
|
|
|
describe('formatThousands', () => {
|
|
it('千分位格式化 12846 → "12,846"', () => {
|
|
expect(formatThousands(12846)).toBe('12,846');
|
|
});
|
|
it('不足千位原样返回', () => {
|
|
expect(formatThousands(123)).toBe('123');
|
|
});
|
|
});
|
|
|
|
describe('formatCurrency', () => {
|
|
it('默认 0 位小数 30000 → "30,000"', () => {
|
|
expect(formatCurrency(30000)).toBe('30,000');
|
|
});
|
|
it('指定 2 位小数补齐', () => {
|
|
expect(formatCurrency(12345.6, 2)).toBe('12,345.60');
|
|
});
|
|
});
|
|
|
|
describe('formatViews', () => {
|
|
it('≥1万 → 万单位', () => {
|
|
expect(formatViews(23000)).toBe('2.3万');
|
|
});
|
|
it('整万去掉 .0', () => {
|
|
expect(formatViews(20000)).toBe('2万');
|
|
});
|
|
it('<1万返回原数字', () => {
|
|
expect(formatViews(8500)).toBe('8500');
|
|
});
|
|
});
|
|
|
|
/*
|
|
* 进度文案此前是写在页面上的字面量(P6 写「3/3」),与实际步骤数无关。
|
|
* 这里锁定「序号 = 数组下标 + 1」与「分母 = 主线长度」,
|
|
* 以后加删步骤不需要改测试,但写死数字会被抓出来。
|
|
*/
|
|
describe('onboardingStepLabel', () => {
|
|
it('主线每一步的序号与位置一致,分母为步骤总数', () => {
|
|
ONBOARDING_STEP_ORDER.forEach((step, i) => {
|
|
expect(onboardingStepLabel(step)).toBe(`第 ${i + 1} / ${ONBOARDING_STEP_ORDER.length} 步`);
|
|
});
|
|
});
|
|
|
|
it('非主线屏返回 null(不渲染进度)', () => {
|
|
// 登录/协议/企业端,以及 P5、P17 两个「进入后返回」的子页
|
|
['P1', 'P2', 'P2Q', 'P2P', 'P2R', 'P2W', 'P5', 'P17', 'B1', 'B2', 'B3', 'P23', ''].forEach(
|
|
(step) => {
|
|
expect(onboardingStepLabel(step)).toBeNull();
|
|
},
|
|
);
|
|
});
|
|
|
|
it('主线不含重复项(重复会把同一步算两次)', () => {
|
|
expect(new Set(ONBOARDING_STEP_ORDER).size).toBe(ONBOARDING_STEP_ORDER.length);
|
|
});
|
|
});
|
|
|
|
/*
|
|
* 工作经历编辑页回填滚轮用。解析不出时必须给空串(滚轮显示「请滑动选择」),
|
|
* 给它一个兜底索引会静默把用户的入职年月改成别的年份。
|
|
*/
|
|
describe('splitYearMonth', () => {
|
|
it('「2023年2月」拆成「2023年」与「2月」', () => {
|
|
expect(splitYearMonth('2023年2月')).toEqual({ year: '2023年', month: '2月' });
|
|
});
|
|
|
|
it('两位月份同样拆得开', () => {
|
|
expect(splitYearMonth('2023年12月')).toEqual({ year: '2023年', month: '12月' });
|
|
});
|
|
|
|
it('前后空白不影响解析', () => {
|
|
expect(splitYearMonth(' 2023年2月 ')).toEqual({ year: '2023年', month: '2月' });
|
|
});
|
|
|
|
it('空串与「至今」解析出两个空串(调用方据此走未选中态)', () => {
|
|
expect(splitYearMonth('')).toEqual({ year: '', month: '' });
|
|
expect(splitYearMonth('至今')).toEqual({ year: '', month: '' });
|
|
});
|
|
|
|
it('演示简历里的「2022.07」这种格式解析不出,也不猜', () => {
|
|
expect(splitYearMonth('2022.07')).toEqual({ year: '', month: '' });
|
|
});
|
|
});
|
|
|
|
describe('formatDateTime', () => {
|
|
it('输出「MM-DD HH:mm」', () => {
|
|
const local = new Date(2026, 8, 15, 14, 20);
|
|
expect(formatDateTime(local.toISOString())).toBe('09-15 14:20');
|
|
});
|
|
|
|
it('时分补零', () => {
|
|
const local = new Date(2026, 0, 3, 9, 5);
|
|
expect(formatDateTime(local.toISOString())).toBe('01-03 09:05');
|
|
});
|
|
|
|
it('空串与非法时间串返回空串(页面据此不渲染该行,而不是显示 Invalid Date)', () => {
|
|
expect(formatDateTime('')).toBe('');
|
|
expect(formatDateTime('不是时间')).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('isValidEmail', () => {
|
|
it.each(['a@b.co', 'zhang.xiao-man+1@example.com', ' a@b.cn '])('合法:%s', (email) => {
|
|
expect(isValidEmail(email)).toBe(true);
|
|
});
|
|
|
|
it.each(['', 'a', 'a@', '@b.com', 'a@b', 'a b@c.com', 'a@b.c'])('非法:%s', (email) => {
|
|
expect(isValidEmail(email)).toBe(false);
|
|
});
|
|
});
|
|
|