Files
co-lite/solvway-admin-portal/app/lib/format.test.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

99 lines
4.2 KiB
TypeScript

import { test, expect } from 'vitest'
import { compactRM, displaySymbol, formatPhone, formatRM } from './format'
test('formats ringgit with thousands separators and 2dp', () => {
expect(formatRM(12426)).toBe('RM 12,426.00')
expect(formatRM(0)).toBe('RM 0.00')
expect(formatRM(1234567.5)).toBe('RM 1,234,567.50')
})
test('formats a phone as +prefix number, tolerating a stored "+" and missing parts', () => {
expect(formatPhone('60', '01928312323')).toBe('+60 01928312323')
expect(formatPhone('+60', '123456789')).toBe('+60 123456789')
expect(formatPhone(' 65 ', ' 91234567 ')).toBe('+65 91234567')
expect(formatPhone('', '0123')).toBe('0123')
expect(formatPhone(null, undefined)).toBe('')
expect(formatPhone('60', '')).toBe('')
})
// unify-admin-page-patterns: one on-screen date format. Wall-time strings (no zone) are shown as
// written; zoned values are shown in Malaysia time; empty -> "—"; unparseable -> unchanged.
test('formats a wall-time ISO date-time as d MMM yyyy, HH:mm', async () => {
const { formatDateTime } = await import('./format')
expect(formatDateTime('2026-09-28T23:41:22')).toBe('28 Sep 2026, 23:41')
expect(formatDateTime('2026-09-02T08:05:00.000')).toBe('2 Sep 2026, 08:05')
expect(formatDateTime('2026-09-21 10:12')).toBe('21 Sep 2026, 10:12')
expect(formatDateTime('2026-09-21 10:12:59')).toBe('21 Sep 2026, 10:12')
})
test('shows a zoned date-time in Malaysia time', async () => {
const { formatDateTime } = await import('./format')
expect(formatDateTime('2026-09-29T12:53:19Z')).toBe('29 Sep 2026, 20:53')
expect(formatDateTime('2026-09-29T12:53:19.123+00:00')).toBe('29 Sep 2026, 20:53')
expect(formatDateTime('2026-09-29T20:53:19+08:00')).toBe('29 Sep 2026, 20:53')
expect(formatDateTime('2026-12-31T16:30:00Z')).toBe('1 Jan 2027, 00:30')
})
test('formats a date-only value, and a date-time as date only', async () => {
const { formatDate } = await import('./format')
expect(formatDate('2026-09-02')).toBe('2 Sep 2026')
expect(formatDate('2026-09-28T23:41:22')).toBe('28 Sep 2026')
expect(formatDate('2026-12-31T16:30:00Z')).toBe('1 Jan 2027')
})
test('a date-only value passed to formatDateTime has no time part', async () => {
const { formatDateTime } = await import('./format')
expect(formatDateTime('2026-09-02')).toBe('2 Sep 2026')
})
test('empty dates show a dash and unparseable ones pass through', async () => {
const { formatDate, formatDateTime } = await import('./format')
for (const f of [formatDate, formatDateTime]) {
expect(f(null)).toBe('—')
expect(f(undefined)).toBe('—')
expect(f('')).toBe('—')
expect(f('not a date')).toBe('not a date')
expect(f('31-12-2026')).toBe('31-12-2026')
}
})
test('returns out-of-range dates unchanged', async () => {
const { formatDate, formatDateTime } = await import('./format')
for (const f of [formatDate, formatDateTime]) {
expect(f('2026-13-40')).toBe('2026-13-40')
expect(f('2026-00-10')).toBe('2026-00-10')
expect(f('2026-05-32')).toBe('2026-05-32')
}
})
test('compactRM keeps neighbouring axis ticks apart below RM 10k (BABYBOO-25)', () => {
// A 0-3,000 axis ticks at 750 steps; whole thousands made 1,500 and 2,250 both "RM 2k".
expect([0, 750, 1500, 2250, 3000].map(compactRM)).toEqual(['RM 0', 'RM 750', 'RM 1.5k', 'RM 2.3k', 'RM 3k'])
})
test('compactRM uses whole thousands from RM 10k and millions from RM 1m', () => {
expect(compactRM(12_400)).toBe('RM 12k')
expect(compactRM(2_500_000)).toBe('RM 2.5m')
})
test('formatRM shows the sale currency symbol, RM by default', () => {
expect(formatRM(42.9)).toBe('RM 42.90')
expect(formatRM(42.9, 'MYR')).toBe('RM 42.90')
expect(formatRM(42.9, 'SGD')).toBe('S$ 42.90')
expect(formatRM(42.9, 'USD')).toBe('USD 42.90')
})
test('compactRM shows the sale currency symbol', () => {
expect(compactRM(12_000, 'SGD')).toBe('S$ 12k')
expect(compactRM(12_000, 'MYR')).toBe('RM 12k')
expect(compactRM(2_500_000)).toBe('RM 2.5m')
})
test('displaySymbol maps ISO codes to symbols', () => {
expect(displaySymbol('MYR')).toBe('RM')
expect(displaySymbol('SGD')).toBe('S$')
expect(displaySymbol('USD')).toBe('USD')
expect(displaySymbol('')).toBe('RM')
expect(displaySymbol(undefined)).toBe('RM')
})