57272a4c7f
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
2.9 KiB
TypeScript
89 lines
2.9 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
||
import {
|
||
formatCompact,
|
||
formatDateTime,
|
||
formatWeekday,
|
||
formatDate,
|
||
formatMoney,
|
||
formatNumber,
|
||
formatPercent,
|
||
formatQuantity,
|
||
parseMoney,
|
||
toIsoDate,
|
||
} from './format'
|
||
|
||
describe('formatMoney', () => {
|
||
it('kuruşu Türkçe biçimde, para birimi sağda ve boşluklu yazar', () => {
|
||
expect(formatMoney(125000)).toBe('1.250,00\u00a0₺')
|
||
expect(formatMoney(30000, 'EUR')).toBe('300,00\u00a0€')
|
||
expect(formatMoney(15000, 'USD')).toBe('150,00\u00a0$')
|
||
expect(formatMoney(0)).toBe('0,00\u00a0₺')
|
||
expect(formatMoney(5)).toBe('0,05\u00a0₺')
|
||
expect(formatMoney(123456789)).toBe('1.234.567,89\u00a0₺')
|
||
})
|
||
|
||
it('negatif ve işaretli', () => {
|
||
expect(formatMoney(-125000)).toBe('-1.250,00\u00a0₺')
|
||
expect(formatMoney(125000, 'TRY', { signed: true })).toBe('+1.250,00\u00a0₺')
|
||
expect(formatMoney(-500, 'TRY', { signed: true })).toBe('-5,00\u00a0₺')
|
||
})
|
||
})
|
||
|
||
describe('diğer biçimler', () => {
|
||
it('miktar, sayı, yüzde', () => {
|
||
expect(formatQuantity(12_500_000, 'gr')).toBe('12,50\u00a0gr')
|
||
expect(formatQuantity(1_234_567, 'gr', 3)).toBe('1,235\u00a0gr')
|
||
expect(formatNumber(1250.5)).toBe('1.250,50')
|
||
expect(formatPercent(0.123)).toBe('%12,3')
|
||
})
|
||
|
||
it('grafik ekseni için kısa tutar', () => {
|
||
expect(formatCompact(2_700_000)).toBe('27\u00a0B') // bölünmez boşluk
|
||
expect(formatCompact(150_000_000)).toBe('1,5\u00a0Mn')
|
||
expect(formatCompact(95_000)).toBe('950')
|
||
})
|
||
|
||
it('tarih GG.AA.YYYY', () => {
|
||
expect(formatDate('2026-12-09')).toBe('09.12.2026')
|
||
expect(formatDate(new Date(2027, 0, 1))).toBe('01.01.2027')
|
||
expect(toIsoDate(new Date(2026, 11, 9))).toBe('2026-12-09')
|
||
expect(formatDateTime(new Date(2026, 10, 13, 0, 55).toISOString())).toBe('13.11.2026 00:55')
|
||
})
|
||
})
|
||
|
||
describe('parseMoney', () => {
|
||
it.each([
|
||
['1250', 125000],
|
||
['1.250', 125000],
|
||
['1250,5', 125050],
|
||
['1.250,50', 125050],
|
||
['1250.50', 125050], // telefon klavyesindeki nokta
|
||
['1250.5', 125050],
|
||
[' 1.250,00 ₺ ', 125000],
|
||
['1.250,00\u00a0₺', 125000], // ekrandan kopyalanan (bölünmez boşluk)
|
||
[',99', 99],
|
||
['0,1', 10],
|
||
['1.234.567,89', 123456789],
|
||
['-45,5', -4550],
|
||
])('%s → %i kuruş', (input, expected) => {
|
||
expect(parseMoney(input)).toBe(expected)
|
||
})
|
||
|
||
it.each(['', 'abc', '1,2,3', '12,345', '1.250,5.0', '₺'])('geçersiz: "%s"', (input) => {
|
||
expect(parseMoney(input)).toBeNull()
|
||
})
|
||
|
||
it('kayan nokta hatası yok (0,1 + 0,2 gibi değerler tam)', () => {
|
||
expect(parseMoney('0,29')).toBe(29)
|
||
expect(parseMoney('19,99')).toBe(1999)
|
||
})
|
||
})
|
||
|
||
describe('formatWeekday', () => {
|
||
it('Türkçe gün adı, saat diliminden bağımsız', () => {
|
||
expect(formatWeekday('2026-11-12')).toBe('Perşembe')
|
||
expect(formatWeekday('2026-11-01')).toBe('Pazar')
|
||
expect(formatWeekday('2026-11-02')).toBe('Pazartesi')
|
||
})
|
||
})
|