Files
trackgo-fe/packages/utils/src/currency/currency.utils.test.ts
T
Firman Ramdhani a1db9b93e1 feat: add DateUtils and StringUtils for date and string manipulation
- Implement DateUtils for handling date operations with timezone support.
- Add unit tests for DateUtils to ensure functionality and edge cases.
- Create StringUtils for string manipulation with a fluent API.
- Include unit tests for StringUtils covering various string operations.
- Introduce EncryptionUtils for secure data encryption and decryption.
- Add tests for EncryptionUtils to validate encryption and decryption processes.
- Update index.ts to export new utility modules.
- Introduce encryption key management with a dedicated key file.
2026-01-28 18:18:29 +07:00

115 lines
3.8 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { CurrencyUtils } from './currency.utils';
describe('CurrencyUtils', () => {
let currency: CurrencyUtils;
beforeEach(() => {
// Reset global prefix and decimal separator before each test
CurrencyUtils.setGlobalPrefix('Rp ');
CurrencyUtils.setGlobalDecimalSeparator(',');
// Mock console to keep tests clean
vi.spyOn(console, 'warn').mockImplementation(() => {});
vi.spyOn(console, 'error').mockImplementation(() => {});
// Default instance for testing
currency = new CurrencyUtils();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('Global Prefix', () => {
it('should set and get global prefix', () => {
CurrencyUtils.setGlobalPrefix('USD ');
expect(CurrencyUtils.getGlobalPrefix()).toBe('USD ');
});
it('should use global prefix if instance prefix is not provided', () => {
CurrencyUtils.setGlobalPrefix('USD ');
const c = new CurrencyUtils();
expect(c.format(1000)).toBe('USD 1.000');
});
it('should override instance prefix', () => {
const c = new CurrencyUtils({ prefix: '€ ' });
expect(c.format(1000)).toBe('€ 1.000');
});
});
describe('Global Decimal Separator', () => {
it('should set and get global decimal separator', () => {
CurrencyUtils.setGlobalDecimalSeparator('.');
expect(CurrencyUtils.getGlobalDecimalSeparator()).toBe('.');
});
it('should use global decimal separator if instance separator is not provided', () => {
CurrencyUtils.setGlobalDecimalSeparator('.');
const c = new CurrencyUtils();
expect(c.format(1234.56)).toBe('Rp 1,234.56');
expect(c.parseToRaw('Rp 1,234.56')).toBe('1234.56');
});
it('should override instance decimal separator', () => {
const c = new CurrencyUtils({ decimalSeparator: '.' });
expect(c.format(1234.56)).toBe('Rp 1,234.56');
expect(c.parseToRaw('Rp 1,234.56')).toBe('1234.56');
});
});
describe('Formatting', () => {
it('should format integer without decimalScale', () => {
expect(currency.format(1234567)).toBe('Rp 1.234.567');
});
it('should format number with decimal without decimalScale', () => {
expect(currency.format(1234.5678)).toBe('Rp 1.234,5678');
});
it('should round decimal when decimalScale is set', () => {
const c = new CurrencyUtils({ decimalScale: 2 });
expect(c.format(1234.5678)).toBe('Rp 1.234,56');
});
it('should handle zero and negative values', () => {
expect(currency.format(0)).toBe('Rp 0');
expect(currency.format(-12345)).toBe('Rp -12.345');
});
it('should handle string input', () => {
expect(currency.format('1234567')).toBe('Rp 1.234.567');
expect(currency.format('1234.5678')).toBe('Rp 1.234,5678');
});
it('should return empty string for null or undefined', () => {
expect(currency.format(null)).toBe('');
expect(currency.format(undefined)).toBe('');
});
});
describe('Parsing', () => {
it('should parse formatted string to raw numeric string', () => {
expect(currency.parseToRaw('Rp 1.234.567')).toBe('1234567');
expect(currency.parseToRaw('Rp 1.234,5678')).toBe('1234.5678');
});
it('should return empty string for null or undefined input', () => {
expect(currency.parseToRaw(null)).toBe('');
expect(currency.parseToRaw(undefined)).toBe('');
});
});
describe('Edge Cases', () => {
it('should format very large numbers correctly', () => {
expect(currency.format(1234567890123)).toBe('Rp 1.234.567.890.123');
});
it('should format numbers with multiple decimals correctly when decimalScale is set', () => {
const c = new CurrencyUtils({ decimalScale: 3 });
expect(c.format(1234.56789)).toBe('Rp 1.234,567');
});
});
});