formatters.test.ts
2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { describe, expect, it } from 'vitest';
import { dictionaryLabel, formatDateTime, formatMoney, formatPolicyValue } from './formatters';
// Contract fixtures, not production amount samples: the API currently has no store records.
describe('store numeric display', () => {
it.each([
[0, '¥0.00'],
['0.00', '¥0.00'],
['-12.5', '¥-12.50'],
['12345678901234567890.123456', '¥12,345,678,901,234,567,890.123456'],
['1,234.50', '¥1,234.50'],
['1.234567890123456789e5', '¥123,456.7890123456789'],
['1e-7', '¥0.0000001'],
['1.2300e2', '¥123.00'],
['-0', '¥0.00'],
])('preserves exact amount %s instead of rounding or coercing it', (value, expected) => {
expect(formatMoney(value)).toBe(expected);
});
it.each([
undefined,
null,
'',
' ',
'abc',
'1,23',
'NaN',
'Infinity',
Infinity,
NaN,
{},
'1e999999999',
])('does not turn missing or invalid %s into zero', (value) => {
expect(formatMoney(value)).toBe('—');
});
it('uses the authoritative policy unit, preserving zero and unknown units', () => {
expect(formatPolicyValue('5', 'RATIO')).toBe('5%');
expect(formatPolicyValue('0', 'RATIO')).toBe('0%');
expect(formatPolicyValue('12.34', 'AMOUNT')).toBe('¥12.34');
expect(formatPolicyValue('5', 'FUTURE_UNIT')).toBe('5');
expect(formatPolicyValue('0.333333333333333333', 'RATIO')).toBe('0.333333333333333333%');
expect(formatPolicyValue('', 'RATIO')).toBe('—');
});
});
describe('store labels', () => {
it('resolves known dictionaries without hiding unknown or zero values', () => {
expect(dictionaryLabel([{ value: '0', label: '关闭' }], 0)).toBe('关闭');
expect(dictionaryLabel([], 'NEW_SCENE')).toBe('NEW_SCENE');
expect(dictionaryLabel(undefined, '')).toBe('—');
});
it('formats server date strings without changing their timezone', () => {
expect(formatDateTime('2026-10-09T12:30:45')).toBe('2026-10-09 12:30:45');
expect(formatDateTime('2026-10-09 12:30:45')).toBe('2026-10-09 12:30:45');
expect(formatDateTime(null)).toBe('—');
});
});