| 1 |
import { |
| 2 |
cn, |
| 3 |
fmt, |
| 4 |
formatDate, |
| 5 |
formatTooltipDate, |
| 6 |
formatXAxisDate, |
| 7 |
} from '../../lib/utils'; |
| 8 |
|
| 9 |
beforeAll(() => { |
| 10 |
global.DISCO = { ...global.DISCO, base_currency: '$' }; |
| 11 |
}); |
| 12 |
|
| 13 |
describe('Utility functions', () => { |
| 14 |
describe('cn', () => { |
| 15 |
it('merges class names', () => { |
| 16 |
const result = cn('foo', 'bar'); |
| 17 |
expect(result).toContain('foo'); |
| 18 |
expect(result).toContain('bar'); |
| 19 |
}); |
| 20 |
|
| 21 |
it('handles conditional classes', () => { |
| 22 |
const result = cn('base', false && 'hidden', 'visible'); |
| 23 |
expect(result).toContain('base'); |
| 24 |
expect(result).toContain('visible'); |
| 25 |
expect(result).not.toContain('hidden'); |
| 26 |
}); |
| 27 |
|
| 28 |
it('handles undefined and null', () => { |
| 29 |
const result = cn('base', undefined, null); |
| 30 |
expect(result).toBe('base'); |
| 31 |
}); |
| 32 |
}); |
| 33 |
|
| 34 |
describe('fmt', () => { |
| 35 |
it('formats a number as USD currency', () => { |
| 36 |
expect(fmt(1234.5)).toBe('$1,234.50'); |
| 37 |
}); |
| 38 |
|
| 39 |
it('formats zero', () => { |
| 40 |
expect(fmt(0)).toBe('$0.00'); |
| 41 |
}); |
| 42 |
|
| 43 |
it('formats large numbers with commas', () => { |
| 44 |
expect(fmt(1000000)).toBe('$1,000,000.00'); |
| 45 |
}); |
| 46 |
|
| 47 |
it('formats decimals to 2 places', () => { |
| 48 |
expect(fmt(99.999)).toBe('$100.00'); |
| 49 |
}); |
| 50 |
|
| 51 |
it('handles string numbers', () => { |
| 52 |
expect(fmt('250.5')).toBe('$250.50'); |
| 53 |
}); |
| 54 |
}); |
| 55 |
|
| 56 |
describe('formatDate', () => { |
| 57 |
it('formats a date string to locale format', () => { |
| 58 |
const result = formatDate('2025-05-15'); |
| 59 |
expect(result).toMatch(/May\s+15,\s+2025/); |
| 60 |
}); |
| 61 |
|
| 62 |
it('returns dash for null/undefined', () => { |
| 63 |
expect(formatDate(null)).toBe('—'); |
| 64 |
expect(formatDate(undefined)).toBe('—'); |
| 65 |
expect(formatDate('')).toBe('—'); |
| 66 |
}); |
| 67 |
}); |
| 68 |
|
| 69 |
describe('formatTooltipDate', () => { |
| 70 |
it('formats a day interval date', () => { |
| 71 |
const result = formatTooltipDate('2025-05-15', 'day'); |
| 72 |
expect(result).toMatch(/May\s+15,\s+2025/); |
| 73 |
}); |
| 74 |
|
| 75 |
it('formats a week interval with range', () => { |
| 76 |
const result = formatTooltipDate('2025-05-12', 'week'); |
| 77 |
// Should show May 12 - May 18 (7 day range) |
| 78 |
expect(result).toContain('-'); |
| 79 |
expect(result).toMatch(/May\s+12/); |
| 80 |
expect(result).toMatch(/May\s+18/); |
| 81 |
}); |
| 82 |
|
| 83 |
it('formats a month interval with full month range', () => { |
| 84 |
const result = formatTooltipDate('2025-05-01', 'month'); |
| 85 |
expect(result).toContain('-'); |
| 86 |
expect(result).toMatch(/May\s+1/); |
| 87 |
expect(result).toMatch(/May\s+31/); |
| 88 |
}); |
| 89 |
}); |
| 90 |
|
| 91 |
describe('formatXAxisDate', () => { |
| 92 |
it('formats short range date with month and day', () => { |
| 93 |
const result = formatXAxisDate('2025-05-15', false); |
| 94 |
expect(result).toMatch(/May\s+15/); |
| 95 |
}); |
| 96 |
|
| 97 |
it('formats long range date with month and year', () => { |
| 98 |
const result = formatXAxisDate('2025-05-15', true); |
| 99 |
expect(result).toMatch(/May\s+25/); |
| 100 |
}); |
| 101 |
}); |
| 102 |
|
| 103 |
describe('Edge cases', () => { |
| 104 |
it('fmt handles NaN gracefully', () => { |
| 105 |
const result = fmt(NaN); |
| 106 |
expect(result).toBe('$NaN'); |
| 107 |
}); |
| 108 |
|
| 109 |
it('fmt handles undefined', () => { |
| 110 |
const result = fmt(undefined); |
| 111 |
expect(result).toBe('$NaN'); |
| 112 |
}); |
| 113 |
|
| 114 |
it('fmt handles null', () => { |
| 115 |
const result = fmt(null); |
| 116 |
expect(result).toBe('$0.00'); |
| 117 |
}); |
| 118 |
|
| 119 |
it('fmt handles negative numbers', () => { |
| 120 |
const result = fmt(-500.5); |
| 121 |
expect(result).toMatch(/-?\$?500\.50/); |
| 122 |
}); |
| 123 |
|
| 124 |
it('fmt handles very large numbers', () => { |
| 125 |
const result = fmt(99999999.99); |
| 126 |
expect(result).toBe('$99,999,999.99'); |
| 127 |
}); |
| 128 |
}); |
| 129 |
}); |
| 130 |
|